From 9a4f9ff32dd3ec9005bee177fc4be6d5f8250aee Mon Sep 17 00:00:00 2001 From: Dreampuf Date: Wed, 2 Feb 2022 18:02:01 +0800 Subject: [PATCH] Add subdirectories related templates (#158) --- main.go | 3 ++- pkg/mark/includes/templates.go | 6 ++++-- pkg/mark/macro/macro.go | 3 ++- pkg/mark/stdlib/stdlib.go | 1 + 4 files changed, 9 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 2a3fc84..0812e5e 100644 --- a/main.go +++ b/main.go @@ -209,6 +209,7 @@ func processFile( for { templates, markdown, recurse, err = includes.ProcessIncludes( + filepath.Dir(file), markdown, templates, ) @@ -221,7 +222,7 @@ func processFile( } } - macros, markdown, err := macro.ExtractMacros(markdown, templates) + macros, markdown, err := macro.ExtractMacros(filepath.Dir(file), markdown, templates) if err != nil { log.Fatal(err) } diff --git a/pkg/mark/includes/templates.go b/pkg/mark/includes/templates.go index 75725a0..06aada8 100644 --- a/pkg/mark/includes/templates.go +++ b/pkg/mark/includes/templates.go @@ -21,6 +21,7 @@ var reIncludeDirective = regexp.MustCompile( `(?s)`) func LoadTemplate( + base string, path string, templates *template.Template, ) (*template.Template, error) { @@ -35,7 +36,7 @@ func LoadTemplate( var body []byte - body, err := ioutil.ReadFile(path) + body, err := ioutil.ReadFile(filepath.Join(base, path)) if err != nil { err = facts.Format( err, @@ -65,6 +66,7 @@ func LoadTemplate( } func ProcessIncludes( + base string, contents []byte, templates *template.Template, ) (*template.Template, []byte, bool, error) { @@ -122,7 +124,7 @@ func ProcessIncludes( log.Tracef(vardump(facts, data), "including template %q", path) - templates, err = LoadTemplate(path, templates) + templates, err = LoadTemplate(base, path, templates) if err != nil { err = facts.Format(err, "unable to load template") diff --git a/pkg/mark/macro/macro.go b/pkg/mark/macro/macro.go index 796061f..a88fc47 100644 --- a/pkg/mark/macro/macro.go +++ b/pkg/mark/macro/macro.go @@ -105,6 +105,7 @@ func (macro *Macro) configure(node interface{}, groups [][]byte) interface{} { } func ExtractMacros( + base string, contents []byte, templates *template.Template, ) ([]Macro, []byte, error) { @@ -133,7 +134,7 @@ func ExtractMacros( macro Macro ) - macro.Template, err = includes.LoadTemplate(template, templates) + macro.Template, err = includes.LoadTemplate(base, template, templates) if err != nil { err = karma.Format(err, "unable to load template") diff --git a/pkg/mark/stdlib/stdlib.go b/pkg/mark/stdlib/stdlib.go index 01f6f4e..463d638 100644 --- a/pkg/mark/stdlib/stdlib.go +++ b/pkg/mark/stdlib/stdlib.go @@ -41,6 +41,7 @@ func macros(templates *template.Template) ([]macro.Macro, error) { } macros, _, err := macro.ExtractMacros( + "", []byte(text( `