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
::RDoc::Text
- Included
MARKUP_FORMAT, SPACE_SEPARATED_LETTER_CLASS, TO_HTML_CHARACTERS
ToHtml
- Inherited
Class Method Summary
-
.new(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 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 |
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.
-
#characters
readonly
Internal use only
The number of characters seen so far.
ToHtml
- Inherited
#code_object | The |
#from_path | Path to this document for relative links. |
#in_list_entry, #list, #res |
::RDoc::Text
- Included
#language | The language for this text. |
Formatter
- Inherited
#in_tt? | Are we currently inside tt tags? |
Instance Method Summary
-
#accept_heading(heading)
Adds
heading
to 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)
Adds
paragraph
to the output. -
#accept_raw(*node)
Raw
sections are untrusted and ignored. -
#accept_rule(*node)
Rules are ignored.
-
#accept_verbatim(verbatim)
Adds
verbatim
to the output. -
#add_paragraph
Throws
:done
when 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
,url
is only used to determine the link type. -
#handle_regexp_CROSSREF(target)
Removes escaping from the cross-references in
target
-
#handle_regexp_HARD_BREAK(target)
target
is a<br>
. -
#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
text
at 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_table | Adds |
#accept_verbatim | Adds |
#convert_string | CGI-escapes |
#end_accepting | Returns the generated output. |
#gen_url | Generate a link to |
#handle_regexp_HARD_BREAK |
|
#handle_regexp_HYPERLINK |
|
#handle_regexp_RDOCLINK |
|
#handle_regexp_TIDYLINK | This |
#html_list_name | Determines the HTML list element for |
#init_link_notation_regexp_handlings | Adds regexp handlings about link notations. |
#init_regexp_handlings | Adds regexp handlings. |
#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 |
#handle_RDOCLINK |
::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_regexp_handling_RDOCLINK | Adds a regexp handling for links of the form rdoc-…: |
#add_regexp_handling_TIDYLINK | Adds a regexp handling for links of the form |
#add_tag | Add a new set of tags for an attribute. |
#annotate | Allows |
#convert | Marks up |
#convert_flow | Converts flow items |
#convert_regexp_handling | Converts added regexp handlings. |
#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 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 (readonly)
After this many characters the input will be cut off.
# File 'lib/rdoc/markup/to_html_snippet.rb', line 10
attr_reader :character_limit
#characters (readonly)
The number of characters seen so far.
# File 'lib/rdoc/markup/to_html_snippet.rb', line 15
attr_reader :characters # :nodoc:
#mask (readonly)
The attribute bitmask
# File 'lib/rdoc/markup/to_html_snippet.rb', line 20
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 25
attr_reader :paragraph_limit
#paragraphs (readonly)
Count of paragraphs found
# File 'lib/rdoc/markup/to_html_snippet.rb', line 30
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 53
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 85
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 91
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 98
def accept_list_start list @list << list.type @res << html_list_name(list.type, true) @in_list_entry.push '' end
#accept_paragraph(paragraph)
Adds paragraph
to the output
# 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_raw(*node)
Raw
sections are untrusted and ignored
# File 'lib/rdoc/markup/to_html_snippet.rb', line 62
alias accept_raw ignore
#accept_rule(*node)
Rules are ignored
# File 'lib/rdoc/markup/to_html_snippet.rb', line 67
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 198
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 207
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 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)
Returns just the text of link
, url
is only used to determine the link type.
# 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)
Removes escaping from the cross-references in target
# 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)
target
is a <br>
# 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)
In snippets, there are no lists
# 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)
Lists are paragraphs, but notes and labels have a separator
# 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)
Maintains a bitmask to allow HTML elements to be closed properly. See Formatter
.
# File 'lib/rdoc/markup/to_html_snippet.rb', line 264
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 254
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 122
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 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