Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ begin
rescue LoadError
require 'rdoc/rdoc'
require 'rake/rdoctask'
require 'rdoc/task'
require 'rdoc/task'
RDoc::Task = Rake::RDocTask
end

Expand Down
2 changes: 1 addition & 1 deletion lib/ruby-bbcode-to-md.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def self.validation_enabled
# This method converts the given text (with BBCode tags) into a HTML representation
# The escape_html parameter (default: true) escapes HTML tags that were present in the given text and therefore blocking (mallicious) HTML in the original text
# The additional_tags parameter is used to add additional BBCode tags that should be accepted
# The method paramter determines whether the tags parameter needs to be used to blacklist (when set to :disable) or whitelist (when not set to :disable) the list of BBCode tags
# The method parameter determines whether the tags parameter needs to be used to blacklist (when set to :disable) or whitelist (when not set to :disable) the list of BBCode tags
def self.to_html(text, escape_html = true, additional_tags = {}, method = :disable, *tags)
text = text.clone

Expand Down
11 changes: 4 additions & 7 deletions lib/ruby-bbcode-to-md/bbtree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -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...
Expand All @@ -91,4 +88,4 @@ def to_html(tags = {})
end

end
end
end
44 changes: 22 additions & 22 deletions lib/ruby-bbcode-to-md/debugging.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
module RubyBBCode
def self.log(string, clear_file = true)
clear_log_file_at_beginning_of_execution clear_file

File.open('/tmp/ruby-bbcode.log', 'a') do |f|
f.puts string
end
end

def self.clear_log_file_at_beginning_of_execution(clear_file)
return if !clear_file
if defined?(@@cleared_file).nil?
Expand All @@ -16,16 +16,16 @@ def self.clear_log_file_at_beginning_of_execution(clear_file)
end
end
end


# This module can be included in the BBTree and TagNode to give them debugging features
module DebugBBTree
# For Debugging/ visualization purposes.
# This can be used to render the #nodes array in a pretty manor, showing the hirarchy.
# This can be used to render the #nodes array in a pretty manor, showing the hierarchy.
def to_v
tree = self
visual_string = ''

walk_tree(tree) do |node, depth|
indentation = ' ' * depth
case node[:is_tag]
Expand All @@ -35,11 +35,11 @@ def to_v
visual_string += "#{indentation}\"#{node[:text]}\"\n"
end
end

visual_string
end


# this blocky method counts how many children are
# in the TagNode.children, recursively walking the tree
def count_child_nodes(hash = self)
Expand All @@ -50,13 +50,13 @@ def count_child_nodes(hash = self)
count
end

# For BBTree, teh to_s method shows the count of the children plus a graphical
# depiction of the tree, and the relation of the nodes.
# For TagNodes, the root-most tag is displayed, and the children are counted.
# For BBTree, the to_s method shows the count of the children plus a graphical
# depiction of the tree, and the relation of the nodes.
# For TagNodes, the root-most tag is displayed, and the children are counted.
def to_s
object_identifier = "#<#{self.class.to_s}:0x#{'%x' % (self.object_id << 1)}\n"
close_object = ">\n"

case self
when RubyBBCode::BBTree
object_identifier + "Children: #{count_child_nodes}\n" + self.to_v + close_object
Expand All @@ -68,26 +68,26 @@ def to_s
end
end
end

private
# This function is used by to_v and anything else that needs to iterate through the

# This function is used by to_v and anything else that needs to iterate through the
# @bbtree
def walk_tree(tree, depth = -1, &blk)
return enum_for(:walk_tree) unless blk # ignore me for now, I'm a convention for being versatile

# Perform the block action specified at top level!!!
yield tree, depth unless depth == -1

# next if we're a text node
return if tree.type == :text

# Enter into recursion (including block action) for each child node in this node
tree.children.each do |node|
walk_tree(node, depth + 1, &blk)
end
end

end
end

end
10 changes: 9 additions & 1 deletion lib/ruby-bbcode-to-md/tag_collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ def to_html(tags)
# let child node render:
children_html = node.has_children? ? node.children.to_html(tags) : nil

if children_html
if node.definition[:newlines] == :to_br
children_html.gsub!(/\n/, "<br>")
elsif node.definition[:paragraphs].nil? || node.definition[:paragraphs] == :to_br
children_html.gsub!(/\n\s*\n/, "\n<br>")
end
end

t = HtmlTemplate.new node

