content listing
This commit is contained in:
parent
358c96a8f5
commit
66a05a48f2
@ -1,4 +1,3 @@
|
|||||||
introduction
|
|
||||||
hello-world
|
hello-world
|
||||||
values
|
values
|
||||||
variables
|
variables
|
@ -1,3 +0,0 @@
|
|||||||
# Go by Example
|
|
||||||
|
|
||||||
### Mark McGranaghan
|
|
140
tool/generate.go
140
tool/generate.go
@ -14,6 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var cacheDir = "/tmp/gobyexample-cache"
|
var cacheDir = "/tmp/gobyexample-cache"
|
||||||
|
var siteDir = "site"
|
||||||
|
|
||||||
func check(err error) {
|
func check(err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -31,6 +32,11 @@ func filterStrings(vs []string, f func(string) bool) []string {
|
|||||||
return vsf
|
return vsf
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ensureDir(dir string) {
|
||||||
|
err := os.MkdirAll(dir, 0700)
|
||||||
|
check(err)
|
||||||
|
}
|
||||||
|
|
||||||
func pipe(bin string, arg []string, src string) []byte {
|
func pipe(bin string, arg []string, src string) []byte {
|
||||||
cmd := exec.Command(bin, arg...)
|
cmd := exec.Command(bin, arg...)
|
||||||
in, _ := cmd.StdinPipe()
|
in, _ := cmd.StdinPipe()
|
||||||
@ -58,6 +64,7 @@ func mustReadFile(path string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func cachedPygmentize(lex string, src string) string {
|
func cachedPygmentize(lex string, src string) string {
|
||||||
|
ensureDir(cacheDir)
|
||||||
arg := []string{"-l", lex, "-f", "html"}
|
arg := []string{"-l", lex, "-f", "html"}
|
||||||
bin := "/usr/local/bin/pygmentize"
|
bin := "/usr/local/bin/pygmentize"
|
||||||
cachePath := cacheDir + "/pygmentize-" + strings.Join(arg, "-") + "-" + sha1Sum(src)
|
cachePath := cacheDir + "/pygmentize-" + strings.Join(arg, "-") + "-" + sha1Sum(src)
|
||||||
@ -71,11 +78,6 @@ func cachedPygmentize(lex string, src string) string {
|
|||||||
return string(renderBytes)
|
return string(renderBytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
func ensureCache() {
|
|
||||||
mkdirErr := os.MkdirAll(cacheDir, 0700)
|
|
||||||
check(mkdirErr)
|
|
||||||
}
|
|
||||||
|
|
||||||
func markdown(src string) string {
|
func markdown(src string) string {
|
||||||
return string(blackfriday.MarkdownCommon([]byte(src)))
|
return string(blackfriday.MarkdownCommon([]byte(src)))
|
||||||
}
|
}
|
||||||
@ -185,78 +187,66 @@ func parseAndRenderSegs(sourcePath string) []*seg {
|
|||||||
return segs
|
return segs
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func generateIndex() {
|
||||||
if len(os.Args) != 2 {
|
indexF, err := os.Create(siteDir + "/index.html")
|
||||||
panic("Wrong number of args")
|
|
||||||
}
|
|
||||||
outF, err := os.Create(os.Args[1])
|
|
||||||
check(err)
|
check(err)
|
||||||
ensureCache()
|
fmt.Fprint(indexF,
|
||||||
|
|
||||||
// Header
|
|
||||||
fmt.Fprint(outF,
|
|
||||||
`<!DOCTYPE html>
|
`<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<meta http-eqiv="content-type" content="text/html;charset=utf-8">
|
<meta http-eqiv="content-type" content="text/html;charset=utf-8">
|
||||||
<title>Go by Example</title>
|
<title>Go by Example</title>
|
||||||
<link rel=stylesheet href="../src/style.css">
|
<link rel=stylesheet href="../style/site.css">
|
||||||
</head>
|
</head>
|
||||||
<body>`)
|
<body>
|
||||||
|
<div class="chapter" id="contents"><h2>Contents</h2><ul>`)
|
||||||
// Title page
|
chapterIds := readLines("meta/contents.txt")
|
||||||
fmt.Fprintf(outF,
|
|
||||||
`<div class="chapter" id="title">%s</div>`,
|
|
||||||
markdown(mustReadFile("src/title.md")))
|
|
||||||
|
|
||||||
// Contents page
|
|
||||||
chapterIds := readLines("src/contents.txt")
|
|
||||||
fmt.Fprint(outF, `<div class="chapter" id="contents"><h2>Contents</h2><ul>`)
|
|
||||||
for _, chapterId := range chapterIds {
|
for _, chapterId := range chapterIds {
|
||||||
var chapterName string
|
chapterLines := readLines("src/" + chapterId + "/" + chapterId + ".go")
|
||||||
if chapterId == "introduction" {
|
chapterName := chapterLines[0][6:]
|
||||||
chapterName = "Introduction"
|
fmt.Fprintf(indexF, `<li><a href="%s.html">%s</a></li>`, chapterId, chapterName)
|
||||||
} else {
|
|
||||||
chapterLines := readLines("src/" + chapterId + "/" + chapterId + ".go")
|
|
||||||
chapterName = chapterLines[0][6:]
|
|
||||||
}
|
|
||||||
fmt.Fprintf(outF, `<li><a href="#%s">%s</a></li>`, chapterId, chapterName)
|
|
||||||
}
|
}
|
||||||
fmt.Fprint(outF, `</ul></div>`)
|
fmt.Fprint(indexF, `</ul></div></body></html>`)
|
||||||
|
|
||||||
// Content chapters
|
|
||||||
for _, chapterId := range chapterIds {
|
|
||||||
fmt.Fprintf(outF, `<div class="chapter" id="%s">`, chapterId)
|
|
||||||
if chapterId == "introduction" {
|
|
||||||
fmt.Fprint(outF, markdown(mustReadFile("src/introduction.md")))
|
|
||||||
} else {
|
|
||||||
chapterPath := "src/" + chapterId
|
|
||||||
fmt.Fprintf(outF,
|
|
||||||
`<table cellspacing="0" cellpadding="0" id="%s"><tbody>`,
|
|
||||||
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,
|
|
||||||
`<tr>
|
|
||||||
<td class=docs>%s</td>
|
|
||||||
<td class="%s">%s</td>
|
|
||||||
</tr>`,
|
|
||||||
seg.docsRendered, codeClasses, seg.codeRendered)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
fmt.Fprint(outF, `</tbody></table>`)
|
|
||||||
}
|
|
||||||
fmt.Fprintf(outF, `</div>`)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Footer
|
|
||||||
fmt.Fprint(outF, `</body></html>`)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
ensureDir(siteDir)
|
||||||
|
generateIndex()
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// // Content chapters
|
||||||
|
// for _, chapterId := range chapterIds {
|
||||||
|
// fmt.Fprintf(outF, `<div class="chapter" id="%s">`, chapterId)
|
||||||
|
// if chapterId == "introduction" {
|
||||||
|
// fmt.Fprint(outF, markdown(mustReadFile("src/introduction.md")))
|
||||||
|
// } else {
|
||||||
|
// chapterPath := "src/" + chapterId
|
||||||
|
// fmt.Fprintf(outF,
|
||||||
|
// `<table cellspacing="0" cellpadding="0" id="%s"><tbody>`,
|
||||||
|
// 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,
|
||||||
|
// `<tr>
|
||||||
|
// <td class=docs>%s</td>
|
||||||
|
// <td class="%s">%s</td>
|
||||||
|
// </tr>`,
|
||||||
|
// seg.docsRendered, codeClasses, seg.codeRendered)
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
// fmt.Fprint(outF, `</tbody></table>`)
|
||||||
|
// }
|
||||||
|
// fmt.Fprintf(outF, `</div>`)
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // Footer
|
||||||
|
// fmt.Fprint(outF, `</body></html>`)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user