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 // render a single paragraph that has already been parsed out
func renderParagraph(out *bytes.Buffer, rndr *render, data []byte) { 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) end := len(data)
for end > 0 && data[end-1] == '\n' { for end > beg && isspace(data[end-1]) {
end-- end--
} }
if end == 0 || rndr.mk.Paragraph == nil { if end == beg || rndr.mk.Paragraph == nil {
return return
} }
var work bytes.Buffer var work bytes.Buffer
parseInline(&work, rndr, data[:end]) parseInline(&work, rndr, data[beg:end])
rndr.mk.Paragraph(out, work.Bytes(), rndr.mk.Opaque) 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{}) { func htmlParagraph(out *bytes.Buffer, text []byte, opaque interface{}) {
options := opaque.(*htmlOptions) options := opaque.(*htmlOptions)
i := 0
if out.Len() > 0 { if out.Len() > 0 {
out.WriteByte('\n') out.WriteByte('\n')
} }
if len(text) == 0 {
return
}
for i < len(text) && isspace(text[i]) {
i++
}
if i == len(text) {
return
}
out.WriteString("<p>") out.WriteString("<p>")
if options.flags&HTML_HARD_WRAP != 0 { if options.flags&HTML_HARD_WRAP != 0 {
for i < len(text) { org := 0
org := i for i := 0; i < len(text); i++ {
for i < len(text) && text[i] != '\n' { if text[i] != '\n' {
i++ continue
} }
if i > org { if i > org {
out.Write(text[org:i]) out.Write(text[org:i])
} }
org = i
if i >= len(text) { out.WriteString("<br")
break
}
out.WriteString("<br>")
out.WriteString(options.closeTag) out.WriteString(options.closeTag)
i++
} }
out.Write(text[org:])
} else { } else {
out.Write(text[i:]) out.Write(text)
} }
out.WriteString("</p>\n") out.WriteString("</p>\n")
} }