aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--common.go27
-rw-r--r--compiled.go6
-rw-r--r--dynamic.go4
-rw-r--r--interactive.go24
-rw-r--r--main.go13
5 files changed, 42 insertions, 32 deletions
diff --git a/common.go b/common.go
index 0cf2655..460f74d 100644
--- a/common.go
+++ b/common.go
@@ -34,10 +34,11 @@ var wait sync.WaitGroup
34 34
35// Common templating 35// Common templating
36 36
37func isParsable(fileName string) bool { 37func isParsable(fileName string, exts []string) bool {
38 switch path.Ext(fileName) { 38 for _, ext := range exts {
39 case ".md", ".html", ".txt": 39 if path.Ext(fileName) == ext {
40 return true 40 return true
41 }
41 } 42 }
42 return false 43 return false
43} 44}
@@ -67,10 +68,10 @@ func merge(files map[string][]byte) (merged []byte) {
67 68
68// render and write everything inside 69// render and write everything inside
69 70
70func parse(dirPath string, elements map[string][]byte, overwrite bool) map[string][]byte { 71func parse(dirPath string, elements map[string][]byte, exts []string, overwrite bool) map[string][]byte {
71 _, filesList := ls(dirPath) 72 _, filesList := ls(dirPath)
72 for _, fileName := range filesList { 73 for _, fileName := range filesList {
73 if isParsable(fileName) && (overwrite || elements[fileName[:len(fileName)-len(path.Ext(fileName))]] == nil) { 74 if isParsable(fileName, exts) && (overwrite || elements[fileName[:len(fileName)-len(path.Ext(fileName))]] == nil) {
74 var err error 75 var err error
75 elements[fileName[:len(fileName)-len(path.Ext(fileName))]], err = read(path.Join(dirPath, fileName)) 76 elements[fileName[:len(fileName)-len(path.Ext(fileName))]], err = read(path.Join(dirPath, fileName))
76 if err != nil { 77 if err != nil {
@@ -81,7 +82,7 @@ func parse(dirPath string, elements map[string][]byte, overwrite bool) map[strin
81 return elements 82 return elements
82} 83}
83 84
84func compile(dirPath string, elements map[string][]byte, sourceDir, outputDir string, recursive bool) { 85func compile(dirPath string, elements map[string][]byte, sourceDir, outputDir, saveAs string, exts []string, recursive bool) {
85 wait.Add(1) 86 wait.Add(1)
86 defer wait.Done() 87 defer wait.Done()
87 88
@@ -89,26 +90,26 @@ func compile(dirPath string, elements map[string][]byte, sourceDir, outputDir st
89 return 90 return
90 } 91 }
91 92
92 elements = parse(dirPath, elements, true) 93 elements = parse(dirPath, elements, exts, true)
93 94
94 if recursive { 95 if recursive {
95 dirs, _ := ls(dirPath) 96 dirs, _ := ls(dirPath)
96 for _, dir := range dirs { 97 for _, dir := range dirs {
97 go compile(path.Join(dirPath, dir), elements, sourceDir, outputDir, recursive) 98 go compile(path.Join(dirPath, dir), elements, sourceDir, outputDir, saveAs, exts, recursive)
98 } 99 }
99 } 100 }
100 101
101 template := merge(elements) 102 template := merge(elements)
102 page := mustache.Render(string(template), nil /* TODO: generate contextual variables */) 103 page := mustache.Render(string(template), nil /* TODO: generate contextual variables */)
103 104
104 err := writeFile(path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), "index.html"), []byte(page)) 105 err := writeFile(path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), saveAs), []byte(page))
105 if err != nil { 106 if err != nil {
106 fmt.Println(err) 107 fmt.Println(err)
107 return 108 return
108 } 109 }
109} 110}
110 111
111func copyFiles(dirPath, sourceDir, outputDir string, recursive bool) { 112func copyFiles(dirPath, sourceDir, outputDir string, exts []string, recursive bool) {
112 wait.Add(1) 113 wait.Add(1)
113 defer wait.Done() 114 defer wait.Done()
114 115
@@ -118,7 +119,7 @@ func copyFiles(dirPath, sourceDir, outputDir string, recursive bool) {
118 119
119 dirs, files := ls(dirPath) 120 dirs, files := ls(dirPath)
120 for _, file := range files { 121 for _, file := range files {
121 if !isParsable(file) { 122 if !isParsable(file, exts) {
122 err := cp(path.Join(dirPath, file), path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), file)) 123 err := cp(path.Join(dirPath, file), path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), file))
123 if err != nil { 124 if err != nil {
124 fmt.Println(err) 125 fmt.Println(err)
@@ -128,7 +129,7 @@ func copyFiles(dirPath, sourceDir, outputDir string, recursive bool) {
128 129
129 if recursive { 130 if recursive {
130 for _, dir := range dirs { 131 for _, dir := range dirs {
131 go copyFiles(path.Join(dirPath, dir), sourceDir, outputDir, recursive) 132 go copyFiles(path.Join(dirPath, dir), sourceDir, outputDir, exts, recursive)
132 } 133 }
133 } 134 }
134} 135}
diff --git a/compiled.go b/compiled.go
index 5b2c19b..1cd391e 100644
--- a/compiled.go
+++ b/compiled.go
@@ -25,7 +25,7 @@ import (
25 "time" 25 "time"
26) 26)
27 27
28func compiled(sourceDir, outputDir string) { 28func compiled(sourceDir, outputDir string, exts []string, saveAs string) {
29 // remove previously compiled site 29 // remove previously compiled site
30 err := os.RemoveAll(outputDir) 30 err := os.RemoveAll(outputDir)
31 if err != nil { 31 if err != nil {
@@ -34,8 +34,8 @@ func compiled(sourceDir, outputDir string) {
34 } 34 }
35 35
36 // compile everything 36 // compile everything
37 go compile(sourceDir, make(map[string][]byte), sourceDir, outputDir, true) 37 go compile(sourceDir, make(map[string][]byte), sourceDir, outputDir, saveAs, exts, true)
38 go copyFiles(sourceDir, sourceDir, outputDir, true) 38 go copyFiles(sourceDir, sourceDir, outputDir, exts, true)
39 39
40 // sleep some milliseconds to prevent early exit 40 // sleep some milliseconds to prevent early exit
41 time.Sleep(time.Millisecond * 100) 41 time.Sleep(time.Millisecond * 100)
diff --git a/dynamic.go b/dynamic.go
index 0307003..b6388bc 100644
--- a/dynamic.go
+++ b/dynamic.go
@@ -29,7 +29,7 @@ import (
29 29
30func handle(w http.ResponseWriter, r *http.Request) { 30func handle(w http.ResponseWriter, r *http.Request) {
31 // serve static files 31 // serve static files
32 if !(path.Ext(r.URL.Path) == "" || isParsable(path.Ext(r.URL.Path))) { 32 if !(path.Ext(r.URL.Path) == "" || isParsable(path.Ext(r.URL.Path), settings.exts)) {
33 http.ServeFile(w, r, path.Join(*settings.sourceDir, r.URL.Path)) 33 http.ServeFile(w, r, path.Join(*settings.sourceDir, r.URL.Path))
34 return 34 return
35 } 35 }
@@ -44,7 +44,7 @@ func handle(w http.ResponseWriter, r *http.Request) {
44 // parse these dirs 44 // parse these dirs
45 elements := make(map[string][]byte) 45 elements := make(map[string][]byte)
46 for _, dir := range dirs { 46 for _, dir := range dirs {
47 parse(path.Join(*settings.sourceDir, dir), elements, false) 47 parse(path.Join(*settings.sourceDir, dir), elements, settings.exts, false)
48 } 48 }
49 49
50 // render the page 50 // render the page
diff --git a/interactive.go b/interactive.go
index 34c2f68..22f5933 100644
--- a/interactive.go
+++ b/interactive.go
@@ -42,19 +42,19 @@ func watch(dirPath string, watcher *fsnotify.Watcher) *fsnotify.Watcher {
42 return watcher 42 return watcher
43} 43}
44 44
45func parseParents(dir, sourceDir string) map[string][]byte { 45func parseParents(dir, sourceDir string, exts []string) map[string][]byte {
46 dirs := strings.Split(strings.TrimPrefix(dir, sourceDir), "/") 46 dirs := strings.Split(strings.TrimPrefix(dir, sourceDir), "/")
47 elements := make(map[string][]byte) 47 elements := make(map[string][]byte)
48 for _, dir := range dirs { 48 for _, dir := range dirs {
49 elements = parse(path.Join(sourceDir, dir), elements, false) 49 elements = parse(path.Join(sourceDir, dir), elements, exts, false)
50 } 50 }
51 return elements 51 return elements
52} 52}
53 53
54func interactive(sourceDir, outputDir string) { 54func interactive(sourceDir, outputDir string, exts []string, saveAs string) {
55 55
56 // compile the whole site 56 // compile the whole site
57 compiled(sourceDir, outputDir) 57 compiled(sourceDir, outputDir, exts, saveAs)
58 58
59 // watch the source dir 59 // watch the source dir
60 watcher, err := fsnotify.NewWatcher() 60 watcher, err := fsnotify.NewWatcher()
@@ -90,7 +90,7 @@ func interactive(sourceDir, outputDir string) {
90 // remove previously compiled files 90 // remove previously compiled files
91 if ev.IsDelete() || ev.IsRename() || ev.IsModify() { 91 if ev.IsDelete() || ev.IsRename() || ev.IsModify() {
92 var err error 92 var err error
93 if isDir(ev.Name) || !isParsable(ev.Name) { 93 if isDir(ev.Name) || !isParsable(ev.Name, exts) {
94 err = os.RemoveAll(path.Join(outputDir, strings.TrimPrefix(ev.Name, sourceDir))) 94 err = os.RemoveAll(path.Join(outputDir, strings.TrimPrefix(ev.Name, sourceDir)))
95 } else { 95 } else {
96 err = os.RemoveAll(path.Join(outputDir, strings.TrimPrefix(dir, sourceDir))) 96 err = os.RemoveAll(path.Join(outputDir, strings.TrimPrefix(dir, sourceDir)))
@@ -104,17 +104,17 @@ func interactive(sourceDir, outputDir string) {
104 // recompile changed files 104 // recompile changed files
105 if ev.IsCreate() || ev.IsModify() { 105 if ev.IsCreate() || ev.IsModify() {
106 if isDir(ev.Name) { 106 if isDir(ev.Name) {
107 elements := parseParents(ev.Name, sourceDir) 107 elements := parseParents(ev.Name, sourceDir, exts)
108 dirPath := path.Join(sourceDir, strings.TrimPrefix(ev.Name, sourceDir)) 108 dirPath := path.Join(sourceDir, strings.TrimPrefix(ev.Name, sourceDir))
109 go compile(dirPath, elements, sourceDir, outputDir, true) 109 go compile(dirPath, elements, sourceDir, outputDir, saveAs, exts, true)
110 go copyFiles(dirPath, sourceDir, outputDir, true) 110 go copyFiles(dirPath, sourceDir, outputDir, exts, true)
111 } else { 111 } else {
112 dirPath := path.Join(sourceDir, strings.TrimPrefix(dir, sourceDir)) 112 dirPath := path.Join(sourceDir, strings.TrimPrefix(dir, sourceDir))
113 if isParsable(path.Ext(ev.Name)) { 113 if isParsable(path.Ext(ev.Name), exts) {
114 elements := parseParents(dir, sourceDir) 114 elements := parseParents(dir, sourceDir, exts)
115 go compile(dirPath, elements, sourceDir, outputDir, true) 115 go compile(dirPath, elements, sourceDir, outputDir, saveAs, exts, true)
116 } 116 }
117 go copyFiles(dirPath, sourceDir, outputDir, false) 117 go copyFiles(dirPath, sourceDir, outputDir, exts, false)
118 } 118 }
119 } 119 }
120 120
diff --git a/main.go b/main.go
index 49b27fa..2c0cf81 100644
--- a/main.go
+++ b/main.go
@@ -22,6 +22,7 @@ package main
22import ( 22import (
23 "flag" 23 "flag"
24 "fmt" 24 "fmt"
25 "strings"
25) 26)
26 27
27var settings struct { 28var settings struct {
@@ -29,6 +30,8 @@ var settings struct {
29 sourceDir *string 30 sourceDir *string
30 outputDir *string // for compiled site 31 outputDir *string // for compiled site
31 port *string // for the integrated web server (dynamic mode only) 32 port *string // for the integrated web server (dynamic mode only)
33 exts []string
34 saveAs *string