more deliniated parsing
This commit is contained in:
parent
7f128eadd8
commit
eaa044e1d5
@ -1,14 +1,13 @@
|
|||||||
// ## Line Filters
|
// ## Line Filters
|
||||||
|
|
||||||
// A _line filter_ is a very common type of program that
|
// A _line filter_ is a common type of program that reads
|
||||||
// reads input on stdin, processes it, and then prints
|
// input on stdin, processes it, and then prints some
|
||||||
// some derived results to stdout. `grep` and `sed` for
|
// derived result to stdout. `grep` and `sed` are common
|
||||||
// example are common line filters.
|
// line filters.
|
||||||
|
|
||||||
// Here's an example line filter in Go that writes a
|
// Here's an example line filter in Go that writes a
|
||||||
// capitalized version of all text it reads. You can use
|
// capitalized version of all input text. You can use this
|
||||||
// this pattern to write your own Go line filters.
|
// pattern to write your own Go line filters.
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
// Package `bufio` will help us read line-by-line, and
|
// Package `bufio` will help us read line-by-line, and
|
||||||
@ -22,6 +21,7 @@ import "io"
|
|||||||
var newline = []byte("\n")
|
var newline = []byte("\n")
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
// Wrapping the unbuffered `os.Stdin` with a buffered
|
// Wrapping the unbuffered `os.Stdin` with a buffered
|
||||||
// reader gives us the convenient `ReadLine` method.
|
// reader gives us the convenient `ReadLine` method.
|
||||||
in := bufio.NewReader(os.Stdin)
|
in := bufio.NewReader(os.Stdin)
|
||||||
|
@ -101,7 +101,7 @@ td.code {
|
|||||||
padding-top: 5px;
|
padding-top: 5px;
|
||||||
padding-right: 5px;
|
padding-right: 5px;
|
||||||
padding-left: 5px;
|
padding-left: 5px;
|
||||||
padding-bottom: 5px;
|
padding-bottom: 11px;
|
||||||
vertical-align: top;
|
vertical-align: top;
|
||||||
background: #f0f0f0;
|
background: #f0f0f0;
|
||||||
}
|
}
|
||||||
|
@ -100,7 +100,6 @@ func debug(msg string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var docsPat = regexp.MustCompile("^\\s*(\\/\\/|#)\\s")
|
var docsPat = regexp.MustCompile("^\\s*(\\/\\/|#)\\s")
|
||||||
var headerPat = regexp.MustCompile("^\\s*(\\/\\/|#)\\s#+\\s")
|
|
||||||
var todoPat = regexp.MustCompile("\\/\\/ todo: ")
|
var todoPat = regexp.MustCompile("\\/\\/ todo: ")
|
||||||
|
|
||||||
type Seg struct {
|
type Seg struct {
|
||||||
@ -118,50 +117,39 @@ func parseSegs(sourcePath string) []*Seg {
|
|||||||
segs = append(segs, &Seg{code: "", docs: ""})
|
segs = append(segs, &Seg{code: "", docs: ""})
|
||||||
lastSeen := ""
|
lastSeen := ""
|
||||||
for _, line := range lines {
|
for _, line := range lines {
|
||||||
|
if line == "" {
|
||||||
|
lastSeen = ""
|
||||||
|
continue
|
||||||
|
}
|
||||||
if todoPat.MatchString(line) {
|
if todoPat.MatchString(line) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
headerMatch := headerPat.MatchString(line)
|
matchDocs := docsPat.MatchString(line)
|
||||||
docsMatch := docsPat.MatchString(line)
|
matchCode := !matchDocs
|
||||||
emptyMatch := line == ""
|
|
||||||
lastSeg := segs[len(segs)-1]
|
lastSeg := segs[len(segs)-1]
|
||||||
lastHeader := lastSeen == "header"
|
newDocs := (lastSeen == "") || ((lastSeen != "docs") && (lastSeg.docs != ""))
|
||||||
lastDocs := lastSeen == "docs"
|
newCode := (lastSeen == "") || ((lastSeen != "code") && (lastSeg.code != ""))
|
||||||
newHeader := lastSeen != "header" && lastSeg.docs != ""
|
if newDocs || newCode {
|
||||||
newDocs := lastSeen == "code" || lastSeen == "header"
|
|
||||||
newCode := (lastSeen != "code" && lastSeg.code != "") || lastSeen == "header"
|
|
||||||
if newHeader || newDocs || newCode {
|
|
||||||
debug("NEWSEG")
|
debug("NEWSEG")
|
||||||
}
|
}
|
||||||
if headerMatch || (emptyMatch && lastHeader) {
|
if matchDocs {
|
||||||
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) {
|
|
||||||
trimmed := docsPat.ReplaceAllString(line, "")
|
trimmed := docsPat.ReplaceAllString(line, "")
|
||||||
if newDocs {
|
if newDocs {
|
||||||
debug("NEWSEG")
|
|
||||||
newSeg := Seg{docs: trimmed, code: ""}
|
newSeg := Seg{docs: trimmed, code: ""}
|
||||||
segs = append(segs, &newSeg)
|
segs = append(segs, &newSeg)
|
||||||
} else {
|
} else {
|
||||||
lastSeg.docs = lastSeg.docs + "\n" + trimmed
|
lastSeg.docs = lastSeg.docs + "\n" + trimmed
|
||||||
}
|
}
|
||||||
debug("DOCS")
|
debug("DOCS: " + line)
|
||||||
lastSeen = "docs"
|
lastSeen = "docs"
|
||||||
} else {
|
} else if matchCode {
|
||||||
if newCode {
|
if newCode {
|
||||||
newSeg := Seg{docs: "", code: line}
|
newSeg := Seg{docs: "", code: line}
|
||||||
segs = append(segs, &newSeg)
|
segs = append(segs, &newSeg)
|
||||||
} else {
|
} else {
|
||||||
lastSeg.code = lastSeg.code + "\n" + line
|
lastSeg.code = lastSeg.code + "\n" + line
|
||||||
}
|
}
|
||||||
debug("CODE")
|
debug("CODE: " + line)
|
||||||
lastSeen = "code"
|
lastSeen = "code"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user