make ugly-urls work

This commit is contained in:
dre 2021-07-11 10:46:19 +08:00
parent 5e4ab1ccd5
commit 3dab063ba2
2 changed files with 32 additions and 26 deletions

View file

@ -1,6 +1,6 @@
# hugoext # hugoext
Utility to parse a hugo config file and create the same file structure for content through an Utility to parse the hugo config file and create the same file structure for content through an
arbitrary output pipe extension. arbitrary output pipe extension.
Hugo parses primarily markdown files and go templates. The initial motivation for this utility was Hugo parses primarily markdown files and go templates. The initial motivation for this utility was
@ -19,7 +19,7 @@ Features
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.
``` ```
hugoext -proc="" -ext txt hugoext -ext txt -pipe=""
``` ```
The markdown files from `./content` will be written as `.txt` files to the `./public` directory. We The markdown files from `./content` will be written as `.txt` files to the `./public` directory. We

54
main.go
View file

@ -29,11 +29,11 @@ const (
) )
func main() { func main() {
var ext, processor, source, destination, cfgPath string var ext, pipe, source, destination, cfgPath string
var uglyURLs, noSectionList, withDrafts bool var uglyURLs, noSectionList, withDrafts bool
flag.StringVar(&ext, "ext", defaultExt, "ext to look for templates in ./layout") flag.StringVar(&ext, "ext", defaultExt, "ext to look for templates in ./layout")
flag.StringVar(&processor, "proc", defaultProcessor, "processor to pipe markdown content through") flag.StringVar(&pipe, "pipe", defaultProcessor, "pipe markdown to this program for content processing")
flag.StringVar(&source, "source", defaultSource, "source directory") flag.StringVar(&source, "source", defaultSource, "source directory")
flag.StringVar(&destination, "destination", defaultDestination, "output directory") flag.StringVar(&destination, "destination", defaultDestination, "output directory")
flag.StringVar(&cfgPath, "config", defaultConfigPath, "hugo config path") flag.StringVar(&cfgPath, "config", defaultConfigPath, "hugo config path")
@ -42,7 +42,7 @@ func main() {
flag.BoolVar(&withDrafts, "enable-withDrafts", false, "include withDrafts in processing and output") flag.BoolVar(&withDrafts, "enable-withDrafts", false, "include withDrafts in processing and output")
flag.Parse() flag.Parse()
fmt.Printf("converting markdown to %v with %v\n", ext, processor) fmt.Printf("converting markdown to %v with %v\n", ext, pipe)
osfs := afero.NewOsFs() osfs := afero.NewOsFs()
cfg, err := config.FromFile(osfs, "config.toml") cfg, err := config.FromFile(osfs, "config.toml")
@ -87,7 +87,7 @@ func main() {
// call proc and pipe content through it, catch output of proc // call proc and pipe content through it, catch output of proc
for i, file := range tree.Files { for i, file := range tree.Files {
// fmt.Printf("%s -> %s (%d)\n", file.Source, file.Destination, len(file.Body)) // fmt.Printf("%s -> %s (%d)\n", file.Source, file.Destination, len(file.Body))
extpipe := exec.Command(processor) extpipe := exec.Command(pipe)
buf := bytes.NewReader(file.Body) buf := bytes.NewReader(file.Body)
extpipe.Stdin = buf extpipe.Stdin = buf
@ -113,44 +113,50 @@ func main() {
//fmt.Printf("%s -> %s (%d)\n", file.Source, file.Destination, len(file.Body)) //fmt.Printf("%s -> %s (%d)\n", file.Source, file.Destination, len(file.Body))
outfile := "index." + ext outfile := "index." + ext
outdir := filepath.Join(destination, file.Destination) outdir := file.Destination
if file.Destination != "index" {
newpath := filepath.Join(destination, file.Destination) if uglyURLs && file.Destination != "index" {
if err = os.MkdirAll(newpath, os.ModePerm); err != nil { // make the last element in destination the file
log.Println(err) outfile = filepath.Base(file.Destination) + "." + ext
continue // set the parent directory of that file to be the dir to create
} outdir = filepath.Dir(file.Destination)
// TODO: change dir^
// TODO: consider links
if uglyURLs {
outdir += ".gmi"
outfile = ""
}
} else {
outdir = destination
} }
fullpath := filepath.Join(outdir, outfile) if file.Destination == "index" {
outdir = "."
}
// ensure directory exists
newpath := filepath.Join(destination, outdir)
fmt.Printf("mkdir %s\n", newpath)
if err = os.MkdirAll(newpath, os.ModePerm); err != nil {
log.Fatalf("cannot directory file %s, error: %v", newpath, err)
}
// create file based on directory and filename
fullpath := filepath.Join(newpath, outfile)
newfile, err := os.Create(fullpath) newfile, err := os.Create(fullpath)
if err != nil { if err != nil {
log.Println(err) log.Fatalf("cannot create file %s, error: %v", fullpath, err)
continue
} }
n, _ := newfile.Write(file.NewBody) n, err := newfile.Write(file.NewBody)
if err != nil {
log.Fatalf("cannot write file %s, error: %v", fullpath, err)
}
newfile.Close() newfile.Close()
fmt.Printf("written %s (%dbytes)\n", fullpath, n) fmt.Printf("written %s (%dbytes)\n", fullpath, n)
} }
// TODO // TODO
// ugly urls
// //
// append section listings // append section listings
// => link title line // => link title line
// date - summary block // date - summary block
// TODO // TODO
// in watch mode, compare timestamps of files before replacement, keep index?
// check/replace links // check/replace links
// write rss? // write rss?
// write listings from template? // write listings from template?