1
0
mirror of https://github.com/danog/blackfriday.git synced 2024-11-26 20:14:43 +01:00

fixed ordered lists

This commit is contained in:
Russ Ross 2011-05-25 16:00:01 -06:00
parent f565f32504
commit cb7b546677

View File

@ -144,6 +144,10 @@ func parse_block(ob *bytes.Buffer, rndr *render, data []byte) {
data = data[parse_list(ob, rndr, data, 0):] data = data[parse_list(ob, rndr, data, 0):]
continue continue
} }
if prefix_oli(data) > 0 {
data = data[parse_list(ob, rndr, data, MKD_LIST_ORDERED):]
continue
}
data = data[parse_paragraph(ob, rndr, data):] data = data[parse_paragraph(ob, rndr, data):]
} }
@ -194,35 +198,37 @@ func parse_atxheader(ob *bytes.Buffer, rndr *render, data []byte) int {
} }
func is_headerline(data []byte) int { func is_headerline(data []byte) int {
i := 0 i := 0
// test of level 1 header // test of level 1 header
if data[i] == '=' { if data[i] == '=' {
for i = 1; i < len(data) && data[i] == '='; i++ {} for i = 1; i < len(data) && data[i] == '='; i++ {
for i < len(data) && (data[i] == ' ' || data[i] == '\t') { }
i++ for i < len(data) && (data[i] == ' ' || data[i] == '\t') {
} i++
if i >= len(data) || data[i] == '\n' { }
return 1 if i >= len(data) || data[i] == '\n' {
} else { return 1
return 0 } else {
} return 0
} }
}
// test of level 2 header // test of level 2 header
if data[i] == '-' { if data[i] == '-' {
for i = 1; i < len(data) && data[i] == '-'; i++ {} for i = 1; i < len(data) && data[i] == '-'; i++ {
for i < len(data) && (data[i] == ' ' || data[i] == '\t') { }
i++ for i < len(data) && (data[i] == ' ' || data[i] == '\t') {
} i++
if i >= len(data) || data[i] == '\n' { }
return 2 if i >= len(data) || data[i] == '\n' {
} else { return 2
return 0 } else {
} return 0
} }
}
return 0 return 0
} }
func parse_htmlblock(ob *bytes.Buffer, rndr *render, data []byte, do_render bool) int { func parse_htmlblock(ob *bytes.Buffer, rndr *render, data []byte, do_render bool) int {
@ -867,7 +873,7 @@ func prefix_oli(data []byte) int {
func parse_list(ob *bytes.Buffer, rndr *render, data []byte, flags int) int { func parse_list(ob *bytes.Buffer, rndr *render, data []byte, flags int) int {
work := bytes.NewBuffer(nil) work := bytes.NewBuffer(nil)
i, j, flags := 0, 0, 0 i, j := 0, 0
for i < len(data) { for i < len(data) {
j, flags = parse_listitem(work, rndr, data[i:], flags) j, flags = parse_listitem(work, rndr, data[i:], flags)
i += j i += j
@ -1013,83 +1019,84 @@ func parse_listitem(ob *bytes.Buffer, rndr *render, data []byte, flags_in int) (
} }
func parse_paragraph(ob *bytes.Buffer, rndr *render, data []byte) int { func parse_paragraph(ob *bytes.Buffer, rndr *render, data []byte) int {
i, end, level := 0, 0, 0 i, end, level := 0, 0, 0
for i < len(data) { for i < len(data) {
for end = i + 1; end < len(data) && data[end-1] != '\n'; end++ {} for end = i + 1; end < len(data) && data[end-1] != '\n'; end++ {
}
if is_empty(data[i:]) > 0 { if is_empty(data[i:]) > 0 {
break break
} }
if level = is_headerline(data[i:]); level > 0 { if level = is_headerline(data[i:]); level > 0 {
break break
} }
if rndr.ext_flags & MKDEXT_LAX_HTML_BLOCKS != 0 { if rndr.ext_flags&MKDEXT_LAX_HTML_BLOCKS != 0 {
if data[i] == '<' && rndr.mk.blockhtml != nil && parse_htmlblock(ob, rndr, data[i:], false) > 0 { if data[i] == '<' && rndr.mk.blockhtml != nil && parse_htmlblock(ob, rndr, data[i:], false) > 0 {
end = i end = i
break break
} }
} }
if is_atxheader(rndr, data[i:]) || is_hrule(data[i:]) { if is_atxheader(rndr, data[i:]) || is_hrule(data[i:]) {
end = i end = i
break break
} }
i = end i = end
} }
work := data work := data
size := i size := i
for size > 0 && work[size-1] == '\n' { for size > 0 && work[size-1] == '\n' {
size-- size--
} }
if level == 0 {
tmp := bytes.NewBuffer(nil)
parse_inline(tmp, rndr, work[:size])
if rndr.mk.paragraph != nil {
rndr.mk.paragraph(ob, tmp.Bytes(), rndr.mk.opaque)
}
} else {
if size > 0 {
beg := 0
i = size
size--
for size > 0 && work[size] != '\n' { if level == 0 {
size-- tmp := bytes.NewBuffer(nil)
} parse_inline(tmp, rndr, work[:size])
if rndr.mk.paragraph != nil {
rndr.mk.paragraph(ob, tmp.Bytes(), rndr.mk.opaque)
}
} else {
if size > 0 {
beg := 0
i = size
size--
beg = size + 1 for size > 0 && work[size] != '\n' {
for size > 0 && work[size-1] == '\n' { size--
size-- }
}
if size > 0 { beg = size + 1
tmp := bytes.NewBuffer(nil) for size > 0 && work[size-1] == '\n' {
parse_inline(tmp, rndr, work[:size]) size--
if rndr.mk.paragraph != nil { }
rndr.mk.paragraph(ob, tmp.Bytes(), rndr.mk.opaque)
}
work = work[beg:] if size > 0 {
size = i - beg tmp := bytes.NewBuffer(nil)
} else { parse_inline(tmp, rndr, work[:size])
size = i if rndr.mk.paragraph != nil {
} rndr.mk.paragraph(ob, tmp.Bytes(), rndr.mk.opaque)
} }
header_work := bytes.NewBuffer(nil) work = work[beg:]
parse_inline(header_work, rndr, work[:size]) size = i - beg
} else {
size = i
}
}
if rndr.mk.header != nil { header_work := bytes.NewBuffer(nil)
rndr.mk.header(ob, header_work.Bytes(), level, rndr.mk.opaque) parse_inline(header_work, rndr, work[:size])
}
}
return end if rndr.mk.header != nil {
rndr.mk.header(ob, header_work.Bytes(), level, rndr.mk.opaque)
}
}
return end
} }
@ -1282,48 +1289,48 @@ func rndr_listitem(ob *bytes.Buffer, text []byte, flags int, opaque interface{})
func rndr_paragraph(ob *bytes.Buffer, text []byte, opaque interface{}) { func rndr_paragraph(ob *bytes.Buffer, text []byte, opaque interface{}) {
options := opaque.(*html_renderopts) options := opaque.(*html_renderopts)
i := 0 i := 0
if ob.Len() > 0 { if ob.Len() > 0 {
ob.WriteByte('\n') ob.WriteByte('\n')
} }
if len(text) == 0 { if len(text) == 0 {
return return
} }
for i < len(text) && unicode.IsSpace(int(text[i])) { for i < len(text) && unicode.IsSpace(int(text[i])) {
i++ i++
} }
if i == len(text) { if i == len(text) {
return return
} }
ob.WriteString("<p>") ob.WriteString("<p>")
if options.flags & HTML_HARD_WRAP != 0 { if options.flags&HTML_HARD_WRAP != 0 {
for i < len(text) { for i < len(text) {
org := i org := i
for i < len(text) && text[i] != '\n' { for i < len(text) && text[i] != '\n' {
i++ i++
} }
if i > org { if i > org {
ob.Write(text[org:i]) ob.Write(text[org:i])
} }
if i >= len(text) { if i >= len(text) {
break break
} }
ob.WriteString("<br>") ob.WriteString("<br>")
ob.WriteString(options.close_tag) ob.WriteString(options.close_tag)
i++ i++
} }
} else { } else {
ob.Write(text[i:]) ob.Write(text[i:])
} }
ob.WriteString("</p>\n") ob.WriteString("</p>\n")
} }
@ -1403,7 +1410,7 @@ func main() {
rndrer.hrule = rndr_hrule rndrer.hrule = rndr_hrule
rndrer.list = rndr_list rndrer.list = rndr_list
rndrer.listitem = rndr_listitem rndrer.listitem = rndr_listitem
rndrer.paragraph = rndr_paragraph rndrer.paragraph = rndr_paragraph
rndrer.table = rndr_table rndrer.table = rndr_table
rndrer.table_row = rndr_tablerow rndrer.table_row = rndr_tablerow
rndrer.table_cell = rndr_tablecell rndrer.table_cell = rndr_tablecell