2019-02-10 13:49:27 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-02-11 15:00:27 +01:00
|
|
|
"flag"
|
2019-02-10 13:49:27 +01:00
|
|
|
"io/ioutil"
|
2019-02-11 15:00:27 +01:00
|
|
|
"os"
|
2019-02-12 11:35:25 +01:00
|
|
|
"path"
|
2019-02-11 15:25:48 +01:00
|
|
|
"regexp"
|
2019-02-11 15:00:27 +01:00
|
|
|
"strings"
|
2019-02-10 13:49:27 +01:00
|
|
|
|
2019-02-12 15:28:22 +01:00
|
|
|
"github.com/imdario/mergo"
|
|
|
|
|
2019-02-12 12:52:46 +01:00
|
|
|
"github.com/aymerick/raymond"
|
2019-02-11 15:00:27 +01:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
|
|
|
"github.com/op/go-logging"
|
2019-02-12 19:09:25 +01:00
|
|
|
cpy "github.com/otiai10/copy"
|
2019-02-12 11:35:25 +01:00
|
|
|
"gopkg.in/russross/blackfriday.v2"
|
2019-02-11 15:00:27 +01:00
|
|
|
"gopkg.in/yaml.v2"
|
2019-02-10 13:49:27 +01:00
|
|
|
)
|
|
|
|
|
2019-02-11 15:00:27 +01:00
|
|
|
var log = logging.MustGetLogger("myLogger")
|
|
|
|
|
|
|
|
var inDir *string
|
|
|
|
var outDir *string
|
|
|
|
|
2019-02-12 12:52:46 +01:00
|
|
|
var templateCache = make(map[string]*raymond.Template)
|
|
|
|
|
2019-02-11 15:00:27 +01:00
|
|
|
// GlobalConfig is config which is used only once in root dir
|
|
|
|
type GlobalConfig struct {
|
|
|
|
Webserver struct {
|
2019-02-12 19:09:25 +01:00
|
|
|
Type string `yaml:"Type"`
|
2019-02-12 12:52:46 +01:00
|
|
|
} `yaml:"Webserver"`
|
2019-02-11 15:00:27 +01:00
|
|
|
|
|
|
|
Assets struct {
|
2019-02-12 19:09:25 +01:00
|
|
|
FromPath string `yaml:"FromPath"`
|
|
|
|
ToPath string `yaml:"ToPath"`
|
|
|
|
Action string `yaml:"Action"`
|
|
|
|
FixTemplate struct {
|
|
|
|
Find string `yaml:"Find"`
|
|
|
|
Replace string `yaml:"Replace"`
|
|
|
|
} `yaml:"FixTemplate"`
|
2019-02-12 12:52:46 +01:00
|
|
|
} `yaml:"Assets"`
|
2019-02-11 15:00:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var globalConfig = new(GlobalConfig)
|
|
|
|
|
|
|
|
// PathConfig of subdir
|
|
|
|
type PathConfig struct {
|
2019-02-12 12:52:46 +01:00
|
|
|
This struct {
|
|
|
|
Navname *string `yaml:"Navname"`
|
|
|
|
GoTo *string `yaml:"GoTo"`
|
|
|
|
} `yaml:"This"`
|
|
|
|
|
|
|
|
Template *string `yaml:"Template"`
|
2019-02-12 11:35:25 +01:00
|
|
|
|
|
|
|
Index struct {
|
2019-02-12 12:52:46 +01:00
|
|
|
InputFile *string `yaml:"InputFile"`
|
|
|
|
OutputFile *string `yaml:"OutputFile"`
|
|
|
|
} `yaml:"Index"`
|
2019-02-11 15:00:27 +01:00
|
|
|
|
|
|
|
Meta struct {
|
2019-02-12 12:52:46 +01:00
|
|
|
Title *string `yaml:"Title"`
|
|
|
|
Description *string `yaml:"Description"`
|
|
|
|
Keywords *string `yaml:"Keywords"`
|
|
|
|
} `yaml:"Meta"`
|
2019-02-11 15:00:27 +01:00
|
|
|
|
|
|
|
Path struct {
|
2019-02-12 12:52:46 +01:00
|
|
|
Strip *string `yaml:"Strip"`
|
|
|
|
IgnoreForNav *string `yaml:"IgnoreForNav"`
|
|
|
|
} `yaml:"Path"`
|
2019-02-12 10:21:51 +01:00
|
|
|
|
|
|
|
Filename struct {
|
2019-02-12 12:52:46 +01:00
|
|
|
Strip *string `yaml:"Strip"`
|
|
|
|
Ignore *string `yaml:"Ignore"`
|
|
|
|
OutputExtension *string `yaml:"OutputExtension"`
|
|
|
|
} `yaml:"Filename"`
|
2019-02-12 13:05:26 +01:00
|
|
|
|
|
|
|
Data interface{} `yaml:"Data"`
|
2019-02-11 15:00:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// PathConfigTree is complete config tree of content dir
|
|
|
|
type PathConfigTree struct {
|
|
|
|
InputPath string
|
|
|
|
OutputPath string
|
|
|
|
|
2019-02-12 10:21:51 +01:00
|
|
|
InputFiles []string
|
|
|
|
|
2019-02-11 15:00:27 +01:00
|
|
|
Config *PathConfig
|
|
|
|
Sub []*PathConfigTree
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeConfig(mergeInto *PathConfig, mergeFrom *PathConfig) {
|
2019-02-12 12:52:46 +01:00
|
|
|
// 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
|
|
|
|
}
|
2019-02-12 11:35:25 +01:00
|
|
|
|
|
|
|
if mergeInto.Index.InputFile == nil {
|
|
|
|
mergeInto.Index.InputFile = mergeFrom.Index.InputFile
|
2019-02-11 15:00:27 +01:00
|
|
|
}
|
|
|
|
|
2019-02-12 11:35:25 +01:00
|
|
|
if mergeInto.Index.OutputFile == nil {
|
|
|
|
mergeInto.Index.OutputFile = mergeFrom.Index.OutputFile
|
2019-02-11 15:00:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if mergeInto.Meta.Title == nil {
|
|
|
|
mergeInto.Meta.Title = mergeFrom.Meta.Title
|
|
|
|
}
|
|
|
|
if mergeInto.Meta.Description == nil {
|
|
|
|
mergeInto.Meta.Description = mergeFrom.Meta.Description
|
|
|
|
}
|
|
|
|
if mergeInto.Meta.Keywords == nil {
|
|
|
|
mergeInto.Meta.Keywords = mergeFrom.Meta.Keywords
|
|
|
|
}
|
|
|
|
|
|
|
|
if mergeInto.Path.IgnoreForNav == nil {
|
|
|
|
mergeInto.Path.IgnoreForNav = mergeFrom.Path.IgnoreForNav
|
|
|
|
}
|
|
|
|
if mergeInto.Path.Strip == nil {
|
|
|
|
mergeInto.Path.Strip = mergeFrom.Path.Strip
|
|
|
|
}
|
2019-02-12 11:35:25 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2019-02-11 15:00:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var contentConfig = new(PathConfigTree)
|
|
|
|
|
2019-02-12 10:21:51 +01:00
|
|
|
func readContentDir(inBase string, outBase string, dir string, conf *PathConfig, tree *PathConfigTree) {
|
|
|
|
inPath := inBase
|
|
|
|
if dir != "" {
|
|
|
|
inPath += "/" + dir
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Noticef("reading input directory: %s", inPath)
|
|
|
|
|
|
|
|
files, err := ioutil.ReadDir(inPath)
|
2019-02-11 15:00:27 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
|
2019-02-12 10:21:51 +01:00
|
|
|
tree.InputPath = inPath
|
2019-02-11 15:00:27 +01:00
|
|
|
|
|
|
|
// read config
|
2019-02-12 15:28:22 +01:00
|
|
|
newConfig := new(PathConfig)
|
2019-02-11 15:00:27 +01:00
|
|
|
log.Debug("looking for config.yml ...")
|
2019-02-12 10:21:51 +01:00
|
|
|
configFile := inPath + "/config.yml"
|
|
|
|
if _, err = os.Stat(configFile); os.IsNotExist(err) {
|
2019-02-11 15:00:27 +01:00
|
|
|
log.Debug("no config.yml found in this directory, using upper configs")
|
2019-02-12 15:28:22 +01:00
|
|
|
mergo.Merge(newConfig, conf)
|
2019-02-12 12:52:46 +01:00
|
|
|
// remove this
|
|
|
|
newConfig.This.GoTo = nil
|
|
|
|
newConfig.This.Navname = nil
|
2019-02-11 15:00:27 +01:00
|
|
|
} else {
|
|
|
|
log.Debug("reading config...")
|
2019-02-12 10:21:51 +01:00
|
|
|
data, err := ioutil.ReadFile(configFile)
|
2019-02-11 15:00:27 +01:00
|
|
|
if err != nil {
|
2019-02-12 10:21:51 +01:00
|
|
|
log.Panicf("could not read file '%s': %s", configFile, err)
|
2019-02-11 15:00:27 +01:00
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(data, newConfig)
|
|
|
|
if err != nil {
|
2019-02-12 10:21:51 +01:00
|
|
|
log.Panicf("could not parse YAML file '%s': %s", configFile, err)
|
2019-02-11 15:00:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("merging config with upper config")
|
|
|
|
mergeConfig(newConfig, conf)
|
|
|
|
log.Debug(spew.Sdump(newConfig))
|
|
|
|
}
|
|
|
|
|
|
|
|
tree.Config = newConfig
|
|
|
|
|
2019-02-11 15:25:48 +01:00
|
|
|
// calc outDir
|
2019-02-12 10:21:51 +01:00
|
|
|
stripedDir := dir
|
2019-02-11 15:25:48 +01:00
|
|
|
regexStr := newConfig.Path.Strip
|
|
|
|
if regexStr != nil && *regexStr != "" {
|
2019-02-12 10:21:51 +01:00
|
|
|
if regex, err := regexp.Compile(*regexStr); err != nil {
|
|
|
|
log.Panicf("error compiling path.strip regex '%s' from '%s': %s", *regexStr, inBase+"/"+dir, err)
|
2019-02-11 15:25:48 +01:00
|
|
|
} else {
|
2019-02-12 11:35:25 +01:00
|
|
|
stripedDir = regex.ReplaceAllString(stripedDir, "$1")
|
2019-02-11 15:25:48 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-12 15:43:11 +01:00
|
|
|
regex := regexp.MustCompile("[^a-zA-Z]")
|
|
|
|
stripedDir = regex.ReplaceAllString(stripedDir, "_")
|
2019-02-12 10:21:51 +01:00
|
|
|
outPath := outBase + "/" + stripedDir
|
2019-02-12 11:35:25 +01:00
|
|
|
outPath = path.Clean(outPath)
|
2019-02-11 15:00:27 +01:00
|
|
|
|
2019-02-12 10:21:51 +01:00
|
|
|
log.Noticef("calculated output directory: %s", outPath)
|
|
|
|
tree.OutputPath = outPath
|
2019-02-11 15:00:27 +01:00
|
|
|
|
2019-02-12 15:28:22 +01:00
|
|
|
if tree.Config.This.Navname == nil {
|
|
|
|
navname := strings.Replace(stripedDir, "_", " ", -1)
|
|
|
|
tree.Config.This.Navname = &navname
|
|
|
|
}
|
|
|
|
|
2019-02-11 15:00:27 +01:00
|
|
|
// first only files
|
|
|
|
for _, f := range files {
|
2019-02-12 10:21:51 +01:00
|
|
|
p := inPath + "/" + f.Name()
|
2019-02-11 15:00:27 +01:00
|
|
|
if strings.HasSuffix(f.Name(), ".md") {
|
|
|
|
log.Debugf(".MD %s", p)
|
2019-02-12 10:21:51 +01:00
|
|
|
if tree.InputFiles == nil {
|
|
|
|
tree.InputFiles = make([]string, 0)
|
|
|
|
}
|
|
|
|
tree.InputFiles = append(tree.InputFiles, f.Name())
|
2019-02-11 15:00:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// only directorys, needed config before
|
|
|
|
for _, f := range files {
|
2019-02-12 10:21:51 +01:00
|
|
|
p := inPath + "/" + f.Name()
|
2019-02-11 15:00:27 +01:00
|
|
|
if f.IsDir() {
|
|
|
|
log.Debugf("DIR %s", p)
|
|
|
|
newTree := new(PathConfigTree)
|
|
|
|
if tree.Sub == nil {
|
2019-02-12 10:21:51 +01:00
|
|
|
tree.Sub = make([]*PathConfigTree, 0)
|
2019-02-11 15:00:27 +01:00
|
|
|
}
|
|
|
|
tree.Sub = append(tree.Sub, newTree)
|
2019-02-12 10:21:51 +01:00
|
|
|
readContentDir(inPath, outPath, f.Name(), newConfig, newTree)
|
2019-02-11 15:00:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 15:28:22 +01:00
|
|
|
type navElement struct {
|
|
|
|
Navname string
|
|
|
|
GoTo string
|
|
|
|
|
|
|
|
SubMap *map[string]*navElement
|
|
|
|
SubSlice *[]*navElement
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildNavigation(conf *PathConfigTree, curNavMap *map[string]*navElement, curNavSlice *[]*navElement, activeNav string) {
|
|
|
|
for _, el := range conf.Sub {
|
|
|
|
subMap := make(map[string]*navElement)
|
|
|
|
subSlice := make([]*navElement, 0)
|
|
|
|
navEl := navElement{
|
|
|
|
SubMap: &subMap,
|
|
|
|
SubSlice: &subSlice,
|
|
|
|
}
|
|
|
|
elPath := strings.TrimPrefix(el.OutputPath, *outDir+"/")
|
|
|
|
|
|
|
|
n := el.Config.This.Navname
|
|
|
|
if n != nil {
|
|
|
|
navEl.Navname = *n
|
|
|
|
}
|
|
|
|
g := el.Config.This.GoTo
|
|
|
|
if g != nil {
|
|
|
|
if strings.HasPrefix(*g, "/") {
|
|
|
|
// abslute
|
|
|
|
navEl.GoTo = *g
|
|
|
|
} else {
|
|
|
|
// relative
|
|
|
|
navEl.GoTo = elPath + "/" + *g
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
navEl.GoTo = elPath + "/"
|
|
|
|
}
|
|
|
|
|
|
|
|
if activeNav != "" && activeNav != "/" {
|
|
|
|
// calculate relative path
|
|
|
|
tmpPath := ""
|
|
|
|
for i := len(strings.Split(activeNav, "/")); i > 0; i-- {
|
|
|
|
tmpPath += "../"
|
|
|
|
}
|
|
|
|
navEl.GoTo = tmpPath + navEl.GoTo
|
2019-02-12 15:46:14 +01:00
|
|
|
navEl.GoTo = path.Clean(navEl.GoTo)
|
2019-02-12 15:28:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
(*curNavMap)[navEl.Navname] = &navEl
|
|
|
|
if curNavSlice != nil {
|
|
|
|
*curNavSlice = append(*curNavSlice, &navEl)
|
|
|
|
}
|
|
|
|
|
|
|
|
buildNavigation(el, &subMap, &subSlice, activeNav)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-11 15:25:48 +01:00
|
|
|
func processContent(conf *PathConfigTree) {
|
2019-02-12 10:21:51 +01:00
|
|
|
log.Debugf("trying to create output directory: %s", conf.OutputPath)
|
|
|
|
|
|
|
|
if dirH, err := os.Stat(conf.OutputPath); os.IsNotExist(err) {
|
|
|
|
err := os.MkdirAll(conf.OutputPath, 0755)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("could not create output directory '%s': %s", conf.OutputPath, err)
|
|
|
|
}
|
|
|
|
log.Noticef("created output directory: %s", conf.OutputPath)
|
|
|
|
} else if dirH != nil {
|
|
|
|
if dirH.IsDir() {
|
|
|
|
log.Noticef("output directory '%s' already exists", conf.OutputPath)
|
|
|
|
} else {
|
|
|
|
log.Panicf("output directory '%s' is no directory", conf.OutputPath)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Panicf("unknown error for output directory '%s': %s", conf.OutputPath, err)
|
|
|
|
}
|
2019-02-11 15:25:48 +01:00
|
|
|
|
2019-02-12 11:35:25 +01:00
|
|
|
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)
|
|
|
|
|
2019-02-12 15:28:22 +01:00
|
|
|
newConfig := new(PathConfig)
|
2019-02-12 11:35:25 +01:00
|
|
|
|
|
|
|
regex := regexp.MustCompile("(?sm)^---(.*)^---")
|
|
|
|
yamlData := regex.Find(input)
|
|
|
|
if string(yamlData) != "" {
|
|
|
|
log.Debugf("found yaml header in '%s', merging config", inFile)
|
|
|
|
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)
|
|
|
|
log.Debug(spew.Sdump(newConfig))
|
|
|
|
|
|
|
|
input = regex.ReplaceAll(input, []byte(""))
|
|
|
|
} else {
|
2019-02-12 15:28:22 +01:00
|
|
|
mergo.Merge(newConfig, conf.Config)
|
2019-02-12 11:35:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
2019-02-12 12:52:46 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 15:28:22 +01:00
|
|
|
// build navigation
|
|
|
|
curNavPath := strings.TrimPrefix(conf.OutputPath, *outDir+"/")
|
|
|
|
var navMap = make(map[string]*navElement)
|
|
|
|
buildNavigation(contentConfig, &navMap, nil, curNavPath)
|
|
|
|
|
2019-02-12 12:52:46 +01:00
|
|
|
// read yaml header as data for template
|
|
|
|
ctx := make(map[string]interface{})
|
2019-02-12 13:05:26 +01:00
|
|
|
ctx["Meta"] = newConfig.Meta
|
|
|
|
ctx["Data"] = newConfig.Data
|
2019-02-12 15:28:22 +01:00
|
|
|
ctx["NavMap"] = navMap
|
2019-02-12 12:52:46 +01:00
|
|
|
ctx["Body"] = string(html)
|
|
|
|
|
|
|
|
result, err := template.Exec(ctx)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("could not execute template '%s' for input file '%s': %s", templateFile, inFile, err)
|
|
|
|
}
|
|
|
|
|
2019-02-12 19:09:25 +01:00
|
|
|
if find := globalConfig.Assets.FixTemplate.Find; find != "" {
|
|
|
|
log.Debugf("fixing assets paths in '%s' for '%s'", templateFile, inFile)
|
|
|
|
tmpPath := ""
|
|
|
|
for i := len(strings.Split(curNavPath, "/")); i > 0; i-- {
|
|
|
|
tmpPath += "../"
|
|
|
|
}
|
|
|
|
regex, err := regexp.Compile(find)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("could not compile regexp '%s' for assets path: %s", find, err)
|
|
|
|
}
|
|
|
|
repl := globalConfig.Assets.FixTemplate.Replace
|
|
|
|
repl = tmpPath + "/" + globalConfig.Assets.ToPath + "/" + repl
|
|
|
|
repl = path.Clean(repl) + "/"
|
|
|
|
result = regex.ReplaceAllString(result, repl)
|
|
|
|
}
|
|
|
|
|
2019-02-12 11:35:25 +01:00
|
|
|
log.Noticef("writing to output file: %s", outFile)
|
2019-02-12 12:52:46 +01:00
|
|
|
err = ioutil.WriteFile(outFile, []byte(result), 0644)
|
2019-02-12 11:35:25 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Panicf("could not write to output file '%s': %s", outFile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
//fmt.Println(string(html))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-12 10:21:51 +01:00
|
|
|
for _, el := range conf.Sub {
|
|
|
|
processContent(el)
|
|
|
|
}
|
2019-02-11 15:25:48 +01:00
|
|
|
}
|
|
|
|
|
2019-02-12 19:09:25 +01:00
|
|
|
func processAssets() {
|
|
|
|
switch globalConfig.Assets.Action {
|
|
|
|
case "copy":
|
|
|
|
from := globalConfig.Assets.FromPath
|
|
|
|
to := globalConfig.Assets.ToPath
|
|
|
|
if !strings.HasPrefix(from, "/") {
|
|
|
|
from = *inDir + "/" + from
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(to, "/") {
|
|
|
|
to = *outDir + "/" + to
|
|
|
|
}
|
|
|
|
log.Noticef("copying assets from '%s' to '%s'", from, to)
|
|
|
|
err := cpy.Copy(from, to)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("could not copy assets from '%s' to '%s': %s", from, to, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-10 13:49:27 +01:00
|
|
|
func main() {
|
2019-02-11 15:00:27 +01:00
|
|
|
spew.Config.DisablePointerAddresses = true
|
|
|
|
spew.Config.DisableCapacities = true
|
|
|
|
spew.Config.DisableMethods = true
|
|
|
|
spew.Config.DisablePointerMethods = true
|
|
|
|
|
|
|
|
inDir = flag.String("in", "./", "input directory")
|
|
|
|
outDir = flag.String("out", "html", "output directory")
|
|
|
|
createOutDir := flag.Bool("create", false, "create output directory if not existing")
|
|
|
|
//clearOutDir := flag.Bool("clear", false, "clear output directory before generating website")
|
|
|
|
logLevel := flag.String("logLevel", "info", "log level: debug, info, warning, error")
|
|
|
|
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
logBackend := logging.NewLogBackend(os.Stderr, "", 0)
|
|
|
|
logBackendFormatter := logging.NewBackendFormatter(logBackend, logging.MustStringFormatter(
|
|
|
|
`%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}`,
|
|
|
|
))
|
|
|
|
logBackendLeveled := logging.AddModuleLevel(logBackendFormatter)
|
|
|
|
logBackendLevel := logging.INFO
|
|
|
|
if logLevel != nil {
|
|
|
|
switch *logLevel {
|
|
|
|
case "debug":
|
|
|
|
logBackendLevel = logging.DEBUG
|
|
|
|
break
|
|
|
|
|
|
|
|
case "info":
|
|
|
|
logBackendLevel = logging.INFO
|
|
|
|
break
|
|
|
|
|
|
|
|
case "warning":
|
|
|
|
logBackendLevel = logging.WARNING
|
|
|
|
break
|
|
|
|
|
|
|
|
case "error":
|
|
|
|
logBackendLevel = logging.ERROR
|
|
|
|
break
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
logBackendLeveled.SetLevel(logBackendLevel, "")
|
|
|
|
logging.SetBackend(logBackendLeveled)
|
|
|
|
|
|
|
|
if inDir == nil || *inDir == "" {
|
|
|
|
log.Panic("input directory not specified")
|
|
|
|
}
|
|
|
|
log.Noticef("input directory: %s", *inDir)
|
|
|
|
|
|
|
|
if outDir == nil || *outDir == "" {
|
|
|
|
log.Panic("output directory not specified")
|
|
|
|
}
|
|
|
|
log.Noticef("output directory: %s", *outDir)
|
|
|
|
|
|
|
|
if createOutDir != nil && *createOutDir {
|
|
|
|
if _, err := os.Stat(*outDir); os.IsNotExist(err) {
|
|
|
|
log.Debugf("output directory '%s' does not exist", *outDir)
|
|
|
|
log.Debugf("trying to create output directory: %s", *outDir)
|
|
|
|
err := os.MkdirAll(*outDir, 0755)
|
|
|
|
if err != nil {
|
|
|
|
log.Panic(err)
|
|
|
|
}
|
|
|
|
log.Noticef("created output directory: %s", *outDir)
|
|
|
|
} else {
|
|
|
|
log.Noticef("output directory '%s' already exists", *outDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if fD, err := os.Stat(*outDir); os.IsNotExist(err) {
|
|
|
|
log.Panicf("output directory '%s' does not exist, try -create parameter or create manually", *outDir)
|
|
|
|
} else {
|
|
|
|
if fD == nil {
|
|
|
|
log.Panicf("something went wrong, could not get file handle for output dir %s", *outDir)
|
|
|
|
} else if !fD.IsDir() {
|
|
|
|
log.Panicf("output directory '%s' is not a directory", *outDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Debug("reading global config...")
|
|
|
|
p := *inDir + "/config.yml"
|
|
|
|
data, err := ioutil.ReadFile(p)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("could not read file '%s': %s", p, err)
|
|
|
|
}
|
|
|
|
err = yaml.Unmarshal(data, globalConfig)
|
2019-02-10 13:49:27 +01:00
|
|
|
if err != nil {
|
2019-02-11 15:00:27 +01:00
|
|
|
log.Panicf("could not parse YAML file '%s': %s", p, err)
|
2019-02-10 13:49:27 +01:00
|
|
|
}
|
2019-02-11 15:00:27 +01:00
|
|
|
|
|
|
|
log.Debug(spew.Sdump(globalConfig))
|
|
|
|
|
|
|
|
log.Debugf("reading input directory %s", *inDir)
|
|
|
|
|
2019-02-12 12:52:46 +01:00
|
|
|
defaultTemplate := "index.html"
|
2019-02-12 10:21:51 +01:00
|
|
|
defaultInputFile := "README.md"
|
|
|
|
defaultOutputFile := "index.html"
|
2019-02-12 11:35:25 +01:00
|
|
|
defaultPathStrip := "^[0-9]*_(.*)"
|
2019-02-12 10:21:51 +01:00
|
|
|
defaultPathIgnoreForNav := "^_"
|
2019-02-12 11:35:25 +01:00
|
|
|
defaultFilenameStrip := "(.*).md$"
|
|
|
|
defaultFilenameIgnore := "^_"
|
2019-02-12 10:21:51 +01:00
|
|
|
defaultFilenameOutputExtension := "html"
|
|
|
|
|
|
|
|
defaultPathConfig := new(PathConfig)
|
2019-02-12 12:52:46 +01:00
|
|
|
defaultPathConfig.Template = &defaultTemplate
|
2019-02-12 11:35:25 +01:00
|
|
|
defaultPathConfig.Index.InputFile = &defaultInputFile
|
|
|
|
defaultPathConfig.Index.OutputFile = &defaultOutputFile
|
2019-02-12 10:21:51 +01:00
|
|
|
defaultPathConfig.Path.Strip = &defaultPathStrip
|
|
|
|
defaultPathConfig.Path.IgnoreForNav = &defaultPathIgnoreForNav
|
|
|
|
defaultPathConfig.Filename.Strip = &defaultFilenameStrip
|
|
|
|
defaultPathConfig.Filename.Ignore = &defaultFilenameIgnore
|
|
|
|
defaultPathConfig.Filename.OutputExtension = &defaultFilenameOutputExtension
|
|
|
|
|
|
|
|
readContentDir(*inDir+"/content", *outDir, "", defaultPathConfig, contentConfig)
|
|
|
|
//spew.Dump(contentConfig)
|
2019-02-11 15:00:27 +01:00
|
|
|
|
2019-02-12 15:28:22 +01:00
|
|
|
//spew.Dump(navMap)
|
|
|
|
|
2019-02-11 15:25:48 +01:00
|
|
|
processContent(contentConfig)
|
|
|
|
|
2019-02-12 19:09:25 +01:00
|
|
|
processAssets()
|
|
|
|
|
2019-02-10 13:49:27 +01:00
|
|
|
}
|