fixed yaml

This commit is contained in:
Sebastian Frank
2019-02-12 12:52:46 +01:00
parent 6150fd377b
commit 35561aa97f
22 changed files with 137 additions and 64 deletions

96
main.go
View File

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