From eaa044e1d5b1427d50a3c2e08ac681596228eacd Mon Sep 17 00:00:00 2001 From: Mark McGranaghan Date: Tue, 9 Oct 2012 10:55:27 -0700 Subject: [PATCH] more deliniated parsing --- src/line-filters/line-filters.go | 14 ++++++------ style/site.css | 2 +- tool/generate.go | 38 +++++++++++--------------------- 3 files changed, 21 insertions(+), 33 deletions(-) diff --git a/src/line-filters/line-filters.go b/src/line-filters/line-filters.go index 3b8d8f3..3f66a61 100644 --- a/src/line-filters/line-filters.go +++ b/src/line-filters/line-filters.go @@ -1,14 +1,13 @@ // ## Line Filters -// A _line filter_ is a very common type of program that -// reads input on stdin, processes it, and then prints -// some derived results to stdout. `grep` and `sed` for -// example are common line filters. +// A _line filter_ is a common type of program that reads +// input on stdin, processes it, and then prints some +// derived result to stdout. `grep` and `sed` are common +// line filters. // Here's an example line filter in Go that writes a -// capitalized version of all text it reads. You can use -// this pattern to write your own Go line filters. - +// capitalized version of all input text. You can use this +// pattern to write your own Go line filters. package main // Package `bufio` will help us read line-by-line, and @@ -22,6 +21,7 @@ import "io" var newline = []byte("\n") func main() { + // Wrapping the unbuffered `os.Stdin` with a buffered // reader gives us the convenient `ReadLine` method. in := bufio.NewReader(os.Stdin) diff --git a/style/site.css b/style/site.css index 59d2a70..65550d0 100644 --- a/style/site.css +++ b/style/site.css @@ -101,7 +101,7 @@ td.code { padding-top: 5px; padding-right: 5px; padding-left: 5px; - padding-bottom: 5px; + padding-bottom: 11px; vertical-align: top; background: #f0f0f0; } diff --git a/tool/generate.go b/tool/generate.go index ffd3dbd..73f8b20 100644 --- a/tool/generate.go +++ b/tool/generate.go @@ -100,7 +100,6 @@ func debug(msg string) { } var docsPat = regexp.MustCompile("^\\s*(\\/\\/|#)\\s") -var headerPat = regexp.MustCompile("^\\s*(\\/\\/|#)\\s#+\\s") var todoPat = regexp.MustCompile("\\/\\/ todo: ") type Seg struct { @@ -118,50 +117,39 @@ func parseSegs(sourcePath string) []*Seg { segs = append(segs, &Seg{code: "", docs: ""}) lastSeen := "" for _, line := range lines { + if line == "" { + lastSeen = "" + continue + } if todoPat.MatchString(line) { continue } - headerMatch := headerPat.MatchString(line) - docsMatch := docsPat.MatchString(line) - emptyMatch := line == "" + matchDocs := docsPat.MatchString(line) + matchCode := !matchDocs lastSeg := segs[len(segs)-1] - lastHeader := lastSeen == "header" - lastDocs := lastSeen == "docs" - newHeader := lastSeen != "header" && lastSeg.docs != "" - newDocs := lastSeen == "code" || lastSeen == "header" - newCode := (lastSeen != "code" && lastSeg.code != "") || lastSeen == "header" - if newHeader || newDocs || newCode { + newDocs := (lastSeen == "") || ((lastSeen != "docs") && (lastSeg.docs != "")) + newCode := (lastSeen == "") || ((lastSeen != "code") && (lastSeg.code != "")) + if newDocs || newCode { debug("NEWSEG") } - 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 - } - debug("HEAD") - lastSeen = "header" - } else if docsMatch || (emptyMatch && lastDocs) { + if matchDocs { trimmed := docsPat.ReplaceAllString(line, "") if newDocs { - debug("NEWSEG") newSeg := Seg{docs: trimmed, code: ""} segs = append(segs, &newSeg) } else { lastSeg.docs = lastSeg.docs + "\n" + trimmed } - debug("DOCS") + debug("DOCS: " + line) lastSeen = "docs" - } else { + } else if matchCode { if newCode { newSeg := Seg{docs: "", code: line} segs = append(segs, &newSeg) } else { lastSeg.code = lastSeg.code + "\n" + line } - debug("CODE") + debug("CODE: " + line) lastSeen = "code" } }