diff options
Diffstat (limited to 'common.go')
-rw-r--r-- | common.go | 143 |
1 files changed, 0 insertions, 143 deletions
diff --git a/common.go b/common.go deleted file mode 100644 index 3203945..0000000 --- a/common.go +++ /dev/null | |||
@@ -1,143 +0,0 @@ | |||
1 | /* | ||
2 | |||
3 | This file is part of FoldaWeb <https://github.com/Pacien/FoldaWeb> | ||
4 | |||
5 | FoldaWeb is free software: you can redistribute it and/or modify | ||
6 | it under the terms of the GNU Affero General Public License as published by | ||
7 | the Free Software Foundation, either version 3 of the License, or | ||
8 | (at your option) any later version. | ||
9 | |||
10 | FoldaWeb is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
13 | GNU Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with FoldaWeb. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | */ | ||
19 | |||
20 | package main | ||
21 | |||
22 | import ( | ||
23 | "bytes" | ||
24 | "fmt" | ||
25 | "github.com/Pacien/fcmd" | ||
26 | "github.com/drbawb/mustache" | ||
27 | "github.com/russross/blackfriday" | ||
28 | "io/ioutil" | ||
29 | "path" | ||
30 | "strings" | ||
31 | "sync" | ||
32 | ) | ||
33 | |||
34 | var wait sync.WaitGroup | ||
35 | |||
36 | // Common templating | ||
37 | |||
38 | func isParsable(fileName string, exts []string) bool { | ||
39 | for _, ext := range exts { | ||
40 | if path.Ext(fileName) == ext { | ||
41 | return true | ||
42 | } | ||
43 | } | ||
44 | return false | ||
45 | } | ||
46 | |||
47 | func read(fileName string) ([]byte, error) { | ||
48 | fileBody, err := ioutil.ReadFile(fileName) | ||
49 | if err != nil { | ||
50 | return nil, err | ||
51 | } | ||
52 | if path.Ext(fileName) == ".md" { | ||
53 | fileBody = blackfriday.MarkdownCommon(fileBody) | ||
54 | } | ||
55 | return fileBody, nil | ||
56 | } | ||
57 | |||
58 | func merge(files map[string][]byte) (merged []byte) { | ||
59 | merged = files["index"] | ||
60 | for pass := 0; bytes.Contains(merged, []byte("{{> ")) && pass < 4000; pass++ { | ||
61 | for fileName, fileBody := range files { | ||
62 | merged = bytes.Replace(merged, []byte("{{> "+fileName+"}}"), fileBody, -1) | ||
63 | } | ||
64 | } | ||
65 | return | ||
66 | } | ||
67 | |||
68 | // COMPILED and INTERACTIVE modes | ||
69 | |||
70 | func parse(dirPath string, elements map[string][]byte, exts []string, overwrite bool) (map[string][]byte, bool) { | ||
71 | parsed := false | ||
72 | _, filesList := fcmd.Ls(dirPath) | ||
73 | for _, fileName := range filesList { | ||
74 | if isParsable(fileName, exts) && (overwrite || elements[fileName[:len(fileName)-len(path.Ext(fileName))]] == nil) { | ||
75 | var err error | ||
76 | elements[fileName[:len(fileName)-len(path.Ext(fileName))]], err = read(path.Join(dirPath, fileName)) | ||
77 | if err != nil { | ||
78 | fmt.Println(err) | ||
79 | } | ||
80 | parsed = true | ||
81 | } | ||
82 | } | ||
83 | return elements, parsed | ||
84 | } | ||
85 | |||
86 | func compile(dirPath string, elements map[string][]byte, sourceDir, outputDir, saveAs string, exts []string, recursive bool) { | ||
87 | defer wait.Done() | ||
88 | |||
89 | if strings.HasPrefix(dirPath, outputDir) { | ||
90 | return | ||
91 | } | ||
92 | |||
93 | parsed := false | ||
94 | elements, parsed = parse(dirPath, elements, exts, true) | ||
95 | |||
96 | if recursive { | ||
97 | dirs, _ := fcmd.Ls(dirPath) | ||
98 | for _, dir := range dirs { | ||
99 | wait.Add(1) | ||
100 | go compile(path.Join(dirPath, dir), elements, sourceDir, outputDir, saveAs, exts, recursive) | ||
101 | } | ||
102 | } | ||
103 | |||
104 | if !parsed { | ||
105 | return | ||
106 | } | ||
107 | |||
108 | pagePath := strings.TrimPrefix(dirPath, sourceDir) | ||
109 | |||
110 | template := merge(elements) | ||
111 | page := mustache.Render(string(template), makeContext(pagePath, sourceDir, exts)) | ||
112 | |||
113 | err := fcmd.WriteFile(path.Join(outputDir, pagePath, saveAs), []byte(page)) | ||
114 | if err != nil { | ||
115 | fmt.Println(err) | ||
116 | return | ||
117 | } | ||
118 | } | ||
119 | |||
120 | func copyFiles(dirPath, sourceDir, outputDir string, exts []string, recursive bool) { | ||
121 | defer wait.Done() | ||
122 | |||
123 | if strings.HasPrefix(dirPath, outputDir) { | ||
124 | return | ||
125 | } | ||
126 | |||
127 | dirs, files := fcmd.Ls(dirPath) | ||
128 | for _, file := range files { | ||
129 | if !isParsable(file, exts) { | ||
130 | err := fcmd.Cp(path.Join(dirPath, file), path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), file)) | ||
131 | if err != nil { | ||
132 | fmt.Println(err) | ||
133 | } | ||
134 | } | ||
135 | } | ||
136 | |||
137 | if recursive { | ||
138 | for _, dir := range dirs { | ||
139 | wait.Add(1) | ||
140 | go copyFiles(path.Join(dirPath, dir), sourceDir, outputDir, exts, recursive) | ||
141 | } | ||
142 | } | ||
143 | } | ||