123456789_123456789_123456789_123456789_123456789_

Class: RDoc::Markup::ToHtmlSnippet

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ToHtml, Formatter
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

LIST_TYPE_TO_HTML, URL_CHARACTERS_REGEXP_STR

Class Method Summary

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

ToHtml - Inherited

#code_object

The ::RDoc::CodeObject HTML is being generated for.

#from_path

Path to this document for relative links.

#in_tidylink_label?

Returns true if we are processing inside a tidy link label.

#in_list_entry, #list, #res

::RDoc::Text - Included

#language

The language for this text.

Instance Method Summary

ToHtml - Inherited

#accept_blank_line

Adds blank_line to the output.

#accept_block_quote

Adds block_quote to the output.

#accept_heading

Adds heading to the output.

#accept_list_end

Finishes consumption of list

#accept_list_item_end

Finishes consumption of list_item

#accept_list_item_start

Prepares the visitor for consuming list_item

#accept_list_start

Prepares the visitor for consuming list

#accept_paragraph

Adds paragraph to the output.

#accept_raw

Adds raw to the output.

#accept_rule

Adds rule to the output.

#accept_table

Adds table to the output.

#accept_verbatim

Adds verbatim to the output.

#apply_tidylink_label_special_handling

Special handling for tidy link labels.

#convert_string

CGI-escapes text

#deduplicate_heading_id

Returns a unique heading ID, appending -1, -2, etc.

#emit_inline,
#end_accepting

Returns the generated output.

#gen_url

Generates an HTML link or image tag for the given url and text.

#handle_BOLD, #handle_BOLD_WORD, #handle_EM, #handle_EM_WORD, #handle_HARD_BREAK, #handle_PLAIN_TEXT, #handle_REGEXP_HANDLING_TEXT,
#handle_regexp_HYPERLINK

target is a potential link.

#handle_regexp_RDOCLINK

target is an rdoc-schemed link that will be converted into a hyperlink.

#handle_regexp_SUPPRESSED_CROSSREF

Converts suppressed cross-reference text to HTML by removing the leading backslash.

#handle_STRIKE, #handle_TIDYLINK, #handle_TT,
#html_list_name

Determines the HTML list element for list_type and open_tag

#init_link_notation_regexp_handlings

Adds regexp handlings about link notations.

#init_regexp_handlings

Adds regexp handlings.

#list_end_for

Returns the HTML end-tag for list_type

#list_item_start

Returns the HTML tag for list_type, possible using a label from list_item

#parseable?

Returns true if text is valid ruby syntax.

#start_accepting

Prepares the visitor for HTML generation.

#to_html

Converts item to HTML using Text#to_html

#handle_inline, #handle_RDOCLINK

::RDoc::Text - Included

#flush_left

Flush text left based on the shortest line.

#markup

Convert a string in markup format into HTML.

#normalize_comment

Strips hashes, expands tabs then flushes text to the left.

#parse

Normalizes text then builds a Document from it.

#snippet

The first limit characters of text as HTML.

#strip_hashes

Strips leading # characters from text

#strip_newlines

Strips leading and trailing n characters from text

#strip_stars

Strips /* */ style comments.

#to_html,
#to_html_characters

Converts ampersand, dashes, ellipsis, quotes, copyright and registered trademark symbols in text to properly encoded characters.

#wrap

Wraps txt to line_len

Formatter - Inherited

#accept_document

Adds document to the output.

#add_regexp_handling_RDOCLINK

Adds a regexp handling for links of the form rdoc-…:

#annotate

Allows tag to be decorated with additional information.

#apply_regexp_handling

Applies regexp handling to text and returns an array of [text, converted?] pairs.

#convert

Marks up content

#convert_string

Converts a string to be fancier if desired.

#handle_BOLD

Called when processing bold nodes while traversing inline nodes from handle_inline.

#handle_BOLD_WORD

Called when processing bold word nodes while traversing inline nodes from handle_inline.

#handle_EM

Called when processing emphasis nodes while traversing inline nodes from handle_inline.

#handle_EM_WORD

Called when processing emphasis word nodes while traversing inline nodes from handle_inline.

#handle_HARD_BREAK

Called when processing a hard break while traversing inline nodes from handle_inline.

#handle_inline

Parses inline text, traverse the resulting nodes, and calls the appropriate handler methods.

#handle_PLAIN_TEXT

Called when processing plain text while traversing inline nodes from handle_inline.

#handle_REGEXP_HANDLING_TEXT

Called when processing regexp-handling-processed text while traversing inline nodes from handle_inline.

