Skip to content

Instantly share code, notes, and snippets.

@TheNotary
Last active December 15, 2015 18:59
Show Gist options
  • Save TheNotary/5307428 to your computer and use it in GitHub Desktop.
Save TheNotary/5307428 to your computer and use it in GitHub Desktop.
module RubyBBCode
# This class builds objects that hold TagNodes. It's really just a simple array, with the addition of the #to_html method
class TagCollection < Array
class HtmlTemplate
def initialize(tag_definition)
@tag_definition = tag_definition
@data = tag_definition[:html_open].dup
end
def inlay_between_text!(node)
@data.gsub!('%between%', node[:between]) if between_text_goes_into_html_output_as_param? # set the between text to where it goes if required to do so...
end
def inlay_inline_params!(node)
# TODO: refactor this into #...
# Get list of paramaters to feed
match_array = node[:params][:tag_param].scan(@tag_definition[:tag_param])[0]
# for each parameter to feed
match_array.each.with_index do |match, i|
if i < @tag_definition[:tag_param_tokens].length
@data.gsub!("%#{@tag_definition[:tag_param_tokens][i][:token].to_s}%",
@tag_definition[:tag_param_tokens][i][:prefix].to_s +
match +
@tag_definition[:tag_param_tokens][i][:postfix].to_s)
end
end
end
def remove_unused_tokens!(node)
@tag_definition[:tag_param_tokens].each do |token|
@data.gsub!("%#{token[:token]}%", '')
end
end
def output
@data
end
def to_s
@data.to_s
end
def +(s)
@data + s
end
private
def between_text_goes_into_html_output_as_param?
@tag_definition[:require_between]
end
end
def to_html(tags)
require 'pry'
html_string = ""
self.each do |node|
if node.type == :tag
@tag_definition = tags[node[:tag]]
t = HtmlTemplate.new @tag_definition
# t = @tag_definition[:html_open].dup # t = html Template
t.inlay_between_text!(node)
if @tag_definition[:allow_tag_param]
if node.param_set?
t.inlay_inline_params!(node)
else
# Remove unused tokens
t.remove_unused_tokens!
end
end
binding.pry
html_string += t
html_string += node[:nodes].to_html(tags) if node[:nodes].length > 0
t = @tag_definition[:html_close]
t.gsub!('%between%', node[:between]) if @tag_definition[:require_between]
html_string += t
elsif node.type == :text
html_string += node[:text] unless node[:text].nil?
end
end
html_string
end
# TODO: Deleteme, I got moved
def between_text_goes_into_html_output_as_param?
@tag_definition[:require_between]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment