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 | |
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')
-rw-r--r-- | helpers/content.go | 44 | ||||
-rw-r--r-- | helpers/content_renderer.go | 13 | ||||
-rw-r--r-- | helpers/content_renderer_test.go | 54 | ||||
-rw-r--r-- | helpers/pygments.go | 240 | ||||
-rw-r--r-- | helpers/pygments_test.go | 201 | ||||
-rw-r--r-- | helpers/testhelpers_test.go | 6 |
6 files changed, 480 insertions, 78 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, } } diff --git a/helpers/content_renderer.go b/helpers/content_renderer.go index 63be58104..9026a683b 100644 --- a/helpers/content_renderer.go +++ b/helpers/content_renderer.go @@ -16,6 +16,7 @@ package helpers import ( "bytes" "html" + "strings" "github.com/gohugoio/hugo/config" "github.com/miekg/mmark" @@ -25,6 +26,7 @@ import ( // HugoHTMLRenderer wraps a blackfriday.Renderer, typically a blackfriday.Html // Enabling Hugo to customise the rendering experience type HugoHTMLRenderer struct { + cs *ContentSpec *RenderingContext blackfriday.Renderer } @@ -34,8 +36,9 @@ type HugoHTMLRenderer struct { func (r *HugoHTMLRenderer) BlockCode(out *bytes.Buffer, text []byte, lang string) { if r.Cfg.GetBool("pygmentsCodeFences") && (lang != "" || r.Cfg.GetBool("pygmentsCodeFencesGuessSyntax")) { opts := r.Cfg.GetString("pygmentsOptions") - str := html.UnescapeString(string(text)) - out.WriteString(Highlight(r.RenderingContext.Cfg, str, lang, opts)) + str := strings.Trim(html.UnescapeString(string(text)), "\n\r") + highlighted, _ := r.cs.Highlight(str, lang, opts) + out.WriteString(highlighted) } else { r.Renderer.BlockCode(out, text, lang) } @@ -88,6 +91,7 @@ func (r *HugoHTMLRenderer) List(out *bytes.Buffer, text func() bool, flags int) // HugoMmarkHTMLRenderer wraps a mmark.Renderer, typically a mmark.html, // enabling Hugo to customise the rendering experience. type HugoMmarkHTMLRenderer struct { + cs *ContentSpec mmark.Renderer Cfg config.Provider } @@ -96,8 +100,9 @@ type HugoMmarkHTMLRenderer struct { // Pygments is used if it is setup to handle code fences. func (r *HugoMmarkHTMLRenderer) BlockCode(out *bytes.Buffer, text []byte, lang string, caption []byte, subfigure bool, callouts bool) { if r.Cfg.GetBool("pygmentsCodeFences") && (lang != "" || r.Cfg.GetBool("pygmentsCodeFencesGuessSyntax")) { - str := html.UnescapeString(string(text)) - out.WriteString(Highlight(r.Cfg, str, lang, "")) + str := strings.Trim(html.UnescapeString(string(text)), "\n\r") + highlighted, _ := r.cs.Highlight(str, lang, "") + out.WriteString(highlighted) } else { r.Renderer.BlockCode(out, text, lang, caption, subfigure, callouts) } diff --git a/helpers/content_renderer_test.go b/helpers/content_renderer_test.go index 3bd038547..698e3a151 100644 --- a/helpers/content_renderer_test.go +++ b/helpers/content_renderer_test.go @@ -19,6 +19,7 @@ import ( "testing" "github.com/spf13/viper" + "github.com/stretchr/testify/require" ) // Renders a codeblock using Blackfriday @@ -42,11 +43,7 @@ func (c ContentSpec) renderWithMmark(input string) string { } func TestCodeFence(t *testing.T) { - - if !HasPygments() { - t.Skip("Skipping Pygments test as Pygments is not installed or available.") - return - } + assert := require.New(t) type test struct { enabled bool @@ -55,36 +52,39 @@ func TestCodeFence(t *testing.T) { // Pygments 2.0 and 2.1 have slightly different outputs so only do partial matching data := []test{ - {true, "<html></html>", `(?s)^<div class="highlight"><pre><code class="language-html" data-lang="html">.*?</code></pre></div>\n$`}, - {false, "<html></html>", `(?s)^<pre><code class="language-html">.*?</code></pre>\n$`}, + {true, "<html></html>", `(?s)^<div class="highlight">\n?<pre.*><code class="language-html" data-lang="html">.*?</code></pre>\n?</div>\n?$`}, + {false, "<html></html>", `(?s)^<pre.*><code class="language-html">.*?</code></pre>\n$`}, } - for i, d := range data { - v := viper.New() + for _, useClassic := range []bool{false, true} { + for i, d := range data { + v := viper.New() + v.Set("pygmentsStyle", "monokai") + v.Set("pygmentsUseClasses", true) + v.Set("pygmentsCodeFences", d.enabled) + v.Set("pygmentsUseClassic", useClassic) - v.Set("pygmentsStyle", "monokai") - v.Set("pygmentsUseClasses", true) - v.Set("pygmentsCodeFences", d.enabled) + c, err := NewContentSpec(v) + assert.NoError(err) - c := NewContentSpec(v) + result := c.render(d.input) - result := c.render(d.input) + expectedRe, err := regexp.Compile(d.expected) - expectedRe, err := regexp.Compile(d.expected) + if err != nil { + t.Fatal("Invalid regexp", err) + } + matched := expectedRe.MatchString(result) - if err != nil { - t.Fatal("Invalid regexp", err) - } - matched := expectedRe.MatchString(result) - - if !matched { - t.Errorf("Test %d failed. BlackFriday enabled:%t, Expected:\n%q got:\n%q", i, d.enabled, d.expected, result) - } + if !matched { + t.Errorf("Test %d failed. BlackFriday enabled:%t, Expected:\n%q got:\n%q", i, d.enabled, d.expected, result) + } - result = c.renderWithMmark(d.input) - matched = expectedRe.MatchString(result) - if !matched { - t.Errorf("Test %d failed. Mmark enabled:%t, Expected:\n%q got:\n%q", i, d.enabled, d.expected, result) + result = c.renderWithMmark(d.input) + matched = expectedRe.MatchString(result) + if !matched { + t.Errorf("Test %d failed. Mmark enabled:%t, Expected:\n%q got:\n%q", i, d.enabled, d.expected, result) + } } } } diff --git a/helpers/pygments.go b/helpers/pygments.go index 60f62a88f..9253445e7 100644 --- a/helpers/pygments.go +++ b/helpers/pygments.go @@ -21,9 +21,18 @@ import ( "io/ioutil" "os/exec" "path/filepath" + "regexp" "sort" + "strconv" "strings" + "github.com/alecthomas/chroma" + "github.com/alecthomas/chroma/formatters" + "github.com/alecthomas/chroma/formatters/html" + "github.com/alecthomas/chroma/lexers" + "github.com/alecthomas/chroma/styles" + bp "github.com/gohugoio/hugo/bufferpool" + "github.com/gohugoio/hugo/config" "github.com/gohugoio/hugo/hugofs" jww "github.com/spf13/jwalterweatherman" @@ -31,27 +40,62 @@ import ( const pygmentsBin = "pygmentize" -// HasPygments checks to see if Pygments is installed and available +// TODO(bep) document chroma -s perldoc --html --html-styles +// hasPygments checks to see if Pygments is installed and available // on the system. -func HasPygments() bool { +func hasPygments() bool { if _, err := exec.LookPath(pygmentsBin); err != nil { return false } return true } -// Highlight takes some code and returns highlighted code. -func Highlight(cfg config.Provider, code, lang, optsStr string) string { - if !HasPygments() { - jww.WARN.Println("Highlighting requires Pygments to be installed and in the path") - return code +type highlighters struct { + cs *ContentSpec + ignoreCache bool + cacheDir string +} + +func newHiglighters(cs *ContentSpec) highlighters { + return highlighters{cs: cs, ignoreCache: cs.cfg.GetBool("ignoreCache"), cacheDir: cs.cfg.GetString("cacheDir")} +} + +func (h highlighters) chromaHighlight(code, lang, optsStr string) (string, error) { + opts, err := h.cs.parsePygmentsOpts(optsStr) + if err != nil { + jww.ERROR.Print(err.Error()) + return code, err } - options, err := parsePygmentsOpts(cfg, optsStr) + style, found := opts["style"] + if !found || style == "" { + style = "friendly" + } + f, err := h.cs.chromaFormatterFromOptions(opts) if err != nil { jww.ERROR.Print(err.Error()) - return code + return code, err + } + + b := bp.GetBuffer() + defer bp.PutBuffer(b) + + err = chromaHighlight(b, code, lang, style, f) + if err != nil { + jww.ERROR.Print(err.Error()) + return code, err + } + + return h.injectCodeTag(`<div class="highlight">`+b.String()+"</div>", lang), nil +} + +func (h highlighters) pygmentsHighlight(code, lang, optsStr string) (string, error) { + options, err := h.cs.createPygmentsOptionsString(optsStr) + + if err != nil { + jww.ERROR.Print(err.Error()) + return code, nil } // Try to read from cache first @@ -62,32 +106,30 @@ func Highlight(cfg config.Provider, code, lang, optsStr string) string { fs := hugofs.Os - ignoreCache := cfg.GetBool("ignoreCache") - cacheDir := cfg.GetString("cacheDir") var cachefile string - if !ignoreCache && cacheDir != "" { - cachefile = filepath.Join(cacheDir, fmt.Sprintf("pygments-%x", hash.Sum(nil))) + if !h.ignoreCache && h.cacheDir != "" { + cachefile = filepath.Join(h.cacheDir, fmt.Sprintf("pygments-%x", hash.Sum(nil))) exists, err := Exists(cachefile, fs) if err != nil { jww.ERROR.Print(err.Error()) - return code + return code, nil } if exists { f, err := fs.Open(cachefile) if err != nil { jww.ERROR.Print(err.Error()) - return code + return code, nil } s, err := ioutil.ReadAll(f) if err != nil { jww.ERROR.Print(err.Error()) - return code + return code, nil } - return string(s) + return string(s), nil } } @@ -109,26 +151,58 @@ func Highlight(cfg config.Provider, code, lang, optsStr string) string { if err := cmd.Run(); err != nil { jww.ERROR.Print(stderr.String()) - return code + return code, err } str := string(normalizeExternalHelperLineFeeds([]byte(out.String()))) - // inject code tag into Pygments output - if lang != "" && strings.Contains(str, "<pre>") { - codeTag := fmt.Sprintf(`<pre><code class="language-%s" data-lang="%s">`, lang, lang) - str = strings.Replace(str, "<pre>", codeTag, 1) - str = strings.Replace(str, "</pre>", "</code></pre>", 1) - } + str = h.injectCodeTag(str, lang) - if !ignoreCache && cachefile != "" { + if !h.ignoreCache && cachefile != "" { // Write cache file if err := WriteToDisk(cachefile, strings.NewReader(str), fs); err != nil { jww.ERROR.Print(stderr.String()) } } - return str + return str, nil +} + +var preRe = regexp.MustCompile(`(?s)(.*?<pre.*?>)(.*?)(</pre>)`) + +func (h highlighters) injectCodeTag(code, lang string) string { + if lang == "" { + return code + } + codeTag := fmt.Sprintf(`<code class="language-%s" data-lang="%s">`, lang, lang) + return preRe.ReplaceAllString(code, fmt.Sprintf("$1%s$2</code>$3", codeTag)) +} + +func chromaHighlight(w io.Writer, source, lexer, style string, f chroma.Formatter) error { + l := lexers.Get(lexer) + if l == nil { + l = lexers.Analyse(source) + } + if l == nil { + l = lexers.Fallback + } + l = chroma.Coalesce(l) + + if f == nil { + f = formatters.Fallback + } + + s := styles.Get(style) + if s == nil { + s = styles.Fallback + } + + it, err := l.Tokenise(nil, source) + if err != nil { + return err + } + + return f.Format(w, s, it) } var pygmentsKeywords = make(map[string]bool) @@ -158,23 +232,30 @@ func init() { pygmentsKeywords["startinline"] = true } -func parseOptions(options map[string]string, in string) error { +func parseOptions(defaults map[string]string, in string) (map[string]string, error) { in = strings.Trim(in, " ") + opts := make(map[string]string) + + if defaults != nil { + for k, v := range defaults { + opts[k] = v + } + } if in == "" { - return nil + return opts, nil } for _, v := range strings.Split(in, ",") { keyVal := strings.Split(v, "=") key := strings.ToLower(strings.Trim(keyVal[0], " ")) if len(keyVal) != 2 || !pygmentsKeywords[key] { - return fmt.Errorf("invalid Pygments option: %s", key) + return opts, fmt.Errorf("invalid Pygments option: %s", key) } - options[key] = keyVal[1] + opts[key] = keyVal[1] } - return nil + return opts, nil } func createOptionsString(options map[string]string) string { @@ -196,8 +277,7 @@ func createOptionsString(options map[string]string) string { } func parseDefaultPygmentsOpts(cfg config.Provider) (map[string]string, error) { - options := make(map[string]string) - err := parseOptions(options, cfg.GetString("pygmentsOptions")) + options, err := parseOptions(nil, cfg.GetString("pygmentsOptions")) if err != nil { return nil, err } @@ -222,16 +302,100 @@ func parseDefaultPygmentsOpts(cfg config.Provider) (map[string]string, error) { return options, nil } -func parsePygmentsOpts(cfg config.Provider, in string) (string, error) { - options, err := parseDefaultPygmentsOpts(cfg) +func (cs *ContentSpec) chromaFormatterFromOptions(pygmentsOpts map[string]string) (chroma.Formatter, error) { + var options = []html.Option{html.TabWidth(4)} + + if pygmentsOpts["noclasses"] == "false" { + options = append(options, html.WithClasses()) + } + + if pygmentsOpts["linenos"] != "" { + options = append(options, html.WithLineNumbers()) + } + + startLineStr := pygmentsOpts["linenostart"] + var startLine = 1 + if startLineStr != "" { + + line, err := strconv.Atoi(strings.TrimSpace(startLineStr)) + if err == nil { + startLine = line + options = append(options, html.BaseLineNumber(startLine)) + } + } + + hlLines := pygmentsOpts["hl_lines"] + + if hlLines != "" { + ranges, err := hlLinesToRanges(startLine, hlLines) + + if err == nil { + options = append(options, html.HighlightLines(ranges)) + } + } + + return html.New(options...), nil +} + +func (cs *ContentSpec) parsePygmentsOpts(in string) (map[string]string, error) { + opts, err := parseOptions(cs.defatultPygmentsOpts, in) if err != nil { - return "", err + return nil, err } + return opts, nil + +} - err = parseOptions(options, in) +func (cs *ContentSpec) createPygmentsOptionsString(in string) (string, error) { + opts, err := cs.parsePygmentsOpts(in) if err != nil { return "", err } + return createOptionsString(opts), nil +} + +// startLine compansates for https://github.com/alecthomas/chroma/issues/30 +func hlLinesToRanges(startLine int, s string) ([][2]int, error) { + var ranges [][2]int + s = strings.TrimSpace(s) + + if s == "" { + return ranges, nil + } + + // Variants: + // 1 2 3 4 + // 1-2 3-4 + // 1-2 3 + // 1 3-4 + // 1 3-4 + fields := strings.Split(s, " ") + for _, field := range fields { + field = strings.TrimSpace(field) + if field == "" { + continue + } + numbers := strings.Split(field, "-") + var r [2]int + first, err := strconv.Atoi(numbers[0]) + if err != nil { + return ranges, err + } + first = first + startLine - 1 + r[0] = first + if len(numbers) > 1 { + second, err := strconv.Atoi(numbers[1]) + if err != nil { + return ranges, err + } + second = second + startLine - 1 + r[1] = second + } else { + r[1] = first + } + + ranges = append(ranges, r) + } + return ranges, nil - return createOptionsString(options), nil } diff --git a/helpers/pygments_test.go b/helpers/pygments_test.go index 1fce17859..ee8076c71 100644 --- a/helpers/pygments_test.go +++ b/helpers/pygments_test.go @@ -14,12 +14,19 @@ package helpers import ( + "fmt" + "reflect" "testing" + "github.com/alecthomas/chroma/formatters/html" + "github.com/spf13/viper" + "github.com/stretchr/testify/require" ) func TestParsePygmentsArgs(t *testing.T) { + assert := require.New(t) + for i, this := range []struct { in string pygmentsStyle string @@ -38,8 +45,10 @@ func TestParsePygmentsArgs(t *testing.T) { v := viper.New() v.Set("pygmentsStyle", this.pygmentsStyle) v.Set("pygmentsUseClasses", this.pygmentsUseClasses) + spec, err := NewContentSpec(v) + assert.NoError(err) - result1, err := parsePygmentsOpts(v, this.in) + result1, err := spec.createPygmentsOptionsString(this.in) if b, ok := this.expect1.(bool); ok && !b { if err == nil { t.Errorf("[%d] parsePygmentArgs didn't return an expected error", i) @@ -58,6 +67,8 @@ func TestParsePygmentsArgs(t *testing.T) { } func TestParseDefaultPygmentsArgs(t *testing.T) { + assert := require.New(t) + expect := "encoding=utf8,noclasses=false,style=foo" for i, this := range []struct { @@ -83,7 +94,10 @@ func TestParseDefaultPygmentsArgs(t *testing.T) { v.Set("pygmentsUseClasses", b) } - result, err := parsePygmentsOpts(v, this.in) + spec, err := NewContentSpec(v) + assert.NoError(err) + + result, err := spec.createPygmentsOptionsString(this.in) if err != nil { t.Errorf("[%d] parsePygmentArgs failed: %s", i, err) continue @@ -93,3 +107,186 @@ func TestParseDefaultPygmentsArgs(t *testing.T) { } } } + +type chromaInfo struct { + classes bool + lineNumbers bool + highlightRangesLen int + highlightRangesStr string + baseLineNumber int +} + +func formatterChromaInfo(f *html.Formatter) chromaInfo { + v := reflect.ValueOf(f).Elem() + c := chromaInfo{} + // Hack: + c.classes = v.FieldByName("classes").Bool() + c.lineNumbers = v.FieldByName("lineNumbers").Bool() + c.baseLineNumber = int(v.FieldByName("baseLineNumber").Int()) + vv := v.FieldByName("highlightRanges") + c.highlightRangesLen = vv.Len() + c.highlightRangesStr = fmt.Sprint(vv) + + return c +} + +func TestChromaHTMLHighlight(t *testing.T) { + assert := require.New(t) + + v := viper.New() + v.Set("pygmentsUseClasses", true) + spec, err := NewContentSpec(v) + assert.NoError(err) + + result, err := spec.Highlight(`echo "Hello"`, "bash", "") + assert.NoError(err) + + assert.Contains(result, `<code class="language-bash" data-lang="bash"><span class="s7d2">echo</span> <span class="sc1c">"Hello"</span></code>`) + +} + +func TestChromaHTMLFormatterFromOptions(t *testing.T) { + assert := require.New(t) + + for i, this := range []struct { + in string + pygmentsStyle interface{} + pygmentsUseClasses interface{} + pygmentsOptions string + assert func(c chromaInfo) + }{ + {"", "monokai", true, "style=manni,noclasses=true", func(c chromaInfo) { + assert.True(c.classes) + assert.False(c.lineNumbers) + assert.Equal(0, c.highlightRangesLen) + + }}, + {"", nil, nil, "style=monokai,noclasses=false", func(c chromaInfo) { + assert.True(c.classes) + }}, + {"linenos=sure,hl_lines=1 2 3", nil, nil, "style=monokai,noclasses=false", func(c chromaInfo) { + assert.True(c.classes) + assert.True(c.lineNumbers) + assert.Equal(3, c.highlightRangesLen) + assert.Equal("[[1 1] [2 2] [3 3]]", c.highlightRangesStr) + assert.Equal(1, c.baseLineNumber) + }}, + {"linenos=sure,hl_lines=1,linenostart=4", nil, nil, "style=monokai,noclasses=false", func(c chromaInfo) { + assert.True(c.classes) + assert.True(c.lineNumbers) + assert.Equal(1, c.highlightRangesLen) + // This compansates for https://github.com/alecthomas/chroma/issues/30 + assert.Equal("[[4 4]]", c.highlightRangesStr) + assert.Equal(4, c.baseLineNumber) + }}, + {"style=monokai,noclasses=false", nil, nil, "style=manni,noclasses=true", func(c chromaInfo) { + assert.True(c.classes) + }}, + {"style=monokai,noclasses=true", "friendly", false, "style=manni,noclasses=false", func(c chromaInfo) { + assert.False(c.classes) + }}, + } { + v := viper.New() + + v.Set("pygmentsOptions", this.pygmentsOptions) + + if s, ok := this.pygmentsStyle.(string); ok { + v.Set("pygmentsStyle", s) + } + + if b, ok := this.pygmentsUseClasses.(bool); ok { + v.Set("pygmentsUseClasses", b) + } + + spec, err := NewContentSpec(v) + assert.NoError(err) + + opts, err := spec.parsePygmentsOpts(this.in) + if err != nil { + t.Fatalf("[%d] parsePygmentsOpts failed: %s", i, err) + } + + chromaFormatter, err := spec.chromaFormatterFromOptions(opts) + if err != nil { + t.Fatalf("[%d] chromaFormatterFromOptions failed: %s", i, err) + } + + this.assert(formatterChromaInfo(chromaFormatter.(*html.Formatter))) + } +} + +func TestHlLinesToRanges(t *testing.T) { + var zero [][2]int + + for _, this := range []struct { + in string + startLine int + expected interface{} + }{ + {"", 1, zero}, + {"1 4", 1, [][2]int{[2]int{1, 1}, [2]int{4, 4}}}, + {"1 4", 2, [][2]int{[2]int{2, 2}, [2]int{5, 5}}}, + {"1-4 5-8", 1, [][2]int{[2]int{1, 4}, [2]int{5, 8}}}, + {" 1 4 ", 1, [][2]int{[2]int{1, 1}, [2]int{4, 4}}}, + {"1-4 5-8 ", 1, [][2]int{[2]int{1, 4}, [2]int{5, 8}}}, + {"1-4 5", 1, [][2]int{[2]int{1, 4}, [2]int{5, 5}}}, + {"4 5-9", 1, [][2]int{[2]int{4, 4}, [2]int{5, 9}}}, + {" 1 -4 5 - 8 ", 1, true}, + {"a b", 1, true}, + } { + got, err := hlLinesToRanges(this.startLine, this.in) + + if expectErr, ok := this.expected.(bool); ok && expectErr { + if err == nil { + t.Fatal("No error") + } + } else if err != nil { + t.Fatalf("Got error: %s", err) + } else if !reflect.DeepEqual(this.expected, got) { + t.Fatalf("Expected\n%v but got\n%v", this.expected, got) + } + } +} + +func BenchmarkChromaHighlight(b *testing.B) { + assert := require.New(b) + v := viper.New() + + v.Set("pygmentsstyle", "trac") + v.Set("pygmentsuseclasses", false) + v.Set("pygmentsuseclassic", false) + + code := `// GetTitleFunc returns a func that can be used to transform a string to +// title case. +// +// The supported styles are +// +// - "Go" (strings.Title) +// - "AP" (see https://www.apstylebook.com/) +// - "Chicago" (see http://www.chicagomanualofstyle.org/home.html) +// +// If an unknown or empty style is provided, AP style is what you get. +func GetTitleFunc(style string) func(s string) string { + switch strings.ToLower(style) { + case "go": + return strings.Title + case "chicago": + tc := transform.NewTitleConverter(transform.ChicagoStyle) + return tc.Title + default: + tc := transform.NewTitleConverter(transform.APStyle) + return tc.Title + } +} +` + + spec, err := NewContentSpec(v) + assert.NoError(err) + + for i := 0; i < b.N; i++ { + _, err := spec.Highlight(code, "go", "linenos=inline,hl_lines=8 15-17") + if err != nil { + b.Fatal(err) + } + } +} diff --git a/helpers/testhelpers_test.go b/helpers/testhelpers_test.go index 86f141146..518a5bc23 100644 --- a/helpers/testhelpers_test.go +++ b/helpers/testhelpers_test.go @@ -34,5 +34,9 @@ func newTestCfg(fs *hugofs.Fs) *viper.Viper { func newTestContentSpec() *ContentSpec { v := viper.New() - return NewContentSpec(v) + spec, err := NewContentSpec(v) + if err != nil { + panic(err) + } + return spec } |