diff --git a/README.md b/README.md index fea9bed..24899d5 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,10 @@ via the Gemini protocol. - supports with and without drafts from config - composable with other tools +TODOs: +- cleanup long main +- gemrss? + To illustrate what this program does, run the following in the hugo directory. ``` diff --git a/file_tree.go b/file_tree.go index 0efc7a7..3b191f6 100644 --- a/file_tree.go +++ b/file_tree.go @@ -28,6 +28,33 @@ type File struct { NewBody []byte } +func (file *File) Write(dest, newext string, uglyURLs bool) (string, error) { + outdir, outfile := targetPath(file.Destination, newext, uglyURLs) + + // ensure directory exists + newdir := filepath.Join(dest, outdir) + if made, err := mkdir(newdir); err != nil { + return "", err + } else if made { + fmt.Printf("mkdir %s\n", newdir) + } + + fullpath := filepath.Join(newdir, outfile) + + // create file based on directory and filename + newfile, err := os.Create(fullpath) + if err != nil { + return fullpath, err + } + + if _, err = newfile.Write(file.NewBody); err != nil { + return fullpath, err + } + + newfile.Close() + return fullpath, nil +} + func parsePage(fullpath string) (hugo.Page, error) { file, err := os.Open(fullpath) if err != nil { diff --git a/hugo/config.go b/hugo/config.go new file mode 100644 index 0000000..1722279 --- /dev/null +++ b/hugo/config.go @@ -0,0 +1,40 @@ +package hugo + +import ( + "fmt" + + hugoconfig "github.com/gohugoio/hugo/config" + "github.com/spf13/afero" +) + +type Config struct { + hugoconfig hugoconfig.Provider +} + +func (c *Config) read() hugoconfig.Provider { + if c.hugoconfig == nil { + cfg, err := hugoconfig.FromFile(afero.NewOsFs(), "config.toml") + if err != nil { + fmt.Println("load config from file failed", err) + return nil + } + c.hugoconfig = cfg + } + return c.hugoconfig +} + +func (c *Config) GetBool(v string) bool { + cfg := c.read() + if cfg == nil || !cfg.IsSet(v) { + fmt.Printf("config: no %v set, using default\n", v) + } + return cfg.GetBool(v) +} + +func (c *Config) GetStringMapString(v string) map[string]string { + cfg := c.read() + if cfg == nil || !cfg.IsSet(v) { + fmt.Printf("config: no %v set, using default\n", v) + } + return cfg.GetStringMapString(v) +} diff --git a/main.go b/main.go index 77084c1..b46e61e 100644 --- a/main.go +++ b/main.go @@ -8,8 +8,7 @@ import ( "os" "path/filepath" - "github.com/gohugoio/hugo/config" - "github.com/spf13/afero" + "github.com/n0x1m/hugoext/hugo" ) const ( @@ -39,21 +38,9 @@ func main() { // what are we doing fmt.Printf("hugoext: converting hugo markdown to %v with %v\n", ext, pipecmd) - // config - cfg, err := config.FromFile(afero.NewOsFs(), "config.toml") - if err != nil { - log.Fatal("config from file", err) - } - + cfg := hugo.Config{} uglyURLs := cfg.GetBool("uglyURLs") - if !cfg.IsSet("uglyURLs") { - fmt.Println("config: no uglyURLs set, using default: ", uglyURLs) - } - buildDrafts := cfg.GetBool("buildDrafts") - if !cfg.IsSet("buildDrafts") { - fmt.Println("config: no buildDrafts set, using default: ", buildDrafts) - } permalinks := cfg.GetStringMapString("permalinks") if permalinks == nil { @@ -61,8 +48,7 @@ func main() { } linkpattern := func(section string) string { - format, ok := permalinks[section] - if ok { + if format, ok := permalinks[section]; ok { return format } @@ -72,13 +58,13 @@ func main() { // process sources // iterate through file tree source - files := make(chan File) - go collectFiles(source, files) + fileChan := make(chan File) + go collectFiles(source, fileChan) // for each file, get destination path, switch file extension, remove underscore for index var tree FileTree - for file := range files { + for file := range fileChan { pattern := linkpattern(file.Parent) err := destinationPath(&file, pattern) @@ -116,36 +102,17 @@ func main() { fmt.Printf("mkdir %s\n", newdir) } - // write to destination + // write new content to destination for _, file := range tree.Files { - outdir, outfile := targetPath(file.Destination, ext, uglyURLs) - - // ensure directory exists - newdir := filepath.Join(destination, outdir) - if made, err := mkdir(newdir); err != nil { - log.Fatal(err) - } else if made { - fmt.Printf("mkdir %s\n", newdir) - } - - fullpath := filepath.Join(newdir, outfile) - - // create file based on directory and filename - newfile, err := os.Create(fullpath) + newpath, err := file.Write(destination, ext, uglyURLs) if err != nil { - log.Fatalf("cannot create file %s, error: %v", fullpath, err) + log.Fatalf("new file write '%v' failed with %v", file.Name, err) } - n, err := newfile.Write(file.NewBody) - if err != nil { - log.Fatalf("cannot write file %s, error: %v", fullpath, err) - } - - newfile.Close() - fmt.Printf("written %s (%dbytes)\n", fullpath, n) + fmt.Printf("written %s (%dbytes)\n", newpath, len(file.NewBody)) } - // we're done if we + // we're done if we don't write any sections if noSectionList { return }