tangled
alpha
login
or
join now
anirudh.fi
/
vite
star
6
fork
atom
Configure Feed
Issues
Pull Requests
Commits
Tags
Feed URL
Select the types of activity you want to include in your feed.
fast and minimal static site generator
ssg
star
6
fork
atom
Configure Feed
Issues
Pull Requests
Commits
Tags
Feed URL
Select the types of activity you want to include in your feed.
overview
issues
1
pulls
pipelines
Init
anirudh.fi
5 years ago
a37830ed
+253
10 changed files
expand all
collapse all
unified
split
.gitignore
build.go
frontmatter.go
go.mod
go.sum
init.go
main.go
makefile
markdown.go
utils.go
+1
.gitignore
reviewed
···
1
1
+
vite
+98
build.go
reviewed
···
1
1
+
package main
2
2
+
3
3
+
import (
4
4
+
"io/ioutil"
5
5
+
"os"
6
6
+
"path/filepath"
7
7
+
"strings"
8
8
+
"text/template"
9
9
+
"github.com/cross-cpm/go-shutil"
10
10
+
)
11
11
+
12
12
+
func processTemplate(tmplPath string) *template.Template {
13
13
+
tmplFile := filepath.Join("templates", tmplPath)
14
14
+
tmpl, err := template.ParseFiles(tmplFile)
15
15
+
if err != nil {
16
16
+
printErr(err)
17
17
+
}
18
18
+
19
19
+
return tmpl
20
20
+
}
21
21
+
22
22
+
func handleMd(mdPath string) {
23
23
+
content, err := ioutil.ReadFile(mdPath)
24
24
+
if err != nil {
25
25
+
printErr(err)
26
26
+
}
27
27
+
28
28
+
restContent, fm := parseFrontmatter(content)
29
29
+
bodyHtml := mdRender(restContent)
30
30
+
relPath, _ := filepath.Rel("pages/", mdPath)
31
31
+
32
32
+
var buildPath string
33
33
+
if strings.HasSuffix(relPath, "_index.md") {
34
34
+
dir, _ := filepath.Split(relPath)
35
35
+
buildPath = filepath.Join("build", dir)
36
36
+
} else {
37
37
+
buildPath = filepath.Join(
38
38
+
"build",
39
39
+
strings.TrimSuffix(relPath, filepath.Ext(relPath)),
40
40
+
)
41
41
+
}
42
42
+
43
43
+
os.MkdirAll(buildPath, 0755)
44
44
+
fm.Body = string(bodyHtml)
45
45
+
46
46
+
htmlFile, err := os.Create(filepath.Join(buildPath, "index.html"))
47
47
+
if err != nil {
48
48
+
printErr(err)
49
49
+
return
50
50
+
}
51
51
+
if fm.Template == "" {
52
52
+
fm.Template = "text.html"
53
53
+
}
54
54
+
tmpl := processTemplate(fm.Template)
55
55
+
err = tmpl.Execute(htmlFile, fm)
56
56
+
if err != nil {
57
57
+
printErr(err)
58
58
+
return
59
59
+
}
60
60
+
htmlFile.Close()
61
61
+
}
62
62
+
63
63
+
func viteBuild() {
64
64
+
err := filepath.Walk("./pages", func(path string, info os.FileInfo, err error) error {
65
65
+
if err != nil {
66
66
+
printErr(err)
67
67
+
return err
68
68
+
}
69
69
+
if filepath.Ext(path) == ".md" {
70
70
+
handleMd(path)
71
71
+
} else {
72
72
+
f, err := os.Stat(path)
73
73
+
if err != nil {
74
74
+
printErr(err)
75
75
+
}
76
76
+
mode := f.Mode()
77
77
+
if mode.IsRegular() {
78
78
+
options := shutil.CopyOptions{}
79
79
+
relPath, _ := filepath.Rel("pages/", path)
80
80
+
options.FollowSymlinks = true
81
81
+
shutil.CopyFile(
82
82
+
path,
83
83
+
filepath.Join("build", relPath),
84
84
+
&options,
85
85
+
)
86
86
+
}
87
87
+
}
88
88
+
return nil
89
89
+
})
90
90
+
if err != nil {
91
91
+
printErr(err)
92
92
+
}
93
93
+
94
94
+
_, err = shutil.CopyTree("static", filepath.Join("build", "static"), nil)
95
95
+
if err != nil {
96
96
+
printErr(err)
97
97
+
}
98
98
+
}
+28
frontmatter.go
reviewed
···
1
1
+
package main
2
2
+
3
3
+
import (
4
4
+
"bytes"
5
5
+
"github.com/adrg/frontmatter"
6
6
+
)
7
7
+
8
8
+
type matter struct {
9
9
+
Template string `yaml:"template"`
10
10
+
URL string `yaml:"url"`
11
11
+
Title string `yaml:"title"`
12
12
+
Subtitle string `yaml:"subtitle"`
13
13
+
Date string `yaml:"date"`
14
14
+
Body string
15
15
+
}
16
16
+
17
17
+
// Parses frontmatter, populates the `matter` struct and
18
18
+
// returns the rest
19
19
+
func parseFrontmatter(inputBytes []byte) ([]byte, matter) {
20
20
+
m := matter{}
21
21
+
input := bytes.NewReader(inputBytes)
22
22
+
rest, err := frontmatter.Parse(input, &m)
23
23
+
24
24
+
if err != nil {
25
25
+
printErr(err)
26
26
+
}
27
27
+
return rest, m
28
28
+
}
+11
go.mod
reviewed
···
1
1
+
module github.com/icyphox/go-vite
2
2
+
3
3
+
go 1.13
4
4
+
5
5
+
require (
6
6
+
github.com/adrg/frontmatter v0.1.0
7
7
+
github.com/cross-cpm/go-shutil v0.0.0-20190908093542-3fcbb1a2151e
8
8
+
github.com/pmezard/go-difflib v1.0.0 // indirect
9
9
+
github.com/russross/blackfriday/v2 v2.0.1
10
10
+
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
11
11
+
)
+16
go.sum
reviewed
···
1
1
+
github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ=
2
2
+
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
3
3
+
github.com/adrg/frontmatter v0.1.0 h1:8gYVjU1CRsoc6in25uKQVpcQpR9cYz2BD0NQk6JepyU=
4
4
+
github.com/adrg/frontmatter v0.1.0/go.mod h1:93rQCj3z3ZlwyxxpQioRKC1wDLto4aXHrbqIsnH9wmE=
5
5
+
github.com/cross-cpm/go-shutil v0.0.0-20190908093542-3fcbb1a2151e h1:x8RmdI3kIwKOGLb/SaraDgArPZ0Kb+0WmkM0K2P1CZk=
6
6
+
github.com/cross-cpm/go-shutil v0.0.0-20190908093542-3fcbb1a2151e/go.mod h1:VPohcIQ6FLWAgzOT7enwtRTAvwfQBDD1v4c7mvY2s2c=
7
7
+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
8
8
+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
9
9
+
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
10
10
+
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
11
11
+
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
12
12
+
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
13
13
+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
14
14
+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
15
15
+
gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU=
16
16
+
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
+21
init.go
reviewed
···
1
1
+
package main
2
2
+
3
3
+
import (
4
4
+
"os"
5
5
+
"path/filepath"
6
6
+
)
7
7
+
8
8
+
func viteInit(path string) {
9
9
+
paths := []string{"build", "pages", "static", "templates"}
10
10
+
var dirPath string
11
11
+
for _, p := range paths {
12
12
+
dirPath = filepath.Join(path, p)
13
13
+
err := os.MkdirAll(dirPath, 0755)
14
14
+
if err != nil {
15
15
+
printErr(err)
16
16
+
return
17
17
+
}
18
18
+
}
19
19
+
fp, _ := filepath.Abs(path)
20
20
+
printMsg("created project:", fp)
21
21
+
}
+37
main.go
reviewed
···
1
1
+
package main
2
2
+
3
3
+
import (
4
4
+
"fmt"
5
5
+
"os"
6
6
+
)
7
7
+
8
8
+
func main() {
9
9
+
args := os.Args
10
10
+
11
11
+
helpStr := `usage: vite [options]
12
12
+
13
13
+
A simple and minimal static site generator.
14
14
+
15
15
+
options:
16
16
+
init PATH create vite project at PATH
17
17
+
build builds the current project
18
18
+
new PATH create a new markdown post
19
19
+
`
20
20
+
21
21
+
if len(args) <= 2 {
22
22
+
fmt.Println(helpStr)
23
23
+
return
24
24
+
}
25
25
+
26
26
+
switch args[1] {
27
27
+
case "init":
28
28
+
initPath := args[2]
29
29
+
viteInit(initPath)
30
30
+
case "build":
31
31
+
viteBuild()
32
32
+
case "new":
33
33
+
// newPath := args[2]
34
34
+
// viteNew(newPath)
35
35
+
}
36
36
+
37
37
+
}
+8
makefile
reviewed
···
1
1
+
default:
2
2
+
go build -o vite
3
3
+
4
4
+
install:
5
5
+
install -Dm755 vite $(DESTDIR)$(PREFIX)/bin/vite
6
6
+
7
7
+
uninstall:
8
8
+
@rm -f $(DESTDIR)$(PREFIX)/bin/vite
+18
markdown.go
reviewed
···
1
1
+
package main
2
2
+
3
3
+
import (
4
4
+
bf "github.com/russross/blackfriday/v2"
5
5
+
)
6
6
+
7
7
+
var bfFlags = bf.UseXHTML | bf.Smartypants | bf.SmartypantsFractions |
8
8
+
bf.SmartypantsDashes | bf.SmartypantsAngledQuotes | bf.NofollowLinks |
9
9
+
bf.FootnoteReturnLinks
10
10
+
11
11
+
func mdRender(input []byte) []byte {
12
12
+
return bf.Run(
13
13
+
input,
14
14
+
bf.WithRenderer(bf.NewHTMLRenderer(bf.HTMLRendererParameters{
15
15
+
Flags: bfFlags,
16
16
+
})),
17
17
+
)
18
18
+
}
+15
utils.go
reviewed
···
1
1
+
package main
2
2
+
3
3
+
import (
4
4
+
"fmt"
5
5
+
"os"
6
6
+
"strings"
7
7
+
)
8
8
+
9
9
+
func printMsg(s ...string) {
10
10
+
fmt.Println("vite:", strings.Join(s, " "))
11
11
+
}
12
12
+
13
13
+
func printErr(e error) {
14
14
+
fmt.Fprintln(os.Stderr, "error:", e)
15
15
+
}