Class: RDoc::Markup::ToHtmlSnippet
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
| Class Chain: | |
| Instance Chain: | |
| Inherits: | RDoc::Markup::ToHtml 
 | 
| Defined in: | lib/rdoc/markup/to_html_snippet.rb | 
Overview
Outputs RDoc markup as paragraphs with inline markup only.
Constant Summary
Class Method Summary
- 
    
      .new(options, characters = 100, paragraphs = 3, markup = nil)  ⇒ ToHtmlSnippet 
    
    constructor
    Creates a new ToHtmlSnippetformatter that will cut off the input on the next word boundary after the given number of #characters or #paragraphs of text have been encountered.
ToHtml - Inherited
| .new | Creates a new formatter that will output HTML. | 
Formatter - Inherited
| .gen_relative_url | Converts a target url to one that is relative to a given path. | 
| .new | Creates a new Formatter. | 
Instance Attribute Summary
- 
    
      #character_limit  
    
    readonly
    After this many characters the input will be cut off. 
- 
    
      #mask  
    
    readonly
    The attribute bitmask. 
- 
    
      #paragraph_limit  
    
    readonly
    After this many paragraphs the input will be cut off. 
- 
    
      #paragraphs  
    
    readonly
    Count of paragraphs found. 
ToHtml - Inherited
| #code_object | The ::RDoc::CodeObject HTML is being generated for. | 
| #from_path | Path to this document for relative links. | 
Formatter - Inherited
| #in_tt? | Are we currently inside tt tags? | 
Instance Method Summary
- 
    
      #accept_heading(heading)  
    
    Adds headingto the output as a paragraph.
- 
    
      #accept_list_item_end(list_item)  
    
    Finishes consumption of list_item
- 
    
      #accept_list_item_start(list_item)  
    
    Prepares the visitor for consuming list_item
- 
    
      #accept_list_start(list)  
    
    Prepares the visitor for consuming list
- #accept_paragraph(paragraph)
- 
    
      #accept_raw(*node)  
    
    Raw sections are untrusted and ignored. 
- 
    
      #accept_rule(*node)  
    
    Rules are ignored. 
- 
    
      #accept_verbatim(verbatim)  
    
    Adds verbatimto the output.
- 
    
      #add_paragraph  
    
    Throws :donewhen paragraph_limit paragraphs have been encountered.
- 
    
      #convert(content)  
    
    Marks up content
- 
    
      #convert_flow(flow)  
    
    Converts flow items flow
- 
    
      #gen_url(url, text)  
    
    Returns just the text of link,urlis only used to determine the link type.
- 
    
      #handle_special_CROSSREF(special)  
    
    Removes escaping from the cross-references in special
- 
    
      #handle_special_HARD_BREAK(special)  
    
    specialis a- 
    
      #html_list_name(list_type, open_tag)  
    
    In snippets, there are no lists. 
- 
    
      #list_item_start(list_item, list_type)  
    
    Lists are paragraphs, but notes and labels have a separator. 
- 
    
      #off_tags(res, item)  
    
    Maintains a bitmask to allow HTML elements to be closed properly. 
- 
    
      #on_tags(res, item)  
    
    Maintains a bitmask to allow HTML elements to be closed properly. 
- 
    
      #start_accepting  
    
    Prepares the visitor for HTML snippet generation. 
- 
    
      #truncate(text)  
    
    Truncates textat the end of the first word after the character_limit.
ToHtml - Inherited
| #accept_blank_line | Adds  | 
| #accept_block_quote | Adds  | 
| #accept_heading | Adds  | 
| #accept_list_end | Finishes consumption of  | 
| #accept_list_item_end | Finishes consumption of  | 
| #accept_list_item_start | Prepares the visitor for consuming  | 
| #accept_list_start | Prepares the visitor for consuming  | 
| #accept_paragraph | Adds  | 
| #accept_raw | Adds  | 
| #accept_rule | Adds  | 
| #accept_verbatim | Adds  | 
| #convert_string | CGI-escapes  | 
| #end_accepting | Returns the generated output. | 
| #gen_url | Generate a link to  | 
| #handle_special_HARD_BREAK | 
 | 
| #handle_special_HYPERLINK | 
 | 
| #handle_special_RDOCLINK | 
 | 
