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)
}},

40
vendor/vendor.json vendored
View file

@ -21,34 +21,34 @@
"revisionTime": "2017-08-10T14:37:23Z"
},
{
"checksumSHA1": "OZOPnvP9OY1ADgmcDttl3Zvytcs=",
"checksumSHA1": "/xPnkXIHTk3EtmQ9NbufTWUG15g=",
"path": "github.com/alecthomas/chroma",
"revision": "282a86e2a0ed1739095c404758b99402fe4f7045",
"revisionTime": "2017-10-03T14:35:07Z"
"revision": "02c4adc0664a104c4ea91c6c3a41a91b93bbd9ef",
"revisionTime": "2017-10-14T09:02:40Z"
},
{
"checksumSHA1": "pnWyHePZ2ZE/GEg2fBw/vyjYhmY=",
"path": "github.com/alecthomas/chroma/formatters",
"revision": "282a86e2a0ed1739095c404758b99402fe4f7045",
"revisionTime": "2017-10-03T14:35:07Z"
"revision": "02c4adc0664a104c4ea91c6c3a41a91b93bbd9ef",
"revisionTime": "2017-10-14T09:02:40Z"
},
{
"checksumSHA1": "cFa4bl6NeNIkSqOsKGY6SI5jprk=",
"checksumSHA1": "YgboNwoGG2Yc/rWKaIb00/+Ad5Y=",
"path": "github.com/alecthomas/chroma/formatters/html",
"revision": "282a86e2a0ed1739095c404758b99402fe4f7045",
"revisionTime": "2017-10-03T14:35:07Z"
"revision": "02c4adc0664a104c4ea91c6c3a41a91b93bbd9ef",
"revisionTime": "2017-10-14T09:02:40Z"
},
{
"checksumSHA1": "MwdDmbQQEpscTjcENv5yUFWIBS0=",
"checksumSHA1": "lSa/vZVJvB3+HngwkxBkPHw4EKg=",
"path": "github.com/alecthomas/chroma/lexers",
"revision": "282a86e2a0ed1739095c404758b99402fe4f7045",
"revisionTime": "2017-10-03T14:35:07Z"
"revision": "02c4adc0664a104c4ea91c6c3a41a91b93bbd9ef",
"revisionTime": "2017-10-14T09:02:40Z"
},
{
"checksumSHA1": "Wxv1e1MLoJGrZ33LgJqaoHcaBTs=",
"path": "github.com/alecthomas/chroma/styles",
"revision": "282a86e2a0ed1739095c404758b99402fe4f7045",
"revisionTime": "2017-10-03T14:35:07Z"
"revision": "02c4adc0664a104c4ea91c6c3a41a91b93bbd9ef",
"revisionTime": "2017-10-14T09:02:40Z"
},
{
"checksumSHA1": "7yrV1Gzr1ajco1xJ1gsyqRDTY2U=",
@ -90,14 +90,14 @@
{
"checksumSHA1": "6y/Ht8J58EotTDBEIuE3+s4AnL8=",
"path": "github.com/dlclark/regexp2",
"revision": "487489b64fb796de2e55f4e8a4ad1e145f80e957",
"revisionTime": "2017-07-18T21:59:41Z"
"revision": "7632a260cbaf5e7594fc1544a503456ecd0827f1",
"revisionTime": "2017-10-09T02:06:23Z"
},
{
"checksumSHA1": "k0JXX65FspyueQ8/1i50DGRiCUk=",
"path": "github.com/dlclark/regexp2/syntax",
"revision": "487489b64fb796de2e55f4e8a4ad1e145f80e957",
"revisionTime": "2017-07-18T21:59:41Z"
"revision": "7632a260cbaf5e7594fc1544a503456ecd0827f1",
"revisionTime": "2017-10-09T02:06:23Z"
},
{
"checksumSHA1": "lTESeJAGIWm6b7Xytf9sP3Gjhj8=",
@ -281,10 +281,10 @@
"revisionTime": "2017-07-05T02:17:15Z"
},
{
"checksumSHA1": "OZpv1QDSDG23bzSk/7auPQRB2Qg=",
"checksumSHA1": "1KVewmQICPa+IH1gYJUKDgNbKpY=",
"path": "github.com/russross/blackfriday",
"revision": "4048872b16cc0fc2c5fd9eacf0ed2c2fedaa0c8c",
"revisionTime": "2017-07-28T17:53:26Z"
"revision": "6d1ef893fcb01b4f50cb6e57ed7df3e2e627b6b2",
"revisionTime": "2017-10-11T18:22:19Z"
},
{
"checksumSHA1": "HkNUuhYSk5C2fTMa536jBiORq44=",