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 @@
package main
import (
"bufio";
"bytes";
"log";
"os";
"bufio"
"bytes"
"log"
"os"
)
var (
lines = make([][]byte, 0, 10000); // assume big enough
linebuf = make([]byte, 10000); // assume big enough
empty = []byte("");
newline = []byte("\n");
tab = []byte("\t");
quote = []byte(`"`);
sectionMarker = []byte("----\n");
preStart = []byte("<pre>");
preEnd = []byte("</pre>\n");
pp = []byte("<p>\n");
);
lines = make([][]byte, 0, 10000) // assume big enough
linebuf = make([]byte, 10000) // assume big enough
empty = []byte("")
newline = []byte("\n")
tab = []byte("\t")
quote = []byte(`"`)
sectionMarker = []byte("----\n")
preStart = []byte("<pre>")
preEnd = []byte("</pre>\n")
pp = []byte("<p>\n")
)
func main() {
read();
headings();
paragraphs();
coalesce(preStart, foldPre);
coalesce(tab, foldTabs);
quotes();
write();
read()
headings()
paragraphs()
coalesce(preStart, foldPre)
coalesce(tab, foldTabs)
quotes()
write()
}
func read() {
b := bufio.NewReader(os.Stdin);
b := bufio.NewReader(os.Stdin)
for {
line, err := b.ReadBytes('\n');
line, err := b.ReadBytes('\n')
if err == os.EOF {
break;
break
}
if err != nil {
log.Exit(err)
}
n := len(lines);
lines = lines[0:n+1];
lines[n] = line;
n := len(lines)
lines = lines[0 : n+1]
lines[n] = line
}
}
func write() {
b := bufio.NewWriter(os.Stdout);
b := bufio.NewWriter(os.Stdout)
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
// line with return value from fold.
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); {
if bytes.HasPrefix(lines[i], prefix) {
nlines, block := fold(i);
lines[j] = block;
i += nlines;
nlines, block := fold(i)
lines[j] = block
i += nlines
} else {
lines[j] = lines[i];
i++;
lines[j] = lines[i]
i++
}
j++;
j++
}
lines = lines[0:j];
lines = lines[0:j]
}
// return the <pre> block as a single slice
func foldPre(i int) (n int, line []byte) {
buf := new(bytes.Buffer);
buf := new(bytes.Buffer)
for i < len(lines) {
buf.Write(lines[i]);
n++;
buf.Write(lines[i])
n++
if bytes.Equal(lines[i], preEnd) {
break
}
i++;
i++
}
return n, buf.Bytes();
return n, buf.Bytes()
}
// return the tab-indented block as a single <pre>-bounded slice
func foldTabs(i int) (n int, line []byte) {
buf := new(bytes.Buffer);
buf.WriteString("<pre>\n");
buf := new(bytes.Buffer)
buf.WriteString("<pre>\n")
for i < len(lines) {
if !bytes.HasPrefix(lines[i], tab) {
break;
break
}
buf.Write(lines[i]);
n++;
i++;
buf.Write(lines[i])
n++
i++
}
buf.WriteString("</pre>\n");
return n, buf.Bytes();
buf.WriteString("</pre>\n")
return n, buf.Bytes()
}
func headings() {
b := bufio.NewWriter(os.Stdout);
b := bufio.NewWriter(os.Stdout)
for i, l := range lines {
if i > 0 && bytes.Equal(l, sectionMarker) {
lines[i-1] = []byte("<h2>" + string(trim(lines[i-1])) + "</h2>\n");
lines[i] = empty;
lines[i-1] = []byte("<h2>" + string(trim(lines[i-1])) + "</h2>\n")
lines[i] = empty
}
}
b.Flush();
b.Flush()
}
func paragraphs() {
for i, l := range lines {
if bytes.Equal(l, newline) {
lines[i] = pp;
lines[i] = pp
}
}
}
func quotes() {
for i, l := range lines {
lines[i] = codeQuotes(l);
lines[i] = codeQuotes(l)
}
}
......@@ -143,12 +143,12 @@ func codeQuotes(l []byte) []byte {
if bytes.HasPrefix(l, preStart) {
return l
}
n := bytes.Index(l, quote);
n := bytes.Index(l, quote)
if n < 0 {
return l
}
buf := new(bytes.Buffer);
inQuote := false;
buf := new(bytes.Buffer)
inQuote := false
for _, c := range l {
if c == '"' {
if inQuote {
......@@ -161,31 +161,31 @@ func codeQuotes(l []byte) []byte {
buf.WriteByte(c)
}
}
return buf.Bytes();
return buf.Bytes()
}
// drop trailing newline
func trim(l []byte) []byte {
n := len(l);
n := len(l)
if n > 0 && l[n-1] == '\n' {
return l[0:n-1]
return l[0 : n-1]
}
return l
}
// expand tabs to 4 spaces. don't worry about columns.
func expandTabs(l []byte) []byte {
j := 0; // position in linebuf.
j := 0 // position in linebuf.
for _, c := range l {
if c == '\t' {
for k := 0; k < 4; k++ {
linebuf[j] = ' ';
j++;
linebuf[j] = ' '
j++
}
} else {
linebuf[j] = c;
j++;
linebuf[j] = c
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