1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792
|
# coding: utf-8
require 'helper'
class TestTags < JekyllUnitTest
def setup
FileUtils.mkdir_p("tmp")
end
def create_post(content, override = {}, converter_class = Jekyll::Converters::Markdown)
site = fixture_site({"highlighter" => "rouge"}.merge(override))
if override['read_posts']
site.posts.docs.concat(PostReader.new(site).read_posts(''))
end
info = { :filters => [Jekyll::Filters], :registers => { :site => site } }
@converter = site.converters.find { |c| c.class == converter_class }
payload = { "highlighter_prefix" => @converter.highlighter_prefix,
"highlighter_suffix" => @converter.highlighter_suffix }
@result = Liquid::Template.parse(content).render!(payload, info)
@result = @converter.convert(@result)
end
def fill_post(code, override = {})
content = <<CONTENT
---
title: This is a test
---
This document has some highlighted code in it.
{% highlight text %}
#{code}
{% endhighlight %}
{% highlight text linenos %}
#{code}
{% endhighlight %}
CONTENT
create_post(content, override)
end
def highlight_block_with_opts(options_string)
Jekyll::Tags::HighlightBlock.parse('highlight', options_string, ["test", "{% endhighlight %}", "\n"], {})
end
context "language name" do
should "match only the required set of chars" do
r = Jekyll::Tags::HighlightBlock::SYNTAX
assert_match r, "ruby"
assert_match r, "c#"
assert_match r, "xml+cheetah"
assert_match r, "x.y"
assert_match r, "coffee-script"
refute_match r, "blah^"
assert_match r, "ruby key=val"
assert_match r, "ruby a=b c=d"
end
end
context "highlight tag in unsafe mode" do
should "set the no options with just a language name" do
tag = highlight_block_with_opts('ruby ')
assert_equal({}, tag.instance_variable_get(:@highlight_options))
end
should "set the linenos option as 'inline' if no linenos value" do
tag = highlight_block_with_opts('ruby linenos ')
assert_equal({ :linenos => 'inline' }, tag.instance_variable_get(:@highlight_options))
end
should "set the linenos option to 'table' if the linenos key is given the table value" do
tag = highlight_block_with_opts('ruby linenos=table ')
assert_equal({ :linenos => 'table' }, tag.instance_variable_get(:@highlight_options))
end
should "recognize nowrap option with linenos set" do
tag = highlight_block_with_opts('ruby linenos=table nowrap ')
assert_equal({ :linenos => 'table', :nowrap => true }, tag.instance_variable_get(:@highlight_options))
end
should "recognize the cssclass option" do
tag = highlight_block_with_opts('ruby linenos=table cssclass=hl ')
assert_equal({ :cssclass => 'hl', :linenos => 'table' }, tag.instance_variable_get(:@highlight_options))
end
should "recognize the hl_linenos option and its value" do
tag = highlight_block_with_opts('ruby linenos=table cssclass=hl hl_linenos=3 ')
assert_equal({ :cssclass => 'hl', :linenos => 'table', :hl_linenos => '3' }, tag.instance_variable_get(:@highlight_options))
end
should "recognize multiple values of hl_linenos" do
tag = highlight_block_with_opts('ruby linenos=table cssclass=hl hl_linenos="3 5 6" ')
assert_equal({ :cssclass => 'hl', :linenos => 'table', :hl_linenos => ['3', '5', '6'] }, tag.instance_variable_get(:@highlight_options))
end
should "treat language name as case insensitive" do
tag = highlight_block_with_opts('Ruby ')
assert_equal "ruby", tag.instance_variable_get(:@lang), "lexers should be case insensitive"
end
end
context "in safe mode" do
setup do
@tag = highlight_block_with_opts('text ')
end
should "allow linenos" do
sanitized = @tag.sanitized_opts({:linenos => true}, true)
assert_equal true, sanitized[:linenos]
end
should "allow hl_lines" do
sanitized = @tag.sanitized_opts({:hl_lines => %w[1 2 3 4]}, true)
assert_equal %w[1 2 3 4], sanitized[:hl_lines]
end
should "allow cssclass" do
sanitized = @tag.sanitized_opts({:cssclass => "ahoy"}, true)
assert_equal "ahoy", sanitized[:cssclass]
end
should "allow startinline" do
sanitized = @tag.sanitized_opts({:startinline => true}, true)
assert_equal true, sanitized[:startinline]
end
should "strip unknown options" do
sanitized = @tag.sanitized_opts({:light => true}, true)
assert_nil sanitized[:light]
end
end
context "with the pygments highlighter" do
setup do
if jruby?
then skip(
"JRuby does not support Pygments."
)
end
end
context "post content has highlight tag" do
setup do
fill_post("test", {'highlighter' => 'pygments'})
end
should "not cause a markdown error" do
refute_match /markdown\-html\-error/, @result
end
should "render markdown with pygments" do
assert_match %{<pre><code class="language-text" data-lang="text">test</code></pre>}, @result
end
should "render markdown with pygments with line numbers" do
assert_match %{<pre><code class="language-text" data-lang="text"><span class="lineno">1</span> test</code></pre>}, @result
end
end
context "post content has highlight with file reference" do
setup do
fill_post("./jekyll.gemspec", {'highlighter' => 'pygments'})
end
should "not embed the file" do
assert_match %{<pre><code class="language-text" data-lang="text">./jekyll.gemspec</code></pre>}, @result
end
end
context "post content has highlight tag with UTF character" do
setup do
fill_post("Æ", {'highlighter' => 'pygments'})
end
should "render markdown with pygments line handling" do
assert_match %{<pre><code class="language-text" data-lang="text">Æ</code></pre>}, @result
end
end
context "post content has highlight tag with preceding spaces & lines" do
setup do
code = <<-EOS
[,1] [,2]
[1,] FALSE TRUE
[2,] FALSE TRUE
EOS
fill_post(code, {'highlighter' => 'pygments'})
end
should "only strip the preceding newlines" do
assert_match %{<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]}, @result
end
end
context "post content has highlight tag with preceding spaces & lines in several places" do
setup do
code = <<-EOS
[,1] [,2]
[1,] FALSE TRUE
[2,] FALSE TRUE
EOS
fill_post(code, {'highlighter' => 'pygments'})
end
should "only strip the newlines which precede and succeed the entire block" do
assert_match "<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]\n\n\n[1,] FALSE TRUE\n[2,] FALSE TRUE</code></pre>", @result
end
end
context "post content has highlight tag with preceding spaces & Windows-style newlines" do
setup do
fill_post "\r\n\r\n\r\n [,1] [,2]", {'highlighter' => 'pygments'}
end
should "only strip the preceding newlines" do
assert_match %{<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]}, @result
end
end
context "post content has highlight tag with only preceding spaces" do
setup do
code = <<-EOS
[,1] [,2]
[1,] FALSE TRUE
[2,] FALSE TRUE
EOS
fill_post(code, {'highlighter' => 'pygments'})
end
should "only strip the preceding newlines" do
assert_match %{<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]}, @result
end
end
end
context "with the rouge highlighter" do
context "post content has highlight tag" do
setup do
fill_post("test")
end
should "render markdown with rouge" do
assert_match %{<pre><code class="language-text" data-lang="text">test</code></pre>}, @result
end
should "render markdown with rouge with line numbers" do
assert_match %{<table style="border-spacing: 0"><tbody><tr><td class="gutter gl" style="text-align: right"><pre class="lineno">1</pre></td><td class="code"><pre>test<span class="w">\n</span></pre></td></tr></tbody></table>}, @result
end
end
context "post content has highlight with file reference" do
setup do
fill_post("./jekyll.gemspec")
end
should "not embed the file" do
assert_match %{<pre><code class="language-text" data-lang="text">./jekyll.gemspec</code></pre>}, @result
end
end
context "post content has highlight tag with UTF character" do
setup do
fill_post("Æ")
end
should "render markdown with pygments line handling" do
assert_match %{<pre><code class="language-text" data-lang="text">Æ</code></pre>}, @result
end
end
context "post content has highlight tag with preceding spaces & lines" do
setup do
fill_post <<-EOS
[,1] [,2]
[1,] FALSE TRUE
[2,] FALSE TRUE
EOS
end
should "only strip the preceding newlines" do
assert_match %{<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]}, @result
end
end
context "post content has highlight tag with preceding spaces & lines in several places" do
setup do
fill_post <<-EOS
[,1] [,2]
[1,] FALSE TRUE
[2,] FALSE TRUE
EOS
end
should "only strip the newlines which precede and succeed the entire block" do
assert_match "<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]\n\n\n[1,] FALSE TRUE\n[2,] FALSE TRUE</code></pre>", @result
end
end
context "post content has highlight tag with preceding spaces & Windows-style newlines" do
setup do
fill_post "\r\n\r\n\r\n [,1] [,2]"
end
should "only strip the preceding newlines" do
assert_match %{<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]}, @result
end
end
context "post content has highlight tag with only preceding spaces" do
setup do
fill_post <<-EOS
[,1] [,2]
[1,] FALSE TRUE
[2,] FALSE TRUE
EOS
end
should "only strip the preceding newlines" do
assert_match %{<pre><code class=\"language-text\" data-lang=\"text\"> [,1] [,2]}, @result
end
end
end
context "simple post with markdown and pre tags" do
setup do
@content = <<CONTENT
---
title: Kramdown vs. RDiscount vs. Redcarpet
---
_FIGHT!_
{% highlight ruby %}
puts "3..2..1.."
{% endhighlight %}
*FINISH HIM*
CONTENT
end
context "using RDiscount" do
setup do
if jruby?
then skip(
"JRuby does not perform well with CExt, test disabled."
)
end
create_post(@content, {
'markdown' => 'rdiscount'
})
end
should "parse correctly" do
assert_match %r{<em>FIGHT!</em>}, @result
assert_match %r{<em>FINISH HIM</em>}, @result
end
end
context "using Kramdown" do
setup do
create_post(@content, 'markdown' => 'kramdown')
end
should "parse correctly" do
assert_match %r{<em>FIGHT!</em>}, @result
assert_match %r{<em>FINISH HIM</em>}, @result
end
end
context "using Redcarpet" do
setup do
if jruby?
skip(
"JRuby does not perform well with CExt, test disabled."
)
end
create_post(@content, {
'markdown' => 'redcarpet'
})
end
should "parse correctly" do
assert_match %r{<em>FIGHT!</em>}, @result
assert_match %r{<em>FINISH HIM</em>}, @result
end
end
end
context "simple page with post linking" do
setup do
content = <<CONTENT
---
title: Post linking
---
{% post_url 2008-11-21-complex %}
CONTENT
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end
should "not cause an error" do
refute_match /markdown\-html\-error/, @result
end
should "have the url to the \"complex\" post from 2008-11-21" do
assert_match %r{/2008/11/21/complex/}, @result
end
end
context "simple page with nested post linking" do
setup do
content = <<CONTENT
---
title: Post linking
---
- 1 {% post_url 2008-11-21-complex %}
- 2 {% post_url /2008-11-21-complex %}
- 3 {% post_url es/2008-11-21-nested %}
- 4 {% post_url /es/2008-11-21-nested %}
CONTENT
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end
should "not cause an error" do
refute_match /markdown\-html\-error/, @result
end
should "have the url to the \"complex\" post from 2008-11-21" do
assert_match %r{1\s/2008/11/21/complex/}, @result
assert_match %r{2\s/2008/11/21/complex/}, @result
end
should "have the url to the \"nested\" post from 2008-11-21" do
assert_match %r{3\s/2008/11/21/nested/}, @result
assert_match %r{4\s/2008/11/21/nested/}, @result
end
end
context "simple page with invalid post name linking" do
should "cause an error" do
content = <<CONTENT
---
title: Invalid post name linking
---
{% post_url abc2008-11-21-complex %}
CONTENT
assert_raises ArgumentError do
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end
end
end
context "include tag with parameters" do
context "with symlink'd include" do
should "not allow symlink includes" do
File.open("tmp/pages-test", 'w') { |file| file.write("SYMLINK TEST") }
assert_raises IOError do
content = <<CONTENT
---
title: Include symlink
---
{% include tmp/pages-test %}
CONTENT
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true, 'safe' => true })
end
refute_match /SYMLINK TEST/, @result
end
should "not expose the existence of symlinked files" do
ex = assert_raises IOError do
content = <<CONTENT
---
title: Include symlink
---
{% include tmp/pages-test-does-not-exist %}
CONTENT
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true, 'safe' => true })
end
assert_match /should exist and should not be a symlink/, ex.message
end
end
context "with one parameter" do
setup do
content = <<CONTENT
---
title: Include tag parameters
---
{% include sig.markdown myparam="test" %}
{% include params.html param="value" %}
CONTENT
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end
should "correctly output include variable" do
assert_match "<span id=\"include-param\">value</span>", @result.strip
end
should "ignore parameters if unused" do
assert_match "<hr />\n<p>Tom Preston-Werner\ngithub.com/mojombo</p>\n", @result
end
end
context "with invalid parameter syntax" do
should "throw a ArgumentError" do
content = <<CONTENT
---
title: Invalid parameter syntax
---
{% include params.html param s="value" %}
CONTENT
assert_raises ArgumentError, 'Did not raise exception on invalid "include" syntax' do
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end
content = <<CONTENT
---
title: Invalid parameter syntax
---
{% include params.html params="value %}
CONTENT
assert_raises ArgumentError, 'Did not raise exception on invalid "include" syntax' do
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end
end
end
context "with several parameters" do
setup do
content = <<CONTENT
---
title: multiple include parameters
---
{% include params.html param1="new_value" param2="another" %}
CONTENT
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end
should "list all parameters" do
assert_match '<li>param1 = new_value</li>', @result
assert_match '<li>param2 = another</li>', @result
end
should "not include previously used parameters" do
assert_match "<span id=\"include-param\"></span>", @result
end
end
context "without parameters" do
setup do
content = <<CONTENT
---
title: without parameters
---
{% include params.html %}
CONTENT
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end
should "include file with empty parameters" do
assert_match "<span id=\"include-param\"></span>", @result
end
end
context "with custom includes directory" do
setup do
content = <<CONTENT
---
title: custom includes directory
---
{% include custom.html %}
CONTENT
create_post(content, {'includes_dir' => '_includes_custom', 'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end
should "include file from custom directory" do
assert_match "custom_included", @result
end
end
context "without parameters within if statement" do
setup do
content = <<CONTENT
---
title: without parameters within if statement
---
{% if true %}{% include params.html %}{% endif %}
CONTENT
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end
should "include file with empty parameters within if statement" do
assert_match "<span id=\"include-param\"></span>", @result
end
end
context "include missing file" do
setup do
@content = <<CONTENT
---
title: missing file
---
{% include missing.html %}
CONTENT
end
should "raise error relative to source directory" do
exception = assert_raises IOError do
create_post(@content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end
assert_equal 'Included file \'_includes/missing.html\' not found', exception.message
end
end
context "include tag with variable and liquid filters" do
setup do
site = fixture_site({'pygments' => true}).tap(&:read).tap(&:render)
post = site.posts.docs.find {|p| p.basename.eql? "2013-12-17-include-variable-filters.markdown" }
@content = post.output
end
should "include file as variable with liquid filters" do
assert_match %r{1 included}, @content
assert_match %r{2 included}, @content
assert_match %r{3 included}, @content
end
should "include file as variable and liquid filters with arbitrary whitespace" do
assert_match %r{4 included}, @content
assert_match %r{5 included}, @content
assert_match %r{6 included}, @content
end
should "include file as variable and filters with additional parameters" do
assert_match '<li>var1 = foo</li>', @content
assert_match '<li>var2 = bar</li>', @content
end
should "include file as partial variable" do
assert_match %r{8 included}, @content
end
end
end
context "relative include tag with variable and liquid filters" do
setup do
site = fixture_site({'pygments' => true}).tap(&:read).tap(&:render)
post = site.posts.docs.find {|p| p.basename.eql? "2014-09-02-relative-includes.markdown" }
@content = post.output
end
should "include file as variable with liquid filters" do
assert_match %r{1 relative_include}, @content
assert_match %r{2 relative_include}, @content
assert_match %r{3 relative_include}, @content
end
should "include file as variable and liquid filters with arbitrary whitespace" do
assert_match %r{4 relative_include}, @content
assert_match %r{5 relative_include}, @content
assert_match %r{6 relative_include}, @content
end
should "include file as variable and filters with additional parameters" do
assert_match '<li>var1 = foo</li>', @content
assert_match '<li>var2 = bar</li>', @content
end
should "include file as partial variable" do
assert_match %r{8 relative_include}, @content
end
should "include files relative to self" do
assert_match %r{9 —\ntitle: Test Post Where YAML}, @content
end
context "trying to do bad stuff" do
context "include missing file" do
setup do
@content = <<CONTENT
---
title: missing file
---
{% include_relative missing.html %}
CONTENT
end
should "raise error relative to source directory" do
exception = assert_raises IOError do
create_post(@content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end
assert_equal 'Included file \'./missing.html\' not found', exception.message
end
end
context "include existing file above you" do
setup do
@content = <<CONTENT
---
title: higher file
---
{% include_relative ../README.markdown %}
CONTENT
end
should "raise error relative to source directory" do
exception = assert_raises ArgumentError do
create_post(@content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true})
end
assert_equal "Invalid syntax for include tag. File contains invalid characters or sequences:\n\n ../README.markdown\n\nValid syntax:\n\n {% include_relative file.ext param='value' param2='value' %}\n\n", exception.message
end
end
end
context "with symlink'd include" do
should "not allow symlink includes" do
File.open("tmp/pages-test", 'w') { |file| file.write("SYMLINK TEST") }
assert_raises IOError do
content = <<CONTENT
---
title: Include symlink
---
{% include_relative tmp/pages-test %}
CONTENT
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true, 'safe' => true })
end
refute_match /SYMLINK TEST/, @result
end
should "not expose the existence of symlinked files" do
ex = assert_raises IOError do
content = <<CONTENT
---
title: Include symlink
---
{% include_relative tmp/pages-test-does-not-exist %}
CONTENT
create_post(content, {'permalink' => 'pretty', 'source' => source_dir, 'destination' => dest_dir, 'read_posts' => true, 'safe' => true })
end
assert_match /should exist and should not be a symlink/, ex.message
end
end
end
end
|