diff --git a/lib/ruby-bbcode-to-md/bbtree.rb b/lib/ruby-bbcode-to-md/bbtree.rb index 2bdc963..eef52e3 100644 --- a/lib/ruby-bbcode-to-md/bbtree.rb +++ b/lib/ruby-bbcode-to-md/bbtree.rb @@ -17,6 +17,7 @@ def initialize(hash = { :nodes => TagCollection.new }, dictionary) @bbtree = hash @current_node = TagNode.new(@bbtree) @tags_list = [] + @nodes_list = [] @dictionary = dictionary end @@ -55,6 +56,7 @@ def parent_has_constraints_on_children? def escalate_bbtree(element) element[:parent_tag] = parent_tag @tags_list.push element[:tag] + @nodes_list.push @current_node @current_node = TagNode.new(element) end @@ -63,12 +65,7 @@ def retrogress_bbtree @tags_list.pop # remove latest tag in tags_list since it's closed now... # The parsed data manifests in @bbtree.current_node.children << TagNode.new(element) which I think is more confusing than needed - if within_open_tag? - # Set the current node to be the node we've just parsed over which is infact within another node??... - @current_node = TagNode.new(self.nodes.last) - else # If we're still at the root of the BBTree or have returned back to the root via encountring closing tags... - @current_node = TagNode.new({:nodes => self.nodes}) - end + @current_node = @nodes_list.pop # OKOKOK! # Since @bbtree = @current_node, if we ever set @current_node to something, we're actually changing @bbtree... diff --git a/lib/ruby-bbcode-to-md/tag_collection.rb b/lib/ruby-bbcode-to-md/tag_collection.rb index 1441236..4fa7a44 100644 --- a/lib/ruby-bbcode-to-md/tag_collection.rb +++ b/lib/ruby-bbcode-to-md/tag_collection.rb @@ -10,7 +10,9 @@ def to_html(tags) children_html = node.has_children? ? node.children.to_html(tags) : nil if children_html - if node.definition[:paragraphs].nil? || node.definition[:paragraphs] == :to_br + if node.definition[:newlines] == :to_br + children_html.gsub!(/\n/, "
") + elsif node.definition[:paragraphs].nil? || node.definition[:paragraphs] == :to_br children_html.gsub!(/\n\s*\n/, "\n
") end end diff --git a/lib/ruby-bbcode-to-md/tag_sifter.rb b/lib/ruby-bbcode-to-md/tag_sifter.rb index 8c32b42..9b9a8b7 100644 --- a/lib/ruby-bbcode-to-md/tag_sifter.rb +++ b/lib/ruby-bbcode-to-md/tag_sifter.rb @@ -25,6 +25,7 @@ def process_text @ti.handle_unregistered_tags_as_text # if the tag isn't in the @dictionary list, then treat it as text handle_closing_tags_that_are_multi_as_text_if_it_doesnt_match_the_latest_opener_tag_on_the_stack + handle_tags_that_are_plain_as_text_if_it_doesnt_match_the_latest_opener_tag_on_the_stack return if !valid_element? @@ -99,6 +100,18 @@ def handle_closing_tags_that_are_multi_as_text_if_it_doesnt_match_the_latest_ope end + def handle_tags_that_are_plain_as_text_if_it_doesnt_match_the_latest_opener_tag_on_the_stack + if @ti.element_is_tag? + return if @bbtree.current_node[:definition].nil? + if parent_tag != @ti[:tag].to_sym and @bbtree.current_node[:definition][:plain_tag] + @ti[:is_tag] = false + @ti[:closing_tag] = false + @ti[:text] = @ti.tag_data[:complete_match] + end + end + + end + private diff --git a/lib/tags/tags.rb b/lib/tags/tags.rb index e5b7f09..c57f5b5 100644 --- a/lib/tags/tags.rb +++ b/lib/tags/tags.rb @@ -17,9 +17,15 @@ module Tags :description => 'Underline text', :example => 'This is [u]underlined[/u].'}, :s => { - :html_open => '~~', :html_close => '~~', + :html_open => '', :html_close => '', :description => 'Strike-through text', :example => 'This is strike-through text.'}, + :code => { + :html_open => "\n```\n", :html_close => "\n```\n", + :description => 'Code block', + :example => '[code]code[/code].', + :only_allow => [], + :plain_tag => true}, :center => { :html_open => '', :html_close => '', :description => 'Center a text', @@ -68,7 +74,7 @@ module Tags :tag_param_tokens => [{:token => :href}]}, :quote => { :first_html_open => "\n", :last_html_close => "\n", - :html_open => "[quote%author%]", :html_close => "[/quote]\n", + :html_open => "[quote%author%]\n", :html_close => "\n[/quote]", :description => 'Quote another person', :example => '[quote]BBCode is great[/quote]', :allow_tag_param => true, :allow_tag_param_between => false, diff --git a/test/fluxbb_test.rb b/test/fluxbb_test.rb index 2be1cce..59db167 100644 --- a/test/fluxbb_test.rb +++ b/test/fluxbb_test.rb @@ -11,9 +11,22 @@ def fluxbb_tags :tag_param_tokens => [{:token => :alt_text, :prefix => ' alt="', :postfix => '"'}], :tag_param_description => 'The img bbcode takes alt text as a parameter' ), + :h => { + :html_open => "\n### ", :html_close => "\n", + :newlines => :to_br, + :description => 'Make a heading', + :example => '[h]My Heading[/h].'} } end + def test_heading + assert_equal "\n### My Heading\n", "[h]My Heading[/h]".bbcode_to_md(false, fluxbb_tags) + end + + def test_heading_on_two_lines + assert_equal "\n### My Heading
on two lines\n", "[h]My Heading\non two lines[/h]".bbcode_to_md(false, fluxbb_tags) + end + def test_image_with_alt_text assert_equal 'FluxBB allows alt text', '[img=FluxBB allows alt text]http://www.ruby-lang.org/images/header-ruby-logo.png[/img]'.bbcode_to_md(false, fluxbb_tags) diff --git a/test/ruby_bbcode_test.rb b/test/ruby_bbcode_test.rb index d935cb2..0b13a9e 100644 --- a/test/ruby_bbcode_test.rb +++ b/test/ruby_bbcode_test.rb @@ -28,7 +28,7 @@ def test_u end def test_s - assert_equal '~~simple~~', '[s]simple[/s]'.bbcode_to_md + assert_equal 'simple', '[s]simple[/s]'.bbcode_to_md end def test_size @@ -63,14 +63,30 @@ def test_whitespace_in_only_allowed_tags end + def test_code + assert_equal "\n```\ncode\n```\n", '[code]code[/code]'.bbcode_to_md + assert_equal "\n```\n\n```\n", "[code][/code]".bbcode_to_md(false) + assert_equal "\n```\n[b]test[/b]\n```\n", "[code][b]test[/b][/code]".bbcode_to_md + assert_equal "\n```\n[b]test\n```\n", "[code][b]test[/code]".bbcode_to_md + assert_equal "\n```\n[b]test\n\n```\n", "[code][b]test\n[/code]".bbcode_to_md(false) + end + def test_quote - assert_equal "\n[quote]quoting[/quote]\n\n", '[quote]quoting[/quote]'.bbcode_to_md - assert_equal "\n[quote=someone]quoting[/quote]\n\n", '[quote=someone]quoting[/quote]'.bbcode_to_md + assert_equal "\n[quote]\nquoting\n[/quote]\n", '[quote]quoting[/quote]'.bbcode_to_md + assert_equal "\n[quote=someone]\nquoting\n[/quote]\n", '[quote=someone]quoting[/quote]'.bbcode_to_md + assert_equal "\n[quote=someone]\nquoting\n[/quote]\n suffix", '[quote=someone]quoting[/quote] suffix'.bbcode_to_md + assert_equal "prefix \n[quote=someone]\nquoting\n[/quote]\n suffix", 'prefix [quote=someone]quoting[/quote] suffix'.bbcode_to_md + assert_equal "prefix\n[quote=someone]\nquoting\n[/quote]\nsuffix", 'prefix[quote=someone]quoting[/quote]suffix'.bbcode_to_md + assert_equal "\n[quote=user]\nquote line 1\nquote line 2\nquote line 3\n[/quote]\n", "[quote=user]quote line 1\nquote line 2\nquote line 3[/quote]".bbcode_to_md end def test_nested_quotes - assert_equal "\n[quote=Kitten]\n[quote=creatiu]f1[/quote]\n\nf2[/quote]\n\n", - '[quote=Kitten][quote=creatiu]f1[/quote]f2[/quote]'.bbcode_to_md + assert_equal "\n[quote=Kitten]\n\n[quote=creatiu]\nf1\n[/quote]\nf2\n[/quote]\n", + '[quote=Kitten][quote=creatiu]f1[/quote]f2[/quote]'.bbcode_to_md(false) + assert_equal "prefix1\n[quote=Kitten]\nf0\n[quote=creatiu]\nf1\n[/quote]\nf2\n[/quote]\nsuffix", + 'prefix1[quote=Kitten]f0[quote=creatiu]f1[/quote]f2[/quote]suffix'.bbcode_to_md(false) + assert_equal "prefix1\n[quote=Kitten]\nf0\n[quote=creatiu]\nf1\n[quote=Kitten]\nf3\n[/quote]\nf4\n[/quote]\nf2\n[/quote]\nsuffix", + 'prefix1[quote=Kitten]f0[quote=creatiu]f1[quote=Kitten]f3[/quote]f4[/quote]f2[/quote]suffix'.bbcode_to_md(false) end def test_link @@ -147,7 +163,7 @@ def test_addition_of_tags end def test_multiple_tag_test - assert_equal "**bold***italic*underline\n[quote]quote[/quote]\n\n[link](https://test.com)", + assert_equal "**bold***italic*underline\n[quote]\nquote\n[/quote]\n[link](https://test.com)", "[b]bold[/b][i]italic[/i][u]underline[/u][quote]quote[/quote][url=https://test.com]link[/url]".bbcode_to_md end