Reputation: 55263
This is a sample code from the Go blackfriday package:
package main
import (
"bytes"
"fmt"
"github.com/russross/blackfriday"
)
func main() {
input := []byte(`##Title
- another paragragh
This is a being rendered in a custom way.
`)
htmlFlags := 0
renderer := &renderer{Html: blackfriday.HtmlRenderer(htmlFlags, "", "").(*blackfriday.Html)}
extensions := 0
unsanitized := blackfriday.Markdown(input, renderer, extensions)
os.Stdout.Write(unsanitized)
}
// renderer implements blackfriday.Renderer and reuses blackfriday.Html for the most part,
// except overriding Link rendering.
type renderer struct {
*blackfriday.Html
}
func (options *renderer) Header(out *bytes.Buffer, text func() bool, level int, id string) {
fmt.Fprintf(out, "<custom link %q to %q>", content, link)
}
The purpose of the code is to customize the link
attributes of the output HTML.
I tried to do the same but with p
tags:
func (options *renderer) Paragraph(out *bytes.Buffer, text func() bool) {
fmt.Fprintf(out, "<p class='custom'>%q</p>", text)
}
But the output was this:
<h1>Title</h1>
<ul>
<li>another paragragh</li>
</ul>
<p class='custom'>%!q(func() bool=0x80a15d0)</p>
So I have no idea how to output the actual text (This is a being rendered in a custom way.
). Any ideas?
This is the source code of the function:
func (options *Html) Paragraph(out *bytes.Buffer, text func() bool) {
marker := out.Len()
doubleSpace(out)
out.WriteString("<p>")
if !text() {
out.Truncate(marker)
return
}
out.WriteString("</p>\n")
}
Upvotes: 0
Views: 101
Reputation: 270
I think your usage of function is wrong.
Take a look at Html.Paragraph
and override this.
Maybe it would look like:
func (options *renderer) Paragraph(out *bytes.Buffer, text func() bool) {
marker := out.Len()
doubleSpace(out)
out.WriteString("<p class='custom'>")
if !text() {
out.Truncate(marker)
return
}
out.WriteString("</p>\n")
}
Upvotes: 1
Reputation: 131968
I believe all you need to do is call text
func (options *renderer) Paragraph(out *bytes.Buffer, text func() bool) {
fmt.Fprintf(out, "<p class='custom'>%q</p>\n", text())
}
Upvotes: 1