Class: RDoc::Markup::ToHtmlSnippet
- Defined in:
- lib/rdoc/markup/to_html_snippet.rb
Overview
Outputs RDoc markup as paragraphs with inline markup only.
Constant Summary
Constants inherited from ToHtml
RDoc::Markup::ToHtml::LIST_TYPE_TO_HTML, RDoc::Markup::ToHtml::URL_CHARACTERS_REGEXP_STR
Constants included from Text
Text::MARKUP_FORMAT, Text::SPACE_SEPARATED_LETTER_CLASS, Text::TO_HTML_CHARACTERS
Instance Attribute Summary collapse
-
#character_limit ⇒ Object
readonly
After this many characters the input will be cut off.
-
#characters ⇒ Object
readonly
The number of characters seen so far.
-
#mask ⇒ Object
readonly
The attribute bitmask.
-
#paragraph_limit ⇒ Object
readonly
After this many paragraphs the input will be cut off.
-
#paragraphs ⇒ Object
readonly
Count of paragraphs found.
Attributes inherited from ToHtml
#code_object, #from_path, #in_list_entry, #list, #res
Attributes included from Text
Instance Method Summary collapse
-
#accept_heading(heading) ⇒ Object
Adds
heading
to the output as a paragraph. -
#accept_list_item_end(list_item) ⇒ Object
Finishes consumption of
list_item
. -
#accept_list_item_start(list_item) ⇒ Object
Prepares the visitor for consuming
list_item
. -
#accept_list_start(list) ⇒ Object
Prepares the visitor for consuming
list
. -
#accept_paragraph(paragraph) ⇒ Object
Adds
paragraph
to the output. -
#accept_verbatim(verbatim) ⇒ Object
Adds
verbatim
to the output. -
#add_paragraph ⇒ Object
Throws
:done
when paragraph_limit paragraphs have been encountered. -
#convert(content) ⇒ Object
Marks up
content
. -
#convert_flow(flow) ⇒ Object
Converts flow items
flow
. -
#gen_url(url, text) ⇒ Object
Returns just the text of
link
,url
is only used to determine the link type. -
#handle_regexp_CROSSREF(target) ⇒ Object
Removes escaping from the cross-references in
target
. -
#handle_regexp_HARD_BREAK(target) ⇒ Object
target
is a<br>
. -
#html_list_name(list_type, open_tag) ⇒ Object
In snippets, there are no lists.
-
#initialize(options, characters = 100, paragraphs = 3, markup = nil) ⇒ ToHtmlSnippet
constructor
Creates a new ToHtmlSnippet formatter that will cut off the input on the next word boundary after the given number of
characters
orparagraphs
of text have been encountered. -
#list_item_start(list_item, list_type) ⇒ Object
Lists are paragraphs, but notes and labels have a separator.
-
#off_tags(res, item) ⇒ Object
Maintains a bitmask to allow HTML elements to be closed properly.
-
#on_tags(res, item) ⇒ Object
Maintains a bitmask to allow HTML elements to be closed properly.
-
#start_accepting ⇒ Object
Prepares the visitor for HTML snippet generation.
-
#truncate(text) ⇒ Object
Truncates
text
at the end of the first word after the character_limit.
Methods inherited from ToHtml
#accept_blank_line, #accept_block_quote, #accept_list_end, #accept_table, #convert_string, #end_accepting, #handle_RDOCLINK, #handle_regexp_HYPERLINK, #handle_regexp_RDOCLINK, #handle_regexp_TIDYLINK, #init_link_notation_regexp_handlings, #init_regexp_handlings, #init_tags, #list_end_for, #parseable?, #to_html
Methods included from Text
encode_fallback, #expand_tabs, #flush_left, #markup, #normalize_comment, #parse, #snippet, #strip_hashes, #strip_newlines, #strip_stars, #to_html, #wrap
Methods inherited from Formatter
#accept_document, #add_regexp_handling_RDOCLINK, #add_regexp_handling_TIDYLINK, #add_tag, #annotate, #convert_regexp_handling, #convert_string, gen_relative_url, #ignore, #in_tt?, #parse_url, #tt?
Constructor Details
#initialize(options, characters = 100, paragraphs = 3, markup = nil) ⇒ ToHtmlSnippet
Creates a new ToHtmlSnippet formatter that will cut off the input on the next word boundary after the given number of characters
or paragraphs
of text have been encountered.
37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 37 def initialize , characters = 100, paragraphs = 3, markup = nil super , markup @character_limit = characters @paragraph_limit = paragraphs @characters = 0 @mask = 0 @paragraphs = 0 @markup.add_regexp_handling RDoc::CrossReference::CROSSREF_REGEXP, :CROSSREF end |
Instance Attribute Details
#character_limit ⇒ Object (readonly)
After this many characters the input will be cut off.
10 11 12 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 10 def character_limit @character_limit end |
#characters ⇒ Object (readonly)
The number of characters seen so far.
15 16 17 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 15 def characters @characters end |
#mask ⇒ Object (readonly)
The attribute bitmask
20 21 22 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 20 def mask @mask end |
#paragraph_limit ⇒ Object (readonly)
After this many paragraphs the input will be cut off.
25 26 27 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 25 def paragraph_limit @paragraph_limit end |
#paragraphs ⇒ Object (readonly)
Count of paragraphs found
30 31 32 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 30 def paragraphs @paragraphs end |
Instance Method Details
#accept_heading(heading) ⇒ Object
Adds heading
to the output as a paragraph
53 54 55 56 57 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 53 def accept_heading heading @res << "<p>#{to_html heading.text}\n" add_paragraph end |
#accept_list_item_end(list_item) ⇒ Object
Finishes consumption of list_item
85 86 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 85 def accept_list_item_end list_item end |
#accept_list_item_start(list_item) ⇒ Object
Prepares the visitor for consuming list_item
91 92 93 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 91 def accept_list_item_start list_item @res << list_item_start(list_item, @list.last) end |
#accept_list_start(list) ⇒ Object
Prepares the visitor for consuming list
98 99 100 101 102 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 98 def accept_list_start list @list << list.type @res << html_list_name(list.type, true) @in_list_entry.push '' end |
#accept_paragraph(paragraph) ⇒ Object
Adds paragraph
to the output
72 73 74 75 76 77 78 79 80 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 72 def accept_paragraph paragraph para = @in_list_entry.last || "<p>" text = paragraph.text @hard_break @res << "#{para}#{to_html text}\n" add_paragraph end |
#accept_verbatim(verbatim) ⇒ Object
Adds verbatim
to the output
107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 107 def accept_verbatim verbatim throw :done if @characters >= @character_limit input = verbatim.text.rstrip text = truncate input text << ' ...' unless text == input super RDoc::Markup::Verbatim.new text add_paragraph end |
#add_paragraph ⇒ Object
Throws :done
when paragraph_limit paragraphs have been encountered
198 199 200 201 202 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 198 def add_paragraph @paragraphs += 1 throw :done if @paragraphs >= @paragraph_limit end |
#convert(content) ⇒ Object
Marks up content
207 208 209 210 211 212 213 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 207 def convert content catch :done do return super end end_accepting end |
#convert_flow(flow) ⇒ Object
Converts flow items flow
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 218 def convert_flow flow throw :done if @characters >= @character_limit res = [] @mask = 0 flow.each do |item| case item when RDoc::Markup::AttrChanger then res, item res, item when String then text = convert_string item res << truncate(text) when RDoc::Markup::RegexpHandling then text = convert_regexp_handling item res << truncate(text) else raise "Unknown flow element: #{item.inspect}" end if @characters >= @character_limit then res, RDoc::Markup::AttrChanger.new(0, @mask) break end end res << ' ...' if @characters >= @character_limit res.join end |
#gen_url(url, text) ⇒ Object
Returns just the text of link
, url
is only used to determine the link type.
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 171 def gen_url url, text if url =~ /^rdoc-label:([^:]*)(?::(.*))?/ then type = "link" elsif url =~ /([A-Za-z]+):(.*)/ then type = $1 else type = "http" end if (type == "http" or type == "https" or type == "link") and url =~ /\.(gif|png|jpg|jpeg|bmp)$/ then '' else text.sub(%r%^#{type}:/*%, '') end end |
#handle_regexp_CROSSREF(target) ⇒ Object
Removes escaping from the cross-references in target
131 132 133 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 131 def handle_regexp_CROSSREF target target.text.sub(/\A\\/, '') end |
#handle_regexp_HARD_BREAK(target) ⇒ Object
target
is a <br>
138 139 140 141 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 138 def handle_regexp_HARD_BREAK target @characters -= 4 '<br>' end |
#html_list_name(list_type, open_tag) ⇒ Object
In snippets, there are no lists
191 192 193 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 191 def html_list_name list_type, open_tag '' end |
#list_item_start(list_item, list_type) ⇒ Object
Lists are paragraphs, but notes and labels have a separator
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 146 def list_item_start list_item, list_type throw :done if @characters >= @character_limit case list_type when :BULLET, :LALPHA, :NUMBER, :UALPHA then "<p>" when :LABEL, :NOTE then labels = Array(list_item.label).map do |label| to_html label end.join ', ' labels << " — " unless labels.empty? start = "<p>#{labels}" @characters += 1 # try to include the label start else raise RDoc::Error, "Invalid list type: #{list_type.inspect}" end end |
#off_tags(res, item) ⇒ Object
Maintains a bitmask to allow HTML elements to be closed properly. See RDoc::Markup::Formatter.
264 265 266 267 268 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 264 def res, item @mask ^= item.turn_off super end |
#on_tags(res, item) ⇒ Object
Maintains a bitmask to allow HTML elements to be closed properly. See RDoc::Markup::Formatter.
254 255 256 257 258 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 254 def res, item @mask ^= item.turn_on super end |
#start_accepting ⇒ Object
Prepares the visitor for HTML snippet generation
122 123 124 125 126 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 122 def start_accepting super @characters = 0 end |
#truncate(text) ⇒ Object
Truncates text
at the end of the first word after the character_limit.
273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/rdoc/markup/to_html_snippet.rb', line 273 def truncate text length = text.length characters = @characters @characters += length return text if @characters < @character_limit remaining = @character_limit - characters text =~ /\A(.{#{remaining},}?)(\s|$)/m # TODO word-break instead of \s? $1 end |