| #handle_special_TIDYLINK | This  | 
| #html_list_name | Determines the HTML list element for  | 
| #init_tags | Maps attributes to HTML tags. | 
| #list_end_for | Returns the HTML end-tag for  | 
| #list_item_start | Returns the HTML tag for  | 
| #parseable? | Returns true if text is valid ruby syntax. | 
| #start_accepting | Prepares the visitor for HTML generation. | 
| #to_html | Converts  | 
::RDoc::Text - Included
| #expand_tabs | Expands tab characters in  | 
| #flush_left | Flush  | 
| #markup | Convert a string in markup format into HTML. | 
| #normalize_comment | Strips hashes, expands tabs then flushes  | 
| #parse | Normalizes  | 
| #snippet | The first  | 
| #strip_hashes | Strips leading # characters from  | 
| #strip_newlines | Strips leading and trailing n characters from  | 
| #strip_stars | Strips /* */ style comments. | 
| #to_html | Converts ampersand, dashes, ellipsis, quotes, copyright and registered trademark symbols in  | 
| #wrap | Wraps  | 
Formatter - Inherited
| #accept_document | Adds  | |
| #add_special_RDOCLINK | Adds a special for links of the form rdoc-…: | |
| #add_special_TIDYLINK | Adds a special for links of the form  | Add a new set of tags for an attribute. | 
| #annotate | Allows  | |
| #convert | Marks up  | |
| #convert_flow | Converts flow items  | |
| #convert_special | Converts added specials. | |
| #convert_string | Converts a string to be fancier if desired. | |
| #ignore | Use ignore in your subclass to ignore the content of a node. | |
| #off_tags | Turns off tags for  | |
| #on_tags | Turns on tags for  | |
| #parse_url | Extracts and a scheme, url and an anchor id from  | |
| #tt? | Is  | 
Constructor Details
    .new(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.
# File 'lib/rdoc/markup/to_html_snippet.rb', line 36
def initialize , characters = 100, paragraphs = 3, markup = nil super , markup @character_limit = characters @paragraph_limit = paragraphs @characters = 0 @mask = 0 @paragraphs = 0 @markup.add_special RDoc::CrossReference::CROSSREF_REGEXP, :CROSSREF end
Instance Attribute Details
#character_limit (readonly)
After this many characters the input will be cut off.
# File 'lib/rdoc/markup/to_html_snippet.rb', line 9
attr_reader :character_limit
#mask (readonly)
The attribute bitmask
# File 'lib/rdoc/markup/to_html_snippet.rb', line 19
attr_reader :mask
#paragraph_limit (readonly)
After this many paragraphs the input will be cut off.
# File 'lib/rdoc/markup/to_html_snippet.rb', line 24
attr_reader :paragraph_limit
#paragraphs (readonly)
Count of paragraphs found
# File 'lib/rdoc/markup/to_html_snippet.rb', line 29
attr_reader :paragraphs
Instance Method Details
#accept_heading(heading)
Adds heading to the output as a paragraph
# File 'lib/rdoc/markup/to_html_snippet.rb', line 52
def accept_heading heading @res << "<p>#{to_html heading.text}\n" add_paragraph end
#accept_list_item_end(list_item)
Finishes consumption of list_item
# File 'lib/rdoc/markup/to_html_snippet.rb', line 81
def accept_list_item_end list_item end
#accept_list_item_start(list_item)
Prepares the visitor for consuming list_item
# File 'lib/rdoc/markup/to_html_snippet.rb', line 87
def accept_list_item_start list_item @res << list_item_start(list_item, @list.last) end
#accept_list_start(list)
Prepares the visitor for consuming list
# File 'lib/rdoc/markup/to_html_snippet.rb', line 94
def accept_list_start list @list << list.type @res << html_list_name(list.type, true) @in_list_entry.push '' end
#accept_paragraph(paragraph)
[ GitHub ]# File 'lib/rdoc/markup/to_html_snippet.rb', line 68
def accept_paragraph paragraph para = @in_list_entry.last || "<p>" text = paragraph.text @hard_break @res << "#{para}#{wrap to_html text}\n" add_paragraph end
#accept_raw(*node)
Raw sections are untrusted and ignored
# File 'lib/rdoc/markup/to_html_snippet.rb', line 61
alias accept_raw ignore
#accept_rule(*node)
Rules are ignored
# File 'lib/rdoc/markup/to_html_snippet.rb', line 66
alias accept_rule ignore
#accept_verbatim(verbatim)
Adds verbatim to the output
#add_paragraph
Throws :done when paragraph_limit paragraphs have been encountered
# File 'lib/rdoc/markup/to_html_snippet.rb', line 194
def add_paragraph @paragraphs += 1 throw :done if @paragraphs >= @paragraph_limit end
#convert(content)
Marks up content
# File 'lib/rdoc/markup/to_html_snippet.rb', line 203
def convert content catch :done do return super end end_accepting end
#convert_flow(flow)
Converts flow items flow
# File 'lib/rdoc/markup/to_html_snippet.rb', line 214
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::Special then text = convert_special 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)
Returns just the text of link, url is only used to determine the link type.
# File 'lib/rdoc/markup/to_html_snippet.rb', line 167
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_special_CROSSREF(special)
Removes escaping from the cross-references in special
# File 'lib/rdoc/markup/to_html_snippet.rb', line 127
def handle_special_CROSSREF special special.text.sub(/\A\\/, '') end
#handle_special_HARD_BREAK(special)
special is a 
# File 'lib/rdoc/markup/to_html_snippet.rb', line 134
def handle_special_HARD_BREAK special @characters -= 4 '<br>' end
#html_list_name(list_type, open_tag)
In snippets, there are no lists
# File 'lib/rdoc/markup/to_html_snippet.rb', line 187
def html_list_name list_type, open_tag '' end
#list_item_start(list_item, list_type)
Lists are paragraphs, but notes and labels have a separator
# File 'lib/rdoc/markup/to_html_snippet.rb', line 142
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)
Maintains a bitmask to allow HTML elements to be closed properly. See Formatter.
# File 'lib/rdoc/markup/to_html_snippet.rb', line 260
def res, item @mask ^= item.turn_off super end
#on_tags(res, item)
Maintains a bitmask to allow HTML elements to be closed properly. See Formatter.
# File 'lib/rdoc/markup/to_html_snippet.rb', line 250
def res, item @mask ^= item.turn_on super end
#start_accepting
Prepares the visitor for HTML snippet generation
# File 'lib/rdoc/markup/to_html_snippet.rb', line 118
def start_accepting super @characters = 0 end
#truncate(text)
Truncates text at the end of the first word after the character_limit.
# File 'lib/rdoc/markup/to_html_snippet.rb', line 269
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