Class: Undress::GreenCloth

Inherits:
Textile show all
Defined in:
lib/undress/greencloth.rb

Instance Attribute Summary

Attributes inherited from Grammar

#post_processing_rules, #pre_processing_rules

Instance Method Summary collapse

Methods inherited from Grammar

#complete_word?, #content_of, default, inherited, #initialize, #method_missing, post_processing, post_processing_rules, pre_processing, pre_processing_rules, #process, process!, #process!, rule_for, #surrounded_by_whitespace?

Constructor Details

This class inherits a constructor from Undress::Grammar

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Undress::Grammar

Instance Method Details



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/undress/greencloth.rb', line 110

def process_as_wiki_link(e)
  inner, name, href = e.inner_html, e.get_attribute("name"), e.get_attribute("href")

  # pages or group pages
  context_name, page_name = href.split("/")[1..2]
  page_name = context_name if page_name.nil?
  wiki_page_name = page_name.gsub(/[a-z-]*[^\/]$/m) {|m| m.tr('-',' ')}

  # simple page
  if context_name == "page"
    return "[#{inner}]" if wiki_page_name == inner
    return "[#{inner} -> #{wiki_page_name}]"
  end
  # group page
  if context_name != page_name
    return "[#{context_name} / #{wiki_page_name}]" if wiki_page_name == inner
    return "[#{inner} -> #{wiki_page_name}]" if context_name == "page"
    return "[#{inner} -> #{context_name} / #{wiki_page_name}]"
  end 
  if inner == page_name || inner == wiki_page_name || inner == wiki_page_name.gsub(/\s/,"-")
    return "[#{wiki_page_name}]"
  end
  # fall back
  return "[#{inner} -> #{href}]"
end

#process_headings(h) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/undress/greencloth.rb', line 65

def process_headings(h) 
  h.children.each {|e| 
    next if e.class == Hpricot::Text
    e.parent.replace_child(e, "") if e.name != "a" || e.has_attribute?("href") && e["href"] !~ /^\/|(https?|s?ftp):\/\//
  }
  case h.name
    when "h1"
      "#{content_of(h)}\n#{'=' * h.inner_text.size}\n\n" if h.name == "h1"
    when "h2"
      "#{content_of(h)}\n#{'-' * h.inner_text.size}\n\n" if h.name == "h2"
    else
      "#{h.name}. #{content_of(h)}\n\n"
  end
end


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/undress/greencloth.rb', line 80

def process_links_and_anchors(e)
  return "" if e.empty?
  inner, name, href = e.inner_html, e.get_attribute("name"), e.get_attribute("href")

  # is an anchor? and cannot be child of any h1..h6
  if name && !e.parent.name.match(/^h1|2|3|4|5|6$/)
    inner == name || inner == name.gsub(/-/,"\s") ? "[# #{inner} #]" : "[# #{inner} -> #{name} #]"
  # is a link?
  elsif href && href != ""
    case href
      when /^\/#/
        "[\"#{inner}\":#{href}"
      when /^#/
        "[#{inner} -> #{href}]"
      when /^(https?|s?ftp):\/\//
        href.gsub(/^(https?|s?ftp):\/\//, "") == inner ? "[#{href}]" : "[#{inner} -> #{href}]"
      when /^[^\/]/
        "[#{e.inner_text}]"
      when /^\/.[^\/]*\/.[^\/]*\//
        "[#{inner} -> #{href}]"
      when /(?:\/page\/\+)[0-9]+$/
        "[#{inner} -> +#{href.gsub(/\+[0-9]+$/)}]"
      else
        process_as_wiki_link(e)
    end
  else
    ""
  end
end