all repos — grayfriday @ 5c12499aa1ddda74561fb899c394f01fd1e8e9e6

blackfriday fork with a few changes

Add ability to convert relative links to absolute
Daniel Imfeld daniel@danielimfeld.com
Sun, 18 May 2014 01:28:15 -0500
commit

5c12499aa1ddda74561fb899c394f01fd1e8e9e6

parent

03a690ac555062cf260afb68797b31acf5eee252

5 files changed, 32 insertions(+), 15 deletions(-)

jump to
M block_test.goblock_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)) }
M html.gohtml.go

@@ -41,6 +41,7 @@ HTML_USE_XHTML // generate XHTML output instead of HTML

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 ) var (

@@ -58,10 +59,11 @@ // 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) + 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 // table of contents data tocMarker int

@@ -84,7 +86,7 @@ // flags is a set of HTML_* options 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 int, title string, css string) Renderer { +func HtmlRenderer(flags int, title string, css string, absolutePrefix string) Renderer { // configure the rendering engine closeTag := htmlClose if flags&HTML_USE_XHTML != 0 {

@@ -92,10 +94,11 @@ closeTag = xhtmlClose

} return &Html{ - flags: flags, - closeTag: closeTag, - title: title, - css: css, + flags: flags, + closeTag: closeTag, + title: title, + css: css, + absolutePrefix: absolutePrefix, headerCount: 0, currentLevel: 0,

@@ -410,7 +413,10 @@

out.WriteString("<a href=\"") if kind == LINK_TYPE_EMAIL { out.WriteString("mailto:") + } else { + options.maybeWriteAbsolutePrefix(out, link) } + entityEscapeWithSkip(out, link, skipRanges) if options.flags&HTML_NOFOLLOW_LINKS != 0 && !isRelativeLink(link) {

@@ -459,12 +465,22 @@ out.Write(text)

out.WriteString("</em>") } +func (options *Html) maybeWriteAbsolutePrefix(out *bytes.Buffer, link []byte) { + if options.flags&HTML_ABSOLUTE_LINKS != 0 && isRelativeLink(link) { + out.WriteString(options.absolutePrefix) + if link[0] != '/' { + out.WriteByte('/') + } + } +} + func (options *Html) Image(out *bytes.Buffer, link []byte, title []byte, alt []byte) { if options.flags&HTML_SKIP_IMAGES != 0 { return } out.WriteString("<img src=\"") + options.maybeWriteAbsolutePrefix(out, link) attrEscape(out, link) out.WriteString("\" alt=\"") if len(alt) > 0 {

@@ -503,6 +519,7 @@ return

} out.WriteString("<a href=\"") + options.maybeWriteAbsolutePrefix(out, link) attrEscape(out, link) if len(title) > 0 { out.WriteString("\" title=\"")
M inline_test.goinline_test.go

@@ -23,7 +23,7 @@ extensions |= EXTENSION_STRIKETHROUGH

htmlFlags |= HTML_USE_XHTML - renderer := HtmlRenderer(htmlFlags, "", "") + renderer := HtmlRenderer(htmlFlags, "", "", "") return string(Markdown([]byte(input), renderer, extensions)) }
M markdown.gomarkdown.go

@@ -202,7 +202,7 @@ // It processes markdown input with no extensions enabled.

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 @@ htmlFlags |= HTML_USE_SMARTYPANTS

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

@@ -332,7 +332,7 @@ if p.flags&EXTENSION_FENCED_CODE != 0 {

// when last line was none blank and a fenced code block comes after if beg >= lastFencedCodeBlockEnd { // tmp var so we don't modify beyond bounds of `input` - var tmp = make([]byte, len(input[beg:]), len(input[beg:]) + 1) + var tmp = make([]byte, len(input[beg:]), len(input[beg:])+1) copy(tmp, input[beg:]) if i := p.fencedCode(&out, append(tmp, '\n'), false); i > 0 { if !lastLineWasBlank {
M upskirtref_test.goupskirtref_test.go

@@ -20,7 +20,7 @@ "testing"

) func runMarkdownReference(input string, flag int) string { - renderer := HtmlRenderer(0, "", "") + renderer := HtmlRenderer(0, "", "", "") return string(Markdown([]byte(input), renderer, flag)) }