| 1 |
// Copyright 2026 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 rst_test |
| 15 |
|
| 16 |
import ( |
| 17 |
"runtime" |
| 18 |
"strings" |
| 19 |
"testing" |
| 20 |
|
| 21 |
qt "github.com/frankban/quicktest" |
| 22 |
|
| 23 |
"github.com/gohugoio/hugo/hugolib" |
| 24 |
"github.com/gohugoio/hugo/markup/rst" |
| 25 |
) |
| 26 |
|
| 27 |
// --syntax-highlight, default = long. |
| 28 |
func TestRSTSyntaxHighlight(t *testing.T) { |
| 29 |
if !rst.Supports() { |
| 30 |
t.Skip("rst not installed") |
| 31 |
} |
| 32 |
if !rst.SupportsPygments() { |
| 33 |
t.Skip("Pygments not installed") |
| 34 |
} |
| 35 |
|
| 36 |
execAllow := `'^rst2html(\.py)?$'` |
| 37 |
if runtime.GOOS == "windows" { |
| 38 |
execAllow = `'^python(\.exe)?$'` |
| 39 |
} |
| 40 |
|
| 41 |
filesTemplate := strings.ReplaceAll(` |
| 42 |
-- hugo.toml -- |
| 43 |
baseURL = "https://example.org" |
| 44 |
disableKinds = ["home", "section", "taxonomy", "term", "rss", "sitemap"] |
| 45 |
|
| 46 |
markup.rst.syntaxHighlight = 'SYNTAX_HIGHLIGHT' |
| 47 |
|
| 48 |
[security.exec] |
| 49 |
allow = [RST_EXEC_ALLOW] |
| 50 |
|
| 51 |
-- layouts/page.html -- |
| 52 |
{{ .Content }} |
| 53 |
-- content/p.rst -- |
| 54 |
--- |
| 55 |
title: p |
| 56 |
--- |
| 57 |
|
| 58 |
.. code:: go |
| 59 |
|
| 60 |
if true {} |
| 61 |
`, "RST_EXEC_ALLOW", execAllow) |
| 62 |
|
| 63 |
files := strings.ReplaceAll(filesTemplate, "SYNTAX_HIGHLIGHT", "none") |
| 64 |
b := hugolib.Test(t, files) |
| 65 |
|
| 66 |
b.AssertFileContent("public/p/index.html", ` |
| 67 |
! Pygments |
| 68 |
`) |
| 69 |
|
| 70 |
b.AssertFileContent("public/p/index.html", ` |
| 71 |
if true {} |
| 72 |
! if</span> |
| 73 |
`) |
| 74 |
|
| 75 |
files = strings.ReplaceAll(filesTemplate, "SYNTAX_HIGHLIGHT", "short") |
| 76 |
b = hugolib.Test(t, files) |
| 77 |
|
| 78 |
b.AssertFileContent("public/p/index.html", ` |
| 79 |
<span class="k">if</span> |
| 80 |
! <span class="keyword">if</span> |
| 81 |
`) |
| 82 |
|
| 83 |
files = strings.ReplaceAll(filesTemplate, "SYNTAX_HIGHLIGHT", "long") |
| 84 |
b = hugolib.Test(t, files) |
| 85 |
|
| 86 |
b.AssertFileContent("public/p/index.html", ` |
| 87 |
! <span class="k">if</span> |
| 88 |
<span class="keyword">if</span> |
| 89 |
`) |
| 90 |
|
| 91 |
// Default long. |
| 92 |
files = strings.ReplaceAll(filesTemplate, "markup.rst.syntaxHighlight = 'SYNTAX_HIGHLIGHT'", "") |
| 93 |
b = hugolib.Test(t, files) |
| 94 |
|
| 95 |
b.AssertFileContent("public/p/index.html", ` |
| 96 |
! <span class="k">if</span> |
| 97 |
<span class="keyword">if</span> |
| 98 |
`) |
| 99 |
|
| 100 |
// Invalid. |
| 101 |
files = strings.ReplaceAll(filesTemplate, "SYNTAX_HIGHLIGHT", "foo") |
| 102 |
b, err := hugolib.TestE(t, files) |
| 103 |
b.Assert(err, qt.IsNotNil) |
| 104 |
b.Assert(err.Error(), qt.Contains, "rst: invalid value for syntaxHighlight: \"foo\"") |
| 105 |
} |