Add configurable support for angled quotes

The flag `HTML_SMARTYPANTS_ANGLED_QUOTES` was added to Blackfriday on Black Friday. This configures rendering of double quotes as angled left and right quotes («
»).

Typical use cases would be either or, or combined, but never in the same
document. As an example would be a person from Norway; he has a blog in both
English and Norwegian (his native tongue); he would then configure Blackfriday
to use angled quotes for the Norwegian section, but keep them as reqular
double quotes for the English.

This commit adds configuration support for this new flag, configuration that can be set in the site configuration, but overridden in page front matter.

Fixes #605
This commit is contained in:
bep 2014-11-28 21:16:57 +01:00
parent bb37698226
commit fbf8bcacc4
5 changed files with 69 additions and 25 deletions

View file

@ -136,6 +136,7 @@ func InitializeConfig() {
viper.SetDefault("FootnoteAnchorPrefix", "") viper.SetDefault("FootnoteAnchorPrefix", "")
viper.SetDefault("FootnoteReturnLinkContents", "") viper.SetDefault("FootnoteReturnLinkContents", "")
viper.SetDefault("NewContentEditor", "") viper.SetDefault("NewContentEditor", "")
viper.SetDefault("Blackfriday", map[string]bool{"angledQuotes": false})
if hugoCmdV.PersistentFlags().Lookup("buildDrafts").Changed { if hugoCmdV.PersistentFlags().Lookup("buildDrafts").Changed {
viper.Set("BuildDrafts", Draft) viper.Set("BuildDrafts", Draft)

View file

@ -77,15 +77,15 @@ func BytesToHTML(b []byte) template.HTML {
return template.HTML(string(b)) return template.HTML(string(b))
} }
func GetHtmlRenderer(defaultFlags int, documentId string) blackfriday.Renderer { func GetHtmlRenderer(defaultFlags int, ctx RenderingContext) blackfriday.Renderer {
renderParameters := blackfriday.HtmlRendererParameters{ renderParameters := blackfriday.HtmlRendererParameters{
FootnoteAnchorPrefix: viper.GetString("FootnoteAnchorPrefix"), FootnoteAnchorPrefix: viper.GetString("FootnoteAnchorPrefix"),
FootnoteReturnLinkContents: viper.GetString("FootnoteReturnLinkContents"), FootnoteReturnLinkContents: viper.GetString("FootnoteReturnLinkContents"),
} }
if len(documentId) != 0 { if len(ctx.DocumentId) != 0 {
renderParameters.FootnoteAnchorPrefix = documentId + ":" + renderParameters.FootnoteAnchorPrefix renderParameters.FootnoteAnchorPrefix = ctx.DocumentId + ":" + renderParameters.FootnoteAnchorPrefix
renderParameters.HeaderIDSuffix = ":" + documentId renderParameters.HeaderIDSuffix = ":" + ctx.DocumentId
} }
htmlFlags := defaultFlags htmlFlags := defaultFlags
@ -95,6 +95,16 @@ func GetHtmlRenderer(defaultFlags int, documentId string) blackfriday.Renderer {
htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
htmlFlags |= blackfriday.HTML_FOOTNOTE_RETURN_LINKS htmlFlags |= blackfriday.HTML_FOOTNOTE_RETURN_LINKS
var angledQuotes bool
if m, ok := ctx.ConfigFlags["angledQuotes"]; ok {
angledQuotes = m
}
if angledQuotes {
htmlFlags |= blackfriday.HTML_SMARTYPANTS_ANGLED_QUOTES
}
return blackfriday.HtmlRendererWithParameters(htmlFlags, "", "", renderParameters) return blackfriday.HtmlRendererWithParameters(htmlFlags, "", "", renderParameters)
} }
@ -106,14 +116,14 @@ func GetMarkdownExtensions() int {
blackfriday.EXTENSION_HEADER_IDS | blackfriday.EXTENSION_AUTO_HEADER_IDS blackfriday.EXTENSION_HEADER_IDS | blackfriday.EXTENSION_AUTO_HEADER_IDS
} }
func MarkdownRender(content []byte, documentId string) []byte { func MarkdownRender(ctx RenderingContext) []byte {
return blackfriday.Markdown(content, GetHtmlRenderer(0, documentId), return blackfriday.Markdown(ctx.Content, GetHtmlRenderer(0, ctx),
GetMarkdownExtensions()) GetMarkdownExtensions())
} }
func MarkdownRenderWithTOC(content []byte, documentId string) []byte { func MarkdownRenderWithTOC(ctx RenderingContext) []byte {
return blackfriday.Markdown(content, return blackfriday.Markdown(ctx.Content,
GetHtmlRenderer(blackfriday.HTML_TOC, documentId), GetHtmlRenderer(blackfriday.HTML_TOC, ctx),
GetMarkdownExtensions()) GetMarkdownExtensions())
} }
@ -153,25 +163,32 @@ func ExtractTOC(content []byte) (newcontent []byte, toc []byte) {
return return
} }
func RenderBytesWithTOC(content []byte, pagefmt string, documentId string) []byte { type RenderingContext struct {
switch pagefmt { Content []byte
PageFmt string
DocumentId string
ConfigFlags map[string]bool
}
func RenderBytesWithTOC(ctx RenderingContext) []byte {
switch ctx.PageFmt {
default: default:
return MarkdownRenderWithTOC(content, documentId) return MarkdownRenderWithTOC(ctx)
case "markdown": case "markdown":
return MarkdownRenderWithTOC(content, documentId) return MarkdownRenderWithTOC(ctx)
case "rst": case "rst":
return []byte(GetRstContent(content)) return []byte(GetRstContent(ctx.Content))
} }
} }
func RenderBytes(content []byte, pagefmt string, documentId string) []byte { func RenderBytes(ctx RenderingContext) []byte {
switch pagefmt { switch ctx.PageFmt {
default: default:
return MarkdownRender(content, documentId) return MarkdownRender(ctx)
case "markdown": case "markdown":
return MarkdownRender(content, documentId) return MarkdownRender(ctx)
case "rst": case "rst":
return []byte(GetRstContent(content)) return []byte(GetRstContent(ctx.Content))
} }
} }

View file

@ -17,10 +17,11 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"github.com/spf13/cast"
"github.com/spf13/hugo/helpers" "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/parser" "github.com/spf13/hugo/parser"
"github.com/spf13/cast"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/hugo/source" "github.com/spf13/hugo/source"
"github.com/spf13/hugo/tpl" "github.com/spf13/hugo/tpl"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
@ -173,11 +174,32 @@ func (p *Page) setSummary() {
} }
func (p *Page) renderBytes(content []byte) []byte { func (p *Page) renderBytes(content []byte) []byte {
return helpers.RenderBytes(content, p.guessMarkupType(), p.UniqueId()) return helpers.RenderBytes(
helpers.RenderingContext{Content: content, PageFmt: p.guessMarkupType(),
DocumentId: p.UniqueId(), ConfigFlags: p.getRenderingConfigFlags()})
} }
func (p *Page) renderContent(content []byte) []byte { func (p *Page) renderContent(content []byte) []byte {
return helpers.RenderBytesWithTOC(content, p.guessMarkupType(), p.UniqueId()) return helpers.RenderBytesWithTOC(helpers.RenderingContext{Content: content, PageFmt: p.guessMarkupType(),
DocumentId: p.UniqueId(), ConfigFlags: p.getRenderingConfigFlags()})
}
func (p *Page) getRenderingConfigFlags() map[string]bool {
flags := make(map[string]bool)
pageParam := p.GetParam("blackfriday")
siteParam := viper.GetStringMap("blackfriday")
flags = cast.ToStringMapBool(siteParam)
if pageParam != nil {
pageFlags := cast.ToStringMapBool(pageParam)
for key, value := range pageFlags {
flags[key] = value
}
}
return flags
} }
func newPage(filename string) *Page { func newPage(filename string) *Page {
@ -472,6 +494,8 @@ func (page *Page) GetParam(key string) interface{} {
return cast.ToTime(v) return cast.ToTime(v)
case []string: case []string:
return helpers.SliceToLower(v.([]string)) return helpers.SliceToLower(v.([]string))
case map[interface{}]interface{}:
return v
} }
return nil return nil
} }

