From ec41294bc4b42e3567f143fc2ec681dd5f49b65c Mon Sep 17 00:00:00 2001 From: Daniel Imfeld Date: Sat, 24 May 2014 02:55:13 -0500 Subject: [PATCH] Add footnote prefix option. Needs testing --- block_test.go | 2 +- html.go | 49 +++++++++++++++++++++++++++++++--------------- inline_test.go | 2 +- markdown.go | 4 ++-- upskirtref_test.go | 2 +- 5 files changed, 38 insertions(+), 21 deletions(-) diff --git a/block_test.go b/block_test.go index 15c3421..3c37202 100644 --- a/block_test.go +++ b/block_test.go @@ -21,7 +21,7 @@ func runMarkdownBlock(input string, extensions int) string { htmlFlags := 0 htmlFlags |= HTML_USE_XHTML - renderer := HtmlRenderer(htmlFlags, "", "", "") + renderer := HtmlRenderer(htmlFlags, "", "") return string(Markdown([]byte(input), renderer, extensions)) } diff --git a/html.go b/html.go index a63be74..f8eafbe 100644 --- a/html.go +++ b/html.go @@ -41,7 +41,7 @@ const ( HTML_USE_SMARTYPANTS // enable smart punctuation substitutions HTML_SMARTYPANTS_FRACTIONS // enable smart fractions (with HTML_USE_SMARTYPANTS) HTML_SMARTYPANTS_LATEX_DASHES // enable LaTeX-style dashes (with HTML_USE_SMARTYPANTS) - HTML_ABSOLUTE_LINKS // convert all links to absolute links + HTML_ABSOLUTE_LINKS // convert all links to absolute links, using AbsolutePrefix ) var ( @@ -55,15 +55,21 @@ var ( htmlEntity = regexp.MustCompile(`&[a-z]{2,5};`) ) +type HtmlRendererParameters struct { + AbsolutePrefix string + FootnotePrefix string +} + // Html is a type that implements the Renderer interface for HTML output. // // Do not create this directly, instead use the HtmlRenderer function. type Html struct { - flags int // HTML_* options - closeTag string // how to end singleton tags: either " />\n" or ">\n" - title string // document title - css string // optional css file url (used with HTML_COMPLETE_PAGE) - absolutePrefix string + flags int // HTML_* options + closeTag string // how to end singleton tags: either " />\n" or ">\n" + title string // document title + css string // optional css file url (used with HTML_COMPLETE_PAGE) + + parameters HtmlRendererParameters // table of contents data tocMarker int @@ -86,7 +92,12 @@ const ( // 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 int, title string, css string, absolutePrefix string) Renderer { +func HtmlRenderer(flags int, title string, css string) Renderer { + return HtmlRendererWithParameters(flags, title, css, HtmlRendererParameters{}) +} + +func HtmlRendererWithParameters(flags int, title string, + css string, renderParameters HtmlRendererParameters) Renderer { // configure the rendering engine closeTag := htmlClose if flags&HTML_USE_XHTML != 0 { @@ -94,11 +105,11 @@ func HtmlRenderer(flags int, title string, css string, absolutePrefix string) Re } return &Html{ - flags: flags, - closeTag: closeTag, - title: title, - css: css, - absolutePrefix: absolutePrefix, + flags: flags, + closeTag: closeTag, + title: title, + css: css, + parameters: renderParameters, headerCount: 0, currentLevel: 0, @@ -352,7 +363,9 @@ func (options *Html) FootnoteItem(out *bytes.Buffer, name, text []byte, flags in if flags&LIST_ITEM_CONTAINS_BLOCK != 0 || flags&LIST_ITEM_BEGINNING_OF_LIST != 0 { doubleSpace(out) } - out.WriteString(`
  • `) out.Write(text) @@ -467,7 +480,7 @@ func (options *Html) Emphasis(out *bytes.Buffer, text []byte) { func (options *Html) maybeWriteAbsolutePrefix(out *bytes.Buffer, link []byte) { if options.flags&HTML_ABSOLUTE_LINKS != 0 && isRelativeLink(link) { - out.WriteString(options.absolutePrefix) + out.WriteString(options.parameters.AbsolutePrefix) if link[0] != '/' { out.WriteByte('/') } @@ -569,9 +582,13 @@ func (options *Html) StrikeThrough(out *bytes.Buffer, text []byte) { func (options *Html) FootnoteRef(out *bytes.Buffer, ref []byte, id int) { slug := slugify(ref) - out.WriteString(``) out.WriteString(strconv.Itoa(id)) diff --git a/inline_test.go b/inline_test.go index e3e1fa3..edda982 100644 --- a/inline_test.go +++ b/inline_test.go @@ -23,7 +23,7 @@ func runMarkdownInline(input string, extensions, htmlFlags int) string { htmlFlags |= HTML_USE_XHTML - renderer := HtmlRenderer(htmlFlags, "", "", "") + renderer := HtmlRenderer(htmlFlags, "", "") return string(Markdown([]byte(input), renderer, extensions)) } diff --git a/markdown.go b/markdown.go index 03234f7..ca67cc6 100644 --- a/markdown.go +++ b/markdown.go @@ -202,7 +202,7 @@ type parser struct { func MarkdownBasic(input []byte) []byte { // set up the HTML renderer htmlFlags := HTML_USE_XHTML - renderer := HtmlRenderer(htmlFlags, "", "", "") + renderer := HtmlRenderer(htmlFlags, "", "") // set up the parser extensions := 0 @@ -237,7 +237,7 @@ func MarkdownCommon(input []byte) []byte { htmlFlags |= HTML_SMARTYPANTS_FRACTIONS htmlFlags |= HTML_SMARTYPANTS_LATEX_DASHES htmlFlags |= HTML_SANITIZE_OUTPUT - renderer := HtmlRenderer(htmlFlags, "", "", "") + renderer := HtmlRenderer(htmlFlags, "", "") // set up the parser extensions := 0 diff --git a/upskirtref_test.go b/upskirtref_test.go index b931404..42a0bdd 100644 --- a/upskirtref_test.go +++ b/upskirtref_test.go @@ -20,7 +20,7 @@ import ( ) func runMarkdownReference(input string, flag int) string { - renderer := HtmlRenderer(0, "", "", "") + renderer := HtmlRenderer(0, "", "") return string(Markdown([]byte(input), renderer, flag)) }