books wat u know about them
This commit is contained in:
parent
f035a0f30f
commit
2a9b41f6a9
@ -17,7 +17,15 @@ func check(err error) {
|
||||
}
|
||||
}
|
||||
|
||||
func pipe(bin string, arg []string, src string) string {
|
||||
func whichRenderers() (string, string) {
|
||||
markdownPath, err := exec.LookPath("markdown")
|
||||
check(err)
|
||||
pygmentizePath, err := exec.LookPath("pygmentize")
|
||||
check(err)
|
||||
return markdownPath, pygmentizePath
|
||||
}
|
||||
|
||||
func render(bin string, arg []string, src string) string {
|
||||
cmd := exec.Command(bin, arg...)
|
||||
in, _ := cmd.StdinPipe()
|
||||
out, _ := cmd.StdoutPipe()
|
||||
@ -36,6 +44,16 @@ func readLines(path string) []string {
|
||||
return strings.Split(string(srcBytes), "\n")
|
||||
}
|
||||
|
||||
func whichLexer(path string) string {
|
||||
if strings.HasSuffix(path, ".go") {
|
||||
return "go"
|
||||
} else if strings.HasSuffix(path, ".sh") {
|
||||
return "console"
|
||||
}
|
||||
panic("No lexer for " + path)
|
||||
return ""
|
||||
}
|
||||
|
||||
var docsPat = regexp.MustCompile("^\\s*(\\/\\/|#)\\s")
|
||||
var headerPat = regexp.MustCompile("^\\s*(\\/\\/|#)\\s#+\\s")
|
||||
|
||||
@ -44,17 +62,28 @@ type seg struct {
|
||||
}
|
||||
|
||||
func main() {
|
||||
if len(os.Args) != 2 {
|
||||
fmt.Fprintln(os.Stderr, "usage: tool/generate input.go > output.html")
|
||||
if len(os.Args) <= 1 {
|
||||
fmt.Fprintln(os.Stderr, "usage: tool/generate *.{go,sh} > output.html")
|
||||
os.Exit(1)
|
||||
}
|
||||
sourcePath := os.Args[1]
|
||||
|
||||
markdownPath, err := exec.LookPath("markdown")
|
||||
check(err)
|
||||
pygmentizePath, err := exec.LookPath("pygmentize")
|
||||
check(err)
|
||||
markdownPath, pygmentizePath := whichRenderers()
|
||||
|
||||
fmt.Print(`<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-eqiv="content-type" content="text/html;charset=utf-8">
|
||||
<title>Go by Example</title>
|
||||
<link rel=stylesheet href="../style/book.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="container">
|
||||
<div id="background"></div>
|
||||
<table cellspacing="0" cellpadding="0">
|
||||
<tbody>`)
|
||||
|
||||
for _, sourcePath := range os.Args[1:] {
|
||||
lexer := whichLexer(sourcePath)
|
||||
lines := readLines(sourcePath)
|
||||
|
||||
segs := []*seg{}
|
||||
@ -98,29 +127,17 @@ func main() {
|
||||
lastSeen = "code"
|
||||
}
|
||||
}
|
||||
segs = append(segs, &seg{code: "", docs: ""})
|
||||
|
||||
for _, seg := range segs {
|
||||
if seg.docs != "" {
|
||||
seg.docsRendered = pipe(markdownPath, []string{}, seg.docs)
|
||||
seg.docsRendered = render(markdownPath, []string{}, seg.docs)
|
||||
}
|
||||
if seg.code != "" {
|
||||
seg.codeRendered = pipe(pygmentizePath, []string{"-l", "go", "-f", "html"}, seg.code+" ")
|
||||
seg.codeRendered = render(pygmentizePath, []string{"-l", lexer, "-f", "html"}, seg.code+" ")
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Print(`<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-eqiv="content-type" content="text/html;charset=utf-8">
|
||||
<title>Go by Example</title>
|
||||
<link rel=stylesheet href="../style/book.css">
|
||||
</head>
|
||||
<body>
|
||||
<div id="container">
|
||||
<div id="background"></div>
|
||||
<table cellspacing="0" cellpadding="0">
|
||||
<tbody>`)
|
||||
|
||||
for _, seg := range segs {
|
||||
codeClasses := "code"
|
||||
if seg.code == "" {
|
||||
@ -132,6 +149,7 @@ func main() {
|
||||
<td class="%s">%s</td>
|
||||
</tr>`, seg.docsRendered, codeClasses, seg.codeRendered)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Print(`</tbody></table></div></body></html>`)
|
||||
}
|
||||
|
@ -1,3 +1,6 @@
|
||||
#!/bin/bash
|
||||
|
||||
ls src/*/*.go | xargs -n 1 gofmt -tabs=false -tabwidth=4 -w=true
|
||||
set -e
|
||||
set -o pipefail
|
||||
|
||||
ls tool/*.go src/*/*.go | xargs -n 1 gofmt -tabs=false -tabwidth=4 -w=true
|
||||
|
Loading…
x
Reference in New Issue
Block a user