write html
This commit is contained in:
parent
29a745283a
commit
6150fd377b
3
.gitignore
vendored
3
.gitignore
vendored
@ -1 +1,2 @@
|
|||||||
test.html
|
test.html
|
||||||
|
html/
|
@ -1,7 +1,8 @@
|
|||||||
title: "Example Website"
|
|
||||||
goTo: "/de/main/home"
|
goTo: "/de/main/home"
|
||||||
inputFile: "README.md"
|
|
||||||
outputFile: "index.html"
|
index:
|
||||||
|
inputFile: "README.md"
|
||||||
|
outputFile: "index.html"
|
||||||
|
|
||||||
meta:
|
meta:
|
||||||
title: "global title"
|
title: "global title"
|
||||||
@ -9,11 +10,11 @@ meta:
|
|||||||
keywords: "global keywords"
|
keywords: "global keywords"
|
||||||
|
|
||||||
path:
|
path:
|
||||||
strip: "^[0-9]*_"
|
strip: "^[0-9]*_(.*)"
|
||||||
ignoreForNav: "^_"
|
ignoreForNav: "^_"
|
||||||
|
|
||||||
filename:
|
filename:
|
||||||
strip: ""
|
strip: "(.*).md$"
|
||||||
ignore: "_"
|
ignore: "^_"
|
||||||
outputExtension: "html"
|
outputExtension: "html"
|
||||||
|
|
||||||
|
137
main.go
137
main.go
@ -4,11 +4,13 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"path"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/davecgh/go-spew/spew"
|
"github.com/davecgh/go-spew/spew"
|
||||||
"github.com/op/go-logging"
|
"github.com/op/go-logging"
|
||||||
|
"gopkg.in/russross/blackfriday.v2"
|
||||||
"gopkg.in/yaml.v2"
|
"gopkg.in/yaml.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -34,11 +36,13 @@ var globalConfig = new(GlobalConfig)
|
|||||||
|
|
||||||
// PathConfig of subdir
|
// PathConfig of subdir
|
||||||
type PathConfig struct {
|
type PathConfig struct {
|
||||||
Title *string `yaml:"title"`
|
Navname *string `yaml:"navname"`
|
||||||
Navname *string `yaml:"navname"`
|
GoTo *string `yaml:"goTo"`
|
||||||
GoTo *string `yaml:"goTo"`
|
|
||||||
InputFile *string `yaml:"inputFile"`
|
Index struct {
|
||||||
OutputFile *string `yaml:"outputFile"`
|
InputFile *string `yaml:"inputFile"`
|
||||||
|
OutputFile *string `yaml:"outputFile"`
|
||||||
|
} `yaml:"index"`
|
||||||
|
|
||||||
Meta struct {
|
Meta struct {
|
||||||
Title *string `yaml:"title"`
|
Title *string `yaml:"title"`
|
||||||
@ -70,12 +74,14 @@ type PathConfigTree struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func mergeConfig(mergeInto *PathConfig, mergeFrom *PathConfig) {
|
func mergeConfig(mergeInto *PathConfig, mergeFrom *PathConfig) {
|
||||||
if mergeInto.InputFile == nil {
|
// goto is individual, so no merging here
|
||||||
mergeInto.InputFile = mergeFrom.InputFile
|
|
||||||
|
if mergeInto.Index.InputFile == nil {
|
||||||
|
mergeInto.Index.InputFile = mergeFrom.Index.InputFile
|
||||||
}
|
}
|
||||||
|
|
||||||
if mergeInto.OutputFile == nil {
|
if mergeInto.Index.OutputFile == nil {
|
||||||
mergeInto.OutputFile = mergeFrom.OutputFile
|
mergeInto.Index.OutputFile = mergeFrom.Index.OutputFile
|
||||||
}
|
}
|
||||||
|
|
||||||
if mergeInto.Meta.Title == nil {
|
if mergeInto.Meta.Title == nil {
|
||||||
@ -94,6 +100,16 @@ func mergeConfig(mergeInto *PathConfig, mergeFrom *PathConfig) {
|
|||||||
if mergeInto.Path.Strip == nil {
|
if mergeInto.Path.Strip == nil {
|
||||||
mergeInto.Path.Strip = mergeFrom.Path.Strip
|
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)
|
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 {
|
if regex, err := regexp.Compile(*regexStr); err != nil {
|
||||||
log.Panicf("error compiling path.strip regex '%s' from '%s': %s", *regexStr, inBase+"/"+dir, err)
|
log.Panicf("error compiling path.strip regex '%s' from '%s': %s", *regexStr, inBase+"/"+dir, err)
|
||||||
} else {
|
} else {
|
||||||
stripedDir = regex.ReplaceAllString(stripedDir, "")
|
stripedDir = regex.ReplaceAllString(stripedDir, "$1")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
outPath := outBase + "/" + stripedDir
|
outPath := outBase + "/" + stripedDir
|
||||||
outPath = strings.Replace(outPath, "//", "/", -1)
|
outPath = path.Clean(outPath)
|
||||||
outPath = strings.TrimSuffix(outPath, "/")
|
|
||||||
|
|
||||||
log.Noticef("calculated output directory: %s", outPath)
|
log.Noticef("calculated output directory: %s", outPath)
|
||||||
tree.OutputPath = outPath
|
tree.OutputPath = outPath
|
||||||
@ -203,6 +218,94 @@ func processContent(conf *PathConfigTree) {
|
|||||||
log.Panicf("unknown error for output directory '%s': %s", conf.OutputPath, err)
|
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 {
|
for _, el := range conf.Sub {
|
||||||
processContent(el)
|
processContent(el)
|
||||||
}
|
}
|
||||||
@ -302,15 +405,15 @@ func main() {
|
|||||||
|
|
||||||
defaultInputFile := "README.md"
|
defaultInputFile := "README.md"
|
||||||
defaultOutputFile := "index.html"
|
defaultOutputFile := "index.html"
|
||||||
defaultPathStrip := "^[0-9]*_"
|
defaultPathStrip := "^[0-9]*_(.*)"
|
||||||
defaultPathIgnoreForNav := "^_"
|
defaultPathIgnoreForNav := "^_"
|
||||||
defaultFilenameStrip := ""
|
defaultFilenameStrip := "(.*).md$"
|
||||||
defaultFilenameIgnore := "_"
|
defaultFilenameIgnore := "^_"
|
||||||
defaultFilenameOutputExtension := "html"
|
defaultFilenameOutputExtension := "html"
|
||||||
|
|
||||||
defaultPathConfig := new(PathConfig)
|
defaultPathConfig := new(PathConfig)
|
||||||
defaultPathConfig.InputFile = &defaultInputFile
|
defaultPathConfig.Index.InputFile = &defaultInputFile
|
||||||
defaultPathConfig.OutputFile = &defaultOutputFile
|
defaultPathConfig.Index.OutputFile = &defaultOutputFile
|
||||||
defaultPathConfig.Path.Strip = &defaultPathStrip
|
defaultPathConfig.Path.Strip = &defaultPathStrip
|
||||||
defaultPathConfig.Path.IgnoreForNav = &defaultPathIgnoreForNav
|
defaultPathConfig.Path.IgnoreForNav = &defaultPathIgnoreForNav
|
||||||
defaultPathConfig.Filename.Strip = &defaultFilenameStrip
|
defaultPathConfig.Filename.Strip = &defaultFilenameStrip
|
||||||
|
Loading…
Reference in New Issue
Block a user