|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "helper" |
| 4 | + |
| 5 | +class TestTagHighlight < TagUnitTest |
| 6 | + def render_content_with_text_to_highlight(code) |
| 7 | + content = <<~CONTENT |
| 8 | + --- |
| 9 | + title: This is a test |
| 10 | + --- |
| 11 | +
|
| 12 | + This document has some highlighted code in it. |
| 13 | +
|
| 14 | + {% highlight text %} |
| 15 | + #{code} |
| 16 | + {% endhighlight %} |
| 17 | +
|
| 18 | + {% highlight text linenos %} |
| 19 | + #{code} |
| 20 | + {% endhighlight %} |
| 21 | + CONTENT |
| 22 | + render_content(content) |
| 23 | + end |
| 24 | + |
| 25 | + # Syntax sugar for low-level version of: |
| 26 | + # ``` |
| 27 | + # {% highlight markup%}test{% endhighlight %} |
| 28 | + # ``` |
| 29 | + def highlight_block_with_markup(markup) |
| 30 | + Jekyll::Tags::HighlightBlock.parse( |
| 31 | + "highlight", |
| 32 | + markup, |
| 33 | + Liquid::Tokenizer.new("test{% endhighlight %}\n"), |
| 34 | + Liquid::ParseContext.new |
| 35 | + ) |
| 36 | + end |
| 37 | + |
| 38 | + context "language name" do |
| 39 | + should "match only the required set of chars" do |
| 40 | + r = Jekyll::Tags::HighlightBlock::SYNTAX |
| 41 | + [ |
| 42 | + "ruby", |
| 43 | + "c#", |
| 44 | + "xml+cheetah", |
| 45 | + "x.y", |
| 46 | + "coffee-script", |
| 47 | + "shell_session", |
| 48 | + "ruby key=val", |
| 49 | + "ruby a=b c=d", |
| 50 | + ].each { |sample| assert_match(r, sample) } |
| 51 | + |
| 52 | + refute_match r, "blah^" |
| 53 | + end |
| 54 | + end |
| 55 | + |
| 56 | + context "highlight tag in unsafe mode" do |
| 57 | + should "set the no options with just a language name" do |
| 58 | + tag = highlight_block_with_markup("ruby ") |
| 59 | + assert_equal({}, tag.instance_variable_get(:@highlight_options)) |
| 60 | + end |
| 61 | + |
| 62 | + should "set the linenos option as 'inline' if no linenos value" do |
| 63 | + tag = highlight_block_with_markup("ruby linenos ") |
| 64 | + assert_equal( |
| 65 | + { :linenos => "inline" }, |
| 66 | + tag.instance_variable_get(:@highlight_options) |
| 67 | + ) |
| 68 | + end |
| 69 | + |
| 70 | + should "set the linenos option to 'table' " \ |
| 71 | + "if the linenos key is given the table value" do |
| 72 | + tag = highlight_block_with_markup("ruby linenos=table ") |
| 73 | + assert_equal( |
| 74 | + { :linenos => "table" }, |
| 75 | + tag.instance_variable_get(:@highlight_options) |
| 76 | + ) |
| 77 | + end |
| 78 | + |
| 79 | + should "recognize nowrap option with linenos set" do |
| 80 | + tag = highlight_block_with_markup("ruby linenos=table nowrap ") |
| 81 | + assert_equal( |
| 82 | + { :linenos => "table", :nowrap => true }, |
| 83 | + tag.instance_variable_get(:@highlight_options) |
| 84 | + ) |
| 85 | + end |
| 86 | + |
| 87 | + should "recognize the cssclass option" do |
| 88 | + tag = highlight_block_with_markup("ruby linenos=table cssclass=hl ") |
| 89 | + assert_equal( |
| 90 | + { :cssclass => "hl", :linenos => "table" }, |
| 91 | + tag.instance_variable_get(:@highlight_options) |
| 92 | + ) |
| 93 | + end |
| 94 | + |
| 95 | + should "recognize the hl_linenos option and its value" do |
| 96 | + tag = highlight_block_with_markup("ruby linenos=table cssclass=hl hl_linenos=3 ") |
| 97 | + assert_equal( |
| 98 | + { :cssclass => "hl", :linenos => "table", :hl_linenos => "3" }, |
| 99 | + tag.instance_variable_get(:@highlight_options) |
| 100 | + ) |
| 101 | + end |
| 102 | + |
| 103 | + should "recognize multiple values of hl_linenos" do |
| 104 | + tag = highlight_block_with_markup 'ruby linenos=table cssclass=hl hl_linenos="3 5 6" ' |
| 105 | + assert_equal( |
| 106 | + { :cssclass => "hl", :linenos => "table", :hl_linenos => %w(3 5 6) }, |
| 107 | + tag.instance_variable_get(:@highlight_options) |
| 108 | + ) |
| 109 | + end |
| 110 | + |
| 111 | + should "treat language name as case insensitive" do |
| 112 | + tag = highlight_block_with_markup("Ruby ") |
| 113 | + assert_equal "ruby", tag.instance_variable_get(:@lang), "lexers should be case insensitive" |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + context "with the rouge highlighter" do |
| 118 | + context "post content has highlight tag" do |
| 119 | + setup do |
| 120 | + render_content_with_text_to_highlight "test" |
| 121 | + end |
| 122 | + |
| 123 | + should "render markdown with rouge" do |
| 124 | + assert_match( |
| 125 | + %(<pre><code class="language-text" data-lang="text">test</code></pre>), |
| 126 | + @result |
| 127 | + ) |
| 128 | + end |
| 129 | + |
| 130 | + should "render markdown with rouge with line numbers" do |
| 131 | + assert_match( |
| 132 | + %(<table class="rouge-table"><tbody>) + |
| 133 | + %(<tr><td class="gutter gl">) + |
| 134 | + %(<pre class="lineno">1\n</pre></td>) + |
| 135 | + %(<td class="code"><pre>test\n</pre></td></tr>) + |
| 136 | + %(</tbody></table>), |
| 137 | + @result |
| 138 | + ) |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + context "post content has highlight with file reference" do |
| 143 | + setup do |
| 144 | + render_content_with_text_to_highlight "./jekyll.gemspec" |
| 145 | + end |
| 146 | + |
| 147 | + should "not embed the file" do |
| 148 | + assert_match( |
| 149 | + '<pre><code class="language-text" data-lang="text">' \ |
| 150 | + "./jekyll.gemspec</code></pre>", |
| 151 | + @result |
| 152 | + ) |
| 153 | + end |
| 154 | + end |
| 155 | + |
| 156 | + context "post content has highlight tag with UTF character" do |
| 157 | + setup do |
| 158 | + render_content_with_text_to_highlight "Æ" |
| 159 | + end |
| 160 | + |
| 161 | + should "render markdown with pygments line handling" do |
| 162 | + assert_match( |
| 163 | + '<pre><code class="language-text" data-lang="text">Æ</code></pre>', |
| 164 | + @result |
| 165 | + ) |
| 166 | + end |
| 167 | + end |
| 168 | + |
| 169 | + context "post content has highlight tag with preceding spaces & lines" do |
| 170 | + setup do |
| 171 | + render_content_with_text_to_highlight <<~EOS |
| 172 | +
|
| 173 | +
|
| 174 | + [,1] [,2] |
| 175 | + [1,] FALSE TRUE |
| 176 | + [2,] FALSE TRUE |
| 177 | + EOS |
| 178 | + end |
| 179 | + |
| 180 | + should "only strip the preceding newlines" do |
| 181 | + assert_match( |
| 182 | + '<pre><code class="language-text" data-lang="text"> [,1] [,2]', |
| 183 | + @result |
| 184 | + ) |
| 185 | + end |
| 186 | + end |
| 187 | + |
| 188 | + context "post content has highlight tag with preceding spaces & lines in several places" do |
| 189 | + setup do |
| 190 | + render_content_with_text_to_highlight <<~EOS |
| 191 | +
|
| 192 | +
|
| 193 | + [,1] [,2] |
| 194 | +
|
| 195 | +
|
| 196 | + [1,] FALSE TRUE |
| 197 | + [2,] FALSE TRUE |
| 198 | +
|
| 199 | +
|
| 200 | + EOS |
| 201 | + end |
| 202 | + |
| 203 | + should "only strip the newlines which precede and succeed the entire block" do |
| 204 | + assert_match( |
| 205 | + "<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]\n\n\n" \ |
| 206 | + "[1,] FALSE TRUE\n[2,] FALSE TRUE</code></pre>", |
| 207 | + @result |
| 208 | + ) |
| 209 | + end |
| 210 | + end |
| 211 | + |
| 212 | + context "post content has highlight tag with linenumbers" do |
| 213 | + setup do |
| 214 | + render_content <<~EOS |
| 215 | + --- |
| 216 | + title: This is a test |
| 217 | + --- |
| 218 | +
|
| 219 | + This is not yet highlighted |
| 220 | + {% highlight php linenos %} |
| 221 | + test |
| 222 | + {% endhighlight %} |
| 223 | +
|
| 224 | + This should not be highlighted, right? |
| 225 | + EOS |
| 226 | + end |
| 227 | + |
| 228 | + should "should stop highlighting at boundary with rouge" do |
| 229 | + expected = <<~EOS |
| 230 | + <p>This is not yet highlighted</p> |
| 231 | +
|
| 232 | + <figure class="highlight"><pre><code class="language-php" data-lang="php"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1 |
| 233 | + </pre></td><td class="code"><pre><span class="n">test</span> |
| 234 | + </pre></td></tr></tbody></table></code></pre></figure> |
| 235 | +
|
| 236 | + <p>This should not be highlighted, right?</p> |
| 237 | + EOS |
| 238 | + assert_match(expected, @result) |
| 239 | + end |
| 240 | + end |
| 241 | + |
| 242 | + context "post content has highlight tag with " \ |
| 243 | + "preceding spaces & Windows-style newlines" do |
| 244 | + setup do |
| 245 | + render_content_with_text_to_highlight "\r\n\r\n\r\n [,1] [,2]" |
| 246 | + end |
| 247 | + |
| 248 | + should "only strip the preceding newlines" do |
| 249 | + assert_match( |
| 250 | + '<pre><code class="language-text" data-lang="text"> [,1] [,2]', |
| 251 | + @result |
| 252 | + ) |
| 253 | + end |
| 254 | + end |
| 255 | + |
| 256 | + context "post content has highlight tag with only preceding spaces" do |
| 257 | + setup do |
| 258 | + render_content_with_text_to_highlight <<~EOS |
| 259 | + [,1] [,2] |
| 260 | + [1,] FALSE TRUE |
| 261 | + [2,] FALSE TRUE |
| 262 | + EOS |
| 263 | + end |
| 264 | + |
| 265 | + should "only strip the preceding newlines" do |
| 266 | + assert_match( |
| 267 | + '<pre><code class="language-text" data-lang="text"> [,1] [,2]', |
| 268 | + @result |
| 269 | + ) |
| 270 | + end |
| 271 | + end |
| 272 | + end |
| 273 | + |
| 274 | + context "simple post with markdown and pre tags" do |
| 275 | + setup do |
| 276 | + @content = <<~CONTENT |
| 277 | + --- |
| 278 | + title: Kramdown post with pre |
| 279 | + --- |
| 280 | +
|
| 281 | + _FIGHT!_ |
| 282 | +
|
| 283 | + {% highlight ruby %} |
| 284 | + puts "3..2..1.." |
| 285 | + {% endhighlight %} |
| 286 | +
|
| 287 | + *FINISH HIM* |
| 288 | + CONTENT |
| 289 | + end |
| 290 | + |
| 291 | + context "using Kramdown" do |
| 292 | + setup do |
| 293 | + render_content(@content, "markdown" => "kramdown") |
| 294 | + end |
| 295 | + |
| 296 | + should "parse correctly" do |
| 297 | + assert_match %r{<em>FIGHT!</em>}, @result |
| 298 | + assert_match %r!<em>FINISH HIM</em>!, @result |
| 299 | + end |
| 300 | + end |
| 301 | + end |
| 302 | +end |
0 commit comments