2015-12-10 17:19:38 -05:00
|
|
|
// Copyright 2015 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.
|
|
|
|
|
2015-04-15 14:31:05 -04:00
|
|
|
package helpers
|
|
|
|
|
|
|
|
import (
|
2017-09-25 02:59:02 -04:00
|
|
|
"fmt"
|
|
|
|
"reflect"
|
2015-04-15 14:31:05 -04:00
|
|
|
"testing"
|
2015-05-20 02:21:21 -04:00
|
|
|
|
2017-09-25 02:59:02 -04:00
|
|
|
"github.com/alecthomas/chroma/formatters/html"
|
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
qt "github.com/frankban/quicktest"
|
2015-05-20 02:21:21 -04:00
|
|
|
"github.com/spf13/viper"
|
2015-04-15 14:31:05 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestParsePygmentsArgs(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2017-09-25 02:59:02 -04:00
|
|
|
|
2015-04-15 14:31:05 -04:00
|
|
|
for i, this := range []struct {
|
|
|
|
in string
|
|
|
|
pygmentsStyle string
|
|
|
|
pygmentsUseClasses bool
|
|
|
|
expect1 interface{}
|
|
|
|
}{
|
2015-07-08 21:51:54 -04:00
|
|
|
{"", "foo", true, "encoding=utf8,noclasses=false,style=foo"},
|
2015-04-15 14:31:05 -04:00
|
|
|
{"style=boo,noclasses=true", "foo", true, "encoding=utf8,noclasses=true,style=boo"},
|
|
|
|
{"Style=boo, noClasses=true", "foo", true, "encoding=utf8,noclasses=true,style=boo"},
|
|
|
|
{"noclasses=true", "foo", true, "encoding=utf8,noclasses=true,style=foo"},
|
|
|
|
{"style=boo", "foo", true, "encoding=utf8,noclasses=false,style=boo"},
|
|
|
|
{"boo=invalid", "foo", false, false},
|
|
|
|
{"style", "foo", false, false},
|
|
|
|
} {
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
v := viper.New()
|
|
|
|
v.Set("pygmentsStyle", this.pygmentsStyle)
|
|
|
|
v.Set("pygmentsUseClasses", this.pygmentsUseClasses)
|
2019-08-16 09:55:03 -04:00
|
|
|
spec, err := NewContentSpec(v, nil, nil)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2017-02-04 22:20:06 -05:00
|
|
|
|
2017-09-25 02:59:02 -04:00
|
|
|
result1, err := spec.createPygmentsOptionsString(this.in)
|
2015-04-15 14:31:05 -04:00
|
|
|
if b, ok := this.expect1.(bool); ok && !b {
|
|
|
|
if err == nil {
|
|
|
|
t.Errorf("[%d] parsePygmentArgs didn't return an expected error", i)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] parsePygmentArgs failed: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if result1 != this.expect1 {
|
|
|
|
t.Errorf("[%d] parsePygmentArgs got %v but expected %v", i, result1, this.expect1)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-08 21:51:54 -04:00
|
|
|
|
|
|
|
func TestParseDefaultPygmentsArgs(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2017-09-25 02:59:02 -04:00
|
|
|
|
2015-07-08 21:51:54 -04:00
|
|
|
expect := "encoding=utf8,noclasses=false,style=foo"
|
|
|
|
|
|
|
|
for i, this := range []struct {
|
|
|
|
in string
|
|
|
|
pygmentsStyle interface{}
|
|
|
|
pygmentsUseClasses interface{}
|
|
|
|
pygmentsOptions string
|
|
|
|
}{
|
|
|
|
{"", "foo", true, "style=override,noclasses=override"},
|
|
|
|
{"", nil, nil, "style=foo,noclasses=false"},
|
|
|
|
{"style=foo,noclasses=false", nil, nil, "style=override,noclasses=override"},
|
|
|
|
{"style=foo,noclasses=false", "override", false, "style=override,noclasses=override"},
|
|
|
|
} {
|
2017-02-04 22:20:06 -05:00
|
|
|
v := viper.New()
|
2015-07-08 21:51:54 -04:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
v.Set("pygmentsOptions", this.pygmentsOptions)
|
2015-07-08 21:51:54 -04:00
|
|
|
|
|
|
|
if s, ok := this.pygmentsStyle.(string); ok {
|
2017-02-04 22:20:06 -05:00
|
|
|
v.Set("pygmentsStyle", s)
|
2015-07-08 21:51:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if b, ok := this.pygmentsUseClasses.(bool); ok {
|
2017-02-04 22:20:06 -05:00
|
|
|
v.Set("pygmentsUseClasses", b)
|
2015-07-08 21:51:54 -04:00
|
|
|
}
|
|
|
|
|
2019-08-16 09:55:03 -04:00
|
|
|
spec, err := NewContentSpec(v, nil, nil)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2017-09-25 02:59:02 -04:00
|
|
|
|
|
|
|
result, err := spec.createPygmentsOptionsString(this.in)
|
2015-07-08 21:51:54 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Errorf("[%d] parsePygmentArgs failed: %s", i, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if result != expect {
|
|
|
|
t.Errorf("[%d] parsePygmentArgs got %v but expected %v", i, result, expect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-25 02:59:02 -04:00
|
|
|
|
|
|
|
type chromaInfo struct {
|
|
|
|
classes bool
|
|
|
|
lineNumbers bool
|
2017-10-13 03:37:47 -04:00
|
|
|
lineNumbersInTable bool
|
2017-09-25 02:59:02 -04:00
|
|
|
highlightRangesLen int
|
|
|
|
highlightRangesStr string
|
|
|
|
baseLineNumber int
|
|
|
|
}
|
|
|
|
|
|
|
|
func formatterChromaInfo(f *html.Formatter) chromaInfo {
|
|
|
|
v := reflect.ValueOf(f).Elem()
|
|
|
|
c := chromaInfo{}
|
|
|
|
// Hack:
|
2017-10-13 03:37:47 -04:00
|
|
|
|
|
|
|
c.classes = f.Classes
|
2017-09-25 02:59:02 -04:00
|
|
|
c.lineNumbers = v.FieldByName("lineNumbers").Bool()
|
2017-10-13 03:37:47 -04:00
|
|
|
c.lineNumbersInTable = v.FieldByName("lineNumbersInTable").Bool()
|
2017-09-25 02:59:02 -04:00
|
|
|
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) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2017-09-25 02:59:02 -04:00
|
|
|
|
|
|
|
v := viper.New()
|
|
|
|
v.Set("pygmentsUseClasses", true)
|
2019-08-16 09:55:03 -04:00
|
|
|
spec, err := NewContentSpec(v, nil, nil)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2017-09-25 02:59:02 -04:00
|
|
|
|
|
|
|
result, err := spec.Highlight(`echo "Hello"`, "bash", "")
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2017-09-25 02:59:02 -04:00
|
|
|
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(result, qt.Contains, `<div class="highlight"><pre class="chroma"><code class="language-bash" data-lang="bash"><span class="nb">echo</span> <span class="s2">"Hello"</span></code></pre></div>`)
|
2017-09-25 02:59:02 -04:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestChromaHTMLFormatterFromOptions(t *testing.T) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(t)
|
2017-09-25 02:59:02 -04:00
|
|
|
|
|
|
|
for i, this := range []struct {
|
|
|
|
in string
|
|
|
|
pygmentsStyle interface{}
|
|
|
|
pygmentsUseClasses interface{}
|
|
|
|
pygmentsOptions string
|
|
|
|
assert func(c chromaInfo)
|
|
|
|
}{
|
2019-08-10 15:05:17 -04:00
|
|
|
{"", "monokai", true, "style=manni,noclasses=true", func(ci chromaInfo) {
|
|
|
|
c.Assert(ci.classes, qt.Equals, true)
|
|
|
|
c.Assert(ci.lineNumbers, qt.Equals, false)
|
|
|
|
c.Assert(ci.highlightRangesLen, qt.Equals, 0)
|
2017-09-25 02:59:02 -04:00
|
|
|
|
|
|
|
}},
|
2019-08-10 15:05:17 -04:00
|
|
|
{"", nil, nil, "style=monokai,noclasses=false", func(ci chromaInfo) {
|
|
|
|
c.Assert(ci.classes, qt.Equals, true)
|
2017-09-25 02:59:02 -04:00
|
|
|
}},
|
2019-08-10 15:05:17 -04:00
|
|
|
{"linenos=sure,hl_lines=1 2 3", nil, nil, "style=monokai,noclasses=false", func(ci chromaInfo) {
|
|
|
|
c.Assert(ci.classes, qt.Equals, true)
|
|
|
|
c.Assert(ci.lineNumbers, qt.Equals, true)
|
|
|
|
c.Assert(ci.highlightRangesLen, qt.Equals, 3)
|
|
|
|
c.Assert(ci.highlightRangesStr, qt.Equals, "[[1 1] [2 2] [3 3]]")
|
|
|
|
c.Assert(ci.baseLineNumber, qt.Equals, 1)
|
2017-09-25 02:59:02 -04:00
|
|
|
}},
|
2019-08-10 15:05:17 -04:00
|
|
|
{"linenos=inline,hl_lines=1,linenostart=4", nil, nil, "style=monokai,noclasses=false", func(ci chromaInfo) {
|
|
|
|
c.Assert(ci.classes, qt.Equals, true)
|
|
|
|
c.Assert(ci.lineNumbers, qt.Equals, true)
|
|
|
|
c.Assert(ci.lineNumbersInTable, qt.Equals, false)
|
|
|
|
c.Assert(ci.highlightRangesLen, qt.Equals, 1)
|
2017-09-25 02:59:02 -04:00
|
|
|
// This compansates for https://github.com/alecthomas/chroma/issues/30
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(ci.highlightRangesStr, qt.Equals, "[[4 4]]")
|
|
|
|
c.Assert(ci.baseLineNumber, qt.Equals, 4)
|
2017-09-25 02:59:02 -04:00
|
|
|
}},
|
2019-08-10 15:05:17 -04:00
|
|
|
{"linenos=table", nil, nil, "style=monokai", func(ci chromaInfo) {
|
|
|
|
c.Assert(ci.lineNumbers, qt.Equals, true)
|
|
|
|
c.Assert(ci.lineNumbersInTable, qt.Equals, true)
|
2017-10-13 03:37:47 -04:00
|
|
|
}},
|
2019-08-10 15:05:17 -04:00
|
|
|
{"style=monokai,noclasses=false", nil, nil, "style=manni,noclasses=true", func(ci chromaInfo) {
|
|
|
|
c.Assert(ci.classes, qt.Equals, true)
|
2017-09-25 02:59:02 -04:00
|
|
|
}},
|
2019-08-10 15:05:17 -04:00
|
|
|
{"style=monokai,noclasses=true", "friendly", false, "style=manni,noclasses=false", func(ci chromaInfo) {
|
|
|
|
c.Assert(ci.classes, qt.Equals, false)
|
2017-09-25 02:59:02 -04:00
|
|
|
}},
|
|
|
|
} {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-08-16 09:55:03 -04:00
|
|
|
spec, err := NewContentSpec(v, nil, nil)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2017-09-25 02:59:02 -04:00
|
|
|
|
|
|
|
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},
|
2017-09-25 22:25:33 -04:00
|
|
|
{"1 4", 1, [][2]int{{1, 1}, {4, 4}}},
|
|
|
|
{"1 4", 2, [][2]int{{2, 2}, {5, 5}}},
|
|
|
|
{"1-4 5-8", 1, [][2]int{{1, 4}, {5, 8}}},
|
|
|
|
{" 1 4 ", 1, [][2]int{{1, 1}, {4, 4}}},
|
|
|
|
{"1-4 5-8 ", 1, [][2]int{{1, 4}, {5, 8}}},
|
|
|
|
{"1-4 5", 1, [][2]int{{1, 4}, {5, 5}}},
|
|
|
|
{"4 5-9", 1, [][2]int{{4, 4}, {5, 9}}},
|
2017-09-25 02:59:02 -04:00
|
|
|
{" 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) {
|
2019-08-10 15:05:17 -04:00
|
|
|
c := qt.New(b)
|
2017-09-25 02:59:02 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
2019-08-16 09:55:03 -04:00
|
|
|
spec, err := NewContentSpec(v, nil, nil)
|
2019-08-10 15:05:17 -04:00
|
|
|
c.Assert(err, qt.IsNil)
|
2017-09-25 02:59:02 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|