refactoring paragraph rendering

This commit is contained in:
Russ Ross 2011-06-25 15:18:34 -06:00
parent eff64c563f
commit 812e8d0185
2 changed files with 18 additions and 28 deletions

View File

@ -1035,17 +1035,23 @@ func blockListItem(out *bytes.Buffer, rndr *render, data []byte, flags *int) int
// render a single paragraph that has already been parsed out
func renderParagraph(out *bytes.Buffer, rndr *render, data []byte) {
// trim trailing newlines
// trim leading whitespace
beg := 0
for beg < len(data) && isspace(data[beg]) {
beg++
}
// trim trailing whitespace
end := len(data)
for end > 0 && data[end-1] == '\n' {
for end > beg && isspace(data[end-1]) {
end--
}
if end == 0 || rndr.mk.Paragraph == nil {
if end == beg || rndr.mk.Paragraph == nil {
return
}
var work bytes.Buffer
parseInline(&work, rndr, data[:end])
parseInline(&work, rndr, data[beg:end])
rndr.mk.Paragraph(out, work.Bytes(), rndr.mk.Opaque)
}

32
html.go
View File

@ -388,46 +388,30 @@ func htmlListItem(out *bytes.Buffer, text []byte, flags int, opaque interface{})
func htmlParagraph(out *bytes.Buffer, text []byte, opaque interface{}) {
options := opaque.(*htmlOptions)
i := 0
if out.Len() > 0 {
out.WriteByte('\n')
}
if len(text) == 0 {
return
}
for i < len(text) && isspace(text[i]) {
i++
}
if i == len(text) {
return
}
out.WriteString("<p>")
if options.flags&HTML_HARD_WRAP != 0 {
for i < len(text) {
org := i
for i < len(text) && text[i] != '\n' {
i++
org := 0
for i := 0; i < len(text); i++ {
if text[i] != '\n' {
continue
}
if i > org {
out.Write(text[org:i])
}
org = i
if i >= len(text) {
break
}
out.WriteString("<br>")
out.WriteString("<br")
out.WriteString(options.closeTag)
i++
}
out.Write(text[org:])
} else {
out.Write(text[i:])
out.Write(text)
}
out.WriteString("</p>\n")
}