diff --git a/.gitmodules b/.gitmodules index b5122f9..67f2634 100644 --- a/.gitmodules +++ b/.gitmodules @@ -25,3 +25,9 @@ [submodule "vendor/github.com/davecgh/go-spew"] path = vendor/github.com/davecgh/go-spew url = https://github.com/davecgh/go-spew +[submodule "vendor/github.com/aymerick/raymond"] + path = vendor/github.com/aymerick/raymond + url = https://github.com/aymerick/raymond +[submodule "vendor/github.com/imdario/mergo"] + path = vendor/github.com/imdario/mergo + url = https://github.com/imdario/mergo diff --git a/example/config.yml b/example/config.yml index d7ba927..23ce083 100644 --- a/example/config.yml +++ b/example/config.yml @@ -1,7 +1,7 @@ -webserver: - type: "apache" # generates .htaccess +Webserver: + Type: "apache" # generates .htaccess -assets: - path: "/assets" - deployType: "symlink" # symlink, copy or move - fixTemplate: True # change path in html templates, no used +Assets: + Path: "/assets" + DeployType: "symlink" # symlink, copy or move + FixTemplate: True # change path in html templates, no used diff --git a/example/content/config.yml b/example/content/config.yml index 384c9a0..7db9a63 100644 --- a/example/content/config.yml +++ b/example/content/config.yml @@ -1,20 +1,23 @@ -goTo: "/de/main/home" +This: + GoTo: "/de/main/home" -index: - inputFile: "README.md" - outputFile: "index.html" +Index: + InputFile: "README.md" + OutputFile: "index.html" -meta: - title: "global title" - description: "global description of example website" - keywords: "global keywords" +Meta: + Title: "global title" + Description: "global description of example website" + Keywords: "global keywords" -path: - strip: "^[0-9]*_(.*)" - ignoreForNav: "^_" +Template: index.html -filename: - strip: "(.*).md$" - ignore: "^_" - outputExtension: "html" +Path: + Strip: "^[0-9]*_(.*)" + IgnoreForNav: "^_" + +Filename: + Strip: "(.*).md$" + Ignore: "^_" + OutputExtension: "html" diff --git a/example/content/de/main/01_home/README.md b/example/content/de/main/01_home/README.md index 59914c1..dd7ef4a 100644 --- a/example/content/de/main/01_home/README.md +++ b/example/content/de/main/01_home/README.md @@ -1,5 +1,6 @@ --- -navname: Home +Meta: + Title: Home --- diff --git a/example/content/de/main/02_leistungen/README.md b/example/content/de/main/02_leistungen/README.md index b782437..c8b6e57 100644 --- a/example/content/de/main/02_leistungen/README.md +++ b/example/content/de/main/02_leistungen/README.md @@ -1,5 +1,6 @@ --- -navname: Leistungen +Meta: + Title: Leistungen --- diff --git a/example/content/de/main/03_kontakt/01_adresse/README.md b/example/content/de/main/03_kontakt/01_adresse/README.md index d64bab2..a80ab4b 100644 --- a/example/content/de/main/03_kontakt/01_adresse/README.md +++ b/example/content/de/main/03_kontakt/01_adresse/README.md @@ -1,5 +1,6 @@ --- -navname: Adresse +Meta: + Title: Adresse --- diff --git a/example/content/de/main/03_kontakt/02_anfahrt/README.md b/example/content/de/main/03_kontakt/02_anfahrt/README.md index 5d9a654..e355a1d 100644 --- a/example/content/de/main/03_kontakt/02_anfahrt/README.md +++ b/example/content/de/main/03_kontakt/02_anfahrt/README.md @@ -1,5 +1,6 @@ --- -navname: Anfahrt +Meta: + Title: Anfahrt --- diff --git a/example/content/de/main/03_kontakt/config.yml b/example/content/de/main/03_kontakt/config.yml index 8d44797..6ace8a4 100644 --- a/example/content/de/main/03_kontakt/config.yml +++ b/example/content/de/main/03_kontakt/config.yml @@ -1 +1,2 @@ -goTo: adresse/ \ No newline at end of file +This: + GoTo: adresse/ \ No newline at end of file diff --git a/example/content/de/main/04_impressum/config.yml b/example/content/de/main/04_impressum/config.yml index aa58544..ebfb866 100644 --- a/example/content/de/main/04_impressum/config.yml +++ b/example/content/de/main/04_impressum/config.yml @@ -1 +1 @@ -goTo: /de/service/impressum/ \ No newline at end of file +GoTo: /de/service/impressum/ \ No newline at end of file diff --git a/example/content/de/service/01_datenschutz/README.md b/example/content/de/service/01_datenschutz/README.md index 7dfae0a..b435f59 100644 --- a/example/content/de/service/01_datenschutz/README.md +++ b/example/content/de/service/01_datenschutz/README.md @@ -1,5 +1,6 @@ --- -navname: Datenschutz +Meta: + Title: Datenschutz --- diff --git a/example/content/de/service/02_agb/README.md b/example/content/de/service/02_agb/README.md index c463439..38cae53 100644 --- a/example/content/de/service/02_agb/README.md +++ b/example/content/de/service/02_agb/README.md @@ -1,5 +1,6 @@ --- -navname: AGB's +Meta: + Title: AGB's --- diff --git a/example/content/de/service/03_impressum/README.md b/example/content/de/service/03_impressum/README.md index 8b11259..462d02e 100644 --- a/example/content/de/service/03_impressum/README.md +++ b/example/content/de/service/03_impressum/README.md @@ -1,5 +1,6 @@ --- -navname: Impressum +Meta: + Title: Impressum --- diff --git a/example/content/en/main/01_home/README.md b/example/content/en/main/01_home/README.md index 59914c1..dd7ef4a 100644 --- a/example/content/en/main/01_home/README.md +++ b/example/content/en/main/01_home/README.md @@ -1,5 +1,6 @@ --- -navname: Home +Meta: + Title: Home --- diff --git a/example/content/en/main/02_services/README.md b/example/content/en/main/02_services/README.md index 5803efc..6cbe4cf 100644 --- a/example/content/en/main/02_services/README.md +++ b/example/content/en/main/02_services/README.md @@ -1,5 +1,6 @@ --- -navname: Services +Meta: + Title: Services --- diff --git a/example/content/en/main/03_contact/README.md b/example/content/en/main/03_contact/README.md index 328e299..4c03780 100644 --- a/example/content/en/main/03_contact/README.md +++ b/example/content/en/main/03_contact/README.md @@ -1,5 +1,6 @@ --- -navname: Contact +Meta: + Title: Contact --- diff --git a/example/content/en/main/04_imprint/config.yml b/example/content/en/main/04_imprint/config.yml index 28d09f9..81c5818 100644 --- a/example/content/en/main/04_imprint/config.yml +++ b/example/content/en/main/04_imprint/config.yml @@ -1 +1,2 @@ -goTo: /en/service/imprint/ \ No newline at end of file +This: + GoTo: /en/service/imprint/ \ No newline at end of file diff --git a/example/content/en/service/01_terms/README.md b/example/content/en/service/01_terms/README.md index b758afd..7ad67ed 100644 --- a/example/content/en/service/01_terms/README.md +++ b/example/content/en/service/01_terms/README.md @@ -1,5 +1,6 @@ --- -navname: Terms +Meta: + Title: Terms --- diff --git a/example/content/en/service/02_imprint/README.md b/example/content/en/service/02_imprint/README.md index 05bd9ec..ea1e711 100644 --- a/example/content/en/service/02_imprint/README.md +++ b/example/content/en/service/02_imprint/README.md @@ -1,5 +1,6 @@ --- -navname: Imprint +Meta: + Title: Imprint --- diff --git a/example/templates/index.html b/example/templates/index.html new file mode 100644 index 0000000..c42d219 --- /dev/null +++ b/example/templates/index.html @@ -0,0 +1,8 @@ + + + {{ Meta.Title }} + + + {{{ Body }}} + + \ No newline at end of file diff --git a/main.go b/main.go index ed1b8db..39a0ce1 100644 --- a/main.go +++ b/main.go @@ -8,7 +8,9 @@ import ( "regexp" "strings" + "github.com/aymerick/raymond" "github.com/davecgh/go-spew/spew" + "github.com/imdario/mergo" "github.com/op/go-logging" "gopkg.in/russross/blackfriday.v2" "gopkg.in/yaml.v2" @@ -19,47 +21,53 @@ var log = logging.MustGetLogger("myLogger") var inDir *string var outDir *string +var templateCache = make(map[string]*raymond.Template) + // GlobalConfig is config which is used only once in root dir type GlobalConfig struct { Webserver struct { - Type *string `yaml:"type"` - } `yaml:"webserver"` + Type *string `yaml:"Type"` + } `yaml:"Webserver"` Assets struct { - Path *string `yaml:"path"` - DeployType *string `yaml:"deployType"` - FixTemplate *bool `yaml:"fixTemplate"` - } `yaml:"assets"` + Path *string `yaml:"Path"` + DeployType *string `yaml:"DeployType"` + FixTemplate *bool `yaml:"FixTemplate"` + } `yaml:"Assets"` } var globalConfig = new(GlobalConfig) // PathConfig of subdir type PathConfig struct { - Navname *string `yaml:"navname"` - GoTo *string `yaml:"goTo"` + This struct { + Navname *string `yaml:"Navname"` + GoTo *string `yaml:"GoTo"` + } `yaml:"This"` + + Template *string `yaml:"Template"` Index struct { - InputFile *string `yaml:"inputFile"` - OutputFile *string `yaml:"outputFile"` - } `yaml:"index"` + InputFile *string `yaml:"InputFile"` + OutputFile *string `yaml:"OutputFile"` + } `yaml:"Index"` Meta struct { - Title *string `yaml:"title"` - Description *string `yaml:"description"` - Keywords *string `yaml:"keywords"` - } `yaml:"meta"` + Title *string `yaml:"Title"` + Description *string `yaml:"Description"` + Keywords *string `yaml:"Keywords"` + } `yaml:"Meta"` Path struct { - Strip *string `yaml:"strip"` - IgnoreForNav *string `yaml:"ignoreForNav"` - } `yaml:"path"` + Strip *string `yaml:"Strip"` + IgnoreForNav *string `yaml:"IgnoreForNav"` + } `yaml:"Path"` Filename struct { - Strip *string `yaml:"strip"` - Ignore *string `yaml:"ignore"` - OutputExtension *string `yaml:"outputExtension"` - } `yaml:"filename"` + Strip *string `yaml:"Strip"` + Ignore *string `yaml:"Ignore"` + OutputExtension *string `yaml:"OutputExtension"` + } `yaml:"Filename"` } // PathConfigTree is complete config tree of content dir @@ -74,7 +82,15 @@ type PathConfigTree struct { } func mergeConfig(mergeInto *PathConfig, mergeFrom *PathConfig) { - // goto is individual, so no merging here + // navname, goto are individual, so no merging here + + if mergeInto.Template == nil || *mergeInto.Template == "" { // always require a template + mergeInto.Template = mergeFrom.Template + } + + if mergeInto.Index.InputFile == nil { + mergeInto.Index.InputFile = mergeFrom.Index.InputFile + } if mergeInto.Index.InputFile == nil { mergeInto.Index.InputFile = mergeFrom.Index.InputFile @@ -136,7 +152,9 @@ func readContentDir(inBase string, outBase string, dir string, conf *PathConfig, if _, err = os.Stat(configFile); os.IsNotExist(err) { log.Debug("no config.yml found in this directory, using upper configs") newConfig = conf - + // remove this + newConfig.This.GoTo = nil + newConfig.This.Navname = nil } else { log.Debug("reading config...") data, err := ioutil.ReadFile(configFile) @@ -242,9 +260,6 @@ func processContent(conf *PathConfigTree) { log.Debug("merging config with upper config") mergeConfig(newConfig, conf.Config) - if newConfig.GoTo == nil { // goto is also valid in header - newConfig.GoTo = conf.Config.GoTo - } log.Debug(spew.Sdump(newConfig)) input = regex.ReplaceAll(input, []byte("")) @@ -296,8 +311,31 @@ func processContent(conf *PathConfigTree) { //html := blackfriday.Run(input, blackfriday.WithRenderer(bfchroma.NewRenderer())) html := blackfriday.Run(input) + log.Debugf("rendering template '%s' for '%s'", *newConfig.Template, outFile) + templateFile := *inDir + "/templates/" + *newConfig.Template + template := templateCache[templateFile] + if template == nil { + var err error + if template, err = raymond.ParseFile(templateFile); err != nil { + log.Panicf("could not parse template '%s': %s", templateFile, err) + } else { + templateCache[templateFile] = template + } + } + + // read yaml header as data for template + ctx := make(map[string]interface{}) + mergo.Map(&ctx, newConfig) + ctx["Body"] = string(html) + //log.Warning(spew.Sdump(ctx)) + + result, err := template.Exec(ctx) + if err != nil { + log.Panicf("could not execute template '%s' for input file '%s': %s", templateFile, inFile, err) + } + log.Noticef("writing to output file: %s", outFile) - err := ioutil.WriteFile(outFile, html, 0644) + err = ioutil.WriteFile(outFile, []byte(result), 0644) if err != nil { log.Panicf("could not write to output file '%s': %s", outFile, err) } @@ -403,6 +441,7 @@ func main() { log.Debugf("reading input directory %s", *inDir) + defaultTemplate := "index.html" defaultInputFile := "README.md" defaultOutputFile := "index.html" defaultPathStrip := "^[0-9]*_(.*)" @@ -412,6 +451,7 @@ func main() { defaultFilenameOutputExtension := "html" defaultPathConfig := new(PathConfig) + defaultPathConfig.Template = &defaultTemplate defaultPathConfig.Index.InputFile = &defaultInputFile defaultPathConfig.Index.OutputFile = &defaultOutputFile defaultPathConfig.Path.Strip = &defaultPathStrip diff --git a/vendor/github.com/aymerick/raymond b/vendor/github.com/aymerick/raymond new file mode 160000 index 0000000..b565731 --- /dev/null +++ b/vendor/github.com/aymerick/raymond @@ -0,0 +1 @@ +Subproject commit b565731e1464263de0bda75f2e45d97b54b60110 diff --git a/vendor/github.com/imdario/mergo b/vendor/github.com/imdario/mergo new file mode 160000 index 0000000..7c29201 --- /dev/null +++ b/vendor/github.com/imdario/mergo @@ -0,0 +1 @@ +Subproject commit 7c29201646fa3de8506f701213473dd407f19646