Add footnote prefix option. Needs testing

pull/86/head
Daniel Imfeld 2014-05-24 02:55:13 -05:00
parent 5c12499aa1
commit ec41294bc4
5 changed files with 38 additions and 21 deletions

View File

@ -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))
}

49
html.go
View File

@ -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(`<li id="fn:`)
out.WriteString(`<li id="`)
out.WriteString(options.parameters.FootnotePrefix)
out.WriteString(`fn:`)
out.Write(slugify(name))
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(`<sup class="footnote-ref" id="fnref:`)
out.WriteString(`<sup class="footnote-ref" id="`)
out.WriteString(options.parameters.FootnotePrefix)
out.WriteString(`fnref:`)
out.Write(slug)
out.WriteString(`"><a rel="footnote" href="#fn:`)
out.WriteString(`"><a rel="footnote" href="#`)
out.WriteString(options.parameters.FootnotePrefix)
out.WriteString(`fn:`)
out.Write(slug)
out.WriteString(`">`)
out.WriteString(strconv.Itoa(id))

View File

@ -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))
}

View File

@ -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

View File

@ -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))
}