1
0
mirror of https://github.com/danog/blackfriday.git synced 2024-12-02 09:27:49 +01:00

new tests pass but old tests now fail...

This commit is contained in:
moshee 2013-06-26 15:57:51 +00:00
parent be082a1ef2
commit 7bdb82c53a
6 changed files with 120 additions and 60 deletions

View File

@ -459,7 +459,7 @@ func (p *parser) isEmpty(data []byte) int {
var i int var i int
for i = 0; data[i] != '\n'; i++ { for i = 0; data[i] != '\n'; i++ {
if data[i] != ' ' { if data[i] != ' ' || data[i] != '\t' {
return 0 return 0
} }
} }

22
html.go
View File

@ -322,20 +322,24 @@ func (options *Html) TableCell(out *bytes.Buffer, text []byte, align int) {
out.WriteString("</td>") out.WriteString("</td>")
} }
func (options *Html) Footnotes(out *bytes.Buffer, p *parser) { func (options *Html) Footnotes(out *bytes.Buffer, text func() bool) {
out.WriteString("<div class=\"footnotes\">\n") out.WriteString("<div class=\"footnotes\">\n")
options.HRule(out) options.HRule(out)
options.List(out, func() bool { options.List(out, text, LIST_TYPE_ORDERED)
for _, ref := range p.notes {
out.WriteString("<li>\n")
out.Write(ref.title)
out.WriteString("</li>\n")
}
return true
}, LIST_TYPE_ORDERED)
out.WriteString("</div>\n") out.WriteString("</div>\n")
} }
func (options *Html) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {
if flags&LIST_ITEM_CONTAINS_BLOCK != 0 || flags&LIST_ITEM_BEGINNING_OF_LIST != 0 {
doubleSpace(out)
}
out.WriteString(`<li class="`)
out.Write(slugify(name))
out.WriteString(`">`)
out.Write(text)
out.WriteString("</li>\n")
}
func (options *Html) List(out *bytes.Buffer, text func() bool, flags int) { func (options *Html) List(out *bytes.Buffer, text func() bool, flags int) {
marker := out.Len() marker := out.Len()
doubleSpace(out) doubleSpace(out)

View File

@ -193,7 +193,7 @@ func link(p *parser, out *bytes.Buffer, data []byte, offset int) int {
if offset > 0 && data[offset-1] == '!' { if offset > 0 && data[offset-1] == '!' {
t = linkImg t = linkImg
} else if p.flags&EXTENSION_FOOTNOTES != 0 { } else if p.flags&EXTENSION_FOOTNOTES != 0 {
if len(data) > offset && data[offset+1] == '^' { if len(data)-1 > offset && data[offset+1] == '^' {
t = linkDeferredFootnote t = linkDeferredFootnote
} }
} }

View File

@ -35,12 +35,13 @@ func doTestsInline(t *testing.T, tests []string) {
func doTestsInlineParam(t *testing.T, tests []string, extensions, htmlFlags int) { func doTestsInlineParam(t *testing.T, tests []string, extensions, htmlFlags int) {
// catch and report panics // catch and report panics
var candidate string var candidate string
defer func() { /*
if err := recover(); err != nil { defer func() {
panic(err) if err := recover(); err != nil {
t.Errorf("\npanic while processing [%#v] (%v)\n", candidate, err) t.Errorf("\npanic while processing [%#v] (%v)\n", candidate, err)
} }
}() }()
*/
for i := 0; i+1 < len(tests); i += 2 { for i := 0; i+1 < len(tests); i += 2 {
input := tests[i] input := tests[i]
@ -506,25 +507,55 @@ func TestAutoLink(t *testing.T) {
func TestFootnotes(t *testing.T) { func TestFootnotes(t *testing.T) {
tests := []string{ tests := []string{
"testing footnotes.[^a]\n\n[^a]: This is the note\n", "testing footnotes.[^a]\n\n[^a]: This is the note\n",
"", `<p>testing footnotes.<sup class="footnote-ref" id="fnref:a"><a rel="footnote" href="#fn:a">1</a></sup></p>
<div class="footnotes">
<hr />
<ol>
<li class="a">This is the note
</li>
</ol>
</div>
`,
`testing long[^b] notes. `testing long[^b] notes.
[^b]: Paragraph 1 [^b]: Paragraph 1
Paragraph 2 Paragraph 2
` + "```\n\tsome code\n\t```" + ` ` + "```\n\tsome code\n\t```" + `
Paragraph 3 Paragraph 3
No longer in the footnote No longer in the footnote
`, `,
"", `<p>testing long<sup class="footnote-ref" id="fnref:b"><a rel="footnote" href="#fn:b">1</a></sup> notes.</p>
<p>No longer in the footnote</p>
<div class="footnotes">
<hr />
<ol>
<li class="b"><p>Paragraph 1</p>
<p>Paragraph 2</p>
<p><code>
some code
</code></p>
<p>Paragraph 3</p>
</li>
</ol>
</div>
`,
`testing[^c] multiple[^d] notes. `testing[^c] multiple[^d] notes.
[^c]: this is note c [^c]: this is [note] c
omg omg
@ -532,15 +563,28 @@ omg
[^d]: this is note d [^d]: this is note d
what happens here what happens here
[note]: /link/c
`,
`<p>testing<sup class="footnote-ref" id="fnref:c"><a rel="footnote" href="#fn:c">1</a></sup> multiple<sup class="footnote-ref" id="fnref:d"><a rel="footnote" href="#fn:d">2</a></sup> notes.</p>
<p>omg</p>
<p>what happens here</p>
<div class="footnotes">
<hr />
<ol>
<li class="c">this is <a href="/link/c">note</a> c
</li>
<li class="d">this is note d
</li>
</ol>
</div>
`, `,
"",
} }
for _, test := range tests { doTestsInlineParam(t, tests, EXTENSION_FOOTNOTES, 0)
if len(test) > 0 {
t.Errorf("Output:\n%s\n", runMarkdownInline(test, EXTENSION_FOOTNOTES, 0))
}
}
//doTestsInlineParam(t, tests, EXTENSION_FOOTNOTES, 0)
} }

View File

@ -159,7 +159,11 @@ func (options *Latex) TableCell(out *bytes.Buffer, text []byte, align int) {
} }
// TODO: this // TODO: this
func (options *Latex) Footnotes(out *bytes.Buffer, p *parser) { func (options *Latex) Footnotes(out *bytes.Buffer, text func() bool) {
}
func (options *Latex) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) {
} }

View File

@ -140,7 +140,8 @@ type Renderer interface {
Table(out *bytes.Buffer, header []byte, body []byte, columnData []int) Table(out *bytes.Buffer, header []byte, body []byte, columnData []int)
TableRow(out *bytes.Buffer, text []byte) TableRow(out *bytes.Buffer, text []byte)
TableCell(out *bytes.Buffer, text []byte, flags int) TableCell(out *bytes.Buffer, text []byte, flags int)
Footnotes(out *bytes.Buffer, p *parser) Footnotes(out *bytes.Buffer, text func() bool)
FootnoteItem(out *bytes.Buffer, name, text []byte, flags int)
// Span-level callbacks // Span-level callbacks
AutoLink(out *bytes.Buffer, link []byte, kind int) AutoLink(out *bytes.Buffer, link []byte, kind int)
@ -344,10 +345,23 @@ func secondPass(p *parser, input []byte) []byte {
p.r.DocumentHeader(&output) p.r.DocumentHeader(&output)
p.block(&output, input) p.block(&output, input)
// NOTE: this is a big hack because we need the parser again for the
// footnotes, so this can't really go in the public interface
if p.flags&EXTENSION_FOOTNOTES != 0 && len(p.notes) > 0 { if p.flags&EXTENSION_FOOTNOTES != 0 && len(p.notes) > 0 {
p.r.Footnotes(&output, p) p.r.Footnotes(&output, func() bool {
flags := LIST_ITEM_BEGINNING_OF_LIST
for _, ref := range p.notes {
var buf bytes.Buffer
if ref.hasBlock {
flags |= LIST_ITEM_CONTAINS_BLOCK
p.block(&buf, ref.title)
} else {
p.inline(&buf, ref.title)
}
p.r.FootnoteItem(&output, ref.link, buf.Bytes(), flags)
flags &^= LIST_ITEM_BEGINNING_OF_LIST | LIST_ITEM_CONTAINS_BLOCK
}
return true
})
} }
p.r.DocumentFooter(&output) p.r.DocumentFooter(&output)
@ -390,9 +404,10 @@ func secondPass(p *parser, input []byte) []byte {
// References are parsed and stored in this struct. // References are parsed and stored in this struct.
type reference struct { type reference struct {
link []byte link []byte
title []byte title []byte
noteId int // 0 if not a footnote ref noteId int // 0 if not a footnote ref
hasBlock bool
} }
// Check whether or not data starts with a reference link. // Check whether or not data starts with a reference link.
@ -401,7 +416,8 @@ type reference struct {
// Returns the number of bytes to skip to move past it, // Returns the number of bytes to skip to move past it,
// or zero if the first line is not a reference. // or zero if the first line is not a reference.
func isReference(p *parser, data []byte, tabSize int) int { func isReference(p *parser, data []byte, tabSize int) int {
println("[", string(data), "]") if len(data) >= 5 {
}
// up to 3 optional leading spaces // up to 3 optional leading spaces
if len(data) < 4 { if len(data) < 4 {
return 0 return 0
@ -434,7 +450,6 @@ func isReference(p *parser, data []byte, tabSize int) int {
idEnd := i idEnd := i
// spacer: colon (space | tab)* newline? (space | tab)* // spacer: colon (space | tab)* newline? (space | tab)*
// /:[ \t]*\n?[ \t]*/
i++ i++
if i >= len(data) || data[i] != ':' { if i >= len(data) || data[i] != ':' {
return 0 return 0
@ -461,11 +476,12 @@ func isReference(p *parser, data []byte, tabSize int) int {
titleOffset, titleEnd int titleOffset, titleEnd int
lineEnd int lineEnd int
raw []byte raw []byte
hasBlock bool
) )
if p.flags&EXTENSION_FOOTNOTES != 0 && noteId > 0 { if p.flags&EXTENSION_FOOTNOTES != 0 && noteId > 0 {
linkOffset, linkEnd, raw = scanFootnote(p, data, i, tabSize) linkOffset, linkEnd, raw, hasBlock = scanFootnote(p, data, i, tabSize)
lineEnd = linkEnd + linkOffset lineEnd = linkEnd
} else { } else {
linkOffset, linkEnd, titleOffset, titleEnd, lineEnd = scanLinkRef(p, data, i) linkOffset, linkEnd, titleOffset, titleEnd, lineEnd = scanLinkRef(p, data, i)
} }
@ -476,11 +492,12 @@ func isReference(p *parser, data []byte, tabSize int) int {
// a valid ref has been found // a valid ref has been found
ref := &reference{ ref := &reference{
noteId: noteId, noteId: noteId,
hasBlock: hasBlock,
} }
if noteId > 0 { if noteId > 0 {
// reusing the link field for the id since footnotes don't have titles // reusing the link field for the id since footnotes don't have links
ref.link = data[idOffset:idEnd] ref.link = data[idOffset:idEnd]
// if footnote, it's not really a title, it's the contained text // if footnote, it's not really a title, it's the contained text
ref.title = raw ref.title = raw
@ -570,7 +587,7 @@ func scanLinkRef(p *parser, data []byte, i int) (linkOffset, linkEnd, titleOffse
// blockEnd is the end of the section in the input buffer, and contents is the // blockEnd is the end of the section in the input buffer, and contents is the
// extracted text that was shifted over one tab. It will need to be rendered at // extracted text that was shifted over one tab. It will need to be rendered at
// the end of the document. // the end of the document.
func scanFootnote(p *parser, data []byte, i, indentSize int) (blockStart, blockEnd int, contents []byte) { func scanFootnote(p *parser, data []byte, i, indentSize int) (blockStart, blockEnd int, contents []byte, hasBlock bool) {
if i == 0 { if i == 0 {
return return
} }
@ -584,10 +601,7 @@ func scanFootnote(p *parser, data []byte, i, indentSize int) (blockStart, blockE
// find the end of the line // find the end of the line
blockEnd = i blockEnd = i
for data[i-1] != '\n' { for i < len(data) && data[i-1] != '\n' {
if i >= len(data) {
return
}
i++ i++
} }
@ -600,14 +614,13 @@ func scanFootnote(p *parser, data []byte, i, indentSize int) (blockStart, blockE
// process the following lines // process the following lines
containsBlankLine := false containsBlankLine := false
hasBlock := false
gatherLines: gatherLines:
for blockEnd < len(data) { for blockEnd < len(data) {
i++ i++
// find the end of this line // find the end of this line
for data[i-1] != '\n' { for i < len(data) && data[i-1] != '\n' {
i++ i++
} }
@ -628,27 +641,22 @@ gatherLines:
// if there were blank lines before this one, insert a new one now // if there were blank lines before this one, insert a new one now
if containsBlankLine { if containsBlankLine {
hasBlock = true
raw.WriteByte('\n') raw.WriteByte('\n')
containsBlankLine = false containsBlankLine = false
} }
// get rid of that first tab, write to buffer // get rid of that first tab, write to buffer
raw.Write(data[blockEnd+n : i]) raw.Write(data[blockEnd+n : i])
hasBlock = true
blockEnd = i blockEnd = i
} }
rawBytes := raw.Bytes() if data[blockEnd-1] != '\n' {
println("raw: {" + string(raw.Bytes()) + "}") raw.WriteByte('\n')
buf := new(bytes.Buffer)
if hasBlock {
p.block(buf, rawBytes)
} else {
p.inline(buf, rawBytes)
} }
contents = buf.Bytes()
contents = raw.Bytes()
return return
} }