all repos — vite @ a37830ede37231e8ac2ed418007cf1a97914e461

a fast (this time, actually) and minimal static site generator

Init
Anirudh Oppiliappan x@icyphox.sh
Fri, 30 Oct 2020 10:35:41 +0530
commit

a37830ede37231e8ac2ed418007cf1a97914e461

10 files changed, 253 insertions(+), 0 deletions(-)

jump to
A .gitignore

@@ -0,0 +1,1 @@

+vite
A build.go

@@ -0,0 +1,98 @@

+package main + +import ( + "io/ioutil" + "os" + "path/filepath" + "strings" + "text/template" + "github.com/cross-cpm/go-shutil" +) + +func processTemplate(tmplPath string) *template.Template { + tmplFile := filepath.Join("templates", tmplPath) + tmpl, err := template.ParseFiles(tmplFile) + if err != nil { + printErr(err) + } + + return tmpl +} + +func handleMd(mdPath string) { + content, err := ioutil.ReadFile(mdPath) + if err != nil { + printErr(err) + } + + restContent, fm := parseFrontmatter(content) + bodyHtml := mdRender(restContent) + relPath, _ := filepath.Rel("pages/", mdPath) + + var buildPath string + if strings.HasSuffix(relPath, "_index.md") { + dir, _ := filepath.Split(relPath) + buildPath = filepath.Join("build", dir) + } else { + buildPath = filepath.Join( + "build", + strings.TrimSuffix(relPath, filepath.Ext(relPath)), + ) + } + + os.MkdirAll(buildPath, 0755) + fm.Body = string(bodyHtml) + + htmlFile, err := os.Create(filepath.Join(buildPath, "index.html")) + if err != nil { + printErr(err) + return + } + if fm.Template == "" { + fm.Template = "text.html" + } + tmpl := processTemplate(fm.Template) + err = tmpl.Execute(htmlFile, fm) + if err != nil { + printErr(err) + return + } + htmlFile.Close() +} + +func viteBuild() { + err := filepath.Walk("./pages", func(path string, info os.FileInfo, err error) error { + if err != nil { + printErr(err) + return err + } + if filepath.Ext(path) == ".md" { + handleMd(path) + } else { + f, err := os.Stat(path) + if err != nil { + printErr(err) + } + mode := f.Mode() + if mode.IsRegular() { + options := shutil.CopyOptions{} + relPath, _ := filepath.Rel("pages/", path) + options.FollowSymlinks = true + shutil.CopyFile( + path, + filepath.Join("build", relPath), + &options, + ) + } + } + return nil + }) + if err != nil { + printErr(err) + } + + _, err = shutil.CopyTree("static", filepath.Join("build", "static"), nil) + if err != nil { + printErr(err) + } +}
A frontmatter.go

@@ -0,0 +1,28 @@

+package main + +import ( + "bytes" + "github.com/adrg/frontmatter" +) + +type matter struct { + Template string `yaml:"template"` + URL string `yaml:"url"` + Title string `yaml:"title"` + Subtitle string `yaml:"subtitle"` + Date string `yaml:"date"` + Body string +} + +// Parses frontmatter, populates the `matter` struct and +// returns the rest +func parseFrontmatter(inputBytes []byte) ([]byte, matter) { + m := matter{} + input := bytes.NewReader(inputBytes) + rest, err := frontmatter.Parse(input, &m) + + if err != nil { + printErr(err) + } + return rest, m +}
A go.mod

@@ -0,0 +1,11 @@

+module github.com/icyphox/go-vite + +go 1.13 + +require ( + github.com/adrg/frontmatter v0.1.0 + github.com/cross-cpm/go-shutil v0.0.0-20190908093542-3fcbb1a2151e + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/russross/blackfriday/v2 v2.0.1 + github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect +)
A go.sum

@@ -0,0 +1,16 @@

+github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/adrg/frontmatter v0.1.0 h1:8gYVjU1CRsoc6in25uKQVpcQpR9cYz2BD0NQk6JepyU= +github.com/adrg/frontmatter v0.1.0/go.mod h1:93rQCj3z3ZlwyxxpQioRKC1wDLto4aXHrbqIsnH9wmE= +github.com/cross-cpm/go-shutil v0.0.0-20190908093542-3fcbb1a2151e h1:x8RmdI3kIwKOGLb/SaraDgArPZ0Kb+0WmkM0K2P1CZk= +github.com/cross-cpm/go-shutil v0.0.0-20190908093542-3fcbb1a2151e/go.mod h1:VPohcIQ6FLWAgzOT7enwtRTAvwfQBDD1v4c7mvY2s2c= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q= +github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= +github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo= +github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
A init.go

@@ -0,0 +1,21 @@

+package main + +import ( + "os" + "path/filepath" +) + +func viteInit(path string) { + paths := []string{"build", "pages", "static", "templates"} + var dirPath string + for _, p := range paths { + dirPath = filepath.Join(path, p) + err := os.MkdirAll(dirPath, 0755) + if err != nil { + printErr(err) + return + } + } + fp, _ := filepath.Abs(path) + printMsg("created project:", fp) +}
A main.go

@@ -0,0 +1,37 @@

+package main + +import ( + "fmt" + "os" +) + +func main() { + args := os.Args + + helpStr := `usage: vite [options] + +A simple and minimal static site generator. + +options: + init PATH create vite project at PATH + build builds the current project + new PATH create a new markdown post + ` + + if len(args) <= 2 { + fmt.Println(helpStr) + return + } + + switch args[1] { + case "init": + initPath := args[2] + viteInit(initPath) + case "build": + viteBuild() + case "new": + // newPath := args[2] + // viteNew(newPath) + } + +}
A makefile

@@ -0,0 +1,8 @@

+default: + go build -o vite + +install: + install -Dm755 vite $(DESTDIR)$(PREFIX)/bin/vite + +uninstall: + @rm -f $(DESTDIR)$(PREFIX)/bin/vite
A markdown.go

@@ -0,0 +1,18 @@

+package main + +import ( + bf "github.com/russross/blackfriday/v2" +) + +var bfFlags = bf.UseXHTML | bf.Smartypants | bf.SmartypantsFractions | + bf.SmartypantsDashes | bf.SmartypantsAngledQuotes | bf.NofollowLinks | + bf.FootnoteReturnLinks + +func mdRender(input []byte) []byte { + return bf.Run( + input, + bf.WithRenderer(bf.NewHTMLRenderer(bf.HTMLRendererParameters{ + Flags: bfFlags, + })), + ) +}
A utils.go

@@ -0,0 +1,15 @@

+package main + +import ( + "fmt" + "os" + "strings" +) + +func printMsg(s ...string) { + fmt.Println("vite:", strings.Join(s, " ")) +} + +func printErr(e error) { + fmt.Fprintln(os.Stderr, "error:", e) +}