/goswf.go
https://bitbucket.org/jrick/goswf · Go · 157 lines · 155 code · 2 blank · 0 comment · 0 complexity · 6939281ca214b71531a567347061a2ac MD5 · raw file
- package main
- import (
- "bufio"
- "bytes"
- "flag"
- "fmt"
- "github.com/knieriem/markdown"
- "html/template"
- "io"
- "os"
- "path/filepath"
- "regexp"
- "strings"
- )
- var (
- titleFlag = flag.String(`title`, `goswf`, `site title`)
- subtitleFlag = flag.String(`subtitle`, ``, `site subtitle`)
- dirFlag = flag.String(`in`, ``, `markdown file tree`)
- ignoreRegExp = flag.String(`ignore`, ``, `ignore regexp`)
- tmplFlag = flag.String(`tmpl`, ``, `template file`)
- mdParser = markdown.NewParser(&markdown.Extensions{Smart: true})
- mdExtMatch = regexp.MustCompile(`\.md$`)
- tmplParser *template.Template
- )
- type Navigation struct {
- Name string
- Link string
- }
- type PageItems struct {
- Title string
- SubTitle string
- Favicon string
- CSS string
- NavItems []Navigation
- Main template.HTML
- }
- func main() {
- flag.Parse()
- fi, err := os.Stat(*dirFlag)
- if err != nil || !fi.IsDir() {
- fmt.Fprintln(os.Stderr, `Fatal: -in flag unset or not a directory`)
- flag.Usage()
- os.Exit(1)
- }
- if *tmplFlag == "" {
- fmt.Fprintln(os.Stderr, `Fatal: -tmpl flag unset or not a file`)
- flag.Usage()
- os.Exit(1)
- }
- abs, _ := filepath.Abs(*dirFlag)
- dir, inDir := filepath.Split(abs)
- outDir := filepath.Join(dir, inDir+`.static`)
- if err = os.Chdir(dir); err != nil {
- panic(err)
- }
- if fi, err = os.Stat(outDir); err == nil {
- if err = os.RemoveAll(outDir); err != nil {
- panic(err)
- }
- }
- tmplParser, err = template.ParseFiles(*tmplFlag)
- if err != nil {
- panic(err)
- }
- filepath.Walk(filepath.Base(*dirFlag), mkPage)
- os.Exit(0)
- }
- func mkPage(srcPath string, info os.FileInfo, err error) error {
- if skip, err := regexp.MatchString(*ignoreRegExp, filepath.Base(srcPath)); err != nil {
- panic(err)
- } else if skip {
- return filepath.SkipDir
- }
- src, err := os.Open(srcPath)
- if err != nil {
- panic(err)
- }
- var writer *bufio.Writer
- if destPath, dest := getDest(srcPath); dest != nil {
- writer = bufio.NewWriter(dest)
- // writer flushes before dest closes
- defer dest.Close()
- defer func() {
- if writer != nil && err == nil {
- err = writer.Flush()
- if err != nil {
- panic(err)
- }
- }
- }()
- } else { // dest will be null if we're iterating over a directory
- if err = os.Mkdir(destPath, 0755); err != nil {
- panic(err)
- }
- return nil
- }
- pageElems := PageItems{
- Title: *titleFlag,
- SubTitle: *subtitleFlag,
- CSS: `/style.css`,
- }
- /* XXX: index and navitems
- dir, err := os.Open(filepath.Dir(srcPath))
- if err != nil {
- return err
- } else {
- defer dir.Close()
- }
- names, err := dir.Readdirnames(0)
- if err != nil {
- fmt.Println(err)
- }
- */
- switch filepath.Ext(srcPath) {
- case `.md`:
- buf := new(bytes.Buffer)
- mdParser.Markdown(src, markdown.ToHTML(buf))
- pageElems.Main = template.HTML(buf.String())
- if err = tmplParser.Execute(writer, pageElems); err != nil {
- panic(err)
- }
- default:
- io.Copy(writer, src)
- }
- return nil
- }
- func getDest(path string) (destPath string, dest *os.File) {
- parts := strings.Split(path, string(filepath.Separator))
- parts[0] = parts[0] + `.static`
- last := &parts[len(parts)-1]
- *last = mdExtMatch.ReplaceAllLiteralString(*last, `.html`)
- destPath = strings.Join(parts, string(filepath.Separator))
- fi, err := os.Stat(path)
- if err == nil && !fi.IsDir() {
- if dest, err = os.Create(destPath); err != nil {
- panic(err)
- }
- }
- return destPath, dest
- }