all repos — grayfriday @ 1303ea142722cfacf9aa5cf3edd76f75973789d5

blackfriday fork with a few changes

Cleanup renderer constructors

Move all parameters under HTMLRendererParameters struct, rename
constructors to New{HTML|Latex}Renderer.
Vytautas Ĺ altenis vytas@rtfb.lt
Tue, 05 Apr 2016 14:18:32 +0300
commit

1303ea142722cfacf9aa5cf3edd76f75973789d5

parent

4f8d2881a232c69b580f6191eb84b3d925bc6aa3

4 files changed, 39 insertions(+), 41 deletions(-)

jump to
M html.gohtml.go

@@ -80,16 +80,20 @@ // uniqueness.

HeaderIDPrefix string // If set, add this text to the back of each Header ID, to ensure uniqueness. HeaderIDSuffix string + + Title string // Document title (used if CompletePage is set) + CSS string // Optional CSS file URL (used if CompletePage is set) + + Flags HTMLFlags + Extensions Extensions } // HTML is a type that implements the Renderer interface for HTML output. // -// Do not create this directly, instead use the HTMLRenderer function. +// Do not create this directly, instead use the NewHTMLRenderer function. type HTML struct { flags HTMLFlags closeTag string // how to end singleton tags: either " />" or ">" - title string // document title - css string // optional css file url (used with HTML_COMPLETE_PAGE) parameters HTMLRendererParameters

@@ -114,17 +118,6 @@ xhtmlClose = " />"

htmlClose = ">" ) -// HTMLRenderer creates and configures an HTML object, which -// satisfies the Renderer interface. -// -// flags is a set of HTMLFlags ORed together. -// title is the title of the document, and css is a URL for the document's -// stylesheet. -// title and css are only used when HTML_COMPLETE_PAGE is selected. -func HTMLRenderer(flags HTMLFlags, extensions Extensions, title string, css string) Renderer { - return HTMLRendererWithParameters(flags, extensions, title, css, HTMLRendererParameters{}) -} - type HTMLWriter struct { bytes.Buffer }

@@ -139,26 +132,25 @@ func (r *HTML) Write(b []byte) (int, error) {

return r.w.Write(b) } -func HTMLRendererWithParameters(flags HTMLFlags, extensions Extensions, title string, - css string, renderParameters HTMLRendererParameters) Renderer { +// NewHTMLRenderer creates and configures an HTML object, which +// satisfies the Renderer interface. +func NewHTMLRenderer(params HTMLRendererParameters) Renderer { // configure the rendering engine closeTag := htmlClose - if flags&UseXHTML != 0 { + if params.Flags&UseXHTML != 0 { closeTag = xhtmlClose } - if renderParameters.FootnoteReturnLinkContents == "" { - renderParameters.FootnoteReturnLinkContents = `<sup>[return]</sup>` + if params.FootnoteReturnLinkContents == "" { + params.FootnoteReturnLinkContents = `<sup>[return]</sup>` } var writer HTMLWriter return &HTML{ - flags: flags, - extensions: extensions, + flags: params.Flags, + extensions: params.Extensions, closeTag: closeTag, - title: title, - css: css, - parameters: renderParameters, + parameters: params, headerCount: 0, currentLevel: 0,

@@ -694,7 +686,7 @@ r.w.WriteString("<html>\n")

} r.w.WriteString("<head>\n") r.w.WriteString(" <title>") - r.NormalText([]byte(r.title)) + r.NormalText([]byte(r.parameters.Title)) r.w.WriteString("</title>\n") r.w.WriteString(" <meta name=\"GENERATOR\" content=\"Blackfriday Markdown Processor v") r.w.WriteString(VERSION)

@@ -704,9 +696,9 @@ r.w.WriteString(">\n")

