2021-07-10 16:27:58 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-07-10 19:09:35 +03:00
|
|
|
"bytes"
|
2021-07-10 16:27:58 +03:00
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2021-07-10 19:09:35 +03:00
|
|
|
"os/exec"
|
2021-07-10 16:27:58 +03:00
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/config"
|
|
|
|
"github.com/spf13/afero"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-07-10 20:29:02 +03:00
|
|
|
defaultExt = "md"
|
|
|
|
defaultProcessor = ""
|
2021-07-10 16:27:58 +03:00
|
|
|
defaultSource = "content"
|
|
|
|
defaultDestination = "public"
|
|
|
|
defaultConfigPath = "config.toml"
|
|
|
|
defaultUglyURLs = false
|
|
|
|
|
|
|
|
defaultPermalinkFormat = "/:year/:month/:title/"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2021-07-11 05:46:19 +03:00
|
|
|
var ext, pipe, source, destination, cfgPath string
|
2021-07-11 06:32:07 +03:00
|
|
|
var noSectionList, withDrafts bool
|
2021-07-10 16:27:58 +03:00
|
|
|
|
|
|
|
flag.StringVar(&ext, "ext", defaultExt, "ext to look for templates in ./layout")
|
2021-07-11 05:46:19 +03:00
|
|
|
flag.StringVar(&pipe, "pipe", defaultProcessor, "pipe markdown to this program for content processing")
|
2021-07-10 16:27:58 +03:00
|
|
|
flag.StringVar(&source, "source", defaultSource, "source directory")
|
|
|
|
flag.StringVar(&destination, "destination", defaultDestination, "output directory")
|
2021-07-10 20:18:52 +03:00
|
|
|
flag.StringVar(&cfgPath, "config", defaultConfigPath, "hugo config path")
|
|
|
|
flag.BoolVar(&noSectionList, "no-section-list", false, "disable auto append of section content lists")
|
|
|
|
flag.BoolVar(&withDrafts, "enable-withDrafts", false, "include withDrafts in processing and output")
|
2021-07-10 16:27:58 +03:00
|
|
|
flag.Parse()
|
|
|
|
|
2021-07-11 06:32:07 +03:00
|
|
|
// what are we doing
|
|
|
|
fmt.Printf("converting hugo markdown to %v with %v\n", ext, pipe)
|
2021-07-10 20:33:55 +03:00
|
|
|
|
2021-07-11 06:32:07 +03:00
|
|
|
// config
|
|
|
|
cfg, err := config.FromFile(afero.NewOsFs(), "config.toml")
|
2021-07-10 16:27:58 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal("config from file", err)
|
|
|
|
}
|
|
|
|
|
2021-07-11 06:32:07 +03:00
|
|
|
uglyURLs := cfg.GetBool("uglyURLs")
|
|
|
|
if !cfg.IsSet("uglyURLs") {
|
|
|
|
fmt.Println("config: no uglyURLs set, using default: ", uglyURLs)
|
|
|
|
}
|
|
|
|
|
2021-07-10 16:27:58 +03:00
|
|
|
permalinks := cfg.GetStringMapString("permalinks")
|
2021-07-10 19:32:21 +03:00
|
|
|
if permalinks == nil {
|
2021-07-11 06:32:07 +03:00
|
|
|
fmt.Println("config: no permalinks set, using default: ", defaultPermalinkFormat)
|
2021-07-10 19:32:21 +03:00
|
|
|
}
|
2021-07-10 16:27:58 +03:00
|
|
|
|
2021-07-11 06:32:07 +03:00
|
|
|
linkpattern := func(section string) string {
|
|
|
|
format, ok := permalinks[section]
|
2021-07-10 16:27:58 +03:00
|
|
|
if ok {
|
|
|
|
return format
|
|
|
|
}
|
|
|
|
return defaultPermalinkFormat
|
|
|
|
}
|
2021-07-10 17:44:25 +03:00
|
|
|
|
2021-07-11 06:32:07 +03:00
|
|
|
// process sources
|
|
|
|
|
2021-07-10 19:09:35 +03:00
|
|
|
// iterate through file tree source
|
2021-07-10 17:44:25 +03:00
|
|
|
files := make(chan File)
|
|
|
|
go collectFiles(source, files)
|
2021-07-10 19:09:35 +03:00
|
|
|
|
|
|
|
// for each file, get destination path, switch file extension, remove underscore for index
|
|
|
|
var tree FileTree
|
2021-07-10 17:44:25 +03:00
|
|
|
for file := range files {
|
2021-07-10 19:32:21 +03:00
|
|
|
pattern := linkpattern(file.Parent)
|
2021-07-10 17:44:25 +03:00
|
|
|
err := destinationPath(&file, pattern)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err, file)
|
|
|
|
continue
|
|
|
|
}
|
2021-07-10 19:09:35 +03:00
|
|
|
//fmt.Printf("%s -> %s (%d)\n", file.Source, file.Destination, len(file.Body))
|
2021-07-10 20:18:52 +03:00
|
|
|
if file.Draft && !withDrafts {
|
|
|
|
fmt.Printf("skipping draft %s (%dbytes)\n", file.Source, len(file.Body))
|
|
|
|
continue
|
|
|
|
}
|
2021-07-10 19:09:35 +03:00
|
|
|
tree.Files = append(tree.Files, file)
|
|
|
|
}
|
|
|
|
|
|
|
|
// call proc and pipe content through it, catch output of proc
|
|
|
|
for i, file := range tree.Files {
|
|
|
|
// fmt.Printf("%s -> %s (%d)\n", file.Source, file.Destination, len(file.Body))
|
2021-07-11 05:46:19 +03:00
|
|
|
extpipe := exec.Command(pipe)
|
2021-07-10 19:09:35 +03:00
|
|
|
buf := bytes.NewReader(file.Body)
|
|
|
|
extpipe.Stdin = buf
|
|
|
|
|
|
|
|
var procout bytes.Buffer
|
|
|
|
extpipe.Stdout = &procout
|
|
|
|
|
|
|
|
extpipe.Start()
|
|
|
|
extpipe.Wait()
|
|
|
|
|
|
|
|
// write to source
|
|
|
|
tree.Files[i].NewBody = procout.Bytes()
|
2021-07-10 19:32:21 +03:00
|
|
|
fmt.Printf("processed %s (%dbytes)\n", file.Source, len(tree.Files[i].Body))
|
2021-07-10 19:09:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
newpath := filepath.Join(".", "public")
|
|
|
|
err = os.MkdirAll(newpath, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// write to destination
|
|
|
|
for _, file := range tree.Files {
|
2021-07-10 19:32:21 +03:00
|
|
|
//fmt.Printf("%s -> %s (%d)\n", file.Source, file.Destination, len(file.Body))
|
2021-07-10 19:09:35 +03:00
|
|
|
|
|
|
|
outfile := "index." + ext
|
2021-07-11 05:46:19 +03:00
|
|
|
outdir := file.Destination
|
|
|
|
|
|
|
|
if uglyURLs && file.Destination != "index" {
|
|
|
|
// make the last element in destination the file
|
|
|
|
outfile = filepath.Base(file.Destination) + "." + ext
|
|
|
|
// set the parent directory of that file to be the dir to create
|
|
|
|
outdir = filepath.Dir(file.Destination)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2021-07-10 19:09:35 +03:00
|
|
|
}
|
|
|
|
|
2021-07-11 05:46:19 +03:00
|
|
|
// create file based on directory and filename
|
|
|
|
fullpath := filepath.Join(newpath, outfile)
|
2021-07-10 19:09:35 +03:00
|
|
|
newfile, err := os.Create(fullpath)
|
|
|
|
if err != nil {
|
2021-07-11 05:46:19 +03:00
|
|
|
log.Fatalf("cannot create file %s, error: %v", fullpath, err)
|
2021-07-10 19:09:35 +03:00
|
|
|
}
|
2021-07-10 19:32:21 +03:00
|
|
|
|
2021-07-11 05:46:19 +03:00
|
|
|
n, err := newfile.Write(file.NewBody)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("cannot write file %s, error: %v", fullpath, err)
|
|
|
|
}
|
2021-07-10 19:09:35 +03:00
|
|
|
newfile.Close()
|
2021-07-10 19:32:21 +03:00
|
|
|
|
|
|
|
fmt.Printf("written %s (%dbytes)\n", fullpath, n)
|
2021-07-10 17:44:25 +03:00
|
|
|
}
|
|
|
|
|
2021-07-10 20:18:52 +03:00
|
|
|
// TODO
|
|
|
|
//
|
|
|
|
// append section listings
|
|
|
|
// => link title line
|
|
|
|
// date - summary block
|
|
|
|
|
2021-07-10 19:32:21 +03:00
|
|
|
// TODO
|
2021-07-11 05:46:19 +03:00
|
|
|
// in watch mode, compare timestamps of files before replacement, keep index?
|
2021-07-10 19:32:21 +03:00
|
|
|
// check/replace links
|
2021-07-10 17:44:25 +03:00
|
|
|
// write rss?
|
|
|
|
// write listings from template?
|
2021-07-10 16:27:58 +03:00
|
|
|
}
|