From 1862f33025171a5a6544955eb4ca0905dfa44c69 Mon Sep 17 00:00:00 2001 From: Daniel Senff Date: Mon, 22 Jun 2020 08:07:36 +0200 Subject: [PATCH 1/3] remove unsupported color and size tags by default --- lib/rbbcode/node_extensions.rb | 44 +++++++++++++++++++++++++++-- lib/rbbcode/rbbcode_grammar.treetop | 16 +++++++++-- test/unsupported_features_test.rb | 37 ++++++++++++++++++++++++ 3 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 test/unsupported_features_test.rb diff --git a/lib/rbbcode/node_extensions.rb b/lib/rbbcode/node_extensions.rb index 38594a9..61e8163 100644 --- a/lib/rbbcode/node_extensions.rb +++ b/lib/rbbcode/node_extensions.rb @@ -185,6 +185,30 @@ def img_to_markdown end end + # You won't find this module in the .treetop file. Instead, it's effectively a specialization + # of TagNode, which calls to ColorTagNode when processing a color tag. + module ColorTagNode + def color_to_html + text.text_value + end + + def color_to_markdown + text.text_value + end + end + + # You won't find this module in the .treetop file. Instead, it's effectively a specialization + # of TagNode, which calls to SizeTagNode when processing a size tag. + module SizeTagNode + def size_to_html + text.text_value + end + + def size_to_markdown + text.text_value + end + end + module UTagNode def u_to_markdown # Underlining is unsupported in Markdown. So we just ignore [u] tags. @@ -198,8 +222,24 @@ module TagNode # For each tag name, we can either: (a) map to a simple HTML tag or Markdown character, or # (b) invoke a separate Ruby module for more advanced logic. TAG_MAPPINGS = { - html: {'b' => 'strong', 'i' => 'em', 'u' => 'u', 'url' => URLTagNode, 'img' => ImgTagNode}, - markdown: {'b' => '**', 'i' => '*', 'u' => UTagNode, 'url' => URLTagNode, 'img' => ImgTagNode} + html: { + 'b' => 'strong', + 'i' => 'em', + 'u' => 'u', + 'url' => URLTagNode, + 'img' => ImgTagNode, + 'color' => ColorTagNode, + 'size' => SizeTagNode + }, + markdown: { + 'b' => '**', + 'i' => '*', + 'u' => UTagNode, + 'url' => URLTagNode, + 'img' => ImgTagNode, + 'color' => ColorTagNode, + 'size' => SizeTagNode + } } def contents diff --git a/lib/rbbcode/rbbcode_grammar.treetop b/lib/rbbcode/rbbcode_grammar.treetop index 818673b..f166a9d 100644 --- a/lib/rbbcode/rbbcode_grammar.treetop +++ b/lib/rbbcode/rbbcode_grammar.treetop @@ -92,7 +92,7 @@ grammar RbbCodeGrammar rule tag # Make sure that anytime you call def_tag, you add it to this list: - (bold / italic / underline / simple_url / complex_url / img) + (bold / italic / underline / simple_url / complex_url / img / color / size) end @@ -101,7 +101,19 @@ grammar RbbCodeGrammar <%= def_tag 'underline', 'u' %> <%= def_tag 'simple_url', 'url' %> <%= def_tag 'img', 'img' %> - + + rule size + '[size=' size_value:[^\]]+ ']' + text:(!'[/size]' .)+ + '[/size]' + end + + rule color + '[color=' color_name:[^\]]+ ']' + text:(!'[/color]' .)+ + '[/color]' + end + rule complex_url '[url=' url:[^\]]+ ']' text:(!'[/url]' .)+ diff --git a/test/unsupported_features_test.rb b/test/unsupported_features_test.rb new file mode 100644 index 0000000..eb4a0b6 --- /dev/null +++ b/test/unsupported_features_test.rb @@ -0,0 +1,37 @@ +require File.join(File.expand_path(File.dirname(__FILE__)), 'test_helper.rb') + +class TestUnsupportedFeatures < Minitest::Test + include RbbCode::OutputAssertions + + def test_remove_color_tag_to_html + assert_converts_to( + '

Not colored but bold.

', + 'Not [color=red]colored[/color] but [b]bold.[/b]', + {} + ) + end + + def test_remove_color_tag_to_markdown + assert_converts_to( + "Not colored but **bold.**\n\n", + 'Not [color=red]colored[/color] but [b]bold.[/b]', + { output_format: :markdown } + ) + end + + def test_remove_size_tag_to_html + assert_converts_to( + '

Not resized but bold.