View file

@ -212,7 +212,9 @@ func renderShortcode(sc shortcode, tokenizedShortcodes map[string](string), cnt
} }
if sc.doMarkup { if sc.doMarkup {
newInner := helpers.RenderBytes([]byte(inner), p.guessMarkupType(), p.UniqueId()) newInner := helpers.RenderBytes(helpers.RenderingContext{
Content: []byte(inner), PageFmt: p.guessMarkupType(),
DocumentId: p.UniqueId(), ConfigFlags: p.getRenderingConfigFlags()})
// If the type is “unknown” or “markdown”, we assume the markdown // If the type is “unknown” or “markdown”, we assume the markdown
// generation has been performed. Given the input: `a line`, markdown // generation has been performed. Given the input: `a line`, markdown

View file

@ -618,7 +618,7 @@ func Highlight(in interface{}, lang string) template.HTML {
} }
func Markdownify(text string) template.HTML { func Markdownify(text string) template.HTML {
return template.HTML(helpers.RenderBytes([]byte(text), "markdown", "")) return template.HTML(helpers.RenderBytes(helpers.RenderingContext{Content: []byte(text), PageFmt: "markdown"}))
} }
func refPage(page interface{}, ref, methodName string) template.HTML { func refPage(page interface{}, ref, methodName string) template.HTML {