Merge pull request #98 from cpuguy83/master Add titleblock support
Vytautas Ĺ altenis vytas@rtfb.lt
Mon, 04 Aug 2014 23:02:18 +0300
5 files changed,
66 insertions(+),
5 deletions(-)
M
block.go
→
block.go
@@ -13,9 +13,7 @@ //
package blackfriday -import ( - "bytes" -) +import "bytes" // Parse block-level data. // Note: this function and many that it calls assume that@@ -53,6 +51,20 @@ if data[0] == '<' {
if i := p.html(out, data, true); i > 0 { data = data[i:] continue + } + } + + // title block + // + // % stuff + // % more stuff + // % even more stuff + if p.flags&EXTENSION_TITLEBLOCK != 0 { + if data[0] == '%' { + if i := p.titleBlock(out, data, true); i > 0 { + data = data[i:] + continue + } } }@@ -190,13 +202,13 @@ id := ""
if p.flags&EXTENSION_HEADER_IDS != 0 { j, k := 0, 0 // find start/end of header id - for j = i; j < end - 1 && (data[j] != '{' || data[j+1] != '#'); j++ { + for j = i; j < end-1 && (data[j] != '{' || data[j+1] != '#'); j++ { } for k = j + 1; k < end && data[k] != '}'; k++ { } // extract header id iff found if j < end && k < end { - id = string(data[j+2:k]) + id = string(data[j+2 : k]) end = j skip = k + 1 for end > 0 && data[end-1] == ' ' {@@ -254,6 +266,25 @@ }
} return 0 +} + +func (p *parser) titleBlock(out *bytes.Buffer, data []byte, doRender bool) int { + if data[0] != '%' { + return 0 + } + splitData := bytes.Split(data, []byte("\n")) + var i int + for idx, b := range splitData { + if !bytes.HasPrefix(b, []byte("%")) { + i = idx // - 1 + break + } + } + + data = bytes.Join(splitData[0:i], []byte("\n")) + p.r.TitleBlock(out, data) + + return len(data) } func (p *parser) html(out *bytes.Buffer, data []byte, doRender bool) int {
M
block_test.go
→
block_test.go
@@ -1045,3 +1045,19 @@ "<pre><code>``` oz\n</code></pre>\n\n<p>leading spaces</p>\n\n<pre><code>```\n</code></pre>\n",
} doTestsBlock(t, tests, EXTENSION_FENCED_CODE|EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK) } + +func TestTitleBlock_EXTENSION_TITLEBLOCK(t *testing.T) { + var tests = []string{ + "% Some title\n" + + "% Another title line\n" + + "% Yep, more here too\n", + "<h1 class=\"title\">" + + "Some title\n" + + "Another title line\n" + + "Yep, more here too\n" + + "</h1>", + } + + doTestsBlock(t, tests, EXTENSION_TITLEBLOCK) + +}
M
html.go
→
html.go
@@ -180,6 +180,14 @@ func (options *Html) GetFlags() int {
return options.flags } +func (options *Html) TitleBlock(out *bytes.Buffer, text []byte) { + text = bytes.TrimPrefix(text, []byte("% ")) + text = bytes.Replace(text, []byte("\n% "), []byte("\n"), -1) + out.WriteString("<h1 class=\"title\">") + out.Write(text) + out.WriteString("\n</h1>") +} + func (options *Html) Header(out *bytes.Buffer, text func() bool, level int, id string) { marker := out.Len() doubleSpace(out)
M
markdown.go
→
markdown.go
@@ -40,6 +40,7 @@ EXTENSION_TAB_SIZE_EIGHT // expand tabs to eight spaces instead of four
EXTENSION_FOOTNOTES // Pandoc-style footnotes EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK // No need to insert an empty line to start a (code, quote, order list, unorder list)block EXTENSION_HEADER_IDS // specify header IDs with {#id} + EXTENSION_TITLEBLOCK // Titleblock ala pandoc ) // These are the possible flag values for the link renderer.@@ -145,6 +146,7 @@ TableHeaderCell(out *bytes.Buffer, text []byte, flags int)
TableCell(out *bytes.Buffer, text []byte, flags int) Footnotes(out *bytes.Buffer, text func() bool) FootnoteItem(out *bytes.Buffer, name, text []byte, flags int) + TitleBlock(out *bytes.Buffer, text []byte) // Span-level callbacks AutoLink(out *bytes.Buffer, link []byte, kind int)