Add site-wide/per-page [blackfriday] extensions option

This commit is contained in:
Naoya Inada
2015-01-25 20:08:02 +09:00
committed by bep
parent 39b2cdece0
commit 407e80a9ab
7 changed files with 126 additions and 59 deletions

View File

@@ -28,6 +28,7 @@ import (
jww "github.com/spf13/jwalterweatherman"
"strings"
"sync"
)
// Length of the summary that Hugo extracts from a content.
@@ -36,6 +37,30 @@ var SummaryLength = 70
// Custom divider <!--more--> let's user define where summarization ends.
var SummaryDivider = []byte("<!--more-->")
type Blackfriday struct {
AngledQuotes bool
Fractions bool
PlainIdAnchors bool
Extensions []string
}
var blackfridayExtensionMap = map[string]int{
"noIntraEmphasis": blackfriday.EXTENSION_NO_INTRA_EMPHASIS,
"tables": blackfriday.EXTENSION_TABLES,
"fencedCode": blackfriday.EXTENSION_FENCED_CODE,
"autolink": blackfriday.EXTENSION_AUTOLINK,
"strikethrough": blackfriday.EXTENSION_STRIKETHROUGH,
"laxHtmlBlocks": blackfriday.EXTENSION_LAX_HTML_BLOCKS,
"spaceHeaders": blackfriday.EXTENSION_SPACE_HEADERS,
"hardLineBreak": blackfriday.EXTENSION_HARD_LINE_BREAK,
"tabSizeEight": blackfriday.EXTENSION_TAB_SIZE_EIGHT,
"footnotes": blackfriday.EXTENSION_FOOTNOTES,
"noEmptyLineBeforeBlock": blackfriday.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK,
"headerIds": blackfriday.EXTENSION_HEADER_IDS,
"titleblock": blackfriday.EXTENSION_TITLEBLOCK,
"autoHeaderIds": blackfriday.EXTENSION_AUTO_HEADER_IDS,
}
// StripHTML accepts a string, strips out all HTML tags and returns it.
func StripHTML(s string) string {
output := ""
@@ -87,7 +112,7 @@ func GetHtmlRenderer(defaultFlags int, ctx RenderingContext) blackfriday.Rendere
b := len(ctx.DocumentId) != 0
if m, ok := ctx.ConfigFlags["plainIdAnchors"]; b && ((ok && !m) || !ok) {
if b && !ctx.getConfig().PlainIdAnchors {
renderParameters.FootnoteAnchorPrefix = ctx.DocumentId + ":" + renderParameters.FootnoteAnchorPrefix
renderParameters.HeaderIDSuffix = ":" + ctx.DocumentId
}
@@ -99,40 +124,40 @@ func GetHtmlRenderer(defaultFlags int, ctx RenderingContext) blackfriday.Rendere
htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
htmlFlags |= blackfriday.HTML_FOOTNOTE_RETURN_LINKS
var angledQuotes bool
if m, ok := ctx.ConfigFlags["angledQuotes"]; ok {
angledQuotes = m
}
if angledQuotes {
if ctx.getConfig().AngledQuotes {
htmlFlags |= blackfriday.HTML_SMARTYPANTS_ANGLED_QUOTES
}
if m, ok := ctx.ConfigFlags["fractions"]; ok && !m {
if !ctx.getConfig().Fractions {
htmlFlags &^= blackfriday.HTML_SMARTYPANTS_FRACTIONS
}
return blackfriday.HtmlRendererWithParameters(htmlFlags, "", "", renderParameters)
}
func GetMarkdownExtensions() int {
return 0 | blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
func GetMarkdownExtensions(ctx RenderingContext) int {
flags := 0 | blackfriday.EXTENSION_NO_INTRA_EMPHASIS |
blackfriday.EXTENSION_TABLES | blackfriday.EXTENSION_FENCED_CODE |
blackfriday.EXTENSION_AUTOLINK | blackfriday.EXTENSION_STRIKETHROUGH |
blackfriday.EXTENSION_SPACE_HEADERS | blackfriday.EXTENSION_FOOTNOTES |
blackfriday.EXTENSION_HEADER_IDS | blackfriday.EXTENSION_AUTO_HEADER_IDS
for _, extension := range ctx.getConfig().Extensions {
if flag, ok := blackfridayExtensionMap[extension]; ok {
flags |= flag
}
}
return flags
}
func MarkdownRender(ctx RenderingContext) []byte {
return blackfriday.Markdown(ctx.Content, GetHtmlRenderer(0, ctx),
GetMarkdownExtensions())
GetMarkdownExtensions(ctx))
}
func MarkdownRenderWithTOC(ctx RenderingContext) []byte {
return blackfriday.Markdown(ctx.Content,
GetHtmlRenderer(blackfriday.HTML_TOC, ctx),
GetMarkdownExtensions())
GetMarkdownExtensions(ctx))
}
// ExtractTOC extracts Table of Contents from content.
@@ -172,10 +197,20 @@ func ExtractTOC(content []byte) (newcontent []byte, toc []byte) {
}
type RenderingContext struct {
Content []byte
PageFmt string
DocumentId string
ConfigFlags map[string]bool
Content []byte
PageFmt string
DocumentId string
Config *Blackfriday
configInit sync.Once
}
func (c *RenderingContext) getConfig() *Blackfriday {
c.configInit.Do(func() {
if c.Config == nil {
c.Config = new(Blackfriday)
}
})
return c.Config
}
func RenderBytesWithTOC(ctx RenderingContext) []byte {
@@ -261,8 +296,8 @@ func GetRstContent(content []byte) string {
path, err = exec.LookPath("rst2html.py")
if err != nil {
jww.ERROR.Println("rst2html / rst2html.py not found in $PATH: Please install.\n",
" Leaving reStructuredText content unrendered.")
return(string(content))
" Leaving reStructuredText content unrendered.")
return (string(content))
}
}