diff options
author | Pacien | 2013-06-28 15:31:04 +0200 |
---|---|---|
committer | Pacien | 2013-06-28 15:31:04 +0200 |
commit | 6f2476510a5b31ada07a42c19065b47bbe784b7a (patch) | |
tree | 79e84f2bf2b8db8d964ad1c3383a6ad2161fa640 | |
parent | caeb79871d045ffeb2fba69c43b32b915a0a31c0 (diff) | |
download | foldaweb-6f2476510a5b31ada07a42c19065b47bbe784b7a.tar.gz |
First version
-rw-r--r-- | common.go | 134 | ||||
-rw-r--r-- | compiled.go | 46 | ||||
-rw-r--r-- | dynamic.go | 69 | ||||
-rw-r--r-- | files.go | 106 | ||||
-rw-r--r-- | interactive.go | 131 | ||||
-rw-r--r-- | main.go | 60 |
6 files changed, 546 insertions, 0 deletions
diff --git a/common.go b/common.go new file mode 100644 index 0000000..0cf2655 --- /dev/null +++ b/common.go | |||
@@ -0,0 +1,134 @@ | |||
1 | /* | ||
2 | |||
3 | This file is part of CompileTree (https://github.com/Pacien/CompileTree) | ||
4 | |||
5 | CompileTree 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 | CompileTree 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 CompileTree. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | */ | ||
19 | |||
20 | package main | ||
21 | |||
22 | import ( | ||
23 | "bytes" | ||
24 | "fmt" | ||
25 | "github.com/hoisie/mustache" | ||
26 | "github.com/russross/blackfriday" | ||
27 | "io/ioutil" | ||
28 | "path" | ||
29 | "strings" | ||
30 | "sync" | ||
31 | ) | ||
32 | |||
33 | var wait sync.WaitGroup | ||
34 | |||
35 | // Common templating | ||
36 | |||
37 | func isParsable(fileName string) bool { | ||
38 | switch path.Ext(fileName) { | ||
39 | case ".md", ".html", ".txt": | ||
40 | return true | ||
41 | } | ||
42 | return false | ||
43 | } | ||
44 | |||
45 | func read(fileName string) ([]byte, error) { | ||
46 | fileBody, err := ioutil.ReadFile(fileName) | ||
47 | if err != nil { | ||
48 | return nil, err | ||
49 | } | ||
50 | if path.Ext(fileName) == ".md" { | ||
51 | fileBody = blackfriday.MarkdownCommon(fileBody) | ||
52 | } | ||
53 | return fileBody, nil | ||
54 | } | ||
55 | |||
56 | func merge(files map[string][]byte) (merged []byte) { | ||
57 | merged = files["index"] | ||
58 | for pass := 0; bytes.Contains(merged, []byte("{{> ")) && pass < 4000; pass++ { | ||
59 | for fileName, fileBody := range files { | ||
60 | merged = bytes.Replace(merged, []byte("{{> "+fileName+"}}"), fileBody, -1) | ||
61 | } | ||
62 | } | ||
63 | return | ||
64 | } | ||
65 | |||
66 | // COMPILED and INTERACTIVE modes | ||
67 | |||
68 | // render and write everything inside | ||
69 | |||
70 | func parse(dirPath string, elements map[string][]byte, overwrite bool) map[string][]byte { | ||
71 | _, filesList := ls(dirPath) | ||
72 | for _, fileName := range filesList { | ||
73 | if isParsable(fileName) && (overwrite || elements[fileName[:len(fileName)-len(path.Ext(fileName))]] == nil) { | ||
74 | var err error | ||
75 | elements[fileName[:len(fileName)-len(path.Ext(fileName))]], err = read(path.Join(dirPath, fileName)) | ||
76 | if err != nil { | ||
77 | fmt.Println(err) | ||
78 | } | ||
79 | } | ||
80 | } | ||
81 | return elements | ||
82 | } | ||
83 | |||
84 | func compile(dirPath string, elements map[string][]byte, sourceDir, outputDir string, recursive bool) { | ||
85 | wait.Add(1) | ||
86 | defer wait.Done() | ||
87 | |||
88 | if strings.HasPrefix(dirPath, outputDir) { | ||
89 | return | ||
90 | } | ||
91 | |||
92 | elements = parse(dirPath, elements, true) | ||
93 | |||
94 | if recursive { | ||
95 | dirs, _ := ls(dirPath) | ||
96 | for _, dir := range dirs { | ||
97 | go compile(path.Join(dirPath, dir), elements, sourceDir, outputDir, recursive) | ||
98 | } | ||
99 | } | ||
100 | |||
101 | template := merge(elements) | ||
102 | page := mustache.Render(string(template), nil /* TODO: generate contextual variables */) | ||
103 | |||
104 | err := writeFile(path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), "index.html"), []byte(page)) | ||
105 | if err != nil { | ||
106 | fmt.Println(err) | ||
107 | return | ||
108 | } | ||
109 | } | ||
110 | |||
111 | func copyFiles(dirPath, sourceDir, outputDir string, recursive bool) { | ||
112 | wait.Add(1) | ||
113 | defer wait.Done() | ||
114 | |||
115 | if strings.HasPrefix(dirPath, outputDir) { | ||
116 | return | ||
117 | } | ||
118 | |||
119 | dirs, files := ls(dirPath) | ||
120 | for _, file := range files { | ||
121 | if !isParsable(file) { | ||
122 | err := cp(path.Join(dirPath, file), path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), file)) | ||
123 | if err != nil { | ||
124 | fmt.Println(err) | ||
125 | } | ||
126 | } | ||
127 | } | ||
128 | |||
129 | if recursive { | ||
130 | for _, dir := range dirs { | ||
131 | go copyFiles(path.Join(dirPath, dir), sourceDir, outputDir, recursive) | ||
132 | } | ||
133 | } | ||
134 | } | ||
diff --git a/compiled.go b/compiled.go new file mode 100644 index 0000000..5b2c19b --- /dev/null +++ b/compiled.go | |||
@@ -0,0 +1,46 @@ | |||
1 | /* | ||
2 | |||
3 | This file is part of CompileTree (https://github.com/Pacien/CompileTree) | ||
4 | |||
5 | CompileTree 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 | CompileTree 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 CompileTree. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | */ | ||
19 | |||
20 | package main | ||
21 | |||
22 | import ( | ||
23 | "fmt" | ||
24 | "os" | ||
25 | "time" | ||
26 | ) | ||
27 | |||
28 | func compiled(sourceDir, outputDir string) { | ||
29 | // remove previously compiled site | ||
30 | err := os.RemoveAll(outputDir) | ||
31 | if err != nil { | ||
32 | fmt.Println(err) | ||
33 | return | ||
34 | } | ||
35 | |||
36 | // compile everything | ||
37 | go compile(sourceDir, make(map[string][]byte), sourceDir, outputDir, true) | ||
38 | go copyFiles(sourceDir, sourceDir, outputDir, true) | ||
39 | |||
40 | // sleep some milliseconds to prevent early exit | ||
41 | time.Sleep(time.Millisecond * 100) | ||
42 | |||
43 | // wait until all tasks are completed | ||
44 | wait.Wait() | ||
45 | fmt.Println("Compilation done.") | ||
46 | } | ||
diff --git a/dynamic.go b/dynamic.go new file mode 100644 index 0000000..0307003 --- /dev/null +++ b/dynamic.go | |||
@@ -0,0 +1,69 @@ | |||
1 | /* | ||
2 | |||
3 | This file is part of CompileTree (https://github.com/Pacien/CompileTree) | ||
4 | |||
5 | CompileTree 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 | CompileTree 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 CompileTree. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | */ | ||
19 | |||
20 | package main | ||
21 | |||
22 | import ( | ||
23 | "fmt" | ||
24 | "github.com/hoisie/mustache" | ||
25 | "net/http" | ||
26 | "path" | ||
27 | "strings" | ||
28 | ) | ||
29 | |||
30 | func handle(w http.ResponseWriter, r *http.Request) { | ||
31 | // serve static files | ||
32 | if !(path.Ext(r.URL.Path) == "" || isParsable(path.Ext(r.URL.Path))) { | ||
33 | http.ServeFile(w, r, path.Join(*settings.sourceDir, r.URL.Path)) | ||
34 | return | ||
35 | } | ||
36 | |||
37 | // get the list of dirs to parse | ||
38 | request := strings.Trim(r.URL.Path, "/") | ||
39 | dirs := strings.Split(request, "/") | ||
40 | if request != "" { | ||
41 | dirs = append(dirs, "") | ||
42 | } | ||
43 | |||
44 | // parse these dirs | ||
45 | elements := make(map[string][]byte) | ||
46 | for _, dir := range dirs { | ||
47 | parse(path.Join(*settings.sourceDir, dir), elements, false) | ||
48 | } | ||
49 | |||
50 | // render the page | ||
51 | template := merge(elements) | ||
52 | page := mustache.Render(string(template), nil /* TODO: generate contextual variables */) | ||
53 | |||
54 | // serve the page | ||
55 | _, err := w.Write([]byte(page)) | ||
56 | if err != nil { | ||
57 | fmt.Println(err) | ||
58 | return | ||
59 | } | ||
60 | } | ||
61 | |||
62 | func dynamic(port string) { | ||
63 | fmt.Println("Listening on: localhost:" + port) | ||
64 | http.HandleFunc("/", handle) | ||
65 | err := http.ListenAndServe(":"+port, nil) | ||
66 | if err != nil { | ||
67 | fmt.Println(err) | ||
68 | } | ||
69 | } | ||
diff --git a/files.go b/files.go new file mode 100644 index 0000000..afdac86 --- /dev/null +++ b/files.go | |||
@@ -0,0 +1,106 @@ | |||
1 | /* | ||
2 | |||
3 | This file is part of CompileTree (https://github.com/Pacien/CompileTree) | ||
4 | |||
5 | CompileTree 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 | CompileTree 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 CompileTree. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | */ | ||
19 | |||
20 | package main | ||
21 | |||
22 | import ( | ||
23 | "io" | ||
24 | "io/ioutil" | ||
25 | "os" | ||
26 | "path" | ||
27 | "strings" | ||
28 | ) | ||
29 | |||
30 | // Filesystem utils | ||
31 | |||
32 | func isDir(dirPath string) bool { | ||
33 | stat, err := os.Stat(dirPath) | ||
34 | if err != nil { | ||
35 | return false | ||
36 | } | ||
37 | return stat.IsDir() | ||
38 | } | ||
39 | |||
40 | func isHidden(fileName string) bool { | ||
41 | return strings.HasPrefix(fileName, ".") | ||
42 | } | ||
43 | |||
44 | func ls(path string) (dirs []string, files []string) { | ||
45 | content, err := ioutil.ReadDir(path) | ||
46 | if err != nil { | ||
47 | return | ||
48 | } | ||
49 | for _, element := range content { | ||
50 | if isHidden(element.Name()) { | ||
51 | continue | ||
52 | } | ||
53 | if element.IsDir() { | ||
54 | dirs = append(dirs, element.Name()) | ||
55 | } else { | ||
56 | files = append(files, element.Name()) | ||
57 | } | ||
58 | } | ||
59 | return | ||
60 | } | ||
61 | |||
62 | func explore(dirPath string) (paths []string) { | ||
63 | dirs, _ := ls(dirPath) | ||
64 | for _, dir := range dirs { | ||
65 | sourceDir := path.Join(dirPath, dir) | ||
66 | paths = append(paths, sourceDir) | ||
67 | subDirs := explore(sourceDir) | ||
68 | for _, subDir := range subDirs { | ||
69 | paths = append(paths, subDir) | ||
70 | } | ||
71 | } | ||
72 | return | ||
73 | } | ||
74 | |||
75 | func cp(source, target string) error { | ||
76 | sourceFile, err := os.Open(source) | ||
77 | if err != nil { | ||
78 | return err | ||
79 | } | ||
80 | defer sourceFile.Close() | ||
81 | |||
82 | dir, _ := path.Split(target) | ||
83 | err = os.MkdirAll(dir, 0777) | ||
84 | if err != nil { | ||
85 | return err | ||
86 | } | ||
87 | |||
88 | targetFile, err := os.Create(target) | ||
89 | if err != nil { | ||
90 | return err | ||
91 | } | ||
92 | defer targetFile.Close() | ||
93 | |||
94 | _, err = io.Copy(targetFile, sourceFile) | ||
95 | return err | ||
96 | } | ||
97 | |||
98 | func writeFile(target string, body []byte) error { | ||
99 | dir, _ := path.Split(target) | ||
100 | err := os.MkdirAll(dir, 0777) | ||
101 | if err != nil { | ||
102 | return err | ||
103 | } | ||
104 | err = ioutil.WriteFile(target, body, 0777) | ||
105 | return err | ||
106 | } | ||
diff --git a/interactive.go b/interactive.go new file mode 100644 index 0000000..34c2f68 --- /dev/null +++ b/interactive.go | |||
@@ -0,0 +1,131 @@ | |||
1 | /* | ||
2 | |||
3 | This file is part of CompileTree (https://github.com/Pacien/CompileTree) | ||
4 | |||
5 | CompileTree 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 | CompileTree 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 CompileTree. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | */ | ||
19 | |||
20 | package main | ||
21 | |||
22 | import ( | ||
23 | "fmt" | ||
24 | "github.com/howeyc/fsnotify" | ||
25 | "os" | ||
26 | "path" | ||
27 | "strings" | ||
28 | "time" | ||
29 | ) | ||
30 | |||
31 | func watch(dirPath string, watcher *fsnotify.Watcher) *fsnotify.Watcher { | ||
32 | watcher.Watch(dirPath) | ||
33 | dirs := explore(dirPath) | ||
34 | for _, dir := range dirs { | ||
35 | if !strings.HasPrefix(dir, *settings.outputDir) { | ||
36 | err := watcher.Watch(dir) | ||
37 | if err != nil { | ||
38 | fmt.Println(err) | ||
39 | } | ||
40 | } | ||
41 | } | ||
42 | return watcher | ||
43 | } | ||
44 | |||
45 | func parseParents(dir, sourceDir string) map[string][]byte { | ||
46 | dirs := strings.Split(strings.TrimPrefix(dir, sourceDir), "/") | ||
47 | elements := make(map[string][]byte) | ||
48 | for _, dir := range dirs { | ||
49 | elements = parse(path.Join(sourceDir, dir), elements, false) | ||
50 | } | ||
51 | return elements | ||
52 | } | ||
53 | |||
54 | func interactive(sourceDir, outputDir string) { | ||
55 | |||
56 | // compile the whole site | ||
57 | compiled(sourceDir, outputDir) | ||
58 | |||
59 | // watch the source dir | ||
60 | watcher, err := fsnotify.NewWatcher() | ||
61 | if err != nil { | ||
62 | fmt.Println(err) | ||
63 | } | ||
64 | defer watcher.Close() | ||
65 | watcher = watch(sourceDir, watcher) | ||
66 | |||
67 | for { | ||
68 | select { | ||
69 | case ev := <-watcher.Event: | ||
70 | fmt.Println(ev) | ||
71 | |||
72 | // ignore hidden files | ||
73 | if isHidden(ev.Name) { | ||
74 | break | ||
75 | } | ||
76 | |||
77 | // manage watchers | ||
78 | if ev.IsDelete() || ev.IsRename() { | ||
79 | err = watcher.RemoveWatch(ev.Name) | ||
80 | if err != nil { | ||
81 | fmt.Println(err) | ||
82 | return | ||
83 | } | ||
84 | } else if ev.IsCreate() && isDir(ev.Name) { | ||
85 | watcher = watch(ev.Name, watcher) | ||
86 | } | ||
87 | |||
88 | dir, _ := path.Split(ev.Name) | ||
89 | |||
90 | // remove previously compiled files | ||
91 | if ev.IsDelete() || ev.IsRename() || ev.IsModify() { | ||
92 | var err error | ||
93 | if isDir(ev.Name) || !isParsable(ev.Name) { | ||
94 | err = os.RemoveAll(path.Join(outputDir, strings.TrimPrefix(ev.Name, sourceDir))) | ||
95 | } else { | ||
96 | err = os.RemoveAll(path.Join(outputDir, strings.TrimPrefix(dir, sourceDir))) | ||
97 | } | ||
98 | if err != nil { | ||
99 | fmt.Println(err) | ||
100 | return | ||
101 | } | ||
102 | } | ||
103 | |||
104 | // recompile changed files | ||
105 | if ev.IsCreate() || ev.IsModify() { | ||
106 | if isDir(ev.Name) { | ||
107 | elements := parseParents(ev.Name, sourceDir) | ||
108 | dirPath := path.Join(sourceDir, strings.TrimPrefix(ev.Name, sourceDir)) | ||
109 | go compile(dirPath, elements, sourceDir, outputDir, true) | ||
110 | go copyFiles(dirPath, sourceDir, outputDir, true) | ||
111 | } else { | ||
112 | dirPath := path.Join(sourceDir, strings.TrimPrefix(dir, sourceDir)) | ||
113 | if isParsable(path.Ext(ev.Name)) { | ||
114 | elements := parseParents(dir, sourceDir) | ||
115 | go compile(dirPath, elements, sourceDir, outputDir, true) | ||
116 | } | ||
117 | go copyFiles(dirPath, sourceDir, outputDir, false) | ||
118 | } | ||
119 | } | ||
120 | |||
121 | // sleep some milliseconds to prevent early exit | ||
122 | time.Sleep(time.Millisecond * 100) | ||
123 | |||
124 | // wait until all tasks are completed | ||
125 | wait.Wait() | ||
126 | |||
127 | case err := <-watcher.Error: | ||
128 | fmt.Println(err) | ||
129 | } | ||
130 | } | ||
131 | } | ||
@@ -0,0 +1,60 @@ | |||
1 | /* | ||
2 | |||
3 | This file is part of CompileTree (https://github.com/Pacien/CompileTree) | ||
4 | |||
5 | CompileTree 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 | CompileTree 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 CompileTree. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | */ | ||
19 | |||
20 | package main | ||
21 | |||
22 | import ( | ||
23 | "flag" | ||
24 | "fmt" | ||
25 | ) | ||
26 | |||
27 | var settings struct { | ||
28 | mode *string // compiled, interactive or dynamic | ||
29 | sourceDir *string | ||
30 | outputDir *string // for compiled site | ||
31 | port *string // for the integrated web server (dynamic mode only) | ||
32 | } | ||
33 | |||
34 | func init() { | ||
35 | // read settings | ||
36 | settings.mode = flag.String("mode", "compiled", "compiled|interactive|dynamic") | ||
37 | settings.sourceDir = flag.String("source", ".", "Path to sources directory.") | ||
38 | settings.outputDir = flag.String("output", "./out", "[compiled mode] Path to output directory.") | ||
39 | settings.port = flag.String("port", "8080", "[dynamic mode] Port to listen.") | ||
40 | flag.Parse() | ||
41 | } | ||
42 | |||
43 | func main() { | ||
44 | fmt.Println("CompileTree") | ||
45 | fmt.Println("Mode: " + *settings.mode) | ||
46 | fmt.Println("Source: " + *settings.sourceDir) | ||
47 | fmt.Println("Output: " + *settings.outputDir) | ||
48 | fmt.Println("====================") | ||
49 | |||
50 | switch *settings.mode { | ||
51 | case "compiled": | ||
52 | compiled(*settings.sourceDir, *settings.outputDir) | ||
53 | case "interactive": | ||
54 | interactive(*settings.sourceDir, *settings.outputDir) | ||
55 | case "dynamic": | ||
56 | dynamic(*settings.port) | ||
57 | default: | ||
58 | panic("Invalid mode.") | ||
59 | } | ||
60 | } | ||