diff options
-rw-r--r-- | common.go | 11 | ||||
-rw-r--r-- | context.go | 3 | ||||
-rw-r--r-- | files.go | 106 | ||||
-rw-r--r-- | interactive.go | 11 | ||||
-rw-r--r-- | main.go | 3 |
5 files changed, 17 insertions, 117 deletions
@@ -22,6 +22,7 @@ package main | |||
22 | import ( | 22 | import ( |
23 | "bytes" | 23 | "bytes" |
24 | "fmt" | 24 | "fmt" |
25 | "github.com/Pacien/fcmd" | ||
25 | "github.com/drbawb/mustache" | 26 | "github.com/drbawb/mustache" |
26 | "github.com/russross/blackfriday" | 27 | "github.com/russross/blackfriday" |
27 | "io/ioutil" | 28 | "io/ioutil" |
@@ -69,7 +70,7 @@ func merge(files map[string][]byte) (merged []byte) { | |||
69 | // render and write everything inside | 70 | // render and write everything inside |
70 | 71 | ||
71 | func parse(dirPath string, elements map[string][]byte, exts []string, overwrite bool) map[string][]byte { | 72 | func parse(dirPath string, elements map[string][]byte, exts []string, overwrite bool) map[string][]byte { |
72 | _, filesList := ls(dirPath) | 73 | _, filesList := fcmd.Ls(dirPath) |
73 | for _, fileName := range filesList { | 74 | for _, fileName := range filesList { |
74 | if isParsable(fileName, exts) && (overwrite || elements[fileName[:len(fileName)-len(path.Ext(fileName))]] == nil) { | 75 | if isParsable(fileName, exts) && (overwrite || elements[fileName[:len(fileName)-len(path.Ext(fileName))]] == nil) { |
75 | var err error | 76 | var err error |
@@ -93,7 +94,7 @@ func compile(dirPath string, elements map[string][]byte, sourceDir, outputDir, s | |||
93 | elements = parse(dirPath, elements, exts, true) | 94 | elements = parse(dirPath, elements, exts, true) |
94 | 95 | ||
95 | if recursive { | 96 | if recursive { |
96 | dirs, _ := ls(dirPath) | 97 | dirs, _ := fcmd.Ls(dirPath) |
97 | for _, dir := range dirs { | 98 | for _, dir := range dirs { |
98 | go compile(path.Join(dirPath, dir), elements, sourceDir, outputDir, saveAs, exts, recursive) | 99 | go compile(path.Join(dirPath, dir), elements, sourceDir, outputDir, saveAs, exts, recursive) |
99 | } | 100 | } |
@@ -102,7 +103,7 @@ func compile(dirPath string, elements map[string][]byte, sourceDir, outputDir, s | |||
102 | template := merge(elements) | 103 | template := merge(elements) |
103 | page := mustache.Render(string(template), makeContext(dirPath, sourceDir, outputDir, exts)) | 104 | page := mustache.Render(string(template), makeContext(dirPath, sourceDir, outputDir, exts)) |
104 | 105 | ||
105 | err := writeFile(path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), saveAs), []byte(page)) | 106 | err := fcmd.WriteFile(path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), saveAs), []byte(page)) |
106 | if err != nil { | 107 | if err != nil { |
107 | fmt.Println(err) | 108 | fmt.Println(err) |
108 | return | 109 | return |
@@ -117,10 +118,10 @@ func copyFiles(dirPath, sourceDir, outputDir string, exts []string, recursive bo | |||
117 | return | 118 | return |
118 | } | 119 | } |
119 | 120 | ||
120 | dirs, files := ls(dirPath) | 121 | dirs, files := fcmd.Ls(dirPath) |
121 | for _, file := range files { | 122 | for _, file := range files { |
122 | if !isParsable(file, exts) { | 123 | if !isParsable(file, exts) { |
123 | err := cp(path.Join(dirPath, file), path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), file)) | 124 | err := fcmd.Cp(path.Join(dirPath, file), path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), file)) |
124 | if err != nil { | 125 | if err != nil { |
125 | fmt.Println(err) | 126 | fmt.Println(err) |
126 | } | 127 | } |
@@ -20,6 +20,7 @@ | |||
20 | package main | 20 | package main |
21 | 21 | ||
22 | import ( | 22 | import ( |
23 | "github.com/Pacien/fcmd" | ||
23 | "path" | 24 | "path" |
24 | "strings" | 25 | "strings" |
25 | ) | 26 | ) |
@@ -48,7 +49,7 @@ func (c context) Title() string { | |||
48 | } | 49 | } |
49 | 50 | ||
50 | func (c context) SubPages() (subPages []page) { | 51 | func (c context) SubPages() (subPages []page) { |
51 | dirs, _ := ls(c.path) | 52 | dirs, _ := fcmd.Ls(c.path) |
52 | for _, dir := range dirs { | 53 | for _, dir := range dirs { |
53 | var page page | 54 | var page page |
54 | page.Title = dir | 55 | page.Title = dir |
diff --git a/files.go b/files.go deleted file mode 100644 index afdac86..0000000 --- a/files.go +++ /dev/null | |||
@@ -1,106 +0,0 @@ | |||
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 index 22f5933..1c9ca71 100644 --- a/interactive.go +++ b/interactive.go | |||
@@ -21,6 +21,7 @@ package main | |||
21 | 21 | ||
22 | import ( | 22 | import ( |
23 | "fmt" | 23 | "fmt" |
24 | "github.com/Pacien/fcmd" | ||
24 | "github.com/howeyc/fsnotify" | 25 | "github.com/howeyc/fsnotify" |
25 | "os" | 26 | "os" |
26 | "path" | 27 | "path" |
@@ -30,7 +31,7 @@ import ( | |||
30 | 31 | ||
31 | func watch(dirPath string, watcher *fsnotify.Watcher) *fsnotify.Watcher { | 32 | func watch(dirPath string, watcher *fsnotify.Watcher) *fsnotify.Watcher { |
32 | watcher.Watch(dirPath) | 33 | watcher.Watch(dirPath) |
33 | dirs := explore(dirPath) | 34 | dirs, _ := fcmd.Explore(dirPath) |
34 | for _, dir := range dirs { | 35 | for _, dir := range dirs { |
35 | if !strings.HasPrefix(dir, *settings.outputDir) { | 36 | if !strings.HasPrefix(dir, *settings.outputDir) { |
36 | err := watcher.Watch(dir) | 37 | err := watcher.Watch(dir) |
@@ -70,7 +71,7 @@ func interactive(sourceDir, outputDir string, exts []string, saveAs string) { | |||
70 | fmt.Println(ev) | 71 | fmt.Println(ev) |
71 | 72 | ||
72 | // ignore hidden files | 73 | // ignore hidden files |
73 | if isHidden(ev.Name) { | 74 | if fcmd.IsHidden(ev.Name) { |
74 | break | 75 | break |
75 | } | 76 | } |
76 | 77 | ||
@@ -81,7 +82,7 @@ func interactive(sourceDir, outputDir string, exts []string, saveAs string) { | |||
81 | fmt.Println(err) | 82 | fmt.Println(err) |
82 | return | 83 | return |
83 | } | 84 | } |
84 | } else if ev.IsCreate() && isDir(ev.Name) { | 85 | } else if ev.IsCreate() && fcmd.IsDir(ev.Name) { |
85 | watcher = watch(ev.Name, watcher) | 86 | watcher = watch(ev.Name, watcher) |
86 | } | 87 | } |
87 | 88 | ||
@@ -90,7 +91,7 @@ func interactive(sourceDir, outputDir string, exts []string, saveAs string) { | |||
90 | // remove previously compiled files | 91 | // remove previously compiled files |
91 | if ev.IsDelete() || ev.IsRename() || ev.IsModify() { | 92 | if ev.IsDelete() || ev.IsRename() || ev.IsModify() { |
92 | var err error | 93 | var err error |
93 | if isDir(ev.Name) || !isParsable(ev.Name, exts) { | 94 | if fcmd.IsDir(ev.Name) || !isParsable(ev.Name, exts) { |
94 | err = os.RemoveAll(path.Join(outputDir, strings.TrimPrefix(ev.Name, sourceDir))) | 95 | err = os.RemoveAll(path.Join(outputDir, strings.TrimPrefix(ev.Name, sourceDir))) |
95 | } else { | 96 | } else { |
96 | err = os.RemoveAll(path.Join(outputDir, strings.TrimPrefix(dir, sourceDir))) | 97 | err = os.RemoveAll(path.Join(outputDir, strings.TrimPrefix(dir, sourceDir))) |
@@ -103,7 +104,7 @@ func interactive(sourceDir, outputDir string, exts []string, saveAs string) { | |||
103 | 104 | ||
104 | // recompile changed files | 105 | // recompile changed files |
105 | if ev.IsCreate() || ev.IsModify() { | 106 | if ev.IsCreate() || ev.IsModify() { |