create output directories

This commit is contained in:
Sebastian Frank 2019-02-12 10:21:51 +01:00
parent bd24925c48
commit 29a745283a
Signed by: apairon
GPG Key ID: 7270D06DDA7FE8C3
3 changed files with 87 additions and 24 deletions

View File

@ -9,5 +9,11 @@ meta:
keywords: "global keywords"
path:
strip: "^[0-9]+_"
strip: "^[0-9]*_"
ignoreForNav: "^_"
filename:
strip: ""
ignore: "_"
outputExtension: "html"

View File

103
main.go
View File

@ -50,6 +50,12 @@ type PathConfig struct {
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"`
}
// PathConfigTree is complete config tree of content dir
@ -57,6 +63,8 @@ type PathConfigTree struct {
InputPath string
OutputPath string
InputFiles []string
Config *PathConfig
Sub []*PathConfigTree
}
@ -90,32 +98,39 @@ func mergeConfig(mergeInto *PathConfig, mergeFrom *PathConfig) {
var contentConfig = new(PathConfigTree)
func readContentDir(d string, conf *PathConfig, tree *PathConfigTree) {
files, err := ioutil.ReadDir(d)
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)
if err != nil {
log.Panic(err)
}
tree.InputPath = d
tree.InputPath = inPath
// read config
var newConfig *PathConfig
log.Debug("looking for config.yml ...")
p := d + "/config.yml"
if _, err = os.Stat(p); os.IsNotExist(err) {
configFile := inPath + "/config.yml"
if _, err = os.Stat(configFile); 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)
data, err := ioutil.ReadFile(configFile)
if err != nil {
log.Panicf("could not read file '%s': %s", p, err)
log.Panicf("could not read file '%s': %s", configFile, err)
}
newConfig = new(PathConfig)
err = yaml.Unmarshal(data, newConfig)
if err != nil {
log.Panicf("could not parse YAML file '%s': %s", p, err)
log.Panicf("could not parse YAML file '%s': %s", configFile, err)
}
log.Debug("merging config with upper config")
@ -126,47 +141,71 @@ func readContentDir(d string, conf *PathConfig, tree *PathConfigTree) {
tree.Config = newConfig
// calc outDir
stripedD := d
stripedDir := dir
regexStr := newConfig.Path.Strip
if regexStr != nil && *regexStr != "" {
if regex, err := regexp.Compile("^[0-9]*_"); err != nil {
log.Panicf("error compiling path.strip regex '%s' from '%s': %s", *regexStr, p, err)
if regex, err := regexp.Compile(*regexStr); err != nil {
log.Panicf("error compiling path.strip regex '%s' from '%s': %s", *regexStr, inBase+"/"+dir, err)
} else {
stripedD = regex.ReplaceAllString(stripedD, "")
stripedDir = regex.ReplaceAllString(stripedDir, "")
}
}
oDir := *outDir + "/" + strings.TrimPrefix(stripedD, *inDir+"/content")
oDir = strings.Replace(oDir, "//", "/", -1)
oDir = strings.TrimSuffix(oDir, "/")
outPath := outBase + "/" + stripedDir
outPath = strings.Replace(outPath, "//", "/", -1)
outPath = strings.TrimSuffix(outPath, "/")
log.Noticef("calculated output directory: %s", oDir)
tree.OutputPath = oDir
log.Noticef("calculated output directory: %s", outPath)
tree.OutputPath = outPath
// first only files
for _, f := range files {
p := d + "/" + f.Name()
p := inPath + "/" + f.Name()
if strings.HasSuffix(f.Name(), ".md") {
log.Debugf(".MD %s", p)
if tree.InputFiles == nil {
tree.InputFiles = make([]string, 0)
}
tree.InputFiles = append(tree.InputFiles, f.Name())
}
}
// only directorys, needed config before
for _, f := range files {
p := d + "/" + f.Name()
p := inPath + "/" + 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 = make([]*PathConfigTree, 0)
}
tree.Sub = append(tree.Sub, newTree)
readContentDir(p, newConfig, newTree)
readContentDir(inPath, outPath, f.Name(), newConfig, newTree)
}
}
}
func processContent(conf *PathConfigTree) {
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)
}
for _, el := range conf.Sub {
processContent(el)
}
}
func main() {
@ -260,9 +299,27 @@ func main() {
log.Debug(spew.Sdump(globalConfig))
log.Debugf("reading input directory %s", *inDir)
readContentDir(*inDir+"/content", nil, contentConfig)
// log.Debug(spew.Sdump(contentConfig))
defaultInputFile := "README.md"
defaultOutputFile := "index.html"
defaultPathStrip := "^[0-9]*_"
defaultPathIgnoreForNav := "^_"
defaultFilenameStrip := ""
defaultFilenameIgnore := "_"
defaultFilenameOutputExtension := "html"
defaultPathConfig := new(PathConfig)
defaultPathConfig.InputFile = &defaultInputFile
defaultPathConfig.OutputFile = &defaultOutputFile
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)
processContent(contentConfig)