Commit 791a2673 authored by James Toy's avatar James Toy Committed by Russ Cox

doc/htmlgen.go: remove unnecessary semicolons

R=rsc
CC=golang-dev
https://golang.org/cl/589043
parent c6a2c49e
...@@ -11,131 +11,131 @@ ...@@ -11,131 +11,131 @@
package main package main
import ( import (
"bufio"; "bufio"
"bytes"; "bytes"
"log"; "log"
"os"; "os"
) )
var ( var (
lines = make([][]byte, 0, 10000); // assume big enough lines = make([][]byte, 0, 10000) // assume big enough
linebuf = make([]byte, 10000); // assume big enough linebuf = make([]byte, 10000) // assume big enough
empty = []byte(""); empty = []byte("")
newline = []byte("\n"); newline = []byte("\n")
tab = []byte("\t"); tab = []byte("\t")
quote = []byte(`"`); quote = []byte(`"`)
sectionMarker = []byte("----\n"); sectionMarker = []byte("----\n")
preStart = []byte("<pre>"); preStart = []byte("<pre>")
preEnd = []byte("</pre>\n"); preEnd = []byte("</pre>\n")
pp = []byte("<p>\n"); pp = []byte("<p>\n")
); )
func main() { func main() {
read(); read()
headings(); headings()
paragraphs(); paragraphs()
coalesce(preStart, foldPre); coalesce(preStart, foldPre)
coalesce(tab, foldTabs); coalesce(tab, foldTabs)
quotes(); quotes()
write(); write()
} }
func read() { func read() {
b := bufio.NewReader(os.Stdin); b := bufio.NewReader(os.Stdin)
for { for {
line, err := b.ReadBytes('\n'); line, err := b.ReadBytes('\n')
if err == os.EOF { if err == os.EOF {
break; break
} }
if err != nil { if err != nil {
log.Exit(err) log.Exit(err)
} }
n := len(lines); n := len(lines)
lines = lines[0:n+1]; lines = lines[0 : n+1]
lines[n] = line; lines[n] = line
} }
} }
func write() { func write() {
b := bufio.NewWriter(os.Stdout); b := bufio.NewWriter(os.Stdout)
for _, line := range lines { for _, line := range lines {
b.Write(expandTabs(line)); b.Write(expandTabs(line))
} }
b.Flush(); b.Flush()
} }
// each time prefix is found on a line, call fold and replace // each time prefix is found on a line, call fold and replace
// line with return value from fold. // line with return value from fold.
func coalesce(prefix []byte, fold func(i int) (n int, line []byte)) { func coalesce(prefix []byte, fold func(i int) (n int, line []byte)) {
j := 0; // output line number; goes up by one each loop j := 0 // output line number goes up by one each loop
for i := 0; i < len(lines); { for i := 0; i < len(lines); {
if bytes.HasPrefix(lines[i], prefix) { if bytes.HasPrefix(lines[i], prefix) {
nlines, block := fold(i); nlines, block := fold(i)
lines[j] = block; lines[j] = block
i += nlines; i += nlines
} else { } else {
lines[j] = lines[i]; lines[j] = lines[i]
i++; i++
} }
j++; j++
} }
lines = lines[0:j]; lines = lines[0:j]
} }
// return the <pre> block as a single slice // return the <pre> block as a single slice
func foldPre(i int) (n int, line []byte) { func foldPre(i int) (n int, line []byte) {
buf := new(bytes.Buffer); buf := new(bytes.Buffer)
for i < len(lines) { for i < len(lines) {
buf.Write(lines[i]); buf.Write(lines[i])
n++; n++
if bytes.Equal(lines[i], preEnd) { if bytes.Equal(lines[i], preEnd) {
break break
} }
i++; i++
} }
return n, buf.Bytes(); return n, buf.Bytes()
} }
// return the tab-indented block as a single <pre>-bounded slice // return the tab-indented block as a single <pre>-bounded slice
func foldTabs(i int) (n int, line []byte) { func foldTabs(i int) (n int, line []byte) {
buf := new(bytes.Buffer); buf := new(bytes.Buffer)
buf.WriteString("<pre>\n"); buf.WriteString("<pre>\n")
for i < len(lines) { for i < len(lines) {
if !bytes.HasPrefix(lines[i], tab) { if !bytes.HasPrefix(lines[i], tab) {
break; break
} }
buf.Write(lines[i]); buf.Write(lines[i])
n++; n++
i++; i++
} }
buf.WriteString("</pre>\n"); buf.WriteString("</pre>\n")
return n, buf.Bytes(); return n, buf.Bytes()
} }
func headings() { func headings() {
b := bufio.NewWriter(os.Stdout); b := bufio.NewWriter(os.Stdout)
for i, l := range lines { for i, l := range lines {
if i > 0 && bytes.Equal(l, sectionMarker) { if i > 0 && bytes.Equal(l, sectionMarker) {
lines[i-1] = []byte("<h2>" + string(trim(lines[i-1])) + "</h2>\n"); lines[i-1] = []byte("<h2>" + string(trim(lines[i-1])) + "</h2>\n")
lines[i] = empty; lines[i] = empty
} }
} }
b.Flush(); b.Flush()
} }
func paragraphs() { func paragraphs() {
for i, l := range lines { for i, l := range lines {
if bytes.Equal(l, newline) { if bytes.Equal(l, newline) {
lines[i] = pp; lines[i] = pp
} }
} }
} }
func quotes() { func quotes() {
for i, l := range lines { for i, l := range lines {
lines[i] = codeQuotes(l); lines[i] = codeQuotes(l)
} }
} }
...@@ -143,12 +143,12 @@ func codeQuotes(l []byte) []byte { ...@@ -143,12 +143,12 @@ func codeQuotes(l []byte) []byte {
if bytes.HasPrefix(l, preStart) { if bytes.HasPrefix(l, preStart) {
return l return l
} }
n := bytes.Index(l, quote); n := bytes.Index(l, quote)
if n < 0 { if n < 0 {
return l return l
} }
buf := new(bytes.Buffer); buf := new(bytes.Buffer)
inQuote := false; inQuote := false
for _, c := range l { for _, c := range l {
if c == '"' { if c == '"' {
if inQuote { if inQuote {
...@@ -161,31 +161,31 @@ func codeQuotes(l []byte) []byte { ...@@ -161,31 +161,31 @@ func codeQuotes(l []byte) []byte {
buf.WriteByte(c) buf.WriteByte(c)
} }
} }
return buf.Bytes(); return buf.Bytes()
} }
// drop trailing newline // drop trailing newline
func trim(l []byte) []byte { func trim(l []byte) []byte {
n := len(l); n := len(l)
if n > 0 && l[n-1] == '\n' { if n > 0 && l[n-1] == '\n' {
return l[0:n-1] return l[0 : n-1]
} }
return l return l
} }
// expand tabs to 4 spaces. don't worry about columns. // expand tabs to 4 spaces. don't worry about columns.
func expandTabs(l []byte) []byte { func expandTabs(l []byte) []byte {
j := 0; // position in linebuf. j := 0 // position in linebuf.
for _, c := range l { for _, c := range l {
if c == '\t' { if c == '\t' {
for k := 0; k < 4; k++ { for k := 0; k < 4; k++ {
linebuf[j] = ' '; linebuf[j] = ' '
j++; j++
} }
} else { } else {
linebuf[j] = c; linebuf[j] = c
j++; j++
} }
} }
return linebuf[0:j]; return linebuf[0:j]
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment