# ~*~ encoding: utf-8 ~*~ require File.expand_path(File.join(File.dirname(__FILE__), "helper")) context "Markup" do setup do @path = testpath("examples/test.git") FileUtils.rm_rf(@path) Grit::Repo.init_bare(@path) @wiki = Gollum::Wiki.new(@path) end teardown do FileUtils.rm_r(File.join(File.dirname(__FILE__), *%w[examples test.git])) end test "formats page from Wiki#pages" do @wiki.write_page("Bilbo Baggins", :markdown, "a [[Foo]][[Bar]] b", commit_details) assert @wiki.pages[0].formatted_data end # This test is to assume that Sanitize.clean doesn't raise Encoding::CompatibilityError on ruby 1.9 test "formats non ASCII-7 character page from Wiki#pages" do wiki = Gollum::Wiki.new(testpath("examples/yubiwa.git")) assert_nothing_raised(defined?(Encoding) && Encoding::CompatibilityError) do assert wiki.page("strider").formatted_data end end test "Gollum::Markup#render yields a DocumentFragment" do yielded = false @wiki.write_page("Yielded", :markdown, "abc", commit_details) page = @wiki.page("Yielded") markup = Gollum::Markup.new(page) markup.render do |doc| assert_kind_of Nokogiri::HTML::DocumentFragment, doc yielded = true end assert yielded end test "Gollum::Page#formatted_data yields a DocumentFragment" do yielded = false @wiki.write_page("Yielded", :markdown, "abc", commit_details) page = @wiki.page("Yielded") page.formatted_data do |doc| assert_kind_of Nokogiri::HTML::DocumentFragment, doc yielded = true end assert yielded end ######################################################################### # # Links # ######################################################################### test "double page links no space" do @wiki.write_page("Bilbo Baggins", :markdown, "a [[Foo]][[Bar]] b", commit_details) # "

a FooBar b

" page = @wiki.page("Bilbo Baggins") doc = Nokogiri::HTML page.formatted_data paras = doc / :p para = paras.first anchors = para / :a assert_equal 1, paras.size assert_equal 2, anchors.size assert_equal 'internal absent', anchors[0]['class'] assert_equal 'internal absent', anchors[1]['class'] assert_equal '/Foo', anchors[0]['href'] assert_equal '/Bar', anchors[1]['href'] assert_equal 'Foo', anchors[0].text assert_equal 'Bar', anchors[1].text end test "double page links with space" do @wiki.write_page("Bilbo Baggins", :markdown, "a [[Foo]] [[Bar]] b", commit_details) # "

a Foo Bar b

