mark2web/main.go
Sebastian Frank 3bce29a384
read config
2019-02-11 15:00:27 +01:00

264 lines
6.6 KiB
Go

package main
import (
"flag"
"io/ioutil"
"os"
"strings"
"github.com/davecgh/go-spew/spew"
"github.com/op/go-logging"
"gopkg.in/yaml.v2"
)
var log = logging.MustGetLogger("myLogger")
var inDir *string
var outDir *string
// GlobalConfig is config which is used only once in root dir
type GlobalConfig struct {
Webserver struct {
Type *string `yaml:"type"`
} `yaml:"webserver"`
Assets struct {
Path *string `yaml:"path"`
DeployType *string `yaml:"deployType"`
FixTemplate *bool `yaml:"fixTemplate"`
} `yaml:"assets"`
}
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"`
Meta struct {
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"`
}
// PathConfigTree is complete config tree of content dir
type PathConfigTree struct {
InputPath string
OutputPath string
Config *PathConfig
Sub []*PathConfigTree
}
func mergeConfig(mergeInto *PathConfig, mergeFrom *PathConfig) {
if mergeInto.InputFile == nil {
mergeInto.InputFile = mergeFrom.InputFile
}
if mergeInto.OutputFile == nil {
mergeInto.OutputFile = mergeFrom.OutputFile
}
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
}
}
var contentConfig = new(PathConfigTree)
func readContentDir(d string, conf *PathConfig, tree *PathConfigTree) {
files, err := ioutil.ReadDir(d)
if err != nil {
log.Panic(err)
}
tree.InputPath = d
// read config
var newConfig *PathConfig
log.Debug("looking for config.yml ...")
p := d + "/config.yml"
if _, err = os.Stat(p); os.IsNotExist(err) {
log.Debug("no config.yml found in this directory, using upper configs")
newConfig = conf
} else {
log.Debug("reading config...")
data, err := ioutil.ReadFile(p)
if err != nil {
log.Panicf("could not read file '%s': %s", p, err)
}
newConfig = new(PathConfig)
err = yaml.Unmarshal(data, newConfig)
if err != nil {
log.Panicf("could not parse YAML file '%s': %s", p, err)
}
log.Debug("merging config with upper config")
mergeConfig(newConfig, conf)
log.Debug(spew.Sdump(newConfig))
}
tree.Config = newConfig
// create outDir
oDir := *outDir + "/" + strings.TrimPrefix(d, *inDir+"/content")
oDir = strings.Replace(oDir, "//", "/", -1)
oDir = strings.TrimSuffix(oDir, "/")
log.Noticef("creating output directory: %s", oDir)
tree.OutputPath = oDir
// first only files
for _, f := range files {
p := d + "/" + f.Name()
if strings.HasSuffix(f.Name(), ".md") {
log.Debugf(".MD %s", p)
}
}
// only directorys, needed config before
for _, f := range files {
p := d + "/" + f.Name()
if f.IsDir() {
log.Debugf("DIR %s", p)
newTree := new(PathConfigTree)
if tree.Sub == nil {
tree.Sub = make([]*PathConfigTree, 0, 0)
}
tree.Sub = append(tree.Sub, newTree)
readContentDir(p, newConfig, newTree)
}
}
}
func main() {
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)
if err != nil {
log.Panicf("could not parse YAML file '%s': %s", p, err)
}
log.Debug(spew.Sdump(globalConfig))
log.Debugf("reading input directory %s", *inDir)
readContentDir(*inDir+"/content", nil, contentConfig)
// log.Debug(spew.Sdump(contentConfig))
/*
input, err := ioutil.ReadFile("README.md")
if err != nil {
panic(err)
}
//html := blackfriday.Run(input, blackfriday.WithRenderer(bfchroma.NewRenderer()))
html := blackfriday.Run(input)
fmt.Println(string(html))
*/
}