diff --git a/.gitmodules b/.gitmodules index 5c73323..4e9b561 100644 --- a/.gitmodules +++ b/.gitmodules @@ -34,3 +34,9 @@ [submodule "vendor/github.com/otiai10/copy"] path = vendor/github.com/otiai10/copy url = https://github.com/otiai10/copy +[submodule "vendor/github.com/flosch/pongo2"] + path = vendor/github.com/flosch/pongo2 + url = https://github.com/flosch/pongo2 +[submodule "vendor/github.com/juju/errors"] + path = vendor/github.com/juju/errors + url = https://github.com/juju/errors diff --git a/.vscode/settings.json b/.vscode/settings.json index 5df254b..90409c5 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,5 +1,5 @@ { "files.associations": { - "*.html": "handlebars" + "**/templates/*.html": "django-html" } } \ No newline at end of file diff --git a/example/content/README.md b/example/content/README.md index 95a9d52..406a3d6 100644 --- a/example/content/README.md +++ b/example/content/README.md @@ -1,5 +1,5 @@ --- -Template: intro.html +Template: base_intro.html --- diff --git a/example/content/config.yml b/example/content/config.yml index 0294211..4a180e7 100644 --- a/example/content/config.yml +++ b/example/content/config.yml @@ -7,7 +7,7 @@ Meta: Description: "global description of example website" Keywords: "global keywords" -Template: index.html +Template: base.html Path: Strip: "^[0-9]*_(.*)" diff --git a/example/content/de/main/02_Leistungen_und_Referenzen/README.md b/example/content/de/main/02_Leistungen_und_Referenzen/README.md index a6438d4..b678ee1 100644 --- a/example/content/de/main/02_Leistungen_und_Referenzen/README.md +++ b/example/content/de/main/02_Leistungen_und_Referenzen/README.md @@ -5,7 +5,7 @@ Meta: Data: bild: bild.jpg -Template: mitBild.html +Template: base_multipart.html --- diff --git a/example/templates/base.html b/example/templates/base.html new file mode 100644 index 0000000..885c597 --- /dev/null +++ b/example/templates/base.html @@ -0,0 +1,82 @@ + + + + + {{ Meta.Title }} + + + + + + + {% block header %} +
+
+ {% for nav in NavSlice %} + {{ nav.Navname }} + {% endfor %} +
+
+ +
+ {% endblock %} + + + {% block breadcrumb %} + + {% endblock %} + + {% block content %} +
+ {{ Body }} +
+ {% endblock %} + + {% block footer %} + + {% endblock %} + + + diff --git a/example/templates/base_intro.html b/example/templates/base_intro.html new file mode 100644 index 0000000..64b97dd --- /dev/null +++ b/example/templates/base_intro.html @@ -0,0 +1,22 @@ +{% extends 'base.html' %} + +{% block header %} +
+
+
+{% endblock header %} + +{% block content %} +
+ {{ Body }} + +
+ {% for nav in NavSlice %} + {{ nav.Navname }} + {% endfor %} +
+
+{% endblock content %} + +{% block footer %} +{% endblock footer %} \ No newline at end of file diff --git a/example/templates/base_multipart.html b/example/templates/base_multipart.html new file mode 100644 index 0000000..9b650ad --- /dev/null +++ b/example/templates/base_multipart.html @@ -0,0 +1,17 @@ +{% extends 'base.html' %} + +{% block content %} +
+ + {% if Data.bild %} + + {% endif %} + + {{ BodyParts.0 }} + + {% if BodyParts.1 %} +
{{ BodyParts.1 }}
+ {% endif %} + +
+{% endblock content %} \ No newline at end of file diff --git a/example/templates/index.html b/example/templates/index.html deleted file mode 100644 index f47c1cf..0000000 --- a/example/templates/index.html +++ /dev/null @@ -1,12 +0,0 @@ - - {{> meta.html }} - - {{> header.html }} - -
- {{{ Body }}} -
- - {{> footer.html }} - - \ No newline at end of file diff --git a/example/templates/intro.html b/example/templates/intro.html deleted file mode 100644 index 896cf9d..0000000 --- a/example/templates/intro.html +++ /dev/null @@ -1,19 +0,0 @@ - - {{> meta.html }} - -
-
-
- -
- {{{ Body }}} - -
- {{#each NavSlice }} - {{ Navname }} - {{/each }} -
-
- - - \ No newline at end of file diff --git a/example/templates/partials/footer.html b/example/templates/partials/footer.html deleted file mode 100644 index 0fa1b58..0000000 --- a/example/templates/partials/footer.html +++ /dev/null @@ -1,8 +0,0 @@ - diff --git a/example/templates/partials/header.html b/example/templates/partials/header.html deleted file mode 100644 index e9d484e..0000000 --- a/example/templates/partials/header.html +++ /dev/null @@ -1,47 +0,0 @@ -
-
- {{#each NavSlice }} - {{ Navname }} - {{/each }} -
-
- -
- - - \ No newline at end of file diff --git a/example/templates/partials/meta.html b/example/templates/partials/meta.html deleted file mode 100644 index 5db3917..0000000 --- a/example/templates/partials/meta.html +++ /dev/null @@ -1,8 +0,0 @@ - - - - {{ Meta.Title }} - - - - diff --git a/main.go b/main.go index c445a38..d8e9ec2 100644 --- a/main.go +++ b/main.go @@ -10,8 +10,8 @@ import ( "github.com/imdario/mergo" - "github.com/aymerick/raymond" "github.com/davecgh/go-spew/spew" + "github.com/flosch/pongo2" "github.com/op/go-logging" cpy "github.com/otiai10/copy" "gopkg.in/russross/blackfriday.v2" @@ -23,7 +23,7 @@ var log = logging.MustGetLogger("myLogger") var inDir *string var outDir *string -var templateCache = make(map[string]*raymond.Template) +var templateCache = make(map[string]*pongo2.Template) // GlobalConfig is config which is used only once in root dir type GlobalConfig struct { @@ -388,9 +388,9 @@ func processContent(conf *PathConfigTree) { // use --- for splitting document in markdown parts regex := regexp.MustCompile("\\r?\\n\\r?---\\r?\\n\\r?") inputParts := regex.Split(string(input), -1) - htmlParts := make([]string, 0) + htmlParts := make([]*pongo2.Value, 0) for _, iPart := range inputParts { - htmlParts = append(htmlParts, string(blackfriday.Run([]byte(iPart)))) + htmlParts = append(htmlParts, pongo2.AsSafeValue(string(blackfriday.Run([]byte(iPart))))) } log.Debugf("rendering template '%s' for '%s'", *newConfig.Template, outFile) @@ -398,7 +398,7 @@ func processContent(conf *PathConfigTree) { template := templateCache[templateFile] if template == nil { var err error - if template, err = raymond.ParseFile(templateFile); err != nil { + if template, err = pongo2.FromFile(templateFile); err != nil { log.Panicf("could not parse template '%s': %s", templateFile, err) } else { templateCache[templateFile] = template @@ -418,16 +418,16 @@ func processContent(conf *PathConfigTree) { buildNavigation(contentConfig, &navMap, &navSlice, &navActive, curNavPath) // read yaml header as data for template - ctx := make(map[string]interface{}) + ctx := make(pongo2.Context) ctx["Meta"] = newConfig.Meta ctx["Data"] = newConfig.Data ctx["NavMap"] = navMap ctx["NavSlice"] = navSlice ctx["NavActive"] = navActive - ctx["Body"] = string(html) + ctx["Body"] = pongo2.AsSafeValue(string(html)) ctx["BodyParts"] = htmlParts - result, err := template.Exec(ctx) + result, err := template.Execute(ctx) if err != nil { log.Panicf("could not execute template '%s' for input file '%s': %s", templateFile, inFile, err) } @@ -591,7 +591,7 @@ func main() { log.Debugf("reading input directory %s", *inDir) - defaultTemplate := "index.html" + defaultTemplate := "base.html" defaultInputFile := "README.md" defaultOutputFile := "index.html" defaultPathStrip := "^[0-9]*_(.*)" @@ -615,33 +615,6 @@ func main() { //spew.Dump(navMap) - partialsPath := *inDir + "/templates/partials" - if d, err := os.Stat(partialsPath); !os.IsNotExist(err) { - if d != nil && d.IsDir() { - log.Debugf("register template partials from '%s'", partialsPath) - if entries, err := ioutil.ReadDir(partialsPath); err == nil { - for _, f := range entries { - if !f.IsDir() { - pFile := partialsPath + "/" + f.Name() - log.Infof("registering partial: %s", pFile) - pContent, err := ioutil.ReadFile(pFile) - if err != nil { - log.Panicf("could not read partial '%s': %s", pFile, err) - } - raymond.RegisterPartial(f.Name(), string(pContent)) - } - } - } else { - log.Panicf("could not read from partials directory '%s': %s", partialsPath, err) - } - } else if err == nil { - log.Panicf("template partials directory '%s' is not a directory", partialsPath) - } else { - log.Panicf("unknown error on partials directory '%s': %s", partialsPath, err) - } - - } - processContent(contentConfig) processAssets() diff --git a/vendor/github.com/flosch/pongo2 b/vendor/github.com/flosch/pongo2 new file mode 160000 index 0000000..79872a7 --- /dev/null +++ b/vendor/github.com/flosch/pongo2 @@ -0,0 +1 @@ +Subproject commit 79872a7b27692599b259dc751bed8b03581dd0de diff --git a/vendor/github.com/juju/errors b/vendor/github.com/juju/errors new file mode 160000 index 0000000..e65537c --- /dev/null +++ b/vendor/github.com/juju/errors @@ -0,0 +1 @@ +Subproject commit e65537c515d77e35697c471d6c2755375cb3adc4