Skip to content

Commit

Permalink
feat: Add fallback directory to load includes from
Browse files Browse the repository at this point in the history
  • Loading branch information
mrueg committed Oct 19, 2023
1 parent 8e16f6f commit d269369
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 9 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ to the template relative to current working dir, e.g.:
<!-- 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:

```markdown
Expand Down Expand Up @@ -767,6 +769,7 @@ GLOBAL OPTIONS:
--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-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
--version, -v print the version
```
Expand Down
9 changes: 9 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,13 @@ var flags = []cli.Flag{
Usage: "defines the scaling factor for mermaid renderings.",
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() {
Expand Down Expand Up @@ -339,6 +346,7 @@ func processFile(
for {
templates, markdown, recurse, err = includes.ProcessIncludes(
filepath.Dir(file),
cCtx.String("include-path"),
markdown,
templates,
)
Expand All @@ -353,6 +361,7 @@ func processFile(

macros, markdown, err := macro.ExtractMacros(
filepath.Dir(file),
cCtx.String("include-path"),
markdown,
templates,
)
Expand Down
2 changes: 1 addition & 1 deletion pkg/mark/attachment/attachment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestPrepareAttachmentsWithWorkDirBase(t *testing.T) {
}

attaches, err := prepareAttachments(testingOpener, ".", replacements)
t.Logf("attatches: %s", err)
t.Logf("attaches: %s", err)
if err != nil {
println(err.Error())
t.Fatal(err)
Expand Down
20 changes: 13 additions & 7 deletions pkg/mark/includes/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var reIncludeDirective = regexp.MustCompile(

func LoadTemplate(
base string,
includePath string,
path string,
left string,
right string,
Expand All @@ -46,12 +47,17 @@ func LoadTemplate(

body, err := os.ReadFile(filepath.Join(base, path))
if err != nil {
err = facts.Format(
err,
"unable to read template file",
)
if includePath != "" {
body, err = os.ReadFile(filepath.Join(includePath, path))
}
if err != nil {
err = facts.Format(
err,
"unable to read template file",
)
return nil, err
}

return nil, err
}

body = bytes.ReplaceAll(
Expand All @@ -75,6 +81,7 @@ func LoadTemplate(

func ProcessIncludes(
base string,
includePath string,
contents []byte,
templates *template.Template,
) (*template.Template, []byte, bool, error) {
Expand Down Expand Up @@ -141,10 +148,9 @@ func ProcessIncludes(

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 {
err = facts.Format(err, "unable to load template")

return nil
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/mark/macro/macro.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ func (macro *Macro) configure(node interface{}, groups [][]byte) interface{} {

func ExtractMacros(
base string,
includePath string,
contents []byte,
templates *template.Template,
) ([]Macro, []byte, error) {
Expand Down Expand Up @@ -167,7 +168,7 @@ func ExtractMacros(
return nil
}
} else {
macro.Template, err = includes.LoadTemplate(base, template, "{{", "}}", templates)
macro.Template, err = includes.LoadTemplate(base, includePath, template, "{{", "}}", templates)
if err != nil {
err = karma.Format(err, "unable to load template")

Expand Down
1 change: 1 addition & 0 deletions pkg/mark/stdlib/stdlib.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func macros(templates *template.Template) ([]macro.Macro, error) {
}

macros, _, err := macro.ExtractMacros(
"",
"",
text(
`<!-- Macro: @\{([^}]+)\}`,
Expand Down

0 comments on commit d269369

Please sign in to comment.