Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
32 changes: 31 additions & 1 deletion internal/got/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
39 changes: 31 additions & 8 deletions internal/got/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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))
Expand All @@ -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 {
Expand Down
35 changes: 35 additions & 0 deletions internal/testdata/expected/TestPredefines.out
Original file line number Diff line number Diff line change
Expand Up @@ -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

21 changes: 21 additions & 0 deletions internal/testdata/src/inc/predef.inc
Original file line number Diff line number Diff line change
@@ -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}}
13 changes: 13 additions & 0 deletions internal/testdata/src/inc/predef2.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{# Checking that predefines work from included file }}

{{templateName}}
{{templateRoot}}
{{templateParent}}
{{outName}}
{{outRoot}}
{{outParent}}
{{includeName}}
{{includeRoot}}
{{includeParent}}


3 changes: 2 additions & 1 deletion internal/testdata/src/inc/runner.inc
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ func {{name}}(_w io.Writer) (err error) {

func init() {
registry.RegisterTest({{name}}, "{{name}}")
}
}

1 change: 1 addition & 0 deletions internal/testdata/src/inc/testInclude2.inc
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ html paragraphs and breaks inserted.
{{# Including a relative path that should be over-ridden }}
{{: "incSub/incSub.inc" }}
}}

7 changes: 7 additions & 0 deletions internal/testdata/src/testPredefines.tpl.got
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
{{outName}}
{{outRoot}}
{{outParent}}
{{# making sure defaults are there }}
{{includeName}}
{{includeRoot}}
{{includeParent}}

{{: "predef.inc" }}

}}
{{end body}}

Expand Down
Loading