class RDoc::Markup::ToHtmlSnippet
Outputs RDoc markup as paragraphs with inline markup only.
Attributes
After this many characters the input will be cut off.
The attribute bitmask
After this many paragraphs the input will be cut off.
Count of paragraphs found
Public Class Methods
# File lib/rdoc/markup/to_html_snippet.rb, line 39 def initialize(characters = 100, paragraphs = 3) super() @character_limit = characters @paragraph_limit = paragraphs @characters = 0 @mask = 0 @paragraphs = 0 @markup.add_regexp_handling CrossReference::CROSSREF_REGEXP, :CROSSREF end
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.
RDoc::Markup::ToHtml::new
Public Instance Methods
Source
# File lib/rdoc/markup/to_html_snippet.rb, line 55 def accept_heading(heading) @res << "<p>#{to_html heading.text}\n" add_paragraph end
Adds heading to the output as a paragraph
# File lib/rdoc/markup/to_html_snippet.rb, line 87 def accept_list_item_end(list_item) end
Finishes consumption of list_item
# File lib/rdoc/markup/to_html_snippet.rb, line 93 def accept_list_item_start(list_item) @res << list_item_start(list_item, @list.last) end
Prepares the visitor for consuming list_item
Source
# File lib/rdoc/markup/to_html_snippet.rb, line 100 def accept_list_start(list) @list << list.type @res << html_list_name(list.type, true) @in_list_entry.push '' end
Prepares the visitor for consuming list
Source
# File lib/rdoc/markup/to_html_snippet.rb, line 74 def accept_paragraph(paragraph) para = @in_list_entry.last || "<p>" text = paragraph.text @hard_break @res << "#{para}#{to_html text}\n" add_paragraph end
Adds paragraph to the output
Source
# File lib/rdoc/markup/to_html_snippet.rb, line 109 def accept_verbatim(verbatim) throw :done if @characters >= @character_limit input = verbatim.text.rstrip text = truncate(input, @character_limit - @characters) @characters += input.length text << " #{TO_HTML_CHARACTERS[text.encoding][:ellipsis]}" unless text == input super Markup::Verbatim.new text add_paragraph end
Adds verbatim to the output
Source
# File lib/rdoc/markup/to_html_snippet.rb, line 192 def add_paragraph @paragraphs += 1 throw :done if @paragraphs >= @paragraph_limit end
Throws :done when paragraph_limit paragraphs have been encountered
Source
# File lib/rdoc/markup/to_html_snippet.rb, line 201 def convert(content) catch :done do return super end end_accepting end
Marks up content
RDoc::Markup::Formatter#convert
Source
# File lib/rdoc/markup/to_html_snippet.rb, line 165 def gen_url(url, text) if url =~ /^rdoc-label:([^:]*)(?::(.*))?/ type = "link" elsif url =~ /([A-Za-z]+):(.*)/ type = $1 else type = "http" end if (type == "http" or type == "https" or type == "link") and url =~ /\.(gif|png|jpg|jpeg|bmp)$/ '' else text.sub(%r%^#{type}:/*%, '') end end
Returns just the text of link, url is only used to determine the link type.
Source
# File lib/rdoc/markup/to_html_snippet.rb, line 226 def handle_BOLD(nodes) super unless inline_limit_reached? end
RDoc::Markup::Formatter#handle_BOLD
Source
# File lib/rdoc/markup/to_html_snippet.rb, line 230 def handle_BOLD_WORD(word) super unless inline_limit_reached? end
RDoc::Markup::Formatter#handle_BOLD_WORD
Source
# File lib/rdoc/markup/to_html_snippet.rb, line 234 def handle_EM(nodes) super unless inline_limit_reached? end
RDoc::Markup::Formatter#handle_EM
Source
# File lib/rdoc/markup/to_html_snippet.rb, line 238 def handle_EM_WORD(word) super unless inline_limit_reached? end
RDoc::Markup::Formatter#handle_EM_WORD
Source
# File lib/rdoc/markup/to_html_snippet.rb, line 250 def handle_HARD_BREAK super unless inline_limit_reached? end
RDoc::Markup::Formatter#handle_HARD_BREAK
Source
# File lib/rdoc/markup/to_html_snippet.rb, line 246 def handle_STRIKE(nodes) super unless inline_limit_reached? end
RDoc::Markup::Formatter#handle_STRIKE
# File lib/rdoc/markup/to_html_snippet.rb, line 254 def handle_TIDYLINK(label_part, url) traverse_inline_nodes(label_part) unless inline_limit_reached? end
Source
# File lib/rdoc/markup/to_html_snippet.rb, line 242 def handle_TT(code) super unless inline_limit_reached? end
RDoc::Markup::Formatter#handle_TT
Source
# File lib/rdoc/markup/to_html_snippet.rb, line 262 def handle_inline(text) limit = @character_limit - @characters return ['', 0] if limit <= 0 @inline_character_limit = limit res = super res << " #{TO_HTML_CHARACTERS[text.encoding][:ellipsis]}" if @inline_character_limit <= 0 @characters += limit - @inline_character_limit res end
RDoc::Markup::Formatter#handle_inline
# File lib/rdoc/markup/to_html_snippet.rb, line 133 def handle_regexp_CROSSREF(text) text.sub(/\A\\/, '') end
Removes escaping from the cross-references in target
# File lib/rdoc/markup/to_html_snippet.rb, line 185 def html_list_name(list_type, open_tag) '' end
In snippets, there are no lists
Source
# File lib/rdoc/markup/to_html_snippet.rb, line 258 def inline_limit_reached? @inline_character_limit <= 0 end
# File lib/rdoc/markup/to_html_snippet.rb, line 140 def list_item_start(list_item, list_type) throw :done if @characters >= @character_limit case list_type when :BULLET, :LALPHA, :NUMBER, :UALPHA "<p>" when :LABEL, :NOTE 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 Error, "Invalid list type: #{list_type.inspect}" end end
Lists are paragraphs, but notes and labels have a separator
Source
# File lib/rdoc/markup/to_html_snippet.rb, line 124 def start_accepting super @characters = 0 end
Prepares the visitor for HTML snippet generation
Source
# File lib/rdoc/markup/to_html_snippet.rb, line 272 def to_html(item) throw :done if @characters >= @character_limit handle_inline(item) end
Source
# File lib/rdoc/markup/to_html_snippet.rb, line 280 def truncate(text, limit) return text if limit >= text.size return '' if limit <= 0 text =~ /\A(.{#{limit},}?)(\s|$)/m # TODO word-break instead of \s? $1 end
Truncates text at the end of the first word after the limit.