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...)
|
cmd := exec.Command(bin, arg...)
|
||||||
in, _ := cmd.StdinPipe()
|
in, _ := cmd.StdinPipe()
|
||||||
out, _ := cmd.StdoutPipe()
|
out, _ := cmd.StdoutPipe()
|
||||||
@ -36,6 +44,16 @@ func readLines(path string) []string {
|
|||||||
return strings.Split(string(srcBytes), "\n")
|
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 docsPat = regexp.MustCompile("^\\s*(\\/\\/|#)\\s")
|
||||||
var headerPat = regexp.MustCompile("^\\s*(\\/\\/|#)\\s#+\\s")
|
var headerPat = regexp.MustCompile("^\\s*(\\/\\/|#)\\s#+\\s")
|
||||||
|
|
||||||
@ -44,17 +62,28 @@ type seg struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) != 2 {
|
if len(os.Args) <= 1 {
|
||||||
fmt.Fprintln(os.Stderr, "usage: tool/generate input.go > output.html")
|
fmt.Fprintln(os.Stderr, "usage: tool/generate *.{go,sh} > output.html")
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
sourcePath := os.Args[1]
|
|
||||||
|
|
||||||
markdownPath, err := exec.LookPath("markdown")
|
markdownPath, pygmentizePath := whichRenderers()
|
||||||
check(err)
|
|
||||||
pygmentizePath, err := exec.LookPath("pygmentize")
|
|
||||||
check(err)
|
|
||||||
|
|
||||||
|
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)
|
lines := readLines(sourcePath)
|
||||||
|
|
||||||
segs := []*seg{}
|
segs := []*seg{}
|
||||||
@ -98,29 +127,17 @@ func main() {
|
|||||||
lastSeen = "code"
|
lastSeen = "code"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
segs = append(segs, &seg{code: "", docs: ""})
|
||||||
|
|
||||||
for _, seg := range segs {
|
for _, seg := range segs {
|
||||||
if seg.docs != "" {
|
if seg.docs != "" {
|
||||||
seg.docsRendered = pipe(markdownPath, []string{}, seg.docs)
|
seg.docsRendered = render(markdownPath, []string{}, seg.docs)
|
||||||
}
|
}
|
||||||
if seg.code != "" {
|
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 {
|
for _, seg := range segs {
|
||||||
codeClasses := "code"
|
codeClasses := "code"
|
||||||
if seg.code == "" {
|
if seg.code == "" {
|
||||||
@ -132,6 +149,7 @@ func main() {
|
|||||||
<td class="%s">%s</td>
|
<td class="%s">%s</td>
|
||||||
</tr>`, seg.docsRendered, codeClasses, seg.codeRendered)
|
</tr>`, seg.docsRendered, codeClasses, seg.codeRendered)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Print(`</tbody></table></div></body></html>`)
|
fmt.Print(`</tbody></table></div></body></html>`)
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
#!/bin/bash
|
#!/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