mirror of
https://github.com/kovetskiy/mark.git
synced 2025-04-24 05:42:40 +08:00
feat: Add fallback directory to load includes from
This commit is contained in:
parent
8e16f6f29c
commit
d269369911
@ -75,6 +75,8 @@ to the template relative to current working dir, e.g.:
|
|||||||
<!-- Include: <path> -->
|
<!-- Include: <path> -->
|
||||||
```
|
```
|
||||||
|
|
||||||
|
If the template cannot be found relative to the current directory, a fallback directory can be defined via `--include-path`. This way it is possible to have global include files while local ones will still take precedence.
|
||||||
|
|
||||||
Optionally the delimiters can be defined:
|
Optionally the delimiters can be defined:
|
||||||
|
|
||||||
```markdown
|
```markdown
|
||||||
@ -767,6 +769,7 @@ GLOBAL OPTIONS:
|
|||||||
--parents-delimiter value The delimiter used for the nested parent (default: "/") [$MARK_PARENTS_DELIMITER]
|
--parents-delimiter value The delimiter used for the nested parent (default: "/") [$MARK_PARENTS_DELIMITER]
|
||||||
--mermaid-provider value defines the mermaid provider to use. Supported options are: cloudscript, mermaid-go. (default: "cloudscript") [$MARK_MERMAID_PROVIDER]
|
--mermaid-provider value defines the mermaid provider to use. Supported options are: cloudscript, mermaid-go. (default: "cloudscript") [$MARK_MERMAID_PROVIDER]
|
||||||
--mermaid-scale value defines the scaling factor for mermaid renderings. (default: 1) [$MARK_MERMAID_SCALE]
|
--mermaid-scale value defines the scaling factor for mermaid renderings. (default: 1) [$MARK_MERMAID_SCALE]
|
||||||
|
--include-path value Path for shared includes, used as a fallback if the include doesn't exist in the current directory. [$MARK_INCLUDE_PATH]
|
||||||
--help, -h show help
|
--help, -h show help
|
||||||
--version, -v print the version
|
--version, -v print the version
|
||||||
```
|
```
|
||||||
|
9
main.go
9
main.go
@ -173,6 +173,13 @@ var flags = []cli.Flag{
|
|||||||
Usage: "defines the scaling factor for mermaid renderings.",
|
Usage: "defines the scaling factor for mermaid renderings.",
|
||||||
EnvVars: []string{"MARK_MERMAID_SCALE"},
|
EnvVars: []string{"MARK_MERMAID_SCALE"},
|
||||||
}),
|
}),
|
||||||
|
altsrc.NewStringFlag(&cli.StringFlag{
|
||||||
|
Name: "include-path",
|
||||||
|
Value: "",
|
||||||
|
Usage: "Path for shared includes, used as a fallback if the include doesn't exist in the current directory.",
|
||||||
|
TakesFile: true,
|
||||||
|
EnvVars: []string{"MARK_INCLUDE_PATH"},
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@ -339,6 +346,7 @@ func processFile(
|
|||||||
for {
|
for {
|
||||||
templates, markdown, recurse, err = includes.ProcessIncludes(
|
templates, markdown, recurse, err = includes.ProcessIncludes(
|
||||||
filepath.Dir(file),
|
filepath.Dir(file),
|
||||||
|
cCtx.String("include-path"),
|
||||||
markdown,
|
markdown,
|
||||||
templates,
|
templates,
|
||||||
)
|
)
|
||||||
@ -353,6 +361,7 @@ func processFile(
|
|||||||
|
|
||||||
macros, markdown, err := macro.ExtractMacros(
|
macros, markdown, err := macro.ExtractMacros(
|
||||||
filepath.Dir(file),
|
filepath.Dir(file),
|
||||||
|
cCtx.String("include-path"),
|
||||||
markdown,
|
markdown,
|
||||||
templates,
|
templates,
|
||||||
)
|
)
|
||||||
|
@ -45,7 +45,7 @@ func TestPrepareAttachmentsWithWorkDirBase(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
attaches, err := prepareAttachments(testingOpener, ".", replacements)
|
attaches, err := prepareAttachments(testingOpener, ".", replacements)
|
||||||
t.Logf("attatches: %s", err)
|
t.Logf("attaches: %s", err)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
println(err.Error())
|
println(err.Error())
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
|
@ -28,6 +28,7 @@ var reIncludeDirective = regexp.MustCompile(
|
|||||||
|
|
||||||
func LoadTemplate(
|
func LoadTemplate(
|
||||||
base string,
|
base string,
|
||||||
|
includePath string,
|
||||||
path string,
|
path string,
|
||||||
left string,
|
left string,
|
||||||
right string,
|
right string,
|
||||||
@ -46,12 +47,17 @@ func LoadTemplate(
|
|||||||
|
|
||||||
body, err := os.ReadFile(filepath.Join(base, path))
|
body, err := os.ReadFile(filepath.Join(base, path))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = facts.Format(
|
if includePath != "" {
|
||||||
err,
|
body, err = os.ReadFile(filepath.Join(includePath, path))
|
||||||
"unable to read template file",
|
}
|
||||||
)
|
if err != nil {
|
||||||
|
err = facts.Format(
|
||||||
|
err,
|
||||||
|
"unable to read template file",
|
||||||
|
)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
body = bytes.ReplaceAll(
|
body = bytes.ReplaceAll(
|
||||||
@ -75,6 +81,7 @@ func LoadTemplate(
|
|||||||
|
|
||||||
func ProcessIncludes(
|
func ProcessIncludes(
|
||||||
base string,
|
base string,
|
||||||
|
includePath string,
|
||||||
contents []byte,
|
contents []byte,
|
||||||
templates *template.Template,
|
templates *template.Template,
|
||||||
) (*template.Template, []byte, bool, error) {
|
) (*template.Template, []byte, bool, error) {
|
||||||
@ -141,10 +148,9 @@ func ProcessIncludes(
|
|||||||
|
|
||||||
log.Tracef(vardump(facts, data), "including template %q", path)
|
log.Tracef(vardump(facts, data), "including template %q", path)
|
||||||
|
|
||||||
templates, err = LoadTemplate(base, path, left, right, templates)
|
templates, err = LoadTemplate(base, includePath, path, left, right, templates)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = facts.Format(err, "unable to load template")
|
err = facts.Format(err, "unable to load template")
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,6 +106,7 @@ func (macro *Macro) configure(node interface{}, groups [][]byte) interface{} {
|
|||||||
|
|
||||||
func ExtractMacros(
|
func ExtractMacros(
|
||||||
base string,
|
base string,
|
||||||
|
includePath string,
|
||||||
contents []byte,
|
contents []byte,
|
||||||
templates *template.Template,
|
templates *template.Template,
|
||||||
) ([]Macro, []byte, error) {
|
) ([]Macro, []byte, error) {
|
||||||
@ -167,7 +168,7 @@ func ExtractMacros(
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
macro.Template, err = includes.LoadTemplate(base, template, "{{", "}}", templates)
|
macro.Template, err = includes.LoadTemplate(base, includePath, template, "{{", "}}", templates)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = karma.Format(err, "unable to load template")
|
err = karma.Format(err, "unable to load template")
|
||||||
|
|
||||||
|
@ -41,6 +41,7 @@ func macros(templates *template.Template) ([]macro.Macro, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
macros, _, err := macro.ExtractMacros(
|
macros, _, err := macro.ExtractMacros(
|
||||||
|
"",
|
||||||
"",
|
"",
|
||||||
text(
|
text(
|
||||||
`<!-- Macro: @\{([^}]+)\}`,
|
`<!-- Macro: @\{([^}]+)\}`,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user