diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-09-25 08:59:02 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-25 08:59:02 +0200 |
commit | fb33d8286d78a78a74deb44355b621852a1c4033 (patch) | |
tree | a726e33fbaa0ca7a315a2e12021306fd5b27690e /helpers/content.go | |
parent | 81ed564793609a32be20a569cc15da2cc02dd734 (diff) |
Use Chroma as new default syntax highlighter
If you want to use Pygments, set `pygmentsUseClassic=true` in your site config.
Fixes #3888
Diffstat (limited to 'helpers/content.go')
-rw-r--r-- | helpers/content.go | 44 |
1 files changed, 38 insertions, 6 deletions
diff --git a/helpers/content.go b/helpers/content.go index 3c81fcd31..7f5975869 100644 --- a/helpers/content.go +++ b/helpers/content.go @@ -48,19 +48,49 @@ type ContentSpec struct { footnoteAnchorPrefix string footnoteReturnLinkContents string + Highlight func(code, lang, optsStr string) (string, error) + defatultPygmentsOpts map[string]string + cfg config.Provider } // NewContentSpec returns a ContentSpec initialized // with the appropriate fields from the given config.Provider. -func NewContentSpec(cfg config.Provider) *ContentSpec { - return &ContentSpec{ +func NewContentSpec(cfg config.Provider) (*ContentSpec, error) { + spec := &ContentSpec{ blackfriday: cfg.GetStringMap("blackfriday"), footnoteAnchorPrefix: cfg.GetString("footnoteAnchorPrefix"), footnoteReturnLinkContents: cfg.GetString("footnoteReturnLinkContents"), cfg: cfg, } + + // Highlighting setup + options, err := parseDefaultPygmentsOpts(cfg) + if err != nil { + return nil, err + } + spec.defatultPygmentsOpts = options + + // Use the Pygmentize on path if present + useClassic := false + h := newHiglighters(spec) + + if cfg.GetBool("pygmentsUseClassic") { + if !hasPygments() { + jww.WARN.Println("Highlighting with pygmentsUseClassic set requires Pygments to be installed and in the path") + } else { + useClassic = true + } + } + + if useClassic { + spec.Highlight = h.pygmentsHighlight + } else { + spec.Highlight = h.chromaHighlight + } + + return spec, nil } // Blackfriday holds configuration values for Blackfriday rendering. @@ -198,7 +228,7 @@ func BytesToHTML(b []byte) template.HTML { } // getHTMLRenderer creates a new Blackfriday HTML Renderer with the given configuration. -func (c ContentSpec) getHTMLRenderer(defaultFlags int, ctx *RenderingContext) blackfriday.Renderer { +func (c *ContentSpec) getHTMLRenderer(defaultFlags int, ctx *RenderingContext) blackfriday.Renderer { renderParameters := blackfriday.HtmlRendererParameters{ FootnoteAnchorPrefix: c.footnoteAnchorPrefix, FootnoteReturnLinkContents: c.footnoteReturnLinkContents, @@ -248,6 +278,7 @@ func (c ContentSpec) getHTMLRenderer(defaultFlags int, ctx *RenderingContext) bl } return &HugoHTMLRenderer{ + cs: c, RenderingContext: ctx, Renderer: blackfriday.HtmlRendererWithParameters(htmlFlags, "", "", renderParameters), } @@ -299,7 +330,7 @@ func (c ContentSpec) markdownRender(ctx *RenderingContext) []byte { } // getMmarkHTMLRenderer creates a new mmark HTML Renderer with the given configuration. -func (c ContentSpec) getMmarkHTMLRenderer(defaultFlags int, ctx *RenderingContext) mmark.Renderer { +func (c *ContentSpec) getMmarkHTMLRenderer(defaultFlags int, ctx *RenderingContext) mmark.Renderer { renderParameters := mmark.HtmlRendererParameters{ FootnoteAnchorPrefix: c.footnoteAnchorPrefix, FootnoteReturnLinkContents: c.footnoteReturnLinkContents, @@ -320,8 +351,9 @@ func (c ContentSpec) getMmarkHTMLRenderer(defaultFlags int, ctx *RenderingContex htmlFlags |= mmark.HTML_FOOTNOTE_RETURN_LINKS return &HugoMmarkHTMLRenderer{ - mmark.HtmlRendererWithParameters(htmlFlags, "", "", renderParameters), - c.cfg, + cs: c, + Renderer: mmark.HtmlRendererWithParameters(htmlFlags, "", "", renderParameters), + Cfg: c.cfg, } } |