Make it possible to configure Blackfroday per language

See #2309
This commit is contained in:
Bjørn Erik Pedersen
2016-08-07 14:03:03 +02:00
parent 596e0e98e4
commit 2079a23dd8
21 changed files with 324 additions and 189 deletions

25
helpers/configProvider.go Normal file
View File

@@ -0,0 +1,25 @@
// Copyright 2016-present The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package helpers implements general utility functions that work with
// and on content. The helper functions defined here lay down the
// foundation of how Hugo works with files and filepaths, and perform
// string operations on content.
package helpers
// ConfigProvider provides the configuration settings for Hugo.
type ConfigProvider interface {
GetString(key string) string
GetStringMap(key string) map[string]interface{}
GetStringMapString(key string) map[string]string
}

View File

@@ -59,7 +59,7 @@ type Blackfriday struct {
}
// NewBlackfriday creates a new Blackfriday filled with site config or some sane defaults.
func NewBlackfriday() *Blackfriday {
func NewBlackfriday(c ConfigProvider) *Blackfriday {
combinedParam := map[string]interface{}{
"smartypants": true,
"angledQuotes": false,
@@ -72,7 +72,7 @@ func NewBlackfriday() *Blackfriday {
"sourceRelativeLinksProjectFolder": "/docs/content",
}
siteParam := viper.GetStringMap("blackfriday")
siteParam := c.GetStringMap("blackfriday")
if siteParam != nil {
siteConfig := cast.ToStringMap(siteParam)
@@ -341,20 +341,25 @@ func ExtractTOC(content []byte) (newcontent []byte, toc []byte) {
// RenderingContext holds contextual information, like content and configuration,
// for a given content rendering.
type RenderingContext struct {
Content []byte
PageFmt string
DocumentID string
Config *Blackfriday
RenderTOC bool
FileResolver FileResolverFunc
LinkResolver LinkResolverFunc
configInit sync.Once
Content []byte
PageFmt string
DocumentID string
Config *Blackfriday
RenderTOC bool
FileResolver FileResolverFunc
LinkResolver LinkResolverFunc
ConfigProvider ConfigProvider
configInit sync.Once
}
func newViperProvidedRenderingContext() *RenderingContext {
return &RenderingContext{ConfigProvider: viper.GetViper()}
}
func (c *RenderingContext) getConfig() *Blackfriday {
c.configInit.Do(func() {
if c.Config == nil {
c.Config = NewBlackfriday()
c.Config = NewBlackfriday(c.ConfigProvider)
}
})
return c.Config

View File

@@ -23,7 +23,7 @@ import (
// Renders a codeblock using Blackfriday
func render(input string) string {
ctx := &RenderingContext{}
ctx := newViperProvidedRenderingContext()
render := getHTMLRenderer(0, ctx)
buf := &bytes.Buffer{}
@@ -33,7 +33,7 @@ func render(input string) string {
// Renders a codeblock using Mmark
func renderWithMmark(input string) string {
ctx := &RenderingContext{}
ctx := newViperProvidedRenderingContext()
render := getMmarkHTMLRenderer(0, ctx)
buf := &bytes.Buffer{}

View File

@@ -22,6 +22,7 @@ import (
"github.com/miekg/mmark"
"github.com/russross/blackfriday"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
)
@@ -124,7 +125,7 @@ func TestTruncateWordsByRune(t *testing.T) {
}
func TestGetHTMLRendererFlags(t *testing.T) {
ctx := &RenderingContext{}
ctx := newViperProvidedRenderingContext()
renderer := getHTMLRenderer(blackfriday.HTML_USE_XHTML, ctx)
flags := renderer.GetFlags()
if flags&blackfriday.HTML_USE_XHTML != blackfriday.HTML_USE_XHTML {
@@ -148,7 +149,7 @@ func TestGetHTMLRendererAllFlags(t *testing.T) {
{blackfriday.HTML_SMARTYPANTS_LATEX_DASHES},
}
defaultFlags := blackfriday.HTML_USE_XHTML
ctx := &RenderingContext{}
ctx := newViperProvidedRenderingContext()
ctx.Config = ctx.getConfig()
ctx.Config.AngledQuotes = true
ctx.Config.Fractions = true
@@ -171,7 +172,7 @@ func TestGetHTMLRendererAllFlags(t *testing.T) {
}
func TestGetHTMLRendererAnchors(t *testing.T) {
ctx := &RenderingContext{}
ctx := newViperProvidedRenderingContext()
ctx.DocumentID = "testid"
ctx.Config = ctx.getConfig()
ctx.Config.PlainIDAnchors = false
@@ -195,7 +196,7 @@ func TestGetHTMLRendererAnchors(t *testing.T) {
}
func TestGetMmarkHTMLRenderer(t *testing.T) {
ctx := &RenderingContext{}
ctx := newViperProvidedRenderingContext()
ctx.DocumentID = "testid"
ctx.Config = ctx.getConfig()
ctx.Config.PlainIDAnchors = false
@@ -219,7 +220,7 @@ func TestGetMmarkHTMLRenderer(t *testing.T) {
}
func TestGetMarkdownExtensionsMasksAreRemovedFromExtensions(t *testing.T) {
ctx := &RenderingContext{}
ctx := newViperProvidedRenderingContext()
ctx.Config = ctx.getConfig()
ctx.Config.Extensions = []string{"headerId"}
ctx.Config.ExtensionsMask = []string{"noIntraEmphasis"}
@@ -234,7 +235,7 @@ func TestGetMarkdownExtensionsByDefaultAllExtensionsAreEnabled(t *testing.T) {
type data struct {
testFlag int
}
ctx := &RenderingContext{}
ctx := newViperProvidedRenderingContext()
ctx.Config = ctx.getConfig()
ctx.Config.Extensions = []string{""}
ctx.Config.ExtensionsMask = []string{""}
@@ -266,7 +267,7 @@ func TestGetMarkdownExtensionsByDefaultAllExtensionsAreEnabled(t *testing.T) {
}
func TestGetMarkdownExtensionsAddingFlagsThroughRenderingContext(t *testing.T) {
ctx := &RenderingContext{}
ctx := newViperProvidedRenderingContext()
ctx.Config = ctx.getConfig()
ctx.Config.Extensions = []string{"definitionLists"}
ctx.Config.ExtensionsMask = []string{""}
@@ -278,7 +279,7 @@ func TestGetMarkdownExtensionsAddingFlagsThroughRenderingContext(t *testing.T) {
}
func TestGetMarkdownRenderer(t *testing.T) {
ctx := &RenderingContext{}
ctx := newViperProvidedRenderingContext()
ctx.Content = []byte("testContent")
ctx.Config = ctx.getConfig()
actualRenderedMarkdown := markdownRender(ctx)
@@ -289,7 +290,7 @@ func TestGetMarkdownRenderer(t *testing.T) {
}
func TestGetMarkdownRendererWithTOC(t *testing.T) {
ctx := &RenderingContext{RenderTOC: true}
ctx := &RenderingContext{RenderTOC: true, ConfigProvider: viper.GetViper()}
ctx.Content = []byte("testContent")
ctx.Config = ctx.getConfig()
actualRenderedMarkdown := markdownRender(ctx)
@@ -304,7 +305,7 @@ func TestGetMmarkExtensions(t *testing.T) {
type data struct {
testFlag int
}
ctx := &RenderingContext{}
ctx := newViperProvidedRenderingContext()
ctx.Config = ctx.getConfig()
ctx.Config.Extensions = []string{"tables"}
ctx.Config.ExtensionsMask = []string{""}
@@ -333,7 +334,7 @@ func TestGetMmarkExtensions(t *testing.T) {
}
func TestMmarkRender(t *testing.T) {
ctx := &RenderingContext{}
ctx := newViperProvidedRenderingContext()
ctx.Content = []byte("testContent")
ctx.Config = ctx.getConfig()
actualRenderedMarkdown := mmarkRender(ctx)