diff options
Diffstat (limited to 'common.go')
-rw-r--r-- | common.go | 27 |
1 files changed, 14 insertions, 13 deletions
@@ -34,10 +34,11 @@ var wait sync.WaitGroup | |||
34 | 34 | ||
35 | // Common templating | 35 | // Common templating |
36 | 36 | ||
37 | func isParsable(fileName string) bool { | 37 | func 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 | ||
70 | func parse(dirPath string, elements map[string][]byte, overwrite bool) map[string][]byte { | 71 | func 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 | ||
84 | func compile(dirPath string, elements map[string][]byte, sourceDir, outputDir string, recursive bool) { | 85 | func 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 | ||
111 | func copyFiles(dirPath, sourceDir, outputDir string, recursive bool) { | 112 | func 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 | } |