#handle_STRIKE

Called when processing strike nodes while traversing inline nodes from handle_inline.

#handle_TEXT

Called when processing text node while traversing inline nodes from handle_inline.

#handle_TIDYLINK

Called when processing tidylink nodes while traversing inline nodes from handle_inline.

#handle_TT

Called when processing tt nodes while traversing inline nodes from handle_inline.

#ignore

Use ignore in your subclass to ignore the content of a node.

#parse_url

Extracts and a scheme, url and an anchor id from url and returns them.

#traverse_inline_nodes

Traverses nodes and calls the appropriate handler methods Nodes formats are described in InlineParser#parse

#tt?

Is tag a tt tag?

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.

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 37

def initialize(options, characters = 100, paragraphs = 3, markup = nil)
  super options, 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.

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 10

attr_reader :character_limit

#characters (readonly)

This method is for internal use only.

The number of characters seen so far.

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 15

attr_reader :characters # :nodoc:

#inline_limit_reached?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 256

def inline_limit_reached?
  @inline_character_limit <= 0
end

#mask (readonly)

The attribute bitmask

[ GitHub ]

  
# 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.

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 25

attr_reader :paragraph_limit

#paragraphs (readonly)

Count of paragraphs found

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# 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

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 62

alias accept_raw ignore

#accept_rule(*node)

Rules are ignored

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 67

alias accept_rule ignore

#accept_verbatim(verbatim)

Adds verbatim to the output

[ GitHub ]

  
# 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, @character_limit - @characters)
  @characters += input.length
  text << ' ...' unless text == input

  super RDoc::Markup::Verbatim.new text

  add_paragraph
end

#add_paragraph

Throws :done when paragraph_limit paragraphs have been encountered

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 190

def add_paragraph
  @paragraphs += 1

  throw :done if @paragraphs >= @paragraph_limit
end

#convert(content)

Marks up content

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 199

def convert(content)
  catch :done do
    return super
  end

  end_accepting
end

#gen_url(url, text)

Returns just the text of link, url is only used to determine the link type.

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 163

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_BOLD(nodes)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 224

def handle_BOLD(nodes)
  super unless inline_limit_reached?
end

#handle_BOLD_WORD(word)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 228

def handle_BOLD_WORD(word)
  super unless inline_limit_reached?
end

#handle_EM(nodes)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 232

def handle_EM(nodes)
  super unless inline_limit_reached?
end

#handle_EM_WORD(word)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 236

def handle_EM_WORD(word)
  super unless inline_limit_reached?
end

#handle_HARD_BREAK

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 248

def handle_HARD_BREAK
  super unless inline_limit_reached?
end

#handle_inline(text)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 260

def handle_inline(text)
  limit = @character_limit - @characters
  return ['', 0] if limit <= 0
  @inline_character_limit = limit
  res = super
  res << ' ...' if @inline_character_limit <= 0
  @characters += limit - @inline_character_limit
  res
end

#handle_PLAIN_TEXT(text)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 207

def handle_PLAIN_TEXT(text) # :nodoc:
  return if inline_limit_reached?

  truncated = truncate(text, @inline_character_limit)
  @inline_character_limit -= text.size
  emit_inline(convert_string(truncated))
end

#handle_regexp_CROSSREF(text)

Removes escaping from the cross-references in target

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 131

def handle_regexp_CROSSREF(text)
  text.sub(/\A\\/, '')
end

#handle_REGEXP_HANDLING_TEXT(text)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 215

def handle_REGEXP_HANDLING_TEXT(text) # :nodoc:
  return if inline_limit_reached?

  # We can't truncate text including html tags.
  # Just emit as is, and count all characters including html tag part.
  emit_inline(text)
  @inline_character_limit -= text.size
end

#handle_STRIKE(nodes)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 244

def handle_STRIKE(nodes)
  super unless inline_limit_reached?
end

#handle_TT(code)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 240

def handle_TT(code)
  super unless inline_limit_reached?
end

#html_list_name(list_type, open_tag)

In snippets, there are no lists

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 183

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

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 138

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 << " &mdash; " 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

#start_accepting

Prepares the visitor for HTML snippet generation

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 122

def start_accepting
  super

  @characters = 0
end

#to_html(item)

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 270

def to_html(item)
  throw :done if @characters >= @character_limit
  to_html_characters(handle_inline(item))
end

#truncate(text, limit)

Truncates text at the end of the first word after the limit.

[ GitHub ]

  
# File 'lib/rdoc/markup/to_html_snippet.rb', line 278

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