create output directories
This commit is contained in:
parent
bd24925c48
commit
29a745283a
@ -9,5 +9,11 @@ meta:
|
|||||||
keywords: "global keywords"
|
keywords: "global keywords"
|
||||||
|
|
||||||
path:
|
path:
|
||||||
strip: "^[0-9]+_"
|
strip: "^[0-9]*_"
|
||||||
ignoreForNav: "^_"
|
ignoreForNav: "^_"
|
||||||
|
|
||||||
|
filename:
|
||||||
|
strip: ""
|
||||||
|
ignore: "_"
|
||||||
|
outputExtension: "html"
|
||||||
|
|
||||||
|
0
example/content/de/main/_zeug/links.md
Normal file
0
example/content/de/main/_zeug/links.md
Normal file
103
main.go
103
main.go
@ -50,6 +50,12 @@ type PathConfig struct {
|
|||||||
Strip *string `yaml:"strip"`
|
Strip *string `yaml:"strip"`
|
||||||
IgnoreForNav *string `yaml:"ignoreForNav"`
|
IgnoreForNav *string `yaml:"ignoreForNav"`
|
||||||
} `yaml:"path"`
|
} `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
|
// PathConfigTree is complete config tree of content dir
|
||||||
@ -57,6 +63,8 @@ type PathConfigTree struct {
|
|||||||
InputPath string
|
InputPath string
|
||||||
OutputPath string
|
OutputPath string
|
||||||
|
|
||||||
|
InputFiles []string
|
||||||
|
|
||||||
Config *PathConfig
|
Config *PathConfig
|
||||||
Sub []*PathConfigTree
|
Sub []*PathConfigTree
|
||||||
}
|
}
|
||||||
@ -90,32 +98,39 @@ func mergeConfig(mergeInto *PathConfig, mergeFrom *PathConfig) {
|
|||||||
|
|
||||||
var contentConfig = new(PathConfigTree)
|
var contentConfig = new(PathConfigTree)
|
||||||
|
|
||||||
func readContentDir(d string, conf *PathConfig, tree *PathConfigTree) {
|
func readContentDir(inBase string, outBase string, dir string, conf *PathConfig, tree *PathConfigTree) {
|
||||||
files, err := ioutil.ReadDir(d)
|
inPath := inBase
|
||||||
|
if dir != "" {
|
||||||
|
inPath += "/" + dir
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Noticef("reading input directory: %s", inPath)
|
||||||
|
|
||||||
|
files, err := ioutil.ReadDir(inPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic(err)
|
log.Panic(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
tree.InputPath = d
|
tree.InputPath = inPath
|
||||||
|
|
||||||
// read config
|
// read config
|
||||||
var newConfig *PathConfig
|
var newConfig *PathConfig
|
||||||
log.Debug("looking for config.yml ...")
|
log.Debug("looking for config.yml ...")
|
||||||
p := d + "/config.yml"
|
configFile := inPath + "/config.yml"
|
||||||
if _, err = os.Stat(p); os.IsNotExist(err) {
|
if _, err = os.Stat(configFile); os.IsNotExist(err) {
|
||||||
log.Debug("no config.yml found in this directory, using upper configs")
|
log.Debug("no config.yml found in this directory, using upper configs")
|
||||||
newConfig = conf
|
newConfig = conf
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
log.Debug("reading config...")
|
log.Debug("reading config...")
|
||||||
data, err := ioutil.ReadFile(p)
|
data, err := ioutil.ReadFile(configFile)
|
||||||
if err != nil {
|
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)
|
newConfig = new(PathConfig)
|
||||||
err = yaml.Unmarshal(data, newConfig)
|
err = yaml.Unmarshal(data, newConfig)
|
||||||
if err != nil {
|
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")
|
log.Debug("merging config with upper config")
|
||||||
@ -126,47 +141,71 @@ func readContentDir(d string, conf *PathConfig, tree *PathConfigTree) {
|
|||||||
tree.Config = newConfig
|
tree.Config = newConfig
|
||||||
|
|
||||||
// calc outDir
|
// calc outDir
|
||||||
stripedD := d
|
stripedDir := dir
|
||||||
regexStr := newConfig.Path.Strip
|
regexStr := newConfig.Path.Strip
|
||||||
if regexStr != nil && *regexStr != "" {
|
if regexStr != nil && *regexStr != "" {
|
||||||
if regex, err := regexp.Compile("^[0-9]*_"); err != nil {
|
if regex, err := regexp.Compile(*regexStr); err != nil {
|
||||||
log.Panicf("error compiling path.strip regex '%s' from '%s': %s", *regexStr, p, err)
|
log.Panicf("error compiling path.strip regex '%s' from '%s': %s", *regexStr, inBase+"/"+dir, err)
|
||||||
} else {
|
} else {
|
||||||
stripedD = regex.ReplaceAllString(stripedD, "")
|
stripedDir = regex.ReplaceAllString(stripedDir, "")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
oDir := *outDir + "/" + strings.TrimPrefix(stripedD, *inDir+"/content")
|
outPath := outBase + "/" + stripedDir
|
||||||
oDir = strings.Replace(oDir, "//", "/", -1)
|
outPath = strings.Replace(outPath, "//", "/", -1)
|
||||||
oDir = strings.TrimSuffix(oDir, "/")
|
outPath = strings.TrimSuffix(outPath, "/")
|
||||||
|
|
||||||
log.Noticef("calculated output directory: %s", oDir)
|
log.Noticef("calculated output directory: %s", outPath)
|
||||||
tree.OutputPath = oDir
|
tree.OutputPath = outPath
|
||||||
|
|
||||||
// first only files
|
// first only files
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
p := d + "/" + f.Name()
|
p := inPath + "/" + f.Name()
|
||||||
if strings.HasSuffix(f.Name(), ".md") {
|
if strings.HasSuffix(f.Name(), ".md") {
|
||||||
log.Debugf(".MD %s", p)
|
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
|
// only directorys, needed config before
|
||||||
for _, f := range files {
|
for _, f := range files {
|
||||||
p := d + "/" + f.Name()
|
p := inPath + "/" + f.Name()
|
||||||
if f.IsDir() {
|
if f.IsDir() {
|
||||||
log.Debugf("DIR %s", p)
|
log.Debugf("DIR %s", p)
|
||||||
newTree := new(PathConfigTree)
|
newTree := new(PathConfigTree)
|
||||||
if tree.Sub == nil {
|
if tree.Sub == nil {
|
||||||
tree.Sub = make([]*PathConfigTree, 0, 0)
|
tree.Sub = make([]*PathConfigTree, 0)
|
||||||
}
|
}
|
||||||
tree.Sub = append(tree.Sub, newTree)
|
tree.Sub = append(tree.Sub, newTree)
|
||||||
readContentDir(p, newConfig, newTree)
|
readContentDir(inPath, outPath, f.Name(), newConfig, newTree)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func processContent(conf *PathConfigTree) {
|
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() {
|
func main() {
|
||||||
@ -260,9 +299,27 @@ func main() {
|
|||||||
log.Debug(spew.Sdump(globalConfig))
|
log.Debug(spew.Sdump(globalConfig))
|
||||||
|
|
||||||
log.Debugf("reading input directory %s", *inDir)
|
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)
|
processContent(contentConfig)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user