', + 'Not [size=3]resized[/size] but [b]bold.[/b]', + {} + ) + end + + def test_remove_size_tag_to_markdown + assert_converts_to( + "Not resized but **bold.**\n\n", + 'Not [size=3]resized[/size] but [b]bold.[/b]', + { output_format: :markdown } + ) + end +end From 9b3dda5ba2af9baa8d8bf91ac632e610a320034e Mon Sep 17 00:00:00 2001 From: Daniel Senff Date: Mon, 22 Jun 2020 08:39:34 +0200 Subject: [PATCH 2/3] add option flag unsupported_features and add support for span-class based formatting --- lib/rbbcode.rb | 25 +++++++++++++++++-------- lib/rbbcode/node_extensions.rb | 24 ++++++++++++++++++++---- test/unsupported_features_test.rb | 29 +++++++++++++++-------------- 3 files changed, 52 insertions(+), 26 deletions(-) diff --git a/lib/rbbcode.rb b/lib/rbbcode.rb index fe11e1b..9bb9477 100644 --- a/lib/rbbcode.rb +++ b/lib/rbbcode.rb @@ -27,9 +27,10 @@ def self.parser_class end def initialize(options = {}) - @options = { + @@options = { :output_format => :html, :sanitize => true, + :unsupported_features => :remove, :sanitize_config => RbbCode::DEFAULT_SANITIZE_CONFIG }.merge(options) end @@ -39,26 +40,34 @@ def convert(bb_code) bb_code = bb_code.gsub("\r\n", "\n").gsub("\r", "\n") # Add linebreaks before and after so that paragraphs etc. can be recognized. bb_code = "\n\n" + bb_code + "\n\n" - output = self.class.parser_class.new.parse(bb_code).send("to_#{@options[:output_format]}") - if @options[:emoticons] + output = self.class.parser_class.new.parse(bb_code).send("to_#{output_format}") + if options[:emoticons] output = convert_emoticons(output) end # Sanitization works for HTML only. - if @options[:output_format] == :html and @options[:sanitize] - Sanitize.clean(output, @options[:sanitize_config]) + if output_format == :html and options[:sanitize] + Sanitize.clean(output, options[:sanitize_config]) else output end end def convert_emoticons(output) - @options[:emoticons].each do |emoticon, url| + options[:emoticons].each do |emoticon, url| output.gsub!(emoticon, 'Emoticon') end output end def output_format - @options[:output_format] + options[:output_format] end -end \ No newline at end of file + + def options + @@options + end + + def self.options + @@options + end +end diff --git a/lib/rbbcode/node_extensions.rb b/lib/rbbcode/node_extensions.rb index 61e8163..9895190 100644 --- a/lib/rbbcode/node_extensions.rb +++ b/lib/rbbcode/node_extensions.rb @@ -189,11 +189,19 @@ def img_to_markdown # of TagNode, which calls to ColorTagNode when processing a color tag. module ColorTagNode def color_to_html - text.text_value + unsupported_tag end def color_to_markdown - text.text_value + unsupported_tag + end + + def unsupported_tag + if RbbCode.options.fetch(:unsupported_features) == :remove + text.text_value + elsif RbbCode.options.fetch(:unsupported_features) == :span + '' + text.text_value + '' + end end end @@ -201,11 +209,19 @@ def color_to_markdown # of TagNode, which calls to SizeTagNode when processing a size tag. module SizeTagNode def size_to_html - text.text_value + unsupported_tag end def size_to_markdown - text.text_value + unsupported_tag + end + + def unsupported_tag + if RbbCode.options.fetch(:unsupported_features) == :remove + text.text_value + elsif RbbCode.options.fetch(:unsupported_features) == :span + '' + text.text_value + '' + end end end diff --git a/test/unsupported_features_test.rb b/test/unsupported_features_test.rb index eb4a0b6..20f9a88 100644 --- a/test/unsupported_features_test.rb +++ b/test/unsupported_features_test.rb @@ -3,35 +3,36 @@ class TestUnsupportedFeatures < Minitest::Test include RbbCode::OutputAssertions - def test_remove_color_tag_to_html + def test_remove_color_and_size_tags_to_html assert_converts_to( - '

Not colored but bold.

', - 'Not [color=red]colored[/color] but [b]bold.[/b]', + '

Not colored or resized but bold.

', + 'Not [color=red]colored[/color] or [size=3]resized[/size] but [b]bold.[/b]', {} ) end - def test_remove_color_tag_to_markdown + def test_remove_color_and_size_tags_to_markdown assert_converts_to( - "Not colored but **bold.**\n\n", - 'Not [color=red]colored[/color] but [b]bold.[/b]', + "Not colored or resized but **bold.**\n\n", + 'Not [color=red]colored[/color] or [size=3]resized[/size] but [b]bold.[/b]', { output_format: :markdown } ) end - def test_remove_size_tag_to_html + def test_color_and_size_with_span_tags_to_html assert_converts_to( - '

Not resized but bold.

', - 'Not [size=3]resized[/size] but [b]bold.[/b]', - {} + '

Not colored or resized but bold.

', + 'Not [color=red]colored[/color] or [size=3]resized[/size] but [b]bold.[/b]', + { :unsupported_features => :span } ) end - def test_remove_size_tag_to_markdown + def test_color_and_size_with_tags_to_markdown assert_converts_to( - "Not resized but **bold.**\n\n", - 'Not [size=3]resized[/size] but [b]bold.[/b]', - { output_format: :markdown } + "Not colored or resized but **bold.**\n\n", + 'Not [color=red]colored[/color] or [size=3]resized[/size] but [b]bold.[/b]', + { :unsupported_features => :span, output_format: :markdown } ) end + end From 0730f5a7f0dc2f70c0c574f150d39929ccae5041 Mon Sep 17 00:00:00 2001 From: Daniel Senff Date: Mon, 22 Jun 2020 09:01:38 +0200 Subject: [PATCH 3/3] update readme to add the new supported tags --- README.markdown | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.markdown b/README.markdown index 6e4bc02..7ba8b7a 100644 --- a/README.markdown +++ b/README.markdown @@ -54,6 +54,8 @@ RbbCode supports the following BBCode features: * [i] * [u] * [s] + * [size] + * [color] * [url] * [img] * [quote]