From 0879e6d6b2b947ff642b542bc7689aa2f871fb14 Mon Sep 17 00:00:00 2001 From: Harry Wood Date: Tue, 8 Nov 2022 04:28:31 +0000 Subject: [PATCH 01/10] Allow :newlines => :to_br tag config Allow a bbcode tag config to force all newlines into
html tags within its children_html. This is useful for outputting markdown headings as follows. Headings need to be all on one line even if the source bbcode allows newline chars. `## My heading
continued on the next line.` Outputting headings? This is something I need to be able to do as a custom "additional tag" at least. --- lib/ruby-bbcode-to-md/tag_collection.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 From ca00fd3a326267ac734d2e4b0ec29fe327d3ad27 Mon Sep 17 00:00:00 2001 From: Harry Wood Date: Fri, 4 Nov 2022 14:43:46 +0000 Subject: [PATCH 02/10] Add [h] tag config Add an example tag config which makes use of the new 'to_br' feature. This is how we would use it for FluxBB (but iobviously doesn't effect default behaviour) --- test/fluxbb_test.rb | 13 +++++++++++++ 1 file changed, 13 insertions(+) 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) From 214aa404ef48f591f7ff8a9fdfe1f63891a8b120 Mon Sep 17 00:00:00 2001 From: Grant Slater Date: Mon, 9 Jan 2023 18:09:39 +0000 Subject: [PATCH 03/10] Disable [s] to ~~ as not supported by discourse --- lib/tags/tags.rb | 8 ++++---- test/ruby_bbcode_test.rb | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/tags/tags.rb b/lib/tags/tags.rb index e5b7f09..85f23a9 100644 --- a/lib/tags/tags.rb +++ b/lib/tags/tags.rb @@ -16,10 +16,10 @@ module Tags :html_open => '', :html_close => '', :description => 'Underline text', :example => 'This is [u]underlined[/u].'}, - :s => { - :html_open => '~~', :html_close => '~~', - :description => 'Strike-through text', - :example => 'This is strike-through text.'}, + # :s => { + # :html_open => '~~', :html_close => '~~', + # :description => 'Strike-through text', + # :example => 'This is strike-through text.'}, :center => { :html_open => '', :html_close => '', :description => 'Center a text', diff --git a/test/ruby_bbcode_test.rb b/test/ruby_bbcode_test.rb index d935cb2..ffb1900 100644 --- a/test/ruby_bbcode_test.rb +++ b/test/ruby_bbcode_test.rb @@ -27,9 +27,9 @@ def test_u assert_equal "line 1\nline 2", "[u]line 1\nline 2[/u]".bbcode_to_md end - def test_s - assert_equal '~~simple~~', '[s]simple[/s]'.bbcode_to_md - end + # def test_s + # assert_equal '~~simple~~', '[s]simple[/s]'.bbcode_to_md + # end def test_size assert_equal '[size=32]32px Text[/size]', '[size=32]32px Text[/size]'.bbcode_to_md From ea9eadbe8fa7a99ab7133474a445344c230fd97c Mon Sep 17 00:00:00 2001 From: Grant Slater Date: Tue, 10 Jan 2023 23:35:43 +0000 Subject: [PATCH 04/10] More spaces for quotes and re-enable strikethrough --- lib/tags/tags.rb | 10 +++++----- test/ruby_bbcode_test.rb | 19 ++++++++++++------- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/lib/tags/tags.rb b/lib/tags/tags.rb index 85f23a9..7b1d1b0 100644 --- a/lib/tags/tags.rb +++ b/lib/tags/tags.rb @@ -16,10 +16,10 @@ module Tags :html_open => '', :html_close => '', :description => 'Underline text', :example => 'This is [u]underlined[/u].'}, - # :s => { - # :html_open => '~~', :html_close => '~~', - # :description => 'Strike-through text', - # :example => 'This is strike-through text.'}, + :s => { + :html_open => '', :html_close => '', + :description => 'Strike-through text', + :example => 'This is strike-through text.'}, :center => { :html_open => '', :html_close => '', :description => 'Center a text', @@ -68,7 +68,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/ruby_bbcode_test.rb b/test/ruby_bbcode_test.rb index ffb1900..6c6146a 100644 --- a/test/ruby_bbcode_test.rb +++ b/test/ruby_bbcode_test.rb @@ -27,9 +27,9 @@ def test_u assert_equal "line 1\nline 2", "[u]line 1\nline 2[/u]".bbcode_to_md end - # def test_s - # assert_equal '~~simple~~', '[s]simple[/s]'.bbcode_to_md - # end + def test_s + assert_equal 'simple', '[s]simple[/s]'.bbcode_to_md + end def test_size assert_equal '[size=32]32px Text[/size]', '[size=32]32px Text[/size]'.bbcode_to_md @@ -64,13 +64,18 @@ def test_whitespace_in_only_allowed_tags 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 end def test_nested_quotes - assert_equal "\n[quote=Kitten]\n[quote=creatiu]f1[/quote]\n\nf2[/quote]\n\n", + 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 + 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 end def test_link @@ -147,7 +152,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 From a5254562b17b7fb7e7f808c025f1a63af5c1e4f6 Mon Sep 17 00:00:00 2001 From: Grant Slater Date: Thu, 12 Jan 2023 02:09:07 +0000 Subject: [PATCH 05/10] Additional quote test --- test/ruby_bbcode_test.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/test/ruby_bbcode_test.rb b/test/ruby_bbcode_test.rb index 6c6146a..6b5a78b 100644 --- a/test/ruby_bbcode_test.rb +++ b/test/ruby_bbcode_test.rb @@ -69,6 +69,7 @@ def test_quote 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 From b18791f1cad2bb6bc2a63de79159c0d102b78e4d Mon Sep 17 00:00:00 2001 From: Grant Slater Date: Thu, 12 Jan 2023 14:59:54 +0000 Subject: [PATCH 06/10] Revert [s] code and add broken [code] converter --- lib/tags/tags.rb | 8 +++++++- test/ruby_bbcode_test.rb | 8 +++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/tags/tags.rb b/lib/tags/tags.rb index 7b1d1b0..aa28065 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%between%", :html_close => "\n```\n", + :description => 'Code block', + :example => '[code]code[/code].', + :only_allow => [], + :require_between => true}, :center => { :html_open => '', :html_close => '', :description => 'Center a text', diff --git a/test/ruby_bbcode_test.rb b/test/ruby_bbcode_test.rb index 6b5a78b..c02e801 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,6 +63,12 @@ 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 + end + def test_quote 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 From 932616993cfaa48406c80f736a9801a266991c64 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Thu, 12 Jan 2023 16:02:27 +0000 Subject: [PATCH 07/10] Treat tags as text inside code blocks --- lib/ruby-bbcode-to-md/tag_sifter.rb | 13 +++++++++++++ lib/tags/tags.rb | 4 ++-- 2 files changed, 15 insertions(+), 2 deletions(-) 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 aa28065..e866bb8 100644 --- a/lib/tags/tags.rb +++ b/lib/tags/tags.rb @@ -21,11 +21,11 @@ module Tags :description => 'Strike-through text', :example => 'This is strike-through text.'}, :code => { - :html_open => "\n```\n%between%", :html_close => "\n```\n", + :html_open => "\n```\n", :html_close => "\n```\n", :description => 'Code block', :example => '[code]code[/code].', :only_allow => [], - :require_between => true}, + :plain_tag => true}, :center => { :html_open => '', :html_close => '', :description => 'Center a text', From 7c7ca15d5902082b019b02374e27c1b8d3f88c3a Mon Sep 17 00:00:00 2001 From: Grant Slater Date: Thu, 12 Jan 2023 17:48:46 +0000 Subject: [PATCH 08/10] Use strikethrough and additional tests --- lib/tags/tags.rb | 2 +- test/ruby_bbcode_test.rb | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/tags/tags.rb b/lib/tags/tags.rb index e866bb8..c57f5b5 100644 --- a/lib/tags/tags.rb +++ b/lib/tags/tags.rb @@ -17,7 +17,7 @@ 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 => { diff --git a/test/ruby_bbcode_test.rb b/test/ruby_bbcode_test.rb index c02e801..2e41778 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 @@ -67,6 +67,8 @@ 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 From 63a72da4199e2b66d43d86f322c11b123c145438 Mon Sep 17 00:00:00 2001 From: Grant Slater Date: Wed, 15 Feb 2023 17:49:52 +0000 Subject: [PATCH 09/10] Add failing nested quote test --- test/ruby_bbcode_test.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/ruby_bbcode_test.rb b/test/ruby_bbcode_test.rb index 2e41778..0b13a9e 100644 --- a/test/ruby_bbcode_test.rb +++ b/test/ruby_bbcode_test.rb @@ -82,9 +82,11 @@ def test_quote def test_nested_quotes 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 + '[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 + '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 From e1f1fc3c0ece06f004ea6c5b30f0636debce5a6d Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Wed, 15 Feb 2023 19:07:31 +0000 Subject: [PATCH 10/10] Track node stack explicitly --- lib/ruby-bbcode-to-md/bbtree.rb | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) 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...