more deliniated parsing
This commit is contained in:
parent
7f128eadd8
commit
eaa044e1d5
@ -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)
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user