2017-06-10 21:38:09 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-06-11 01:32:39 +02:00
|
|
|
"sort"
|
2017-06-12 00:11:52 +02:00
|
|
|
"strings"
|
2017-06-10 21:38:09 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/acstech/liquid"
|
|
|
|
)
|
|
|
|
|
|
|
|
// This is the longest label. Pull it out here so we can both use it, and measure it for alignment.
|
|
|
|
const configurationFileLabel = "Configuration file:"
|
|
|
|
|
|
|
|
func printSetting(label string, value string) {
|
|
|
|
fmt.Printf("%s %s\n",
|
2017-06-11 22:00:03 +02:00
|
|
|
leftPad(label, len(configurationFileLabel)), value)
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func printPathSetting(label string, path string) {
|
|
|
|
path, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
panic("Couldn't convert to absolute path")
|
|
|
|
}
|
|
|
|
printSetting(label, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
liquid.Tags["link"] = LinkFactory
|
|
|
|
|
|
|
|
flag.StringVar(&siteConfig.DestinationDir, "destination", siteConfig.DestinationDir, "Destination directory")
|
|
|
|
flag.StringVar(&siteConfig.SourceDir, "source", siteConfig.SourceDir, "Source directory")
|
2017-06-11 01:32:39 +02:00
|
|
|
|
|
|
|
// routes subcommand
|
|
|
|
dynamic := flag.Bool("dynamic", false, "Dynamic routes only")
|
|
|
|
|
2017-06-10 21:38:09 +02:00
|
|
|
flag.Parse()
|
2017-06-12 00:11:52 +02:00
|
|
|
if len(flag.Args()) < 1 {
|
|
|
|
fmt.Println("A subcommand is required.")
|
|
|
|
return
|
|
|
|
}
|
2017-06-10 21:38:09 +02:00
|
|
|
|
2017-06-10 23:51:46 +02:00
|
|
|
configPath := filepath.Join(siteConfig.SourceDir, "_config.yml")
|
2017-06-10 21:38:09 +02:00
|
|
|
// TODO error if file is e.g. unreadable
|
|
|
|
if _, err := os.Stat(configPath); err == nil {
|
2017-06-11 01:32:39 +02:00
|
|
|
if err := siteConfig.read(configPath); err != nil {
|
2017-06-10 21:38:09 +02:00
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
printPathSetting(configurationFileLabel, configPath)
|
|
|
|
} else {
|
|
|
|
printSetting(configurationFileLabel, "none")
|
|
|
|
}
|
|
|
|
printPathSetting("Source:", siteConfig.SourceDir)
|
|
|
|
|
2017-06-12 00:11:52 +02:00
|
|
|
start := time.Now()
|
2017-06-10 23:51:46 +02:00
|
|
|
fileMap, err := buildSiteMap()
|
2017-06-10 21:38:09 +02:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
siteMap = fileMap
|
|
|
|
|
|
|
|
switch flag.Arg(0) {
|
|
|
|
case "s", "serve", "server":
|
2017-06-12 00:11:52 +02:00
|
|
|
if err = server(); err != nil {
|
2017-06-11 20:57:57 +02:00
|
|
|
fmt.Println(err)
|
|
|
|
}
|
2017-06-10 21:38:09 +02:00
|
|
|
case "b", "build":
|
2017-06-12 00:11:52 +02:00
|
|
|
printPathSetting("Destination:", siteConfig.DestinationDir)
|
2017-06-10 21:38:09 +02:00
|
|
|
printSetting("Generating...", "")
|
2017-06-12 00:11:52 +02:00
|
|
|
if err = build(); err != nil {
|
2017-06-11 20:57:57 +02:00
|
|
|
fmt.Println(err)
|
|
|
|
break
|
|
|
|
}
|
2017-06-10 21:38:09 +02:00
|
|
|
elapsed := time.Since(start)
|
|
|
|
printSetting("", fmt.Sprintf("done in %.2fs.", elapsed.Seconds()))
|
|
|
|
case "routes":
|
|
|
|
fmt.Printf("\nRoutes:\n")
|
2017-06-11 01:32:39 +02:00
|
|
|
urls := []string{}
|
|
|
|
for u, p := range siteMap {
|
|
|
|
if !(*dynamic && p.Static) {
|
|
|
|
urls = append(urls, u)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sort.Strings(urls)
|
|
|
|
for _, u := range urls {
|
|
|
|
fmt.Printf(" %s -> %s\n", u, siteMap[u].Path)
|
2017-06-10 21:38:09 +02:00
|
|
|
}
|
2017-06-10 23:51:46 +02:00
|
|
|
case "render":
|
2017-06-12 00:11:52 +02:00
|
|
|
path := flag.Arg(1)
|
|
|
|
if path == "" {
|
|
|
|
path = "index.md"
|
|
|
|
}
|
|
|
|
if strings.HasPrefix(path, "/") {
|
|
|
|
if page, found := siteMap[path]; found {
|
|
|
|
path = page.Path
|
|
|
|
} else {
|
|
|
|
fmt.Println("No page at", path)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
page, e := readFile(path, siteData, true)
|
|
|
|
if e != nil {
|
|
|
|
fmt.Println(e)
|
2017-06-10 21:38:09 +02:00
|
|
|
break
|
|
|
|
}
|
2017-06-12 00:11:52 +02:00
|
|
|
printPathSetting("Render:", filepath.Join(siteConfig.SourceDir, path))
|
|
|
|
printSetting("URL:", page.Permalink)
|
2017-06-10 21:38:09 +02:00
|
|
|
fmt.Println(string(page.Body))
|
|
|
|
default:
|
|
|
|
fmt.Println("A subcommand is required.")
|
|
|
|
}
|
|
|
|
}
|