fixed yaml

This commit is contained in:
Sebastian Frank 2019-02-12 12:52:46 +01:00
parent 6150fd377b
commit 35561aa97f
Signed by: apairon
GPG Key ID: 7270D06DDA7FE8C3
22 changed files with 137 additions and 64 deletions

6
.gitmodules vendored
View File

@ -25,3 +25,9 @@
[submodule "vendor/github.com/davecgh/go-spew"] [submodule "vendor/github.com/davecgh/go-spew"]
path = vendor/github.com/davecgh/go-spew path = vendor/github.com/davecgh/go-spew
url = https://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

View File

@ -1,7 +1,7 @@
webserver: Webserver:
type: "apache" # generates .htaccess Type: "apache" # generates .htaccess
assets: Assets:
path: "/assets" Path: "/assets"
deployType: "symlink" # symlink, copy or move DeployType: "symlink" # symlink, copy or move
fixTemplate: True # change path in html templates, no <base> used FixTemplate: True # change path in html templates, no <base> used

View File

@ -1,20 +1,23 @@
goTo: "/de/main/home" This:
GoTo: "/de/main/home"
index: Index:
inputFile: "README.md" InputFile: "README.md"
outputFile: "index.html" OutputFile: "index.html"
meta: Meta:
title: "global title" Title: "global title"
description: "global description of example website" Description: "global description of example website"
keywords: "global keywords" Keywords: "global keywords"
path: Template: index.html
strip: "^[0-9]*_(.*)"
ignoreForNav: "^_"
filename: Path:
strip: "(.*).md$" Strip: "^[0-9]*_(.*)"
ignore: "^_" IgnoreForNav: "^_"
outputExtension: "html"
Filename:
Strip: "(.*).md$"
Ignore: "^_"
OutputExtension: "html"

View File

@ -1,5 +1,6 @@
--- ---
navname: Home Meta:
Title: Home
--- ---

View File

@ -1,5 +1,6 @@
--- ---
navname: Leistungen Meta:
Title: Leistungen
--- ---

View File

@ -1,5 +1,6 @@
--- ---
navname: Adresse Meta:
Title: Adresse
--- ---

View File

@ -1,5 +1,6 @@
--- ---
navname: Anfahrt Meta:
Title: Anfahrt
--- ---

View File

@ -1 +1,2 @@
goTo: adresse/ This:
GoTo: adresse/

View File

@ -1 +1 @@
goTo: /de/service/impressum/ GoTo: /de/service/impressum/

View File

@ -1,5 +1,6 @@
--- ---
navname: Datenschutz Meta:
Title: Datenschutz
--- ---

View File

@ -1,5 +1,6 @@
--- ---
navname: AGB's Meta:
Title: AGB's
--- ---

View File

@ -1,5 +1,6 @@
--- ---
navname: Impressum Meta:
Title: Impressum
--- ---

View File

@ -1,5 +1,6 @@
--- ---
navname: Home Meta:
Title: Home
--- ---

View File

@ -1,5 +1,6 @@
--- ---
navname: Services Meta:
Title: Services
--- ---

View File

@ -1,5 +1,6 @@
--- ---
navname: Contact Meta:
Title: Contact
--- ---

View File

@ -1 +1,2 @@
goTo: /en/service/imprint/ This:
GoTo: /en/service/imprint/

View File

@ -1,5 +1,6 @@
--- ---
navname: Terms Meta:
Title: Terms
--- ---

View File

@ -1,5 +1,6 @@
--- ---
navname: Imprint Meta:
Title: Imprint
--- ---

View File

@ -0,0 +1,8 @@
<html>
<meta>
<title>{{ Meta.Title }}</title>
</meta>
<body>
{{{ Body }}}
</body>
</html>

96
main.go
View File

