Add table linenos support for Chroma highlighter

Fixes #3915
This commit is contained in:
Bjørn Erik Pedersen
2017-10-13 09:37:47 +02:00
parent 60bd332c1f
commit 7c30e2cbb0
3 changed files with 35 additions and 24 deletions

View File

@@ -40,7 +40,6 @@ import (
const pygmentsBin = "pygmentize"
// 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 {
@@ -309,8 +308,12 @@ func (cs *ContentSpec) chromaFormatterFromOptions(pygmentsOpts map[string]string
options = append(options, html.WithClasses())
}
if pygmentsOpts["linenos"] != "" {
lineNumbers := pygmentsOpts["linenos"]
if lineNumbers != "" {
options = append(options, html.WithLineNumbers())
if lineNumbers != "inline" {
options = append(options, html.LineNumbersInTable())
}
}
startLineStr := pygmentsOpts["linenostart"]

View File

@@ -111,6 +111,7 @@ func TestParseDefaultPygmentsArgs(t *testing.T) {
type chromaInfo struct {
classes bool
lineNumbers bool
lineNumbersInTable bool
highlightRangesLen int
highlightRangesStr string
baseLineNumber int
@@ -120,8 +121,10 @@ func formatterChromaInfo(f *html.Formatter) chromaInfo {
v := reflect.ValueOf(f).Elem()
c := chromaInfo{}
// Hack:
c.classes = v.FieldByName("classes").Bool()
c.classes = f.Classes
c.lineNumbers = v.FieldByName("lineNumbers").Bool()
c.lineNumbersInTable = v.FieldByName("lineNumbersInTable").Bool()
c.baseLineNumber = int(v.FieldByName("baseLineNumber").Int())
vv := v.FieldByName("highlightRanges")
c.highlightRangesLen = vv.Len()
@@ -171,14 +174,19 @@ func TestChromaHTMLFormatterFromOptions(t *testing.T) {
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) {
{"linenos=inline,hl_lines=1,linenostart=4", nil, nil, "style=monokai,noclasses=false", func(c chromaInfo) {
assert.True(c.classes)
assert.True(c.lineNumbers)
assert.False(c.lineNumbersInTable)
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)
}},
{"linenos=table", nil, nil, "style=monokai", func(c chromaInfo) {
assert.True(c.lineNumbers)
assert.True(c.lineNumbersInTable)
}},
{"style=monokai,noclasses=false", nil, nil, "style=manni,noclasses=true", func(c chromaInfo) {
assert.True(c.classes)
}},