" page = @wiki.page("Bilbo Baggins") doc = Nokogiri::HTML page.formatted_data paras = doc / :p para = paras.first anchors = para / :a assert_equal 1, paras.size assert_equal 2, anchors.size assert_equal 'internal absent', anchors[0]['class'] assert_equal 'internal absent', anchors[1]['class'] assert_equal '/Foo', anchors[0]['href'] assert_equal '/Bar', anchors[1]['href'] assert_equal 'Foo', anchors[0].text assert_equal 'Bar', anchors[1].text end test "page link" do @wiki.write_page("Bilbo Baggins", :markdown, "a [[Bilbo Baggins]] b", commit_details) page = @wiki.page("Bilbo Baggins") output = page.formatted_data assert_match /class="internal present"/, output assert_match /href="\/Bilbo-Baggins"/, output assert_match /\>Bilbo Baggins\J\. R\. R\. Tolkien\ path) @wiki.write_page(name, :markdown, "a [[#{name}]] b", commit_details) page = @wiki.page(name) output = page.formatted_data assert_match /class="internal present"/, output assert_match /href="\/wiki\/Bilbo-Baggins-\d"/, output assert_match /\>Bilbo Baggins \d\a http://example.com b

", page.formatted_data end test "page link with different text" do @wiki.write_page("Potato", :markdown, "a [[Potato Heaad|Potato]] ", commit_details) page = @wiki.page("Potato") output = page.formatted_data assert_equal "

aPotatoHeaad

", normal(output) end test "page link with different text on mediawiki" do @wiki.write_page("Potato", :mediawiki, "a [[Potato|Potato Heaad]] ", commit_details) page = @wiki.page("Potato") output = page.formatted_data assert_equal normal("

\na Potato Heaad

"), normal(output) end ######################################################################### # # Images # ######################################################################### test "image with http url" do ['http', 'https'].each do |scheme| name = "Bilbo Baggins #{scheme}" @wiki.write_page(name, :markdown, "a [[#{scheme}://example.com/bilbo.jpg]] b", commit_details) page = @wiki.page(name) output = page.formatted_data assert_equal %{

a b

}, output end end test "image with extension in caps with http url" do ['http', 'https'].each do |scheme| name = "Bilbo Baggins #{scheme}" @wiki.write_page(name, :markdown, "a [[#{scheme}://example.com/bilbo.JPG]] b", commit_details) page = @wiki.page(name) output = page.formatted_data assert_equal %{

a b

}, output end end test "image with absolute path" do @wiki = Gollum::Wiki.new(@path, :base_path => '/wiki') index = @wiki.repo.index index.add("alpha.jpg", "hi") index.commit("Add alpha.jpg") @wiki.write_page("Bilbo Baggins", :markdown, "a [[/alpha.jpg]] [[a | /alpha.jpg]] b", commit_details) page = @wiki.page("Bilbo Baggins") assert_equal %{

a a b

}, page.formatted_data end test "image with relative path on root" do @wiki = Gollum::Wiki.new(@path, :base_path => '/wiki') index = @wiki.repo.index index.add("alpha.jpg", "hi") index.add("Bilbo-Baggins.md", "a [[alpha.jpg]] [[a | alpha.jpg]] b") index.commit("Add alpha.jpg") page = @wiki.page("Bilbo Baggins") assert_equal %{

a a b

}, page.formatted_data end test "image with relative path" do @wiki = Gollum::Wiki.new(@path, :base_path => '/wiki') index = @wiki.repo.index index.add("greek/alpha.jpg", "hi") index.add("greek/Bilbo-Baggins.md", "a [[alpha.jpg]] [[a | alpha.jpg]] b") index.commit("Add alpha.jpg") page = @wiki.page("Bilbo Baggins") output = page.formatted_data assert_equal %{

a a b

}, output end test "image with absolute path on a preview" do @wiki = Gollum::Wiki.new(@path, :base_path => '/wiki') index = @wiki.repo.index index.add("alpha.jpg", "hi") index.commit("Add alpha.jpg") page = @wiki.preview_page("Test", "a [[/alpha.jpg]] b", :markdown) assert_equal %{

a b

}, page.formatted_data end test "image with relative path on a preview" do @wiki = Gollum::Wiki.new(@path, :base_path => '/wiki') index = @wiki.repo.index index.add("alpha.jpg", "hi") index.add("greek/alpha.jpg", "hi") index.commit("Add alpha.jpg") page = @wiki.preview_page("Test", "a [[alpha.jpg]] [[greek/alpha.jpg]] b", :markdown) assert_equal %{

a b

}, page.formatted_data end test "image with alt" do content = "a [[alpha.jpg|alt=Alpha Dog]] b" output = %{

a Alpha Dog b

} relative_image(content, output) end test "image with em or px dimension" do %w{em px}.each do |unit| %w{width height}.each do |dim| content = "a [[alpha.jpg|#{dim}=100#{unit}]] b" output = "

a b

" relative_image(content, output) end end end test "image with bogus dimension" do %w{width height}.each do |dim| content = "a [[alpha.jpg|#{dim}=100]] b" output = "

a b

" relative_image(content, output) end end test "image with vertical align" do %w{top texttop middle absmiddle bottom absbottom baseline}.each do |align| content = "a [[alpha.jpg|align=#{align}]] b" output = "

a b

" relative_image(content, output) end end test "image with horizontal align" do %w{left center right}.each do |align| content = "a [[alpha.jpg|align=#{align}]] b" output = "

a b

" relative_image(content, output) end end test "image with float" do content = "a\n\n[[alpha.jpg|float]]\n\nb" output = "

a

\n\n

\n\n

b

" relative_image(content, output) end test "image with float and align" do %w{left right}.each do |align| content = "a\n\n[[alpha.jpg|float|align=#{align}]]\n\nb" output = "

a

\n\n

\n\n

b

" relative_image(content, output) end end test "image with frame" do content = "a\n\n[[alpha.jpg|frame]]\n\nb" output = "

a

\n\n

\n\n

b

" relative_image(content, output) end test "absolute image with frame" do content = "a\n\n[[http://example.com/bilbo.jpg|frame]]\n\nb" output = "

a

\n\n

\n\n

b

" relative_image(content, output) end test "image with frame and alt" do content = "a\n\n[[alpha.jpg|frame|alt=Alpha]]\n\nb" output = "

a

\n\n

\"Alpha\"Alpha

\n\n

b

" relative_image(content, output) end ######################################################################### # # File links # ######################################################################### test "file link with absolute path" do index = @wiki.repo.index index.add("alpha.jpg", "hi") index.commit("Add alpha.jpg") @wiki.write_page("Bilbo Baggins", :markdown, "a [[Alpha|/alpha.jpg]] b", commit_details) page = @wiki.page("Bilbo Baggins") output = Gollum::Markup.new(page).render assert_equal %{

a Alpha b

}, output end test "file link with relative path" do index = @wiki.repo.index index.add("greek/alpha.jpg", "hi") index.add("greek/Bilbo-Baggins.md", "a [[Alpha|alpha.jpg]] b") index.commit("Add alpha.jpg") page = @wiki.page("Bilbo Baggins") output = Gollum::Markup.new(page).render assert_equal %{

a Alpha b

}, output end test "file link with external path" do index = @wiki.repo.index index.add("greek/Bilbo-Baggins.md", "a [[Alpha|http://example.com/alpha.jpg]] b") index.commit("Add alpha.jpg") page = @wiki.page("Bilbo Baggins") assert_equal %{

a Alpha b

}, page.formatted_data end ######################################################################### # # Code # ######################################################################### test "code blocks" do content = "a\n\n```ruby\nx = 1\n```\n\nb" output = "

a

\n\n
\n
" +
             "x = " +
             "1\n
\n
\n\n\n

b

" index = @wiki.repo.index index.add("Bilbo-Baggins.md", content) index.commit("Add alpha.jpg") page = @wiki.page("Bilbo Baggins") rendered = Gollum::Markup.new(page).render assert_equal output, rendered end test "code blocks with carriage returns" do content = "a\r\n\r\n```ruby\r\nx = 1\r\n```\r\n\r\nb" output = "

a

\n\n
\n
" +
             "x = " +
             "1\n
\n
\n\n\n

b

" index = @wiki.repo.index index.add("Bilbo-Baggins.md", content) index.commit("Add alpha.jpg") page = @wiki.page("Bilbo Baggins") rendered = Gollum::Markup.new(page).render assert_equal output, rendered end test "code blocks with two-space indent" do content = "a\n\n```ruby\n x = 1\n\n y = 2\n```\n\nb" output = "

a

\n\n
" +
             "x = 1" +
             "\n\ny =" +
             " 2\n
\n
\n\n\n

b

" compare(content, output) end test "code blocks with one-tab indent" do content = "a\n\n```ruby\n\tx = 1\n\n\ty = 2\n```\n\nb" output = "

a

\n\n
" +
             "x = 1" +
             "\n\ny =" +
             " 2\n
\n
\n\n\n

b

" compare(content, output) end test "code with wiki links" do content = <<-END booya ``` python np.array([[2,2],[1,3]],np.float) ``` END # rendered with Gollum::Markup page, rendered = render_page(content) assert_markup_highlights_code Gollum::Markup, rendered if Gollum.const_defined?(:MarkupGFM) rendered_gfm = Gollum::MarkupGFM.new(page).render assert_markup_highlights_code Gollum::MarkupGFM, rendered_gfm end end def assert_markup_highlights_code(markup_class, rendered) assert_match /div class="highlight"/, rendered, "#{markup_class} doesn't highlight code\n #{rendered}" assert_match /span class="n"/, rendered, "#{markup_class} doesn't highlight code\n #{rendered}" assert_match /\(\[\[/, rendered, "#{markup_class} parses out wiki links\n#{rendered}" end ######################################################################### # # Various # ######################################################################### test "strips javscript protocol urls" do content = "[Hack me](javascript:hacked=true)" output = "

Hackme

" compare(content, output) end test "escaped wiki link" do content = "a '[[Foo]], b" output = "

a [[Foo]], b

" compare(content, output) end test "quoted wiki link" do content = "a '[[Foo]]', b" output = "

a 'Foo', b

" compare(content, output, 'md', [ /class="internal absent"/, /href="\/Foo"/, /\>Foo\a Google b

" compare(content, output, 'org') end test "org mode style double file links" do content = "a [[file:f.org][Google]] b" output = "

a Google b

" compare(content, output, 'org') end test "short double links" do content = "a [[b]] c" output = %(

a b c

) compare(content, output, 'org') end test "double linked pipe" do content = "a [[|]] b" output = %(

a b

) compare(content, output, 'org') end # test "id with prefix ok" do # content = "h2(example#wiki-foo). xxxx" # output = %(

xxxx

) # compare(content, output, :textile) # end # test "id prefix added" do # content = "h2(#foo). xxxx[1]\n\nfn1.footnote" # output = "

xxxx" + # "1

" + # "\n

1 footnote

" # compare(content, output, :textile) # end # test "name prefix added" do # content = "abc\n\n__TOC__\n\n==Header==\n\nblah" # compare content, '', :mediawiki, [ # /id="wiki-toc"/, # /href="#wiki-Header"/, # /id="wiki-Header"/, # /name="wiki-Header"/ # ] # end ######################################################################### # # TeX # ######################################################################### test "TeX block syntax" do content = 'a \[ a^2 \] b' output = "

ab

" compare(content, output, 'md') end test "TeX inline syntax" do content = 'a \( a^2 \) b' output = "

ab

" compare(content, output, 'md') end ######################################################################### # # Helpers # ######################################################################### def render_page(content, ext = "md") index = @wiki.repo.index index.add("Bilbo-Baggins.#{ext}", content) index.commit("Add baggins") page = @wiki.page("Bilbo Baggins") [page, Gollum::Markup.new(page).render] end def compare(content, output, ext = "md", regexes = []) page, rendered = render_page(content, ext) if regexes.empty? assert_equal normal(output), normal(rendered) else output = page.formatted_data regexes.each { |r| assert_match r, output } end end def relative_image(content, output) index = @wiki.repo.index index.add("greek/alpha.jpg", "hi") index.add("greek/Bilbo-Baggins.md", content) index.commit("Add alpha.jpg") @wiki.clear_cache page = @wiki.page("Bilbo Baggins") rendered = Gollum::Markup.new(page).render assert_equal normal(output), normal(rendered) end end