mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
helpers: Delete RenderingContext.getConfig
This commit is contained in:
parent
b5b6e81c02
commit
6498d73c08
3 changed files with 45 additions and 57 deletions
|
@ -19,6 +19,7 @@ package helpers
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"os/exec"
|
||||
"unicode"
|
||||
|
@ -33,7 +34,6 @@ import (
|
|||
jww "github.com/spf13/jwalterweatherman"
|
||||
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
// SummaryLength is the length of the summary that Hugo extracts from a content.
|
||||
|
@ -78,7 +78,6 @@ type Blackfriday struct {
|
|||
|
||||
// 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 @@ func (c ContentSpec) getHTMLRenderer(defaultFlags int, ctx *RenderingContext) bl
|
|||
|
||||
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 @@ func (c ContentSpec) getHTMLRenderer(defaultFlags int, ctx *RenderingContext) bl
|
|||
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 @@ func getMarkdownExtensions(ctx *RenderingContext) int {
|
|||
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 @@ func (c ContentSpec) getMmarkHTMLRenderer(defaultFlags int, ctx *RenderingContex
|
|||
|
||||
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 @@ func getMmarkExtensions(ctx *RenderingContext) int {
|
|||
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 @@ func ExtractTOC(content []byte) (newcontent []byte, toc []byte) {
|
|||
|
||||
// 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 @@ type RenderingContext struct {
|
|||
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.
|
||||
|
|
|
@ -23,7 +23,7 @@ import (
|
|||
|
||||
// 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 @@ func (c ContentSpec) render(input string) string {
|
|||
|
||||
// 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{}
|
||||
|
|
|
@ -152,7 +152,7 @@ func TestTruncateWordsByRune(t *testing.T) {
|
|||
|
||||
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 @@ func TestGetHTMLRendererAllFlags(t *testing.T) {
|
|||
{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 TestGetHTMLRendererAllFlags(t *testing.T) {
|
|||
|
||||
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 TestGetHTMLRendererAnchors(t *testing.T) {
|
|||
|
||||
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 TestGetMmarkHTMLRenderer(t *testing.T) {
|
|||
|
||||
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 @@ func TestGetMarkdownExtensionsByDefaultAllExtensionsAreEnabled(t *testing.T) {
|
|||
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 TestGetMarkdownExtensionsByDefaultAllExtensionsAreEnabled(t *testing.T) {
|
|||
|
||||
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 TestGetMarkdownExtensionsAddingFlagsThroughRenderingContext(t *testing.T) {
|
|||
|
||||
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 TestGetMarkdownRenderer(t *testing.T) {
|
|||
|
||||
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 @@ func TestGetMmarkExtensions(t *testing.T) {
|
|||
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 TestGetMmarkExtensions(t *testing.T) {
|
|||
|
||||
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) {
|
||||
|
|
Loading…
Reference in a new issue