@ -8,7 +8,9 @@ import (
"regexp" "regexp"
"strings" "strings"
"github.com/aymerick/raymond"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/imdario/mergo"
"github.com/op/go-logging" "github.com/op/go-logging"
"gopkg.in/russross/blackfriday.v2" "gopkg.in/russross/blackfriday.v2"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@ -19,47 +21,53 @@ var log = logging.MustGetLogger("myLogger")
var inDir *string var inDir *string
var outDir *string var outDir *string
var templateCache = make(map[string]*raymond.Template)
// GlobalConfig is config which is used only once in root dir // GlobalConfig is config which is used only once in root dir
type GlobalConfig struct { type GlobalConfig struct {
Webserver struct { Webserver struct {
Type *string `yaml:"type"` Type *string `yaml:"Type"`
} `yaml:"webserver"` } `yaml:"Webserver"`
Assets struct { Assets struct {
Path *string `yaml:"path"` Path *string `yaml:"Path"`
DeployType *string `yaml:"deployType"` DeployType *string `yaml:"DeployType"`
FixTemplate *bool `yaml:"fixTemplate"` FixTemplate *bool `yaml:"FixTemplate"`
} `yaml:"assets"` } `yaml:"Assets"`
} }
var globalConfig = new(GlobalConfig) var globalConfig = new(GlobalConfig)
// PathConfig of subdir // PathConfig of subdir
type PathConfig struct { type PathConfig struct {
Navname *string `yaml:"navname"` This struct {
GoTo *string `yaml:"goTo"` Navname *string `yaml:"Navname"`
GoTo *string `yaml:"GoTo"`
} `yaml:"This"`
Template *string `yaml:"Template"`
Index struct { Index struct {
InputFile *string `yaml:"inputFile"` InputFile *string `yaml:"InputFile"`
OutputFile *string `yaml:"outputFile"` OutputFile *string `yaml:"OutputFile"`
} `yaml:"index"` } `yaml:"Index"`
Meta struct { Meta struct {
Title *string `yaml:"title"` Title *string `yaml:"Title"`
Description *string `yaml:"description"` Description *string `yaml:"Description"`
Keywords *string `yaml:"keywords"` Keywords *string `yaml:"Keywords"`
} `yaml:"meta"` } `yaml:"Meta"`
Path struct { Path struct {
Strip *string `yaml:"strip"` Strip *string `yaml:"Strip"`
IgnoreForNav *string `yaml:"ignoreForNav"` IgnoreForNav *string `yaml:"IgnoreForNav"`
} `yaml:"path"` } `yaml:"Path"`
Filename struct { Filename struct {
Strip *string `yaml:"strip"` Strip *string `yaml:"Strip"`
Ignore *string `yaml:"ignore"` Ignore *string `yaml:"Ignore"`
OutputExtension *string `yaml:"outputExtension"` OutputExtension *string `yaml:"OutputExtension"`
} `yaml:"filename"` } `yaml:"Filename"`
} }
// PathConfigTree is complete config tree of content dir // PathConfigTree is complete config tree of content dir
@ -74,7 +82,15 @@ type PathConfigTree struct {
} }
func mergeConfig(mergeInto *PathConfig, mergeFrom *PathConfig) { 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 { if mergeInto.Index.InputFile == nil {
mergeInto.Index.InputFile = mergeFrom.Index.InputFile 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) { if _, err = os.Stat(configFile); os.IsNotExist(err) {
log.Debug("no config.yml found in this directory, using upper configs") log.Debug("no config.yml found in this directory, using upper configs")
newConfig = conf newConfig = conf
// remove this
newConfig.This.GoTo = nil
newConfig.This.Navname = nil
} else { } else {
log.Debug("reading config...") log.Debug("reading config...")
data, err := ioutil.ReadFile(configFile) data, err := ioutil.ReadFile(configFile)
@ -242,9 +260,6 @@ func processContent(conf *PathConfigTree) {
log.Debug("merging config with upper config") log.Debug("merging config with upper config")
mergeConfig(newConfig, conf.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)) log.Debug(spew.Sdump(newConfig))
input = regex.ReplaceAll(input, []byte("")) 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, blackfriday.WithRenderer(bfchroma.NewRenderer()))
html := blackfriday.Run(input) 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) log.Noticef("writing to output file: %s", outFile)
err := ioutil.WriteFile(outFile, html, 0644) err = ioutil.WriteFile(outFile, []byte(result), 0644)
if err != nil { if err != nil {
log.Panicf("could not write to output file '%s': %s", outFile, err) 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) log.Debugf("reading input directory %s", *inDir)
defaultTemplate := "index.html"
defaultInputFile := "README.md" defaultInputFile := "README.md"
defaultOutputFile := "index.html" defaultOutputFile := "index.html"
defaultPathStrip := "^[0-9]*_(.*)" defaultPathStrip := "^[0-9]*_(.*)"
@ -412,6 +451,7 @@ func main() {
defaultFilenameOutputExtension := "html" defaultFilenameOutputExtension := "html"
defaultPathConfig := new(PathConfig) defaultPathConfig := new(PathConfig)
defaultPathConfig.Template = &defaultTemplate
defaultPathConfig.Index.InputFile = &defaultInputFile defaultPathConfig.Index.InputFile = &defaultInputFile
defaultPathConfig.Index.OutputFile = &defaultOutputFile defaultPathConfig.Index.OutputFile = &defaultOutputFile
defaultPathConfig.Path.Strip = &defaultPathStrip defaultPathConfig.Path.Strip = &defaultPathStrip

1
vendor/github.com/aymerick/raymond generated vendored Submodule

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

1
vendor/github.com/imdario/mergo generated vendored Submodule

@ -0,0 +1 @@
Subproject commit 7c29201646fa3de8506f701213473dd407f19646