pongo2 as template engine

This commit is contained in:
Sebastian Frank 2019-02-14 13:46:33 +01:00
parent aeb7517473
commit 4df0b5e31f
Signed by: apairon
GPG Key ID: 7270D06DDA7FE8C3
16 changed files with 142 additions and 134 deletions

6
.gitmodules vendored
View File

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

View File

@ -1,5 +1,5 @@
{
"files.associations": {
"*.html": "handlebars"
"**/templates/*.html": "django-html"
}
}

View File

@ -1,5 +1,5 @@
---
Template: intro.html
Template: base_intro.html
---

View File

@ -7,7 +7,7 @@ Meta:
Description: "global description of example website"
Keywords: "global keywords"
Template: index.html
Template: base.html
Path:
Strip: "^[0-9]*_(.*)"

View File

@ -5,7 +5,7 @@ Meta:
Data:
bild: bild.jpg
Template: mitBild.html
Template: base_multipart.html
---

View File

@ -0,0 +1,82 @@
<html>
<meta>
<meta charset="UTF-8">
<title>{{ Meta.Title }}</title>
<meta name="description" content="{{ Meta.Description }}" />
<meta name="keywords" content="{{ Meta.Keywords }}" />
<link rel="stylesheet" type="text/css" href="../assets/css/main.css">
</meta>
<body>
{% block header %}
<header>
<div class="langSelect">
{% for nav in NavSlice %}
<a href="{{ nav.GoTo }}" {% if nav.Active %}class="active"{% endif %}><img src="../assets/img/{{ nav.Navname }}.png" alt="{{ nav.Navname }}" style="height: 20px;"></a>
{% endfor %}
</div>
<div id="logoDiv"><img src="../assets/img/logo.png"></div>
<div id="mainNavDiv" class="nav">
<table border="0" style="width: 100%">
<tr>
<td>
<div><b>main Level 1 ...</b></div>
<ul>
{% for nav in NavActive.0.SubMap.main.SubSlice %}
<li {% if nav.Active %}class="active"{% endif %}><a href="{{ nav.GoTo }}" title="{{ nav.This.Data.hoverText }}">{{ nav.Navname }}</a>
{% if nav.SubSlice %}
<ul>
{% for nav2 in SubSlice %}
<li {% if nav2.Active %}class="active"{% endif %}><a href="{{ nav2.GoTo }}" title="{{ nav2.This.Data.hoverText }}">{{ nav2.Navname }}</a>
</li>
{% endfor %}
</ul
{% endif %}
</li>
{% endfor %}
</ul>
</td>
<td>
<div><b>main/service Level 2</b></div>
<ul>
{% for nav in NavActive.2.SubSlice %}
<li {% if nav.Active %}class="active"{% endif %}><a href="{{ nav.GoTo }}" title="{{ nav.This.Data.hoverText }}">{{ nav.Navname }}</a>
</li>
{% endfor %}
</ul>
</td>
</tr>
</table>
</div>
</header>
{% endblock %}
{% block breadcrumb %}
<div id="breadcrumb">
{% for nav in NavActive %}
<a href="{{ nav.GoTo }}" title="{{ nav.This.Data.hoverText }}">{{ nav.Navname }}</a>
{% endfor %}
</div>
{% endblock %}
{% block content %}
<div id="content">
{{ Body }}
</div>
{% endblock %}
{% block footer %}
<footer class="nav">
<div><b>service Level 1</b></div>
<ul>
{% for nav in NavActive.0.SubMap.service.SubSlice %}
<li {% if nav.Active %}class="active"{% endif %}><a href="{{ nav.GoTo }}" title="{{ nav.This.Data.hoverText }}">{{ nav.Navname }}</a></li>
{% endfor %}
</ul>
</footer>
{% endblock %}
</body>
</html>

View File

@ -0,0 +1,22 @@
{% extends 'base.html' %}
{% block header %}
<header>
<div id="logoDiv"><img src="../assets/img/logo.png"></div>
</header>
{% endblock header %}
{% block content %}
<div id="content">
{{ Body }}
<div class="langSelect">
{% for nav in NavSlice %}
<a href="{{ nav.GoTo }}"><img src="../assets/img/{{ nav.Navname }}.png" alt="{{ nav.Navname }}" style="height: 100px;"></a>
{% endfor %}
</div>
</div>
{% endblock content %}
{% block footer %}
{% endblock footer %}

