books wat u know about them
This commit is contained in:
parent
f035a0f30f
commit
2a9b41f6a9
164
tool/generate.go
164
tool/generate.go
@ -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()
|
||||||
@ -31,9 +39,19 @@ func pipe(bin string, arg []string, src string) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func readLines(path string) []string {
|
func readLines(path string) []string {
|
||||||
srcBytes, err := ioutil.ReadFile(path)
|
srcBytes, err := ioutil.ReadFile(path)
|
||||||
check(err)
|
check(err)
|
||||||
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")
|
||||||
@ -44,69 +62,12 @@ 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)
|
|
||||||
|
|
||||||
lines := readLines(sourcePath)
|
|
||||||
|
|
||||||
segs := []*seg{}
|
|
||||||
segs = append(segs, &seg{code: "", docs: ""})
|
|
||||||
lastSeen := ""
|
|
||||||
for _, line := range lines {
|
|
||||||
headerMatch := headerPat.MatchString(line)
|
|
||||||
docsMatch := docsPat.MatchString(line)
|
|
||||||
emptyMatch := line == ""
|
|
||||||
lastSeg := segs[len(segs)-1]
|
|
||||||
lastHeader := lastSeen == "header"
|
|
||||||
lastDocs := lastSeen == "docs"
|
|
||||||
newHeader := (lastSeen != "header") && lastSeg.docs != ""
|
|
||||||
newDocs := (lastSeen != "docs") && lastSeg.docs != ""
|
|
||||||
newCode := (lastSeen != "code") && lastSeg.code != ""
|
|
||||||
if headerMatch || (emptyMatch && lastHeader) {
|
|
||||||
trimmed := docsPat.ReplaceAllString(line, "")
|
|
||||||
if newHeader {
|
|
||||||
newSeg := seg{docs: trimmed, code: ""}
|
|
||||||
segs = append(segs, &newSeg)
|
|
||||||
} else {
|
|
||||||
lastSeg.docs = lastSeg.docs + "\n" + trimmed
|
|
||||||
}
|
|
||||||
lastSeen = "header"
|
|
||||||
} else if docsMatch || (emptyMatch && lastDocs) {
|
|
||||||
trimmed := docsPat.ReplaceAllString(line, "")
|
|
||||||
if newDocs {
|
|
||||||
newSeg := seg{docs: trimmed, code: ""}
|
|
||||||
segs = append(segs, &newSeg)
|
|
||||||
} else {
|
|
||||||
lastSeg.docs = lastSeg.docs + "\n" + trimmed
|
|
||||||
}
|
|
||||||
lastSeen = "docs"
|
|
||||||
} else {
|
|
||||||
if newCode {
|
|
||||||
newSeg := seg{docs: "", code: line}
|
|
||||||
segs = append(segs, &newSeg)
|
|
||||||
} else {
|
|
||||||
lastSeg.code = lastSeg.code + "\n" + line
|
|
||||||
}
|
|
||||||
lastSeen = "code"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, seg := range segs {
|
|
||||||
if seg.docs != "" {
|
|
||||||
seg.docsRendered = pipe(markdownPath, []string{}, seg.docs)
|
|
||||||
}
|
|
||||||
if seg.code != "" {
|
|
||||||
seg.codeRendered = pipe(pygmentizePath, []string{"-l", "go", "-f", "html"}, seg.code+" ")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Print(`<!DOCTYPE html>
|
fmt.Print(`<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
@ -121,16 +82,73 @@ func main() {
|
|||||||
<table cellspacing="0" cellpadding="0">
|
<table cellspacing="0" cellpadding="0">
|
||||||
<tbody>`)
|
<tbody>`)
|
||||||
|
|
||||||
for _, seg := range segs {
|
for _, sourcePath := range os.Args[1:] {
|
||||||
codeClasses := "code"
|
lexer := whichLexer(sourcePath)
|
||||||
if seg.code == "" {
|
lines := readLines(sourcePath)
|
||||||
codeClasses = codeClasses + " empty"
|
|
||||||
}
|
segs := []*seg{}
|
||||||
fmt.Printf(
|
segs = append(segs, &seg{code: "", docs: ""})
|
||||||
`<tr>
|
lastSeen := ""
|
||||||
<td class=docs>%s</td>
|
for _, line := range lines {
|
||||||
<td class="%s">%s</td>
|
headerMatch := headerPat.MatchString(line)
|
||||||
</tr>`, seg.docsRendered, codeClasses, seg.codeRendered)
|
docsMatch := docsPat.MatchString(line)
|
||||||
|
emptyMatch := line == ""
|
||||||
|
lastSeg := segs[len(segs)-1]
|
||||||
|
lastHeader := lastSeen == "header"
|
||||||
|
lastDocs := lastSeen == "docs"
|
||||||
|
newHeader := (lastSeen != "header") && lastSeg.docs != ""
|
||||||
|
newDocs := (lastSeen != "docs") && lastSeg.docs != ""
|
||||||
|
newCode := (lastSeen != "code") && lastSeg.code != ""
|
||||||
|
if headerMatch || (emptyMatch && lastHeader) {
|
||||||
|
trimmed := docsPat.ReplaceAllString(line, "")
|
||||||
|
if newHeader {
|
||||||
|
newSeg := seg{docs: trimmed, code: ""}
|
||||||
|
segs = append(segs, &newSeg)
|
||||||
|
} else {
|
||||||
|
lastSeg.docs = lastSeg.docs + "\n" + trimmed
|
||||||
|
}
|
||||||
|
lastSeen = "header"
|
||||||
|
} else if docsMatch || (emptyMatch && lastDocs) {
|
||||||
|
trimmed := docsPat.ReplaceAllString(line, "")
|
||||||
|
if newDocs {
|
||||||
|
newSeg := seg{docs: trimmed, code: ""}
|
||||||
|
segs = append(segs, &newSeg)
|
||||||
|
} else {
|
||||||
|
lastSeg.docs = lastSeg.docs + "\n" + trimmed
|
||||||
|
}
|
||||||
|
lastSeen = "docs"
|
||||||
|
} else {
|
||||||
|
if newCode {
|
||||||
|
newSeg := seg{docs: "", code: line}
|
||||||
|
segs = append(segs, &newSeg)
|
||||||
|
} else {
|
||||||
|
lastSeg.code = lastSeg.code + "\n" + line
|
||||||
|
}
|
||||||
|
lastSeen = "code"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
segs = append(segs, &seg{code: "", docs: ""})
|
||||||
|
|
||||||
|
for _, seg := range segs {
|
||||||
|
if seg.docs != "" {
|
||||||
|
seg.docsRendered = render(markdownPath, []string{}, seg.docs)
|
||||||
|
}
|
||||||
|
if seg.code != "" {
|
||||||
|
seg.codeRendered = render(pygmentizePath, []string{"-l", lexer, "-f", "html"}, seg.code+" ")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, seg := range segs {
|
||||||
|
codeClasses := "code"
|
||||||
|
if seg.code == "" {
|
||||||
|
codeClasses = codeClasses + " empty"
|
||||||
|
}
|
||||||
|
fmt.Printf(
|
||||||
|
`<tr>
|
||||||
|
<td class=docs>%s</td>
|
||||||
|
<td class="%s">%s</td>
|
||||||
|
</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
|
||||||
|
@ -28,10 +28,10 @@ func main() {
|
|||||||
}
|
}
|
||||||
baseTrimmer, _ := regexp.Compile("^[0-9x]+-")
|
baseTrimmer, _ := regexp.Compile("^[0-9x]+-")
|
||||||
for _, fi := range fileInfos {
|
for _, fi := range fileInfos {
|
||||||
if fi.Name() != "index.txt" {
|
if fi.Name() != "index.txt" {
|
||||||
baseName := baseTrimmer.ReplaceAllString(fi.Name(), "")
|
baseName := baseTrimmer.ReplaceAllString(fi.Name(), "")
|
||||||
sourceNames = append(sourceNames, baseName)
|
sourceNames = append(sourceNames, baseName)
|
||||||
sourceMap[baseName] = fi.Name()
|
sourceMap[baseName] = fi.Name()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user