Class: RDoc::Markup::Formatter
- Inherits:
-
Object
- Object
- RDoc::Markup::Formatter
- Defined in:
- lib/rdoc/markup/formatter.rb
Direct Known Subclasses
ToHtml, ToJoinedParagraph, ToLabel, ToRdoc, ToTableOfContents, ToTest, ToTtOnly
Class Method Summary collapse
-
.gen_relative_url(path, target) ⇒ Object
Converts a target url to one that is relative to a given path.
Instance Method Summary collapse
-
#accept_document(document) ⇒ Object
Adds
documentto the output. -
#add_regexp_handling_RDOCLINK ⇒ Object
Adds a regexp handling for links of the form rdoc-…:.
-
#annotate(tag) ⇒ Object
Allows
tagto be decorated with additional information. -
#apply_regexp_handling(text) ⇒ Object
Applies regexp handling to
textand returns an array of [text, converted?] pairs. -
#convert(content) ⇒ Object
Marks up
content. -
#convert_string(string) ⇒ Object
Converts a string to be fancier if desired.
-
#handle_BOLD(nodes) ⇒ Object
Called when processing bold nodes while traversing inline nodes from handle_inline.
-
#handle_BOLD_WORD(word) ⇒ Object
Called when processing bold word nodes while traversing inline nodes from handle_inline.
-
#handle_EM(nodes) ⇒ Object
Called when processing emphasis nodes while traversing inline nodes from handle_inline.
-
#handle_EM_WORD(word) ⇒ Object
Called when processing emphasis word nodes while traversing inline nodes from handle_inline.
-
#handle_HARD_BREAK ⇒ Object
Called when processing a hard break while traversing inline nodes from handle_inline.
-
#handle_inline(text) ⇒ Object
Parses inline
text, traverse the resulting nodes, and calls the appropriate handler methods. -
#handle_PLAIN_TEXT(text) ⇒ Object
Called when processing plain text while traversing inline nodes from handle_inline.
-
#handle_REGEXP_HANDLING_TEXT(text) ⇒ Object
Called when processing regexp-handling-processed text while traversing inline nodes from handle_inline.
-
#handle_STRIKE(nodes) ⇒ Object
Called when processing strike nodes while traversing inline nodes from handle_inline.
-
#handle_TEXT(text) ⇒ Object
Called when processing text node while traversing inline nodes from handle_inline.
-
#handle_TIDYLINK(label_part, url) ⇒ Object
Called when processing tidylink nodes while traversing inline nodes from handle_inline.
-
#handle_TT(code) ⇒ Object
Called when processing tt nodes while traversing inline nodes from handle_inline.
-
#ignore(*node) ⇒ Object
Use ignore in your subclass to ignore the content of a node.
-
#initialize ⇒ Formatter
constructor
Creates a new Formatter.
-
#parse_url(url) ⇒ Object
Extracts and a scheme, url and an anchor id from
urland returns them. -
#traverse_inline_nodes(nodes) ⇒ Object
Traverses
nodesand calls the appropriate handler methods Nodes formats are described in RDoc::Markup::InlineParser#parse. -
#tt?(tag) ⇒ Boolean
Is
taga tt tag?.
Constructor Details
Class Method Details
.gen_relative_url(path, target) ⇒ Object
Converts a target url to one that is relative to a given path
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rdoc/markup/formatter.rb', line 20 def self.gen_relative_url(path, target) from = File.dirname path to, to_file = File.split target from = from.split "/" to = to.split "/" from.delete '.' to.delete '.' while from.size > 0 and to.size > 0 and from[0] == to[0] do from.shift to.shift end from.fill ".." from.concat to from << to_file File.join(*from) end |
Instance Method Details
#accept_document(document) ⇒ Object
Adds document to the output
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/rdoc/markup/formatter.rb', line 53 def accept_document(document) document.parts.each do |item| case item when RDoc::Markup::Document then # HACK accept_document item else item.accept self end end end |
#add_regexp_handling_RDOCLINK ⇒ Object
Adds a regexp handling for links of the form rdoc-…:
67 68 69 |
# File 'lib/rdoc/markup/formatter.rb', line 67 def add_regexp_handling_RDOCLINK @markup.add_regexp_handling(/rdoc-[a-z]+:[^\s\]]+/, :RDOCLINK) end |
#annotate(tag) ⇒ Object
Allows tag to be decorated with additional information.
74 75 76 |
# File 'lib/rdoc/markup/formatter.rb', line 74 def annotate(tag) tag end |
#apply_regexp_handling(text) ⇒ Object
Applies regexp handling to text and returns an array of [text, converted?] pairs.
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/rdoc/markup/formatter.rb', line 87 def apply_regexp_handling(text) matched = [] @markup.regexp_handlings.each_with_index do |(pattern, name), priority| text.scan(pattern) do m = Regexp.last_match idx = m[1] ? 1 : 0 matched << [m.begin(idx), m.end(idx), m[idx], name, priority] end end # If the start positions are the same, prefer the earlier-registered one # (lower numeric priority from each_with_index). matched.sort_by! {|beg_pos, _, _, _, priority| [beg_pos, priority] } pos = 0 output = [] matched.each do |beg_pos, end_pos, s, name| next if beg_pos < pos output << [text[pos...beg_pos], false] if beg_pos != pos handled = public_send(:"handle_regexp_#{name}", s) output << [handled, true] pos = end_pos end output << [text[pos..], false] if pos < text.size output end |
#convert(content) ⇒ Object
Marks up content
81 82 83 |
# File 'lib/rdoc/markup/formatter.rb', line 81 def convert(content) @markup.convert content, self end |
#convert_string(string) ⇒ Object
Converts a string to be fancier if desired
233 234 235 |
# File 'lib/rdoc/markup/formatter.rb', line 233 def convert_string(string) string end |
#handle_BOLD(nodes) ⇒ Object
Called when processing bold nodes while traversing inline nodes from handle_inline. Traverse the children nodes and dispatch to the appropriate handlers.
148 149 150 |
# File 'lib/rdoc/markup/formatter.rb', line 148 def handle_BOLD(nodes) traverse_inline_nodes(nodes) end |
#handle_BOLD_WORD(word) ⇒ Object
Called when processing bold word nodes while traversing inline nodes from handle_inline. word may need proper escaping.
162 163 164 |
# File 'lib/rdoc/markup/formatter.rb', line 162 def handle_BOLD_WORD(word) handle_PLAIN_TEXT(word) end |
#handle_EM(nodes) ⇒ Object
Called when processing emphasis nodes while traversing inline nodes from handle_inline. Traverse the children nodes and dispatch to the appropriate handlers.
155 156 157 |
# File 'lib/rdoc/markup/formatter.rb', line 155 def handle_EM(nodes) traverse_inline_nodes(nodes) end |
#handle_EM_WORD(word) ⇒ Object
Called when processing emphasis word nodes while traversing inline nodes from handle_inline. word may need proper escaping.
169 170 171 |
# File 'lib/rdoc/markup/formatter.rb', line 169 def handle_EM_WORD(word) handle_PLAIN_TEXT(word) end |
#handle_HARD_BREAK ⇒ Object
Called when processing a hard break while traversing inline nodes from handle_inline.
142 143 |
# File 'lib/rdoc/markup/formatter.rb', line 142 def handle_HARD_BREAK end |
#handle_inline(text) ⇒ Object
Parses inline text, traverse the resulting nodes, and calls the appropriate handler methods.
198 199 200 201 |
# File 'lib/rdoc/markup/formatter.rb', line 198 def handle_inline(text) nodes = RDoc::Markup::InlineParser.new(text).parse traverse_inline_nodes(nodes) end |
#handle_PLAIN_TEXT(text) ⇒ Object
Called when processing plain text while traversing inline nodes from handle_inline. text may need proper escaping.
118 119 |
# File 'lib/rdoc/markup/formatter.rb', line 118 def handle_PLAIN_TEXT(text) end |
#handle_REGEXP_HANDLING_TEXT(text) ⇒ Object
Called when processing regexp-handling-processed text while traversing inline nodes from handle_inline. text may contain markup tags.
124 125 |
# File 'lib/rdoc/markup/formatter.rb', line 124 def handle_REGEXP_HANDLING_TEXT(text) end |
#handle_STRIKE(nodes) ⇒ Object
Called when processing strike nodes while traversing inline nodes from handle_inline. Traverse the children nodes and dispatch to the appropriate handlers.
183 184 185 |
# File 'lib/rdoc/markup/formatter.rb', line 183 def handle_STRIKE(nodes) traverse_inline_nodes(nodes) end |
#handle_TEXT(text) ⇒ Object
Called when processing text node while traversing inline nodes from handle_inline. Apply regexp handling and dispatch to the appropriate handler: handle_REGEXP_HANDLING_TEXT or handle_PLAIN_TEXT.
130 131 132 133 134 135 136 137 138 |
# File 'lib/rdoc/markup/formatter.rb', line 130 def handle_TEXT(text) apply_regexp_handling(text).each do |part, converted| if converted handle_REGEXP_HANDLING_TEXT(part) else handle_PLAIN_TEXT(part) end end end |
#handle_TIDYLINK(label_part, url) ⇒ Object
Called when processing tidylink nodes while traversing inline nodes from handle_inline. label_part is an array of strings or nodes representing the link label. url is the link URL. Traverse the label_part nodes and dispatch to the appropriate handlers.
192 193 194 |
# File 'lib/rdoc/markup/formatter.rb', line 192 def handle_TIDYLINK(label_part, url) traverse_inline_nodes(label_part) end |
#handle_TT(code) ⇒ Object
Called when processing tt nodes while traversing inline nodes from handle_inline. code may need proper escaping.
176 177 178 |
# File 'lib/rdoc/markup/formatter.rb', line 176 def handle_TT(code) handle_PLAIN_TEXT(code) end |
#ignore(*node) ⇒ Object
Use ignore in your subclass to ignore the content of a node.
##
# We don't support raw nodes in ToNoRaw
alias accept_raw ignore
245 246 |
# File 'lib/rdoc/markup/formatter.rb', line 245 def ignore(*node) end |
#parse_url(url) ⇒ Object
Extracts and a scheme, url and an anchor id from url and returns them.
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'lib/rdoc/markup/formatter.rb', line 251 def parse_url(url) case url when /^rdoc-label:([^:]*)(?::(.*))?/ then scheme = 'link' path = "##{$1}" id = " id=\"#{$2}\"" if $2 when /([A-Za-z]+):(.*)/ then scheme = $1.downcase path = $2 when /^#/ then else scheme = 'http' path = url url = url end if scheme == 'link' then url = if path[0, 1] == '#' then # is this meaningful? path else self.class.gen_relative_url @from_path, path end end [scheme, url, id] end |
#traverse_inline_nodes(nodes) ⇒ Object
Traverses nodes and calls the appropriate handler methods Nodes formats are described in RDoc::Markup::InlineParser#parse
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/rdoc/markup/formatter.rb', line 206 def traverse_inline_nodes(nodes) nodes.each do |node| next handle_TEXT(node) if String === node case node[:type] when :TIDYLINK handle_TIDYLINK(node[:children], node[:url]) when :HARD_BREAK handle_HARD_BREAK when :BOLD handle_BOLD(node[:children]) when :BOLD_WORD handle_BOLD_WORD(node[:children][0] || '') when :EM handle_EM(node[:children]) when :EM_WORD handle_EM_WORD(node[:children][0] || '') when :TT handle_TT(node[:children][0] || '') when :STRIKE handle_STRIKE(node[:children]) end end end |
#tt?(tag) ⇒ Boolean
Is tag a tt tag?
281 282 283 |
# File 'lib/rdoc/markup/formatter.rb', line 281 def tt?(tag) tag.bit == @tt_bit end |