Add subdirectories related templates (#158)

This commit is contained in:
Dreampuf 2022-02-02 18:02:01 +08:00 committed by GitHub
parent 2251e8ad8a
commit 9a4f9ff32d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 9 additions and 4 deletions

View File

@ -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)
}

View File

@ -21,6 +21,7 @@ var reIncludeDirective = regexp.MustCompile(
`(?s)<!--\s*Include:\s*(?P<template>\S+)\s*(\n(?P<config>.*?))?-->`)
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")

View File

@ -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")

View File

@ -41,6 +41,7 @@ func macros(templates *template.Template) ([]macro.Macro, error) {
}
macros, _, err := macro.ExtractMacros(
"",
[]byte(text(
`<!-- Macro: @\{([^}]+)\}`,
` Template: ac:link:user`,