| 1 |
// Copyright 2019 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
// Package highlight provides code highlighting. |
| 15 |
package highlight |
| 16 |
|
| 17 |
import ( |
| 18 |
"testing" |
| 19 |
|
| 20 |
"github.com/alecthomas/chroma/v2" |
| 21 |
"github.com/alecthomas/chroma/v2/styles" |
| 22 |
|
| 23 |
qt "github.com/frankban/quicktest" |
| 24 |
) |
| 25 |
|
| 26 |
func TestHighlight(t *testing.T) { |
| 27 |
c := qt.New(t) |
| 28 |
|
| 29 |
lines := `LINE1 |
| 30 |
LINE2 |
| 31 |
LINE3 |
| 32 |
LINE4 |
| 33 |
LINE5 |
| 34 |
` |
| 35 |
coalesceNeeded := `GET /foo HTTP/1.1 |
| 36 |
Content-Type: application/json |
| 37 |
User-Agent: foo |
| 38 |
|
| 39 |
{ |
| 40 |
"hello": "world" |
| 41 |
}` |
| 42 |
|
| 43 |
c.Run("Basic", func(c *qt.C) { |
| 44 |
cfg := DefaultConfig |
| 45 |
cfg.NoClasses = false |
| 46 |
h := New(cfg) |
| 47 |
|
| 48 |
result, _ := h.Highlight(`echo "Hugo Rocks!"`, "bash", "") |
| 49 |
c.Assert(result, qt.Equals, `<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-bash" data-lang="bash"><span class="line"><span class="cl"><span class="nb">echo</span> <span class="s2">"Hugo Rocks!"</span></span></span></code></pre></div>`) |
| 50 |
result, _ = h.Highlight(`echo "Hugo Rocks!"`, "unknown", "") |
| 51 |
c.Assert(result, qt.Equals, `<pre tabindex="0"><code class="language-unknown" data-lang="unknown">echo "Hugo Rocks!"</code></pre>`) |
| 52 |
}) |
| 53 |
|
| 54 |
c.Run("Highlight lines, default config", func(c *qt.C) { |
| 55 |
cfg := DefaultConfig |
| 56 |
cfg.NoClasses = false |
| 57 |
h := New(cfg) |
| 58 |
|
| 59 |
result, _ := h.Highlight(lines, "bash", "linenos=table,hl_lines=2 4-5,linenostart=3") |
| 60 |
c.Assert(result, qt.Contains, "<div class=\"highlight\"><div class=\"chroma\">\n<table class=\"lntable\"><tr><td class=\"lntd\">\n<pre tabindex=\"0\" class=\"chroma\"><code><span class") |
| 61 |
c.Assert(result, qt.Contains, "<span class=\"hl\"><span class=\"lnt\">4") |
| 62 |
|
| 63 |
result, _ = h.Highlight(lines, "bash", "linenos=inline,hl_lines=2") |
| 64 |
c.Assert(result, qt.Contains, "<span class=\"ln\">2</span><span class=\"cl\">LINE2\n</span></span>") |
| 65 |
c.Assert(result, qt.Not(qt.Contains), "<table") |
| 66 |
|
| 67 |
result, _ = h.Highlight(lines, "bash", "linenos=true,hl_lines=2") |
| 68 |
c.Assert(result, qt.Contains, "<table") |
| 69 |
c.Assert(result, qt.Contains, "<span class=\"hl\"><span class=\"lnt\">2\n</span>") |
| 70 |
}) |
| 71 |
|
| 72 |
c.Run("Highlight lines, linenumbers default on", func(c *qt.C) { |
| 73 |
cfg := DefaultConfig |
| 74 |
cfg.NoClasses = false |
| 75 |
cfg.LineNos = true |
| 76 |
h := New(cfg) |
| 77 |
|
| 78 |
result, _ := h.Highlight(lines, "bash", "") |
| 79 |
c.Assert(result, qt.Contains, "<span class=\"lnt\">2\n</span>") |
| 80 |
result, _ = h.Highlight(lines, "bash", "linenos=false,hl_lines=2") |
| 81 |
c.Assert(result, qt.Not(qt.Contains), "class=\"lnt\"") |
| 82 |
}) |
| 83 |
|
| 84 |
c.Run("Highlight lines, linenumbers default on, anchorlinenumbers default on", func(c *qt.C) { |
| 85 |
cfg := DefaultConfig |
| 86 |
cfg.NoClasses = false |
| 87 |
cfg.LineNos = true |
| 88 |
cfg.AnchorLineNos = true |
| 89 |
h := New(cfg) |
| 90 |
|
| 91 |
result, _ := h.Highlight(lines, "bash", "") |
| 92 |
c.Assert(result, qt.Contains, "<span class=\"lnt\" id=\"2\"><a class=\"lnlinks\" href=\"#2\">2</a>\n</span>") |
| 93 |
result, _ = h.Highlight(lines, "bash", "anchorlinenos=false,hl_lines=2") |
| 94 |
c.Assert(result, qt.Not(qt.Contains), "id=\"2\"") |
| 95 |
}) |
| 96 |
|
| 97 |
c.Run("Highlight lines, linenumbers default on, linenumbers in table default off", func(c *qt.C) { |
| 98 |
cfg := DefaultConfig |
| 99 |
cfg.NoClasses = false |
| 100 |
cfg.LineNos = true |
| 101 |
cfg.LineNumbersInTable = false |
| 102 |
h := New(cfg) |
| 103 |
|
| 104 |
result, _ := h.Highlight(lines, "bash", "") |
| 105 |
c.Assert(result, qt.Contains, "<span class=\"cl\">LINE2\n</span></span>") |
| 106 |
result, _ = h.Highlight(lines, "bash", "linenos=table") |
| 107 |
c.Assert(result, qt.Contains, "<span class=\"lnt\">1\n</span>") |
| 108 |
}) |
| 109 |
|
| 110 |
c.Run("No language", func(c *qt.C) { |
| 111 |
cfg := DefaultConfig |
| 112 |
cfg.NoClasses = false |
| 113 |
cfg.LineNos = true |
| 114 |
h := New(cfg) |
| 115 |
|
| 116 |
result, _ := h.Highlight(lines, "", "") |
| 117 |
c.Assert(result, qt.Equals, "<pre tabindex=\"0\"><code>LINE1\nLINE2\nLINE3\nLINE4\nLINE5\n</code></pre>") |
| 118 |
}) |
| 119 |
|
| 120 |
c.Run("No language, guess syntax", func(c *qt.C) { |
| 121 |
cfg := DefaultConfig |
| 122 |
cfg.NoClasses = false |
| 123 |
cfg.GuessSyntax = true |
| 124 |
cfg.LineNos = true |
| 125 |
cfg.LineNumbersInTable = false |
| 126 |
h := New(cfg) |
| 127 |
|
| 128 |
result, _ := h.Highlight(lines, "", "") |
| 129 |
c.Assert(result, qt.Contains, "<span class=\"cl\">LINE2\n</span></span>") |
| 130 |
}) |
| 131 |
|
| 132 |
c.Run("No language, Escape HTML string", func(c *qt.C) { |
| 133 |
cfg := DefaultConfig |
| 134 |
cfg.NoClasses = false |
| 135 |
h := New(cfg) |
| 136 |
|
| 137 |
result, _ := h.Highlight("Escaping less-than in code block? <fail>", "", "") |
| 138 |
c.Assert(result, qt.Contains, "<fail>") |
| 139 |
}) |
| 140 |
|
| 141 |
c.Run("Highlight lines, default config", func(c *qt.C) { |
| 142 |
cfg := DefaultConfig |
| 143 |
cfg.NoClasses = false |
| 144 |
h := New(cfg) |
| 145 |
|
| 146 |
result, _ := h.Highlight(coalesceNeeded, "http", "linenos=true,hl_lines=2") |
| 147 |
c.Assert(result, qt.Contains, "hello") |
| 148 |
c.Assert(result, qt.Contains, "}") |
| 149 |
}) |
| 150 |
} |
| 151 |
|
| 152 |
func TestGitHubDarkStyleIncludesNameOther(t *testing.T) { |
| 153 |
c := qt.New(t) |
| 154 |
|
| 155 |
style := styles.Get("github-dark") |
| 156 |
c.Assert(style, qt.Not(qt.IsNil)) |
| 157 |
c.Assert(style.Has(chroma.NameOther), qt.Equals, true) |
| 158 |
c.Assert(style.Get(chroma.NameOther).Colour.String(), qt.Equals, "#e6edf3") |
| 159 |
} |