minor cleanup, add todos
This commit is contained in:
parent
fed9f01b82
commit
9356c338c7
4 changed files with 82 additions and 44 deletions
|
@ -17,6 +17,10 @@ via the Gemini protocol.
|
||||||
- supports with and without drafts from config
|
- supports with and without drafts from config
|
||||||
- composable with other tools
|
- composable with other tools
|
||||||
|
|
||||||
|
TODOs:
|
||||||
|
- cleanup long main
|
||||||
|
- gemrss?
|
||||||
|
|
||||||
To illustrate what this program does, run the following in the hugo directory.
|
To illustrate what this program does, run the following in the hugo directory.
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
27
file_tree.go
27
file_tree.go
|
@ -28,6 +28,33 @@ type File struct {
|
||||||
NewBody []byte
|
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) {
|
func parsePage(fullpath string) (hugo.Page, error) {
|
||||||
file, err := os.Open(fullpath)
|
file, err := os.Open(fullpath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
40
hugo/config.go
Normal file
40
hugo/config.go
Normal file
|
@ -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)
|
||||||
|
}
|
55
main.go
55
main.go
|
@ -8,8 +8,7 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/config"
|
"github.com/n0x1m/hugoext/hugo"
|
||||||
"github.com/spf13/afero"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -39,21 +38,9 @@ func main() {
|
||||||
// what are we doing
|
// what are we doing
|
||||||
fmt.Printf("hugoext: converting hugo markdown to %v with %v\n", ext, pipecmd)
|
fmt.Printf("hugoext: converting hugo markdown to %v with %v\n", ext, pipecmd)
|
||||||
|
|
||||||
// config
|
cfg := hugo.Config{}
|
||||||
cfg, err := config.FromFile(afero.NewOsFs(), "config.toml")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal("config from file", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
uglyURLs := cfg.GetBool("uglyURLs")
|
uglyURLs := cfg.GetBool("uglyURLs")
|
||||||
if !cfg.IsSet("uglyURLs") {
|
|
||||||
fmt.Println("config: no uglyURLs set, using default: ", uglyURLs)
|
|
||||||
}
|
|
||||||
|
|
||||||
buildDrafts := cfg.GetBool("buildDrafts")
|
buildDrafts := cfg.GetBool("buildDrafts")
|
||||||
if !cfg.IsSet("buildDrafts") {
|
|
||||||
fmt.Println("config: no buildDrafts set, using default: ", buildDrafts)
|
|
||||||
}
|
|
||||||
|
|
||||||
permalinks := cfg.GetStringMapString("permalinks")
|
permalinks := cfg.GetStringMapString("permalinks")
|
||||||
if permalinks == nil {
|
if permalinks == nil {
|
||||||
|
@ -61,8 +48,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
linkpattern := func(section string) string {
|
linkpattern := func(section string) string {
|
||||||
format, ok := permalinks[section]
|
if format, ok := permalinks[section]; ok {
|
||||||
if ok {
|
|
||||||
return format
|
return format
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,13 +58,13 @@ func main() {
|
||||||
// process sources
|
// process sources
|
||||||
|
|
||||||
// iterate through file tree source
|
// iterate through file tree source
|
||||||
files := make(chan File)
|
fileChan := make(chan File)
|
||||||
go collectFiles(source, files)
|
go collectFiles(source, fileChan)
|
||||||
|
|
||||||
// for each file, get destination path, switch file extension, remove underscore for index
|
// for each file, get destination path, switch file extension, remove underscore for index
|
||||||
var tree FileTree
|
var tree FileTree
|
||||||
|
|
||||||
for file := range files {
|
for file := range fileChan {
|
||||||
pattern := linkpattern(file.Parent)
|
pattern := linkpattern(file.Parent)
|
||||||
|
|
||||||
err := destinationPath(&file, pattern)
|
err := destinationPath(&file, pattern)
|
||||||
|
@ -116,36 +102,17 @@ func main() {
|
||||||
fmt.Printf("mkdir %s\n", newdir)
|
fmt.Printf("mkdir %s\n", newdir)
|
||||||
}
|
}
|
||||||
|
|
||||||
// write to destination
|
// write new content to destination
|
||||||
for _, file := range tree.Files {
|
for _, file := range tree.Files {
|
||||||
outdir, outfile := targetPath(file.Destination, ext, uglyURLs)
|
newpath, err := file.Write(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)
|
|
||||||
if err != nil {
|
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)
|
fmt.Printf("written %s (%dbytes)\n", newpath, len(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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// we're done if we
|
// we're done if we don't write any sections
|
||||||
if noSectionList {
|
if noSectionList {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue