1
0
mirror of https://github.com/danog/blackfriday.git synced 2025-01-23 05:41:27 +01:00

Cleanup renderer constructors

Move all parameters under HTMLRendererParameters struct, rename
constructors to New{HTML|Latex}Renderer.
This commit is contained in:
Vytautas Šaltenis 2016-04-05 14:18:32 +03:00
parent 4f8d2881a2
commit 1303ea1427
4 changed files with 39 additions and 41 deletions

54
html.go
View File

@ -80,16 +80,20 @@ type HTMLRendererParameters struct {
HeaderIDPrefix string HeaderIDPrefix string
// If set, add this text to the back of each Header ID, to ensure uniqueness. // If set, add this text to the back of each Header ID, to ensure uniqueness.
HeaderIDSuffix string 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. // 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 { type HTML struct {
flags HTMLFlags flags HTMLFlags
closeTag string // how to end singleton tags: either " />" or ">" 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 parameters HTMLRendererParameters
@ -114,17 +118,6 @@ const (
htmlClose = ">" 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 { type HTMLWriter struct {
bytes.Buffer bytes.Buffer
} }
@ -139,26 +132,25 @@ func (r *HTML) Write(b []byte) (int, error) {
return r.w.Write(b) return r.w.Write(b)
} }
func HTMLRendererWithParameters(flags HTMLFlags, extensions Extensions, title string, // NewHTMLRenderer creates and configures an HTML object, which
css string, renderParameters HTMLRendererParameters) Renderer { // satisfies the Renderer interface.
func NewHTMLRenderer(params HTMLRendererParameters) Renderer {
// configure the rendering engine // configure the rendering engine
closeTag := htmlClose closeTag := htmlClose
if flags&UseXHTML != 0 { if params.Flags&UseXHTML != 0 {
closeTag = xhtmlClose closeTag = xhtmlClose
} }
if renderParameters.FootnoteReturnLinkContents == "" { if params.FootnoteReturnLinkContents == "" {
renderParameters.FootnoteReturnLinkContents = `<sup>[return]</sup>` params.FootnoteReturnLinkContents = `<sup>[return]</sup>`
} }
var writer HTMLWriter var writer HTMLWriter
return &HTML{ return &HTML{
flags: flags, flags: params.Flags,
extensions: extensions, extensions: params.Extensions,
closeTag: closeTag, closeTag: closeTag,
title: title, parameters: params,
css: css,
parameters: renderParameters,
headerCount: 0, headerCount: 0,
currentLevel: 0, currentLevel: 0,
@ -694,7 +686,7 @@ func (r *HTML) DocumentHeader() {
} }
r.w.WriteString("<head>\n") r.w.WriteString("<head>\n")
r.w.WriteString(" <title>") r.w.WriteString(" <title>")
r.NormalText([]byte(r.title)) r.NormalText([]byte(r.parameters.Title))
r.w.WriteString("</title>\n") r.w.WriteString("</title>\n")
r.w.WriteString(" <meta name=\"GENERATOR\" content=\"Blackfriday Markdown Processor v") r.w.WriteString(" <meta name=\"GENERATOR\" content=\"Blackfriday Markdown Processor v")
r.w.WriteString(VERSION) r.w.WriteString(VERSION)
@ -704,9 +696,9 @@ func (r *HTML) DocumentHeader() {
r.w.WriteString(" <meta charset=\"utf-8\"") r.w.WriteString(" <meta charset=\"utf-8\"")
r.w.WriteString(ending) r.w.WriteString(ending)
r.w.WriteString(">\n") r.w.WriteString(">\n")
if r.css != "" { if r.parameters.CSS != "" {
r.w.WriteString(" <link rel=\"stylesheet\" type=\"text/css\" href=\"") 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("\"")
r.w.WriteString(ending) r.w.WriteString(ending)
r.w.WriteString(">\n") r.w.WriteString(">\n")
@ -1412,9 +1404,9 @@ func (r *HTML) writeDocumentHeader(w *bytes.Buffer, sr *SPRenderer) {
w.WriteString("<head>\n") w.WriteString("<head>\n")
w.WriteString(" <title>") w.WriteString(" <title>")
if r.extensions&Smartypants != 0 { if r.extensions&Smartypants != 0 {
w.Write(sr.Process([]byte(r.title))) w.Write(sr.Process([]byte(r.parameters.Title)))
} else { } else {
w.Write(esc([]byte(r.title), false)) w.Write(esc([]byte(r.parameters.Title), false))
} }
w.WriteString("</title>\n") w.WriteString("</title>\n")
w.WriteString(" <meta name=\"GENERATOR\" content=\"Blackfriday Markdown Processor v") w.WriteString(" <meta name=\"GENERATOR\" content=\"Blackfriday Markdown Processor v")
@ -1425,9 +1417,9 @@ func (r *HTML) writeDocumentHeader(w *bytes.Buffer, sr *SPRenderer) {
w.WriteString(" <meta charset=\"utf-8\"") w.WriteString(" <meta charset=\"utf-8\"")
w.WriteString(ending) w.WriteString(ending)
w.WriteString(">\n") w.WriteString(">\n")
if r.css != "" { if r.parameters.CSS != "" {
w.WriteString(" <link rel=\"stylesheet\" type=\"text/css\" href=\"") w.WriteString(" <link rel=\"stylesheet\" type=\"text/css\" href=\"")
r.attrEscape([]byte(r.css)) r.attrEscape([]byte(r.parameters.CSS))
w.WriteString("\"") w.WriteString("\"")
w.WriteString(ending) w.WriteString(ending)
w.WriteString(">\n") w.WriteString(">\n")

View File

@ -19,17 +19,17 @@ import "bytes"
// Latex is a type that implements the Renderer interface for LaTeX output. // 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 { type Latex struct {
w HTMLWriter w HTMLWriter
} }
// LatexRenderer creates and configures a Latex object, which // NewLatexRenderer creates and configures a Latex object, which
// satisfies the Renderer interface. // satisfies the Renderer interface.
// //
// flags is a set of LATEX_* options ORed together (currently no such options // flags is a set of LATEX_* options ORed together (currently no such options
// are defined). // are defined).
func LatexRenderer(flags int) Renderer { func NewLatexRenderer(flags int) Renderer {
var writer HTMLWriter var writer HTMLWriter
return &Latex{ return &Latex{
w: writer, w: writer,

View File

@ -345,8 +345,10 @@ type Options struct {
// It processes markdown input with no extensions enabled. // It processes markdown input with no extensions enabled.
func MarkdownBasic(input []byte) []byte { func MarkdownBasic(input []byte) []byte {
// set up the HTML renderer // set up the HTML renderer
htmlFlags := UseXHTML renderer := NewHTMLRenderer(HTMLRendererParameters{
renderer := HTMLRenderer(htmlFlags, CommonExtensions, "", "") Flags: UseXHTML,
Extensions: CommonExtensions,
})
// set up the parser // set up the parser
return Markdown(input, renderer, Options{Extensions: 0}) return Markdown(input, renderer, Options{Extensions: 0})
@ -373,7 +375,10 @@ func MarkdownBasic(input []byte) []byte {
// * Custom Header IDs // * Custom Header IDs
func MarkdownCommon(input []byte) []byte { func MarkdownCommon(input []byte) []byte {
// set up the HTML renderer // set up the HTML renderer
renderer := HTMLRenderer(CommonHtmlFlags, CommonExtensions, "", "") renderer := NewHTMLRenderer(HTMLRendererParameters{
Flags: CommonHtmlFlags,
Extensions: CommonExtensions,
})
return Markdown(input, renderer, DefaultOptions) return Markdown(input, renderer, DefaultOptions)
} }
@ -382,8 +387,8 @@ func MarkdownCommon(input []byte) []byte {
// The supplied Renderer is used to format the output, and extensions dictates // The supplied Renderer is used to format the output, and extensions dictates
// which non-standard extensions are enabled. // which non-standard extensions are enabled.
// //
// To use the supplied Html or LaTeX renderers, see HtmlRenderer and // To use the supplied HTML or LaTeX renderers, see NewHTMLRenderer and
// LatexRenderer, respectively. // NewLatexRenderer, respectively.
func Markdown(input []byte, renderer Renderer, options Options) []byte { func Markdown(input []byte, renderer Renderer, options Options) []byte {
if renderer == nil { if renderer == nil {
return nil return nil

View File

@ -43,8 +43,9 @@ func execRecoverableTestSuite(t *testing.T, tests []string, params TestParams, s
} }
func runMarkdown(input string, params TestParams) string { func runMarkdown(input string, params TestParams) string {
renderer := HTMLRendererWithParameters(params.HTMLFlags, params.HTMLRendererParameters.Flags = params.HTMLFlags
params.Options.Extensions, "", "", params.HTMLRendererParameters) params.HTMLRendererParameters.Extensions = params.Options.Extensions
renderer := NewHTMLRenderer(params.HTMLRendererParameters)
return string(Markdown([]byte(input), renderer, params.Options)) return string(Markdown([]byte(input), renderer, params.Options))
} }