Add table linenos support for Chroma highlighter
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Fri, 13 Oct 2017 07:37:47 +0000 (09:37 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sat, 14 Oct 2017 14:00:27 +0000 (16:00 +0200)
Fixes #3915

helpers/pygments.go
helpers/pygments_test.go
vendor/vendor.json

index 9253445e783ab513d07035b0029093546c2e3176..4a90e353deddfc7e0d1ccafcbd25fc5a94f537cd 100644 (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"]
index e8a5199d9e57d1d16279eab11e5c6095b378fec1..860f317d8e906e7e58e6d64fd23c4e46fe8ee622 100644 (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)
                }},
index 1e9f5dc99d27e430267de71f495a105f29a47325..654da069849b6655df7a7ffd01f55ebd3341cf66 100644 (file)
                        "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=",
                {
                        "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=",
                        "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=",