Add ability to convert relative links to absolute

pull/86/head
Daniel Imfeld 2014-05-18 01:28:15 -05:00
parent 03a690ac55
commit 5c12499aa1
5 changed files with 32 additions and 15 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))
}

35
html.go
View File

@ -41,6 +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
)
var (
@ -58,10 +59,11 @@ var (
//
// 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 @@ 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) 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 @@ func HtmlRenderer(flags int, title string, css string) Renderer {
}
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 @@ func (options *Html) AutoLink(out *bytes.Buffer, link []byte, kind int) {
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 @@ func (options *Html) Emphasis(out *bytes.Buffer, text []byte) {
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 @@ func (options *Html) Link(out *bytes.Buffer, link []byte, title []byte, content
}
out.WriteString("<a href=\"")
options.maybeWriteAbsolutePrefix(out, link)
attrEscape(out, link)
if len(title) > 0 {
out.WriteString("\" title=\"")

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
@ -332,7 +332,7 @@ func firstPass(p *parser, input []byte) []byte {
// 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 {

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