t.inlay_between_text!(children_html)
Expand Down Expand Up @@ -118,4 +126,4 @@ def between_text_goes_into_html_output_as_param?
end

end
end
end
2 changes: 1 addition & 1 deletion lib/ruby-bbcode-to-md/tag_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,4 @@ def tag_param=(param)
end

end
end
end
13 changes: 13 additions & 0 deletions lib/ruby-bbcode-to-md/tag_sifter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?

Expand Down Expand Up @@ -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

Expand Down
26 changes: 19 additions & 7 deletions lib/tags/tags.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module RubyBBCode
module Tags
# tagname => tag, HTML open tag, HTML close tag, description, example
# All of these entrys are represented as @dictionary in the classes (or as the variable tags)
# All of these entries are represented as @dictionary in the classes (or as the variable tags)
# A single item from this file (eg the :b entry) is refered to as a @definition
@@tags = {
:b => {
Expand All @@ -16,6 +16,15 @@ 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 <s>strike-through text</s>.'},
:code => {
:html_open => "\n```\n", :html_close => "\n```\n",
:description => 'Code block',
:example => '[code]code[/code].',
:plain_tag => true},
:center => {
:html_open => '', :html_close => '',
:description => 'Center a text',
Expand All @@ -24,12 +33,14 @@ module Tags
:html_open => "\n", :html_close => "\n",
:description => 'Unordered list',
:example => '[ul][li]List item[/li][li]Another list item[/li][/ul].',
:only_allow => [ :li ]},
:only_allow => [ :li ],
:paragraphs => :allow},
:ol => {
:html_open => "\n", :html_close => "\n",
:description => 'Ordered list',
:example => '[ol][li]List item[/li][li]Another list item[/li][/ol].',
:only_allow => [ :li ]},
:only_allow => [ :li ],
:paragraphs => :allow},
:li => {
:html_open => {
:ul => ' - ',
Expand Down Expand Up @@ -61,20 +72,21 @@ module Tags
:tag_param => /(.*)/,
:tag_param_tokens => [{:token => :href}]},
:quote => {
:first_html_open => "\n", :last_html_close => "\n",
:html_open => "[quote%author%]", :html_close => "[/quote]\n",
:html_open => "\n[quote%author%]\n", :html_close => "\n[/quote]\n",
:description => 'Quote another person',
:example => '[quote]BBCode is great[/quote]',
:allow_tag_param => true, :allow_tag_param_between => false,
:tag_param => /(.*)/,
:tag_param_tokens => [{:token => :author, :prefix => '=', :postfix => ""}]},
:tag_param_tokens => [{:token => :author, :prefix => '=', :postfix => ""}],
:paragraphs => :allow},
:size => {
:html_open => '[size=%size%]', :html_close => '[/size]',
:description => 'Change the size of the text',
:example => '[size=32]This is 32px[/size]',
:allow_tag_param => true, :allow_tag_param_between => false,
:tag_param => /(\d*)/,
:tag_param_tokens => [{:token => :size}]},
:tag_param_tokens => [{:token => :size}],
:paragraphs => :allow},
:color => {
:html_open => '', :html_close => '',
:description => 'Change the color of the text',
Expand Down
44 changes: 44 additions & 0 deletions test/fluxbb_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
require_relative 'test_helper'

class FluxbbTest < MiniTest::Unit::TestCase
def fluxbb_tags
return {
img: RubyBBCode::Tags.tag_list[:img].merge(
:html_open => '<img src="%between%"%alt_text%/>',
:html_close => '',
:example => '[img=my alt text]http://www.google.com/intl/en_ALL/images/logo.gif[/img]',
:tag_param => /(.*)/,
: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<br>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 '<img src="http://www.ruby-lang.org/images/header-ruby-logo.png" alt="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)
end

def test_image_without_alt_text
assert_equal '<img src="http://www.ruby-lang.org/images/header-ruby-logo.png"/>',
'[img]http://www.ruby-lang.org/images/header-ruby-logo.png[/img]'.bbcode_to_md(false, fluxbb_tags)
end

def test_ignored_attempt_to_pass_width_height
assert_equal '<img src="http://www.ruby-lang.org/images/header-ruby-logo.png" alt="100x200"/>',
'[img=100x200]http://www.ruby-lang.org/images/header-ruby-logo.png[/img]'.bbcode_to_md(false, fluxbb_tags)
end
end
Loading