r.w.WriteString(" <meta charset=\"utf-8\"") r.w.WriteString(ending) r.w.WriteString(">\n") - if r.css != "" { + if r.parameters.CSS != "" { r.w.WriteString(" <link rel=\"stylesheet\" type=\"text/css\" href=\"") - r.attrEscape([]byte(r.css)) + r.attrEscape([]byte(r.parameters.CSS)) r.w.WriteString("\"") r.w.WriteString(ending) r.w.WriteString(">\n")

@@ -1412,9 +1404,9 @@ }

w.WriteString("<head>\n") w.WriteString(" <title>") if r.extensions&Smartypants != 0 { - w.Write(sr.Process([]byte(r.title))) + w.Write(sr.Process([]byte(r.parameters.Title))) } else { - w.Write(esc([]byte(r.title), false)) + w.Write(esc([]byte(r.parameters.Title), false)) } w.WriteString("</title>\n") w.WriteString(" <meta name=\"GENERATOR\" content=\"Blackfriday Markdown Processor v")

@@ -1425,9 +1417,9 @@ w.WriteString(">\n")

w.WriteString(" <meta charset=\"utf-8\"") w.WriteString(ending) w.WriteString(">\n") - if r.css != "" { + if r.parameters.CSS != "" { w.WriteString(" <link rel=\"stylesheet\" type=\"text/css\" href=\"") - r.attrEscape([]byte(r.css)) + r.attrEscape([]byte(r.parameters.CSS)) w.WriteString("\"") w.WriteString(ending) w.WriteString(">\n")
M latex.golatex.go

@@ -19,17 +19,17 @@ import "bytes"

// Latex is a type that implements the Renderer interface for LaTeX output. // -// Do not create this directly, instead use the LatexRenderer function. +// Do not create this directly, instead use the NewLatexRenderer function. type Latex struct { w HTMLWriter } -// LatexRenderer creates and configures a Latex object, which +// NewLatexRenderer creates and configures a Latex object, which // satisfies the Renderer interface. // // flags is a set of LATEX_* options ORed together (currently no such options // are defined). -func LatexRenderer(flags int) Renderer { +func NewLatexRenderer(flags int) Renderer { var writer HTMLWriter return &Latex{ w: writer,
M markdown.gomarkdown.go

@@ -345,8 +345,10 @@ // MarkdownBasic is a convenience function for simple rendering.

// It processes markdown input with no extensions enabled. func MarkdownBasic(input []byte) []byte { // set up the HTML renderer - htmlFlags := UseXHTML - renderer := HTMLRenderer(htmlFlags, CommonExtensions, "", "") + renderer := NewHTMLRenderer(HTMLRendererParameters{ + Flags: UseXHTML, + Extensions: CommonExtensions, + }) // set up the parser return Markdown(input, renderer, Options{Extensions: 0})

@@ -373,7 +375,10 @@ //

// * Custom Header IDs func MarkdownCommon(input []byte) []byte { // set up the HTML renderer - renderer := HTMLRenderer(CommonHtmlFlags, CommonExtensions, "", "") + renderer := NewHTMLRenderer(HTMLRendererParameters{ + Flags: CommonHtmlFlags, + Extensions: CommonExtensions, + }) return Markdown(input, renderer, DefaultOptions) }

@@ -382,8 +387,8 @@ // It parses and renders a block of markdown-encoded text.

// The supplied Renderer is used to format the output, and extensions dictates // which non-standard extensions are enabled. // -// To use the supplied Html or LaTeX renderers, see HtmlRenderer and -// LatexRenderer, respectively. +// To use the supplied HTML or LaTeX renderers, see NewHTMLRenderer and +// NewLatexRenderer, respectively. func Markdown(input []byte, renderer Renderer, options Options) []byte { if renderer == nil { return nil
M test_helpers.gotest_helpers.go

@@ -43,8 +43,9 @@ suite(&candidate)

} func runMarkdown(input string, params TestParams) string { - renderer := HTMLRendererWithParameters(params.HTMLFlags, - params.Options.Extensions, "", "", params.HTMLRendererParameters) + params.HTMLRendererParameters.Flags = params.HTMLFlags + params.HTMLRendererParameters.Extensions = params.Options.Extensions + renderer := NewHTMLRenderer(params.HTMLRendererParameters) return string(Markdown([]byte(input), renderer, params.Options)) }