From 6150fd377b018b07403079be48c8920da0f6a0c7 Mon Sep 17 00:00:00 2001 From: Sebastian Frank Date: Tue, 12 Feb 2019 11:35:25 +0100 Subject: [PATCH] write html --- .gitignore | 3 +- example/content/config.yml | 13 ++-- main.go | 137 ++++++++++++++++++++++++++++++++----- 3 files changed, 129 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index 6d70cd0..f0566f5 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -test.html \ No newline at end of file +test.html +html/ \ No newline at end of file diff --git a/example/content/config.yml b/example/content/config.yml index 91f5fdc..384c9a0 100644 --- a/example/content/config.yml +++ b/example/content/config.yml @@ -1,7 +1,8 @@ -title: "Example Website" goTo: "/de/main/home" -inputFile: "README.md" -outputFile: "index.html" + +index: + inputFile: "README.md" + outputFile: "index.html" meta: title: "global title" @@ -9,11 +10,11 @@ meta: keywords: "global keywords" path: - strip: "^[0-9]*_" + strip: "^[0-9]*_(.*)" ignoreForNav: "^_" filename: - strip: "" - ignore: "_" + strip: "(.*).md$" + ignore: "^_" outputExtension: "html" diff --git a/main.go b/main.go index 4208295..ed1b8db 100644 --- a/main.go +++ b/main.go @@ -4,11 +4,13 @@ import ( "flag" "io/ioutil" "os" + "path" "regexp" "strings" "github.com/davecgh/go-spew/spew" "github.com/op/go-logging" + "gopkg.in/russross/blackfriday.v2" "gopkg.in/yaml.v2" ) @@ -34,11 +36,13 @@ var globalConfig = new(GlobalConfig) // PathConfig of subdir type PathConfig struct { - Title *string `yaml:"title"` - Navname *string `yaml:"navname"` - GoTo *string `yaml:"goTo"` - InputFile *string `yaml:"inputFile"` - OutputFile *string `yaml:"outputFile"` + Navname *string `yaml:"navname"` + GoTo *string `yaml:"goTo"` + + Index struct { + InputFile *string `yaml:"inputFile"` + OutputFile *string `yaml:"outputFile"` + } `yaml:"index"` Meta struct { Title *string `yaml:"title"` @@ -70,12 +74,14 @@ type PathConfigTree struct { } func mergeConfig(mergeInto *PathConfig, mergeFrom *PathConfig) { - if mergeInto.InputFile == nil { - mergeInto.InputFile = mergeFrom.InputFile + // goto is individual, so no merging here + + if mergeInto.Index.InputFile == nil { + mergeInto.Index.InputFile = mergeFrom.Index.InputFile } - if mergeInto.OutputFile == nil { - mergeInto.OutputFile = mergeFrom.OutputFile + if mergeInto.Index.OutputFile == nil { + mergeInto.Index.OutputFile = mergeFrom.Index.OutputFile } if mergeInto.Meta.Title == nil { @@ -94,6 +100,16 @@ func mergeConfig(mergeInto *PathConfig, mergeFrom *PathConfig) { if mergeInto.Path.Strip == nil { mergeInto.Path.Strip = mergeFrom.Path.Strip } + + if mergeInto.Filename.Strip == nil { + mergeInto.Filename.Strip = mergeFrom.Filename.Strip + } + if mergeInto.Filename.Ignore == nil { + mergeInto.Filename.Ignore = mergeFrom.Filename.Ignore + } + if mergeInto.Filename.OutputExtension == nil { + mergeInto.Filename.OutputExtension = mergeFrom.Filename.OutputExtension + } } var contentConfig = new(PathConfigTree) @@ -147,12 +163,11 @@ func readContentDir(inBase string, outBase string, dir string, conf *PathConfig, if regex, err := regexp.Compile(*regexStr); err != nil { log.Panicf("error compiling path.strip regex '%s' from '%s': %s", *regexStr, inBase+"/"+dir, err) } else { - stripedDir = regex.ReplaceAllString(stripedDir, "") + stripedDir = regex.ReplaceAllString(stripedDir, "$1") } } outPath := outBase + "/" + stripedDir - outPath = strings.Replace(outPath, "//", "/", -1) - outPath = strings.TrimSuffix(outPath, "/") + outPath = path.Clean(outPath) log.Noticef("calculated output directory: %s", outPath) tree.OutputPath = outPath @@ -203,6 +218,94 @@ func processContent(conf *PathConfigTree) { log.Panicf("unknown error for output directory '%s': %s", conf.OutputPath, err) } + for _, file := range conf.InputFiles { + inFile := conf.InputPath + "/" + file + log.Debugf("reading file: %s", inFile) + + input, err := ioutil.ReadFile(inFile) + if err != nil { + log.Panicf("could not read '%s':%s", inFile, err) + } + log.Noticef("processing input file '%s'", inFile) + + var newConfig *PathConfig + + regex := regexp.MustCompile("(?sm)^---(.*)^---") + yamlData := regex.Find(input) + if string(yamlData) != "" { + log.Debugf("found yaml header in '%s', merging config", inFile) + newConfig = new(PathConfig) + err = yaml.Unmarshal(yamlData, newConfig) + if err != nil { + log.Panicf("could not parse YAML header from '%s': %s", inFile, err) + } + + 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("")) + } else { + newConfig = conf.Config + } + + // ignore ??? + ignoreFile := false + ignoreRegex := newConfig.Filename.Ignore + if ignoreRegex != nil && *ignoreRegex != "" { + regex, err := regexp.Compile(*ignoreRegex) + if err != nil { + log.Panicf("could not compile filename.ignore regexp '%s' for file '%s': %s", *ignoreRegex, inFile, err) + } + ignoreFile = regex.MatchString(file) + } + + if ignoreFile { + log.Noticef("ignoring file '%s', because of filename.ignore", inFile) + } else { + + // build output filename + outputFilename := file + + indexInputFile := conf.Config.Index.InputFile + indexOutputFile := conf.Config.Index.OutputFile + + if indexInputFile != nil && *indexInputFile == file && indexOutputFile != nil && *indexOutputFile != "" { + outputFilename = *indexOutputFile + } else { + stripRegex := newConfig.Filename.Strip + if stripRegex != nil && *stripRegex != "" { + regex, err := regexp.Compile(*stripRegex) + if err != nil { + log.Panicf("could not compile filename.strip regexp '%s' for file '%s': %s", *stripRegex, inFile, err) + } + outputFilename = regex.ReplaceAllString(outputFilename, "$1") + } + outputExt := newConfig.Filename.OutputExtension + if outputExt != nil && *outputExt != "" { + outputFilename += "." + *outputExt + } + } + + outFile := conf.OutputPath + "/" + outputFilename + log.Debugf("using '%s' as output file", outFile) + + //html := blackfriday.Run(input, blackfriday.WithRenderer(bfchroma.NewRenderer())) + html := blackfriday.Run(input) + + log.Noticef("writing to output file: %s", outFile) + err := ioutil.WriteFile(outFile, html, 0644) + if err != nil { + log.Panicf("could not write to output file '%s': %s", outFile, err) + } + + //fmt.Println(string(html)) + } + } + for _, el := range conf.Sub { processContent(el) } @@ -302,15 +405,15 @@ func main() { defaultInputFile := "README.md" defaultOutputFile := "index.html" - defaultPathStrip := "^[0-9]*_" + defaultPathStrip := "^[0-9]*_(.*)" defaultPathIgnoreForNav := "^_" - defaultFilenameStrip := "" - defaultFilenameIgnore := "_" + defaultFilenameStrip := "(.*).md$" + defaultFilenameIgnore := "^_" defaultFilenameOutputExtension := "html" defaultPathConfig := new(PathConfig) - defaultPathConfig.InputFile = &defaultInputFile - defaultPathConfig.OutputFile = &defaultOutputFile + defaultPathConfig.Index.InputFile = &defaultInputFile + defaultPathConfig.Index.OutputFile = &defaultOutputFile defaultPathConfig.Path.Strip = &defaultPathStrip defaultPathConfig.Path.IgnoreForNav = &defaultPathIgnoreForNav defaultPathConfig.Filename.Strip = &defaultFilenameStrip