diff --git a/README.md b/README.md index bb621ad..7c7ba7e 100644 --- a/README.md +++ b/README.md @@ -400,13 +400,19 @@ The following fragments are predefined: * `{{templateName}}` is the base name of the template file being processed, including any extensions * `{{templateRoot}}` is the base name of the template file being processed without any extensions * `{{templateParent}}` is the directory name of the template file being processed, without the preceeding path +* `{{includePath}}` is the full path of the file that the includePath fragment appears in. +* `{{includeName}}` is the base name of the file that the includeName fragment appears in. +* `{{includeRoot}}` is the base name of the file that the includeRoot fragment appears in without any extensions. +* `{{includeParent}}` is the directory name of the file that the includeParent fragment appears in without any extensions. * `{{outPath}}` is the full path of the output file being written * `{{outName}}` is the base name of the output file being written, including any extensions * `{{outRoot}}` is the base name of the output file being written without any extensions * `{{outParent}}` is the directory name of the output file being written, without the preceeding path -Note that if you are using these in an included file, these will refer to the parent file. Multiple -levels of includes will return the information for the top level file being processed. +The template* fragments refer to the top level file, the one sent to the got command to process, +while the include* fragments refer to the file being processed currently. For example, if the +"a.tmpl" file included the "b.tmpl" file, then {{includeName}} in the b.tmpl file would +produce "b.tmpl", while {{templateName}} in the b.tmpl file would produce "a.tmpl". #### Example ``` diff --git a/internal/got/lexer.go b/internal/got/lexer.go index f579cf5..6a3f18b 100644 --- a/internal/got/lexer.go +++ b/internal/got/lexer.go @@ -48,7 +48,37 @@ func lexFile(fileName string, namedBlocks: namedBlocks, // use named blocks passed in. This will add to the parent map. } - go l.run() + go func() { + // save and restore the include file info + + fp, _ := filepath.Abs(fileName) + root := strings.TrimSuffix(filepath.Base(fp), filepath.Ext(fp)) + for { + ext := filepath.Ext(root) + if ext == "" { + break + } + root = strings.TrimSuffix(root, ext) + } + + incPath := namedBlocks[blockIncludePath].text + incName := namedBlocks[blockIncludeName].text + incRoot := namedBlocks[blockIncludeRoot].text + incParent := namedBlocks[blockIncludeParent].text + + namedBlocks[blockIncludePath] = namedBlockEntry{fp, 0, locationRef{}} + namedBlocks[blockIncludeName] = namedBlockEntry{filepath.Base(fp), 0, locationRef{}} + namedBlocks[blockIncludeRoot] = namedBlockEntry{root, 0, locationRef{}} + namedBlocks[blockIncludeParent] = namedBlockEntry{filepath.Base(filepath.Dir(fp)), 0, locationRef{}} + + l.run() + + // restore + namedBlocks[blockIncludePath] = namedBlockEntry{incPath, 0, locationRef{}} + namedBlocks[blockIncludeName] = namedBlockEntry{incName, 0, locationRef{}} + namedBlocks[blockIncludeRoot] = namedBlockEntry{incRoot, 0, locationRef{}} + namedBlocks[blockIncludeParent] = namedBlockEntry{incParent, 0, locationRef{}} + }() return l } diff --git a/internal/got/runner.go b/internal/got/runner.go index 0de911d..1fb61f4 100644 --- a/internal/got/runner.go +++ b/internal/got/runner.go @@ -12,6 +12,24 @@ import ( "github.com/goradd/gofile/pkg/sys" ) +// Predefined block values +const ( + blockTemplatePath = "templatePath" + blockTemplateName = "templateName" + blockTemplateRoot = "templateRoot" + blockTemplateParent = "templateParent" + + blockOutPath = "outPath" + blockOutName = "outName" + blockOutRoot = "outRoot" + blockOutParent = "outParent" + + blockIncludePath = "includePath" + blockIncludeName = "includeName" + blockIncludeRoot = "includeRoot" + blockIncludeParent = "includeParent" +) + type namedBlockEntry struct { text string paramCount int @@ -135,6 +153,11 @@ func processFile(file, outDir string, asts []astType, runImports bool) error { } // Default named block values + namedBlocks[blockIncludePath] = namedBlockEntry{"", 0, locationRef{}} + namedBlocks[blockIncludeName] = namedBlockEntry{"", 0, locationRef{}} + namedBlocks[blockIncludeRoot] = namedBlockEntry{"", 0, locationRef{}} + namedBlocks[blockIncludeParent] = namedBlockEntry{"", 0, locationRef{}} + file, _ = filepath.Abs(file) root := strings.TrimSuffix(filepath.Base(file), filepath.Ext(file)) for { @@ -145,10 +168,10 @@ func processFile(file, outDir string, asts []astType, runImports bool) error { root = strings.TrimSuffix(root, ext) } - namedBlocks["templatePath"] = namedBlockEntry{file, 0, locationRef{}} - namedBlocks["templateName"] = namedBlockEntry{filepath.Base(file), 0, locationRef{}} - namedBlocks["templateRoot"] = namedBlockEntry{root, 0, locationRef{}} - namedBlocks["templateParent"] = namedBlockEntry{filepath.Base(filepath.Dir(file)), 0, locationRef{}} + namedBlocks[blockTemplatePath] = namedBlockEntry{file, 0, locationRef{}} + namedBlocks[blockTemplateName] = namedBlockEntry{filepath.Base(file), 0, locationRef{}} + namedBlocks[blockTemplateRoot] = namedBlockEntry{root, 0, locationRef{}} + namedBlocks[blockTemplateParent] = namedBlockEntry{filepath.Base(filepath.Dir(file)), 0, locationRef{}} newPath, _ = filepath.Abs(newPath) root = strings.TrimSuffix(filepath.Base(newPath), filepath.Ext(newPath)) @@ -160,10 +183,10 @@ func processFile(file, outDir string, asts []astType, runImports bool) error { root = strings.TrimSuffix(root, ext) } - namedBlocks["outPath"] = namedBlockEntry{newPath, 0, locationRef{}} - namedBlocks["outName"] = namedBlockEntry{filepath.Base(newPath), 0, locationRef{}} - namedBlocks["outRoot"] = namedBlockEntry{root, 0, locationRef{}} - namedBlocks["outParent"] = namedBlockEntry{filepath.Base(filepath.Dir(newPath)), 0, locationRef{}} + namedBlocks[blockOutPath] = namedBlockEntry{newPath, 0, locationRef{}} + namedBlocks[blockOutName] = namedBlockEntry{filepath.Base(newPath), 0, locationRef{}} + namedBlocks[blockOutRoot] = namedBlockEntry{root, 0, locationRef{}} + namedBlocks[blockOutParent] = namedBlockEntry{filepath.Base(filepath.Dir(newPath)), 0, locationRef{}} a, err := buildAst(file, namedBlocks) if err != nil { diff --git a/internal/testdata/expected/TestPredefines.out b/internal/testdata/expected/TestPredefines.out index b61467c..546a753 100755 --- a/internal/testdata/expected/TestPredefines.out +++ b/internal/testdata/expected/TestPredefines.out @@ -4,3 +4,38 @@ src testPredefines.tpl.go testPredefines template +runner.inc +runner +inc + + +testPredefines.tpl.got +testPredefines +src +testPredefines.tpl.go +testPredefines +template +predef.inc +predef +inc + + +testPredefines.tpl.got +testPredefines +src +testPredefines.tpl.go +testPredefines +template +predef2.inc +predef2 +inc + + + +testPredefines.tpl.go +testPredefines +template +predef.inc +predef +inc + diff --git a/internal/testdata/src/inc/predef.inc b/internal/testdata/src/inc/predef.inc new file mode 100644 index 0000000..21ca886 --- /dev/null +++ b/internal/testdata/src/inc/predef.inc @@ -0,0 +1,21 @@ +{{# Checking that predefines work from included file }} + +{{templateName}} +{{templateRoot}} +{{templateParent}} +{{outName}} +{{outRoot}} +{{outParent}} +{{includeName}} +{{includeRoot}} +{{includeParent}} + +{{: "predef2.inc" }} + +{{# verifying values are restored after the included file }} +{{outName}} +{{outRoot}} +{{outParent}} +{{includeName}} +{{includeRoot}} +{{includeParent}} diff --git a/internal/testdata/src/inc/predef2.inc b/internal/testdata/src/inc/predef2.inc new file mode 100644 index 0000000..5264a37 --- /dev/null +++ b/internal/testdata/src/inc/predef2.inc @@ -0,0 +1,13 @@ +{{# Checking that predefines work from included file }} + +{{templateName}} +{{templateRoot}} +{{templateParent}} +{{outName}} +{{outRoot}} +{{outParent}} +{{includeName}} +{{includeRoot}} +{{includeParent}} + + diff --git a/internal/testdata/src/inc/runner.inc b/internal/testdata/src/inc/runner.inc index 846f535..d9271aa 100644 --- a/internal/testdata/src/inc/runner.inc +++ b/internal/testdata/src/inc/runner.inc @@ -16,4 +16,5 @@ func {{name}}(_w io.Writer) (err error) { func init() { registry.RegisterTest({{name}}, "{{name}}") -} \ No newline at end of file +} + diff --git a/internal/testdata/src/inc/testInclude2.inc b/internal/testdata/src/inc/testInclude2.inc index df7d679..7269c7c 100644 --- a/internal/testdata/src/inc/testInclude2.inc +++ b/internal/testdata/src/inc/testInclude2.inc @@ -15,3 +15,4 @@ html paragraphs and breaks inserted. {{# Including a relative path that should be over-ridden }} {{: "incSub/incSub.inc" }} }} + diff --git a/internal/testdata/src/testPredefines.tpl.got b/internal/testdata/src/testPredefines.tpl.got index 3f246b7..c9dfb8d 100644 --- a/internal/testdata/src/testPredefines.tpl.got +++ b/internal/testdata/src/testPredefines.tpl.got @@ -11,6 +11,13 @@ {{outName}} {{outRoot}} {{outParent}} +{{# making sure defaults are there }} +{{includeName}} +{{includeRoot}} +{{includeParent}} + +{{: "predef.inc" }} + }} {{end body}}