optimized code

This commit is contained in:
Sebastian Frank 2019-03-19 11:46:21 +01:00
parent 0dfe0f8142
commit ada333a0e1
Signed by: apairon
GPG Key ID: 7270D06DDA7FE8C3
5 changed files with 18 additions and 20 deletions

View File

@ -21,7 +21,7 @@ import (
func parseImageParams(str string) (*mark2web.ImagingConfig, error) { func parseImageParams(str string) (*mark2web.ImagingConfig, error) {
p := mark2web.ImagingConfig{} p := mark2web.ImagingConfig{}
if str == "" { if str == "" {
helper.Merge(&p, mark2web.CurrentPathConfig.Imaging) helper.Merge(&p, mark2web.CurrentTreeNode.Config.Imaging)
// Filename and Format are only valid for current image // Filename and Format are only valid for current image
p.Filename = "" p.Filename = ""
p.Format = "" p.Format = ""

View File

@ -10,7 +10,7 @@ import (
func MarkdownFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) { func MarkdownFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
chromaRenderer := false chromaRenderer := false
chromaStyle := "monokai" chromaStyle := "monokai"
if m := mark2web.CurrentPathConfig.Markdown; m != nil { if m := mark2web.CurrentTreeNode.Config.Markdown; m != nil {
if m.ChromaRenderer != nil && *m.ChromaRenderer { if m.ChromaRenderer != nil && *m.ChromaRenderer {
chromaRenderer = true chromaRenderer = true
} }

View File

@ -17,7 +17,6 @@ import (
var CurrentContext *pongo2.Context var CurrentContext *pongo2.Context
var CurrentTreeNode *TreeNode var CurrentTreeNode *TreeNode
var CurrentPathConfig *PathConfig
func NewContext() pongo2.Context { func NewContext() pongo2.Context {
ctx := pongo2.Context{ ctx := pongo2.Context{
@ -146,17 +145,17 @@ func (node *TreeNode) handleCollections() {
} }
} }
Add2Nav(node, node.Config, tpl, goTo, navname, colEl, dataKey, body, navT.Hidden) node.addSubNode(tpl, goTo, navname, colEl, dataKey, body, navT.Hidden)
} }
} }
} }
} }
func (node *TreeNode) fillConfig(inBase, outBase, dir string, conf *PathConfig) { func (node *TreeNode) fillConfig(inBase, outBase, subDir string, conf *PathConfig) {
inPath := inBase inPath := inBase
if dir != "" { if subDir != "" {
inPath += "/" + dir inPath += "/" + subDir
} }
helper.Log.Infof("reading input directory: %s", inPath) helper.Log.Infof("reading input directory: %s", inPath)
@ -194,14 +193,14 @@ func (node *TreeNode) fillConfig(inBase, outBase, dir string, conf *PathConfig)
node.Config = newConfig node.Config = newConfig
// calc outDir // calc outDir
stripedDir := dir stripedDir := subDir
var regexStr *string var regexStr *string
if newConfig.Path != nil { if newConfig.Path != nil {
regexStr = newConfig.Path.Strip regexStr = newConfig.Path.Strip
} }
if regexStr != nil && *regexStr != "" { if regexStr != nil && *regexStr != "" {
if regex, err := regexp.Compile(*regexStr); err != nil { if regex, err := regexp.Compile(*regexStr); err != nil {
helper.Log.Panicf("error compiling path.strip regex '%s' from '%s': %s", *regexStr, inBase+"/"+dir, err) helper.Log.Panicf("error compiling path.strip regex '%s' from '%s': %s", *regexStr, inBase+"/"+subDir, err)
} else { } else {
stripedDir = regex.ReplaceAllString(stripedDir, "$1") stripedDir = regex.ReplaceAllString(stripedDir, "$1")
} }
@ -223,14 +222,14 @@ func (node *TreeNode) fillConfig(inBase, outBase, dir string, conf *PathConfig)
node.handleCollections() node.handleCollections()
} }
func Add2Nav(currentNode *TreeNode, pathConfig *PathConfig, tplFilename, outDir string, navname string, ctx interface{}, dataMapKey string, body string, hidden bool) { func (node *TreeNode) addSubNode(tplFilename, subDir string, navname string, ctx interface{}, dataMapKey string, body string, hideInNav bool) {
newNode := new(TreeNode) newNode := new(TreeNode)
newNode.root = currentNode.root newNode.root = node.root
newNode.fillConfig( newNode.fillConfig(
currentNode.InputPath, node.InputPath,
currentNode.OutputPath, node.OutputPath,
outDir, subDir,
pathConfig, node.Config,
) )
if navname != "" { if navname != "" {
newNode.Config.This = ThisPathConfig{ newNode.Config.This = ThisPathConfig{
@ -263,7 +262,7 @@ func Add2Nav(currentNode *TreeNode, pathConfig *PathConfig, tplFilename, outDir
OutputFile: &indexOutFile, OutputFile: &indexOutFile,
InputString: &body, InputString: &body,
} }
newNode.Hidden = hidden newNode.Hidden = hideInNav
currentNode.Sub = append(currentNode.Sub, newNode) node.Sub = append(node.Sub, newNode)
} }

View File

@ -12,7 +12,7 @@ func RequestFn(url *pongo2.Value, args ...*pongo2.Value) *pongo2.Value {
} }
// RenderFn renders a pongo2 template with additional context // RenderFn renders a pongo2 template with additional context
func RenderFn(templateFilename, outDir, ctx *pongo2.Value, param ...*pongo2.Value) *pongo2.Value { func RenderFn(templateFilename, subDir, ctx *pongo2.Value, param ...*pongo2.Value) *pongo2.Value {
dataMapKey := "" dataMapKey := ""
body := "" body := ""
@ -25,7 +25,7 @@ func RenderFn(templateFilename, outDir, ctx *pongo2.Value, param ...*pongo2.Valu
} }
} }
Add2Nav(CurrentTreeNode, CurrentPathConfig, templateFilename.String(), outDir.String(), "", ctx.Interface(), dataMapKey, body, true) CurrentTreeNode.addSubNode(templateFilename.String(), subDir.String(), "", ctx.Interface(), dataMapKey, body, true)
return pongo2.AsValue(nil) return pongo2.AsValue(nil)
} }

View File

@ -18,7 +18,6 @@ func SetTemplateDir(dir string) {
func renderTemplate(filename string, node *TreeNode, pathConfig *PathConfig, ctx *pongo2.Context) (string, error) { func renderTemplate(filename string, node *TreeNode, pathConfig *PathConfig, ctx *pongo2.Context) (string, error) {
CurrentContext = ctx CurrentContext = ctx
CurrentTreeNode = node CurrentTreeNode = node
CurrentPathConfig = pathConfig
templateFile := templateDir + "/" + filename templateFile := templateDir + "/" + filename
template := templateCache[templateFile] template := templateCache[templateFile]
if template == nil { if template == nil {