working on generate
This commit is contained in:
parent
cb645ae789
commit
072bc78ecd
@ -3,12 +3,16 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"io/ioutil"
|
"regexp"
|
||||||
"fmt"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var docsPat = regexp.MustCompile("^\\s*\\/\\/\\s")
|
||||||
|
|
||||||
// Just be sure to blow up on non-nil errors.
|
// Just be sure to blow up on non-nil errors.
|
||||||
func check(err error) {
|
func check(err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -16,6 +20,8 @@ func check(err error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Pipe source data through binary at path with
|
||||||
|
// given argv, return the output.
|
||||||
func pipedCmd(path string, argv []string, source string) string {
|
func pipedCmd(path string, argv []string, source string) string {
|
||||||
cmd := exec.Command(path, argv...)
|
cmd := exec.Command(path, argv...)
|
||||||
in, err := cmd.StdinPipe()
|
in, err := cmd.StdinPipe()
|
||||||
@ -35,6 +41,10 @@ func pipedCmd(path string, argv []string, source string) string {
|
|||||||
return string(bytes)
|
return string(bytes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type segment struct {
|
||||||
|
docs, code string
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
// Accept exactly 1 argument - the input filename.
|
// Accept exactly 1 argument - the input filename.
|
||||||
if len(os.Args) != 2 {
|
if len(os.Args) != 2 {
|
||||||
@ -44,16 +54,45 @@ func main() {
|
|||||||
|
|
||||||
// Ensure that we have `markdown` and `pygmentize`,
|
// Ensure that we have `markdown` and `pygmentize`,
|
||||||
// binaries, remember their paths.
|
// binaries, remember their paths.
|
||||||
// markdownPath, err := exec.LookPath("markdown");
|
markdownPath, err := exec.LookPath("markdown")
|
||||||
// check(err)
|
check(err)
|
||||||
// pygmentizePath, err := exec.LookPath("pygmentize")
|
pygmentizePath, err := exec.LookPath("pygmentize")
|
||||||
// check(err)
|
check(err)
|
||||||
|
fmt.Println(markdownPath, pygmentizePath)
|
||||||
|
|
||||||
// Read the source file in.
|
// Read the source file in.
|
||||||
sourceBytes, err := ioutil.ReadFile(os.Args[1])
|
sourceBytes, err := ioutil.ReadFile(os.Args[1])
|
||||||
check(err)
|
check(err)
|
||||||
source := string(sourceBytes)
|
|
||||||
fmt.Print(source)
|
|
||||||
|
|
||||||
// ...
|
// Split into lines.
|
||||||
|
lines := strings.Split(string(sourceBytes), "\n")
|
||||||
|
|
||||||
|
// Group lines into docs/code segments.
|
||||||
|
segments := []*segment{}
|
||||||
|
segments = append(segments, &segment{code: "", docs: ""})
|
||||||
|
lastLine := ""
|
||||||
|
for _, line := range lines {
|
||||||
|
head := segments[len(segments)-1]
|
||||||
|
// Doc line. Trim off the comment markers.
|
||||||
|
if (line == "" && lastLine == "docs") || docsPat.MatchString(line) {
|
||||||
|
trimLine := docsPat.ReplaceAllString(line, "")
|
||||||
|
if !(lastLine == "code" && head.docs != "") {
|
||||||
|
head.docs = head.docs + "\n" + trimLine
|
||||||
|
} else {
|
||||||
|
segments = append(segments, &segment{docs: trimLine, code: ""})
|
||||||
|
}
|
||||||
|
lastLine = "docs"
|
||||||
|
// Code line. Preserve all whitespace.
|
||||||
|
} else {
|
||||||
|
if !(lastLine == "docs" && head.code != "") {
|
||||||
|
head.code = head.code + "\n" + line
|
||||||
|
} else {
|
||||||
|
segments = append(segments, &segment{docs: "", code: line})
|
||||||
|
}
|
||||||
|
lastLine = "code"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, seg := range segments {
|
||||||
|
fmt.Printf("%#v\n", *seg)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user