lib/flannel/html_formatter.rb in flannel-0.2.13 vs lib/flannel/html_formatter.rb in flannel-0.2.14

- old
+ new

@@ -1,147 +1,59 @@ +require 'flannel/html_transformable' + module Flannel + + # HtmlFormatter is responsible for formatting text blocks as html class HtmlFormatter - include Wrappable - + include Flannel::HtmlTransformable + def initialize - @tags ={:preformatted => "pre", - :feed => "ul", - :list => "ul", - :dlist => "dl", - :header_1 => "h1", #old style - :header_2 => "h2", - :header_3 => "h3", - :header_4 => "h4", - :header_5 => "h5", - :header_6 => "h6", + @tags ={:preformatted => "pre", + :feed => "ul", + :list => "ul", + :dlist => "dl", :header_one => "h1", #new style - :header_two => "h2", - :header_three => "h3", - :header_four => "h4", - :header_five => "h5", + :header_two => "h2", + :header_three => "h3", + :header_four => "h4", + :header_five => "h5", :header_six => "h6", :paragraph => "p", :blockquote => "blockquote"} end - + def do text, style, id=nil - steps = get_steps_for style, id - inject text, steps - end + @text = text + @style = style + @id = id - def permalink topic - topic.gsub(%r{[^/\w\s\-]},'').gsub(%r{[^\w/]|[\_]},' ').split.join('-').downcase + html = build_html + html.force_encoding("UTF-8") + html end - - def inject text, steps - if steps.empty? - text - else - step = steps.shift - text = step.call text - inject text, steps - end - end - - def get_steps_for style, id - steps = [] - - case style - + + def build_html + case @style + when :preformatted - steps << lambda { |text| html_escape text} + html = html_escape @text when :feed - steps << lambda { |text| parse_feed text } + html = parse_feed @text when :image - steps << lambda { |text| create_img text } + html = create_img @text else - steps << lambda { |text| build_wiki_links text } - steps << lambda { |text| convert_external_links text } - end - - if style == :list - steps << lambda { |text| format_list text } - end - - if style == :dlist - steps << lambda { |text| format_dlist text } - end - - steps << lambda { |text| wrap(text, @tags[style], id) } - - steps - end - - def html_escape text - require 'cgi' + html = build_wiki_links @text + html = convert_external_links html - CGI::escapeHTML(text) - end - - def build_wiki_links text - text.gsub(/-\w(.*?)\w>/) { |match| %{<a href="#{wiki_link match}">#{format_link_display(match)}</a>}} - end - - def create_img text - desc, url = text.split(/\n/, 2) - - return text unless url - - "<img src='#{url}' alt='#{desc}' title='#{desc}' />" - end - - def wiki_link topic - permalink topic[1..-2] - end + if @style == :list + html = format_list html + end - def permalink topic - topic.gsub(%r{[^/\w\s\-]},'').gsub(%r{[^\w/]|[\_]},' ').split.join('-').downcase - end - - def format_link_display text - text[1..-2].split("/").last - end - - def convert_external_links text - text.gsub(/\[([^\|]*\|[^\]]*)\]/) { |match| build_external_link match } - end - - def build_external_link match - text, url, title = match[1..-2].split("|", 3) - - url = format_link url.strip - text.strip! - title.strip! if title - - if title - %{<a href="#{url}" title="#{title}" target="_blank">#{text}</a>} - else - %{<a href="#{url}" target="_blank">#{text}</a>} + if @style == :dlist + html = format_dlist html + end end - end - - def format_link url - return url if /:\/\// =~ url - "http://#{url}" - end - - def format_list text - text.split(/\n/).reject { |item| item == "" }.map { |item| wrap(item.chomp, "li") }.join("\n") - end - - def format_dlist text - text.split(/\n/).reject { |item| item == "" }.map { |item| split_definition(item) }.join("\n") - end - - def split_definition item - term, definition = item.split(/\-/, 2).map{ |term| term.strip } - - return item unless definition - "<dt>#{term}</dt><dd>#{definition}</dd>" - end - - def parse_feed text - parser = Flannel::FeedParser.new Flannel.cache - parser.sub_feeds text + + wrap(html, @tags[@style], @id) end end end \ No newline at end of file