Beispiel #1
0
// Write text to w; optionally html-escaped.
func writeText(w io.Writer, text []byte, html bool) {
	if html {
		template.HtmlEscape(w, text)
		return
	}
	w.Write(text)
}
Beispiel #2
0
// Escape comment text for HTML.
// Also, turn `` into “ and '' into ”.
func commentEscape(w io.Writer, s []byte) {
	last := 0
	for i := 0; i < len(s)-1; i++ {
		if s[i] == s[i+1] && (s[i] == '`' || s[i] == '\'') {
			template.HtmlEscape(w, s[last:i])
			last = i + 2
			switch s[i] {
			case '`':
				w.Write(ldquo)
			case '\'':
				w.Write(rdquo)
			}
			i++ // loop will add one more
		}
	}
	template.HtmlEscape(w, s[last:len(s)])
}
Beispiel #3
0
// Convert comment text to formatted HTML.
// The comment was prepared by DocReader,
// so it is known not to have leading, trailing blank lines
// nor to have trailing spaces at the end of lines.
// The comment markers have already been removed.
//
// Turn each run of multiple \n into </p><p>
// Turn each run of indented lines into <pre> without indent.
//
// TODO(rsc): I'd like to pass in an array of variable names []string
// and then italicize those strings when they appear as words.
func ToHtml(w io.Writer, s []byte) {
	inpara := false

	/* TODO(rsc): 6g cant generate code for these
	close := func() {
		if inpara {
			w.Write(html_endp);
			inpara = false;
		}
	};
	open := func() {
		if !inpara {
			w.Write(html_p);
			inpara = true;
		}
	};
	*/

	lines := split(s)
	unindent(lines)
	for i := 0; i < len(lines); {
		line := lines[i]
		if isBlank(line) {
			// close paragraph
			if inpara {
				w.Write(html_endp)
				inpara = false
			}
			i++
			continue
		}
		if indentLen(line) > 0 {
			// close paragraph
			if inpara {
				w.Write(html_endp)
				inpara = false
			}

			// count indented or blank lines
			j := i + 1
			for j < len(lines) && (isBlank(lines[j]) || indentLen(lines[j]) > 0) {
				j++
			}
			// but not trailing blank lines
			for j > i && isBlank(lines[j-1]) {
				j--
			}
			block := lines[i:j]
			i = j

			unindent(block)

			// put those lines in a pre block.
			// they don't get the nice text formatting,
			// just html escaping
			w.Write(html_pre)
			for _, line := range block {
				template.HtmlEscape(w, line)
			}
			w.Write(html_endpre)
			continue
		}
		// open paragraph
		if !inpara {
			w.Write(html_p)
			inpara = true
		}
		commentEscape(w, lines[i])
		i++
	}
	if inpara {
		w.Write(html_endp)
		inpara = false
	}
}
Beispiel #4
0
func htmlEscape(s string) string {
	var buf bytes.Buffer
	template.HtmlEscape(&buf, strings.Bytes(s))
	return buf.String()
}