From 72b255dd4b0b182e3529a3cead0015e73be81680 Mon Sep 17 00:00:00 2001 From: Pacien Date: Sat, 29 Jun 2013 13:03:56 +0200 Subject: Add custom parameters: parsable extensions, save as --- common.go | 27 ++++++++++++++------------- compiled.go | 6 +++--- dynamic.go | 4 ++-- interactive.go | 24 ++++++++++++------------ main.go | 13 +++++++++++-- 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 // Common templating -func isParsable(fileName string) bool { - switch path.Ext(fileName) { - case ".md", ".html", ".txt": - return true +func isParsable(fileName string, exts []string) bool { + for _, ext := range exts { + if path.Ext(fileName) == ext { + return true + } } return false } @@ -67,10 +68,10 @@ func merge(files map[string][]byte) (merged []byte) { // render and write everything inside -func parse(dirPath string, elements map[string][]byte, overwrite bool) map[string][]byte { +func parse(dirPath string, elements map[string][]byte, exts []string, overwrite bool) map[string][]byte { _, filesList := ls(dirPath) for _, fileName := range filesList { - if isParsable(fileName) && (overwrite || elements[fileName[:len(fileName)-len(path.Ext(fileName))]] == nil) { + if isParsable(fileName, exts) && (overwrite || elements[fileName[:len(fileName)-len(path.Ext(fileName))]] == nil) { var err error elements[fileName[:len(fileName)-len(path.Ext(fileName))]], err = read(path.Join(dirPath, fileName)) if err != nil { @@ -81,7 +82,7 @@ func parse(dirPath string, elements map[string][]byte, overwrite bool) map[strin return elements } -func compile(dirPath string, elements map[string][]byte, sourceDir, outputDir string, recursive bool) { +func compile(dirPath string, elements map[string][]byte, sourceDir, outputDir, saveAs string, exts []string, recursive bool) { wait.Add(1) defer wait.Done() @@ -89,26 +90,26 @@ func compile(dirPath string, elements map[string][]byte, sourceDir, outputDir st return } - elements = parse(dirPath, elements, true) + elements = parse(dirPath, elements, exts, true) if recursive { dirs, _ := ls(dirPath) for _, dir := range dirs { - go compile(path.Join(dirPath, dir), elements, sourceDir, outputDir, recursive) + go compile(path.Join(dirPath, dir), elements, sourceDir, outputDir, saveAs, exts, recursive) } } template := merge(elements) page := mustache.Render(string(template), nil /* TODO: generate contextual variables */) - err := writeFile(path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), "index.html"), []byte(page)) + err := writeFile(path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), saveAs), []byte(page)) if err != nil { fmt.Println(err) return } } -func copyFiles(dirPath, sourceDir, outputDir string, recursive bool) { +func copyFiles(dirPath, sourceDir, outputDir string, exts []string, recursive bool) { wait.Add(1) defer wait.Done() @@ -118,7 +119,7 @@ func copyFiles(dirPath, sourceDir, outputDir string, recursive bool) { dirs, files := ls(dirPath) for _, file := range files { - if !isParsable(file) { + if !isParsable(file, exts) { err := cp(path.Join(dirPath, file), path.Join(outputDir, strings.TrimPrefix(dirPath, sourceDir), file)) if err != nil { fmt.Println(err) @@ -128,7 +129,7 @@ func copyFiles(dirPath, sourceDir, outputDir string, recursive bool) { if recursive { for _, dir := range dirs { - go copyFiles(path.Join(dirPath, dir), sourceDir, outputDir, recursive) + go copyFiles(path.Join(dirPath, dir), sourceDir, outputDir, exts, recursive) } } } diff --git a/compiled.go b/compiled.go index 5b2c19b..1cd391e 100644 --- a/compiled.go +++ b/compiled.go @@ -25,7 +25,7 @@ import ( "time" ) -func compiled(sourceDir, outputDir string) { +func compiled(sourceDir, outputDir string, exts []string, saveAs string) { // remove previously compiled site err := os.RemoveAll(outputDir) if err != nil { @@ -34,8 +34,8 @@ func compiled(sourceDir, outputDir string) { } // compile everything - go compile(sourceDir, make(map[string][]byte), sourceDir, outputDir, true) - go copyFiles(sourceDir, sourceDir, outputDir, true) + go compile(sourceDir, make(map[string][]byte), sourceDir, outputDir, saveAs, exts, true) + go copyFiles(sourceDir, sourceDir, outputDir, exts, true) // sleep some milliseconds to prevent early exit 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 ( func handle(w http.ResponseWriter, r *http.Request) { // serve static files - if !(path.Ext(r.URL.Path) == "" || isParsable(path.Ext(r.URL.Path))) { + if !(path.Ext(r.URL.Path) == "" || isParsable(path.Ext(r.URL.Path), settings.exts)) { http.ServeFile(w, r, path.Join(*settings.sourceDir, r.URL.Path)) return } @@ -44,7 +44,7 @@ func handle(w http.ResponseWriter, r *http.Request) { // parse these dirs elements := make(map[string][]byte) for _, dir := range dirs { - parse(path.Join(*settings.sourceDir, dir), elements, false) + parse(path.Join(*settings.sourceDir, dir), elements, settings.exts, false) } // 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 { return watcher } -func parseParents(dir, sourceDir string) map[string][]byte { +func parseParents(dir, sourceDir string, exts []string) map[string][]byte { dirs := strings.Split(strings.TrimPrefix(dir, sourceDir), "/") elements := make(map[string][]byte) for _, dir := range dirs { - elements = parse(path.Join(sourceDir, dir), elements, false) + elements = parse(path.Join(sourceDir, dir), elements, exts, false) } return elements } -func interactive(sourceDir, outputDir string) { +func interactive(sourceDir, outputDir string, exts []string, saveAs string) { // compile the whole site - compiled(sourceDir, outputDir) + compiled(sourceDir, outputDir, exts, saveAs) // watch the source dir watcher, err := fsnotify.NewWatcher() @@ -90,7 +90,7 @@ func interactive(sourceDir, outputDir string) { // remove previously compiled files if ev.IsDelete() || ev.IsRename() || ev.IsModify() { var err error - if isDir(ev.Name) || !isParsable(ev.Name) { + if isDir(ev.Name) || !isParsable(ev.Name, exts) { err = os.RemoveAll(path.Join(outputDir, strings.TrimPrefix(ev.Name, sourceDir))) } else { err = os.RemoveAll(path.Join(outputDir, strings.TrimPrefix(dir, sourceDir))) @@ -104,17 +104,17 @@ func interactive(sourceDir, outputDir string) { // recompile changed files if ev.IsCreate() || ev.IsModify() { if isDir(ev.Name) { - elements := parseParents(ev.Name, sourceDir) + elements := parseParents(ev.Name, sourceDir, exts) dirPath := path.Join(sourceDir, strings.TrimPrefix(ev.Name, sourceDir)) - go compile(dirPath, elements, sourceDir, outputDir, true) - go copyFiles(dirPath, sourceDir, outputDir, true) + go compile(dirPath, elements, sourceDir, outputDir, saveAs, exts, true) + go copyFiles(dirPath, sourceDir, outputDir, exts, true) } else { dirPath := path.Join(sourceDir, strings.TrimPrefix(dir, sourceDir)) - if isParsable(path.Ext(ev.Name)) { - elements := parseParents(dir, sourceDir) - go compile(dirPath, elements, sourceDir, outputDir, true) + if isParsable(path.Ext(ev.Name), exts) { + elements := parseParents(dir, sourceDir, exts) + go compile(dirPath, elements, sourceDir, outputDir, saveAs, exts, true) } - go copyFiles(dirPath, sourceDir, outputDir, false) + go copyFiles(dirPath, sourceDir, outputDir, exts, false) } } diff --git a/main.go b/main.go index 49b27fa..2c0cf81 100644 --- a/main.go +++ b/main.go @@ -22,6 +22,7 @@ package main import ( "flag" "fmt" + "strings" ) var settings struct { @@ -29,6 +30,8 @@ var settings struct { sourceDir *string outputDir *string // for compiled site port *string // for the integrated web server (dynamic mode only) + exts []string + saveAs *string } func init() { @@ -37,7 +40,13 @@ func init() { settings.sourceDir = flag.String("source", ".", "Path to sources directory.") settings.outputDir = flag.String("output", "./out", "[compiled mode] Path to output directory.") settings.port = flag.String("port", "8080", "[dynamic mode] Port to listen.") + exts := flag.String("exts", "html, txt, md", "List parsable file extensions. Separated by commas.") + settings.saveAs = flag.String("saveAs", "index.html", "[compiled and interactive modes] Save compiled files as named.") flag.Parse() + settings.exts = strings.Split(*exts, ",") + for i, ext := range settings.exts { + settings.exts[i] = "." + strings.Trim(ext, ". ") + } } func main() { @@ -49,9 +58,9 @@ func main() { switch *settings.mode { case "compiled": - compiled(*settings.sourceDir, *settings.outputDir) + compiled(*settings.sourceDir, *settings.outputDir, settings.exts, *settings.saveAs) case "interactive": - interactive(*settings.sourceDir, *settings.outputDir) + interactive(*settings.sourceDir, *settings.outputDir, settings.exts, *settings.saveAs) case "dynamic": dynamic(*settings.port) default: -- cgit v1.2.3