View File

@ -0,0 +1,17 @@
{% extends 'base.html' %}
{% block content %}
<div id="content">
{% if Data.bild %}
<img src="{{ Data.bild }}" id="dataImg">
{% endif %}
{{ BodyParts.0 }}
{% if BodyParts.1 %}
<div class="info">{{ BodyParts.1 }}</div>
{% endif %}
</div>
{% endblock content %}

View File

@ -1,12 +0,0 @@
<html>
{{> meta.html }}
<body>
{{> header.html }}
<div id="content">
{{{ Body }}}
</div>
{{> footer.html }}
</body>
</html>

View File

@ -1,19 +0,0 @@
<html>
{{> meta.html }}
<body>
<header>
<div id="logoDiv"><img src="../assets/img/logo.png"></div>
</header>
<div id="content">
{{{ Body }}}
<div class="langSelect">
{{#each NavSlice }}
<a href="{{ GoTo }}"><img src="../assets/img/{{ Navname }}.png" alt="{{ Navname }}" style="height: 100px;"></a>
{{/each }}
</div>
</div>
</body>
</html>

View File

@ -1,8 +0,0 @@
<footer class="nav">
<div><b>service Level 1</b></div>
<ul>
{{#each NavMap.de.SubMap.service.SubSlice }}
<li {{#if Active }}class="active"{{/if }}><a href="{{ GoTo }}" title="{{ This.Data.hoverText }}">{{ Navname }}</a></li>
{{/each}}
</ul>
</footer>

View File

@ -1,47 +0,0 @@
<header>
<div class="langSelect">
{{#each NavSlice }}
<a href="{{ GoTo }}" {{#if Active}}class="active"{{/if}}><img src="../assets/img/{{ Navname }}.png" alt="{{ Navname }}" style="height: 20px;"></a>
{{/each }}
</div>
<div id="logoDiv"><img src="../assets/img/logo.png"></div>
<div id="mainNavDiv" class="nav">
<table border="0" style="width: 100%">
<tr>
<td>
<div><b>main Level 1 ...</b></div>
<ul>
{{#each NavActive.[0].SubMap.main.SubSlice }}
<li {{#if Active }}class="active"{{/if }}><a href="{{ GoTo }}" title="{{ This.Data.hoverText }}">{{ Navname }}</a>
{{#if SubSlice }}
<ul>
{{#each SubSlice }}
<li {{#if Active }}class="active"{{/if }}><a href="{{ GoTo }}" title="{{ This.Data.hoverText }}">{{ Navname }}</a>
</li>
{{/each }}
</ul
{{/if }}
</li>
{{/each }}
</ul>
</td>
<td>
<div><b>main/service Level 2</b></div>
<ul>
{{#each NavActive.[2].SubSlice }}
<li {{#if Active }}class="active"{{/if }}><a href="{{ GoTo }}" title="{{ This.Data.hoverText }}">{{ Navname }}</a>
</li>
{{/each }}
</ul>
</td>
</tr>
</table>
</div>
</header>
<div id="breadcrumb">
{{#each NavActive }}
<a href="{{ GoTo }}" title="{{ This.Data.hoverText }}">{{ Navname }}</a>
{{/each}}
</div>

View File

@ -1,8 +0,0 @@
<meta>
<meta charset="UTF-8">
<title>{{ Meta.Title }}</title>
<meta name="description" content="{{ Meta.Description }}" />
<meta name="keywords" content="{{ Meta.Keywords }}" />
<link rel="stylesheet" type="text/css" href="../assets/css/main.css">
</meta>

45
main.go
View File

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

1
vendor/github.com/flosch/pongo2 generated vendored Submodule

@ -0,0 +1 @@
Subproject commit 79872a7b27692599b259dc751bed8b03581dd0de

1
vendor/github.com/juju/errors generated vendored Submodule

@ -0,0 +1 @@
Subproject commit e65537c515d77e35697c471d6c2755375cb3adc4