mirror of
https://github.com/kovetskiy/mark.git
synced 2026-05-02 13:22:40 +00:00
feat: Add fallback directory to load includes from
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -28,6 +28,7 @@ var reIncludeDirective = regexp.MustCompile(
|
||||
|
||||
func LoadTemplate(
|
||||
base string,
|
||||
includePath string,
|
||||
path string,
|
||||
left string,
|
||||
right string,
|
||||
@@ -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(
|
||||
@@ -75,6 +81,7 @@ func LoadTemplate(
|
||||
|
||||
func ProcessIncludes(
|
||||
base string,
|
||||
includePath string,
|
||||
contents []byte,
|
||||
templates *template.Template,
|
||||
) (*template.Template, []byte, bool, error) {
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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) {
|
||||
@@ -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")
|
||||
|
||||
|
||||
@@ -41,6 +41,7 @@ func macros(templates *template.Template) ([]macro.Macro, error) {
|
||||
}
|
||||
|
||||
macros, _, err := macro.ExtractMacros(
|
||||
"",
|
||||
"",
|
||||
text(
|
||||
`<!-- Macro: @\{([^}]+)\}`,
|
||||
|
||||
Reference in New Issue
Block a user