From 66a05a48f22760b0d7f41ad904744774772bb1bb Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Tue, 9 Oct 2012 08:15:18 -0700 Subject: [PATCH] content listing --- {src => meta}/contents.txt | 1 - {src => meta}/introduction.md | 0 src/title.md | 3 - src/style.css => style/site.css | 0 tool/generate.go | 140 +++++++++++++++----------------- 5 files changed, 65 insertions(+), 79 deletions(-) rename {src => meta}/contents.txt (97%) rename {src => meta}/introduction.md (100%) delete mode 100644 src/title.md rename src/style.css => style/site.css (100%) diff --git a/src/contents.txt b/meta/contents.txt similarity index 97% rename from src/contents.txt rename to meta/contents.txt index 617ba3a..2de652e 100644 --- a/src/contents.txt +++ b/meta/contents.txt @@ -1,4 +1,3 @@ -introduction hello-world values variables diff --git a/src/introduction.md b/meta/introduction.md similarity index 100% rename from src/introduction.md rename to meta/introduction.md diff --git a/src/title.md b/src/title.md deleted file mode 100644 index f7b5481..0000000 --- a/src/title.md +++ /dev/null @@ -1,3 +0,0 @@ -# Go by Example - -### Mark McGranaghan diff --git a/src/style.css b/style/site.css similarity index 100% rename from src/style.css rename to style/site.css diff --git a/tool/generate.go b/tool/generate.go index 6f7c60f..9b76fbb 100644 --- a/tool/generate.go +++ b/tool/generate.go @@ -14,6 +14,7 @@ import ( ) var cacheDir = "/tmp/gobyexample-cache" +var siteDir = "site" func check(err error) { if err != nil { @@ -31,6 +32,11 @@ func filterStrings(vs []string, f func(string) bool) []string { return vsf } +func ensureDir(dir string) { + err := os.MkdirAll(dir, 0700) + check(err) +} + func pipe(bin string, arg []string, src string) []byte { cmd := exec.Command(bin, arg...) in, _ := cmd.StdinPipe() @@ -58,6 +64,7 @@ func mustReadFile(path string) string { } func cachedPygmentize(lex string, src string) string { + ensureDir(cacheDir) arg := []string{"-l", lex, "-f", "html"} bin := "/usr/local/bin/pygmentize" cachePath := cacheDir + "/pygmentize-" + strings.Join(arg, "-") + "-" + sha1Sum(src) @@ -71,11 +78,6 @@ func cachedPygmentize(lex string, src string) string { return string(renderBytes) } -func ensureCache() { - mkdirErr := os.MkdirAll(cacheDir, 0700) - check(mkdirErr) -} - func markdown(src string) string { return string(blackfriday.MarkdownCommon([]byte(src))) } @@ -185,78 +187,66 @@ func parseAndRenderSegs(sourcePath string) []*seg { return segs } -func main() { - if len(os.Args) != 2 { - panic("Wrong number of args") - } - outF, err := os.Create(os.Args[1]) +func generateIndex() { + indexF, err := os.Create(siteDir + "/index.html") check(err) - ensureCache() - - // Header - fmt.Fprint(outF, + fmt.Fprint(indexF, ` - - - - Go by Example - - - `) - - // Title page - fmt.Fprintf(outF, - `
%s
`, - markdown(mustReadFile("src/title.md"))) - - // Contents page - chapterIds := readLines("src/contents.txt") - fmt.Fprint(outF, `

Contents

`) } + +func main() { + ensureDir(siteDir) + generateIndex() +} + +// +// // Content chapters +// for _, chapterId := range chapterIds { +// fmt.Fprintf(outF, `
`, chapterId) +// if chapterId == "introduction" { +// fmt.Fprint(outF, markdown(mustReadFile("src/introduction.md"))) +// } else { +// chapterPath := "src/" + chapterId +// fmt.Fprintf(outF, +// ``, +// chapterPath) +// sourcePaths := mustGlob(chapterPath + "/*") +// for _, sourcePath := range sourcePaths { +// if strings.HasSuffix(sourcePath, ".go") || strings.HasSuffix(sourcePath, ".sh") { +// segs := parseAndRenderSegs(sourcePath) +// for _, seg := range segs { +// codeClasses := "code" +// if seg.code == "" { +// codeClasses = codeClasses + " empty" +// } +// fmt.Fprintf(outF, +// ` +// +// +// `, +// seg.docsRendered, codeClasses, seg.codeRendered) +// } +// } +// } +// fmt.Fprint(outF, `
%s%s
`) +// } +// fmt.Fprintf(outF, `
`) +// } +// +// // Footer +// fmt.Fprint(outF, ``)