Module: Flannel::HtmlTransformable

Includes:
Wrappable
Included in:
HtmlFormatter
Defined in:
lib/flannel/html_transformable.rb

Overview

Methods for transforming test into html

Instance Method Summary collapse

Methods included from Wrappable

#empty_string?, #wrap

Instance Method Details



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/flannel/html_transformable.rb', line 47

def build_external_link match
  text, url, title = match[1..-2].split("|", 3).map { |part| part.strip }

  url = format_link url

  if title
    %{<a href="#{url}" title="#{title}" target="_blank">#{text}</a>}
  else
    %{<a href="#{url}" target="_blank">#{text}</a>}
  end
end


19
20
21
# File 'lib/flannel/html_transformable.rb', line 19

def build_wiki_links text
  text.gsub(/-\w(.*?)\w>/) { |match| %{<a href="#{wiki_link match}">#{format_link_display(match)}</a>}}
end


43
44
45
# File 'lib/flannel/html_transformable.rb', line 43

def convert_external_links text
  text.gsub(/\[([^\|]*\|[^\]]*)\]/) { |match| build_external_link match }
end

#create_img(text) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/flannel/html_transformable.rb', line 23

def create_img text
  desc, url = text.split(/\n/, 2)
  
  return text unless url
  
  "<img src='#{url}' alt='#{desc}' title='#{desc}' />"
end

#format_dlist(text) ⇒ Object



68
69
70
# File 'lib/flannel/html_transformable.rb', line 68

def format_dlist text
  text.split(/\n/).reject { |item| item == ""  }.map { |item| split_definition(item) }.join("\n")      
end


59
60
61
62
# File 'lib/flannel/html_transformable.rb', line 59

def format_link url
  return url if /:\/\// =~ url
  "http://#{url}"
end


39
40
41
# File 'lib/flannel/html_transformable.rb', line 39

def format_link_display text
  text[1..-2].split("/").last
end

#format_list(text) ⇒ Object



64
65
66
# File 'lib/flannel/html_transformable.rb', line 64

def format_list text
  text.split(/\n/).reject { |item| item == ""  }.map { |item| wrap(item.chomp, "li") }.join("\n")      
end

#html_escape(text) ⇒ Object



9
10
11
12
13
# File 'lib/flannel/html_transformable.rb', line 9

def html_escape text
  require 'cgi'

  CGI::escapeHTML(text)
end

#parse_feed(text) ⇒ Object



79
80
81
82
# File 'lib/flannel/html_transformable.rb', line 79

def parse_feed text
  parser = Flannel::FeedParser.new  Flannel.cache
  parser.sub_feeds text
end


15
16
17
# File 'lib/flannel/html_transformable.rb', line 15

def permalink topic
  topic.gsub(%r{[^/\w\s\-]},'').gsub(%r{[^\w/]|[\_]},' ').split.join('-').downcase
end

#split_definition(item) ⇒ Object



72
73
74
75
76
77
# File 'lib/flannel/html_transformable.rb', line 72

def split_definition item
  term, definition = item.split(/\-/, 2).map{ |term| term.strip }
  
  return item unless definition
  "<dt>#{term}</dt><dd>#{definition}</dd>"
end


31
32
33
# File 'lib/flannel/html_transformable.rb', line 31

def wiki_link topic
  permalink topic[1..-2]
end