ref: 6498d73c08438c55d45900e64c2c04a375f747c2
parent: b5b6e81c0269abf9b0f4bc6a127744a25344e5c6
author: Albert Nigmatzianov <albertnigma@gmail.com>
date: Thu Apr 6 18:24:27 EDT 2017
helpers: Delete RenderingContext.getConfig
--- a/helpers/content.go
+++ b/helpers/content.go
@@ -19,6 +19,7 @@
import (
"bytes"
+ "fmt"
"html/template"
"os/exec"
"unicode"
@@ -33,7 +34,6 @@
jww "github.com/spf13/jwalterweatherman"
"strings"
- "sync"
)
// SummaryLength is the length of the summary that Hugo extracts from a content.
@@ -78,7 +78,6 @@
// NewBlackfriday creates a new Blackfriday filled with site config or some sane defaults.
func (c ContentSpec) NewBlackfriday() *Blackfriday {-
defaultParam := map[string]interface{}{"smartypants": true,
"angledQuotes": false,
@@ -212,9 +211,11 @@
b := len(ctx.DocumentID) != 0
- config := ctx.getConfig()
+ if ctx.Config == nil {+ panic(fmt.Sprintf("RenderingContext of %q doesn't have a config", ctx.DocumentID))+ }
- if b && !config.PlainIDAnchors {+ if b && !ctx.Config.PlainIDAnchors {renderParameters.FootnoteAnchorPrefix = ctx.DocumentID + ":" + renderParameters.FootnoteAnchorPrefix
renderParameters.HeaderIDSuffix = ":" + ctx.DocumentID
}
@@ -223,27 +224,27 @@
htmlFlags |= blackfriday.HTML_USE_XHTML
htmlFlags |= blackfriday.HTML_FOOTNOTE_RETURN_LINKS
- if config.Smartypants {+ if ctx.Config.Smartypants {htmlFlags |= blackfriday.HTML_USE_SMARTYPANTS
}
- if config.AngledQuotes {+ if ctx.Config.AngledQuotes {htmlFlags |= blackfriday.HTML_SMARTYPANTS_ANGLED_QUOTES
}
- if config.Fractions {+ if ctx.Config.Fractions {htmlFlags |= blackfriday.HTML_SMARTYPANTS_FRACTIONS
}
- if config.HrefTargetBlank {+ if ctx.Config.HrefTargetBlank {htmlFlags |= blackfriday.HTML_HREF_TARGET_BLANK
}
- if config.SmartDashes {+ if ctx.Config.SmartDashes {htmlFlags |= blackfriday.HTML_SMARTYPANTS_DASHES
}
- if config.LatexDashes {+ if ctx.Config.LatexDashes {htmlFlags |= blackfriday.HTML_SMARTYPANTS_LATEX_DASHES
}
@@ -271,12 +272,16 @@
blackfriday.EXTENSION_AUTO_HEADER_IDS |
blackfriday.EXTENSION_FOOTNOTES
- for _, extension := range ctx.getConfig().Extensions {+ if ctx.Config == nil {+ panic(fmt.Sprintf("RenderingContext of %q doesn't have a config", ctx.DocumentID))+ }
+
+ for _, extension := range ctx.Config.Extensions { if flag, ok := blackfridayExtensionMap[extension]; ok {flags |= flag
}
}
- for _, extension := range ctx.getConfig().ExtensionsMask {+ for _, extension := range ctx.Config.ExtensionsMask { if flag, ok := blackfridayExtensionMap[extension]; ok {flags &= ^flag
}
@@ -303,7 +308,11 @@
b := len(ctx.DocumentID) != 0
- if b && !ctx.getConfig().PlainIDAnchors {+ if ctx.Config == nil {+ panic(fmt.Sprintf("RenderingContext of %q doesn't have a config", ctx.DocumentID))+ }
+
+ if b && !ctx.Config.PlainIDAnchors {renderParameters.FootnoteAnchorPrefix = ctx.DocumentID + ":" + renderParameters.FootnoteAnchorPrefix
// renderParameters.HeaderIDSuffix = ":" + ctx.DocumentId
}
@@ -333,7 +342,11 @@
flags |= mmark.EXTENSION_NO_EMPTY_LINE_BEFORE_BLOCK
flags |= mmark.EXTENSION_INCLUDE
- for _, extension := range ctx.getConfig().Extensions {+ if ctx.Config == nil {+ panic(fmt.Sprintf("RenderingContext of %q doesn't have a config", ctx.DocumentID))+ }
+
+ for _, extension := range ctx.Config.Extensions { if flag, ok := mmarkExtensionMap[extension]; ok {flags |= flag
}
@@ -384,6 +397,7 @@
// RenderingContext holds contextual information, like content and configuration,
// for a given content rendering.
+// By creating you must set the Config, otherwise it will panic.
type RenderingContext struct {Content []byte
PageFmt string
@@ -394,22 +408,6 @@
FileResolver FileResolverFunc
LinkResolver LinkResolverFunc
Cfg config.Provider
- configInit sync.Once
-}
-
-func newRenderingContext(cfg config.Provider) *RenderingContext {- return &RenderingContext{Cfg: cfg}-}
-
-func (c *RenderingContext) getConfig() *Blackfriday {- // TODO(bep) get rid of this
- c.configInit.Do(func() {- if c.Config == nil {- cs := NewContentSpec(c.Cfg)
- c.Config = cs.NewBlackfriday()
- }
- })
- return c.Config
}
// RenderBytes renders a []byte.
--- a/helpers/content_renderer_test.go
+++ b/helpers/content_renderer_test.go
@@ -23,7 +23,7 @@
// Renders a codeblock using Blackfriday
func (c ContentSpec) render(input string) string {- ctx := newRenderingContext(c.cfg)
+ ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()}render := c.getHTMLRenderer(0, ctx)
buf := &bytes.Buffer{}@@ -33,7 +33,7 @@
// Renders a codeblock using Mmark
func (c ContentSpec) renderWithMmark(input string) string {- ctx := newRenderingContext(c.cfg)
+ ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()}render := c.getMmarkHTMLRenderer(0, ctx)
buf := &bytes.Buffer{}--- a/helpers/content_test.go
+++ b/helpers/content_test.go
@@ -37,12 +37,12 @@
{"</br> strip br2<br />", " strip br2\n"}, {"This <strong>is</strong> a\nnewline", "This is a newline"}, {"No Tags", "No Tags"},- {`<p>Summary Next Line. + {`<p>Summary Next Line.<figure >
-
+
<img src="/not/real" />
-
-
+
+
</figure>
.
More text here.</p>
@@ -152,7 +152,7 @@
func TestGetHTMLRendererFlags(t *testing.T) {c := newTestContentSpec()
- ctx := newRenderingContext(c.cfg)
+ ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()}renderer := c.getHTMLRenderer(blackfriday.HTML_USE_XHTML, ctx)
flags := renderer.GetFlags()
if flags&blackfriday.HTML_USE_XHTML != blackfriday.HTML_USE_XHTML {@@ -178,8 +178,7 @@
{blackfriday.HTML_SMARTYPANTS_LATEX_DASHES},}
defaultFlags := blackfriday.HTML_USE_XHTML
- ctx := newRenderingContext(c.cfg)
- ctx.Config = ctx.getConfig()
+ ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()}ctx.Config.AngledQuotes = true
ctx.Config.Fractions = true
ctx.Config.HrefTargetBlank = true
@@ -202,9 +201,8 @@
func TestGetHTMLRendererAnchors(t *testing.T) {c := newTestContentSpec()
- ctx := newRenderingContext(c.cfg)
+ ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()}ctx.DocumentID = "testid"
- ctx.Config = ctx.getConfig()
ctx.Config.PlainIDAnchors = false
actualRenderer := c.getHTMLRenderer(0, ctx)
@@ -227,9 +225,8 @@
func TestGetMmarkHTMLRenderer(t *testing.T) {c := newTestContentSpec()
- ctx := newRenderingContext(c.cfg)
+ ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()}ctx.DocumentID = "testid"
- ctx.Config = ctx.getConfig()
ctx.Config.PlainIDAnchors = false
actualRenderer := c.getMmarkHTMLRenderer(0, ctx)
@@ -252,8 +249,7 @@
func TestGetMarkdownExtensionsMasksAreRemovedFromExtensions(t *testing.T) {c := newTestContentSpec()
- ctx := newRenderingContext(c.cfg)
- ctx.Config = ctx.getConfig()
+ ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()} ctx.Config.Extensions = []string{"headerId"} ctx.Config.ExtensionsMask = []string{"noIntraEmphasis"}@@ -268,8 +264,7 @@
testFlag int
}
c := newTestContentSpec()
- ctx := newRenderingContext(c.cfg)
- ctx.Config = ctx.getConfig()
+ ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()} ctx.Config.Extensions = []string{""} ctx.Config.ExtensionsMask = []string{""} allExtensions := []data{@@ -301,8 +296,7 @@
func TestGetMarkdownExtensionsAddingFlagsThroughRenderingContext(t *testing.T) {c := newTestContentSpec()
- ctx := newRenderingContext(c.cfg)
- ctx.Config = ctx.getConfig()
+ ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()} ctx.Config.Extensions = []string{"definitionLists"} ctx.Config.ExtensionsMask = []string{""}@@ -314,9 +308,8 @@
func TestGetMarkdownRenderer(t *testing.T) {c := newTestContentSpec()
- ctx := newRenderingContext(c.cfg)
+ ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()} ctx.Content = []byte("testContent")- ctx.Config = ctx.getConfig()
actualRenderedMarkdown := c.markdownRender(ctx)
expectedRenderedMarkdown := []byte("<p>testContent</p>\n") if !bytes.Equal(actualRenderedMarkdown, expectedRenderedMarkdown) {@@ -326,9 +319,8 @@
func TestGetMarkdownRendererWithTOC(t *testing.T) {c := newTestContentSpec()
- ctx := &RenderingContext{RenderTOC: true, Cfg: c.cfg}+ ctx := &RenderingContext{RenderTOC: true, Cfg: c.cfg, Config: c.NewBlackfriday()} ctx.Content = []byte("testContent")- ctx.Config = ctx.getConfig()
actualRenderedMarkdown := c.markdownRender(ctx)
expectedRenderedMarkdown := []byte("<nav>\n</nav>\n\n<p>testContent</p>\n") if !bytes.Equal(actualRenderedMarkdown, expectedRenderedMarkdown) {@@ -342,8 +334,7 @@
testFlag int
}
c := newTestContentSpec()
- ctx := newRenderingContext(c.cfg)
- ctx.Config = ctx.getConfig()
+ ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()} ctx.Config.Extensions = []string{"tables"} ctx.Config.ExtensionsMask = []string{""} allExtensions := []data{@@ -372,9 +363,8 @@
func TestMmarkRender(t *testing.T) {c := newTestContentSpec()
- ctx := newRenderingContext(c.cfg)
+ ctx := &RenderingContext{Cfg: c.cfg, Config: c.NewBlackfriday()} ctx.Content = []byte("testContent")- ctx.Config = ctx.getConfig()
actualRenderedMarkdown := c.mmarkRender(ctx)
expectedRenderedMarkdown := []byte("<p>testContent</p>\n") if !bytes.Equal(actualRenderedMarkdown, expectedRenderedMarkdown) {--
⑨