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

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.

Calls superclass method RDoc::Markup::ToHtml::new
# 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

Public Instance Methods

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

Finishes consumption of list_item

# File lib/rdoc/markup/to_html_snippet.rb, line 85
def accept_list_item_end(list_item)
end

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

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

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

Adds verbatim to the output

Calls superclass method
# 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

Throws :done when paragraph_limit paragraphs have been encountered

# File lib/rdoc/markup/to_html_snippet.rb, line 190
def add_paragraph
  @paragraphs += 1

  throw :done if @paragraphs >= @paragraph_limit
end

Marks up content

Calls superclass method RDoc::Markup::Formatter#convert
# File lib/rdoc/markup/to_html_snippet.rb, line 199
def convert(content)
  catch :done do
    return super
  end

  end_accepting
end

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

# 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
Calls superclass method RDoc::Markup::Formatter#handle_BOLD
# File lib/rdoc/markup/to_html_snippet.rb, line 224
def handle_BOLD(nodes)
  super unless inline_limit_reached?
end
# File lib/rdoc/markup/to_html_snippet.rb, line 228
def handle_BOLD_WORD(word)
  super unless inline_limit_reached?
end
Calls superclass method RDoc::Markup::Formatter#handle_EM
# File lib/rdoc/markup/to_html_snippet.rb, line 232
def handle_EM(nodes)
  super unless inline_limit_reached?
end
Calls superclass method RDoc::Markup::Formatter#handle_EM_WORD
# File lib/rdoc/markup/to_html_snippet.rb, line 236
def handle_EM_WORD(word)
  super unless inline_limit_reached?
end
# File lib/rdoc/markup/to_html_snippet.rb, line 248
def handle_HARD_BREAK
  super unless inline_limit_reached?
end
Calls superclass method RDoc::Markup::Formatter#handle_STRIKE
# File lib/rdoc/markup/to_html_snippet.rb, line 244
def handle_STRIKE(nodes)
  super unless inline_limit_reached?
end
Calls superclass method RDoc::Markup::Formatter#handle_TT
# File lib/rdoc/markup/to_html_snippet.rb, line 240
def handle_TT(code)
  super unless inline_limit_reached?
end
Calls superclass method RDoc::Markup::Formatter#handle_inline
# 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

Removes escaping from the cross-references in target

# File lib/rdoc/markup/to_html_snippet.rb, line 131
def handle_regexp_CROSSREF(text)
  text.sub(/\A\\/, '')
end

In snippets, there are no lists

# File lib/rdoc/markup/to_html_snippet.rb, line 183
def html_list_name(list_type, open_tag)
  ''
end
# File lib/rdoc/markup/to_html_snippet.rb, line 256
def inline_limit_reached?
  @inline_character_limit <= 0
end

Lists are paragraphs, but notes and labels have a separator

# 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

Prepares the visitor for HTML snippet generation

Calls superclass method
# File lib/rdoc/markup/to_html_snippet.rb, line 122
def start_accepting
  super

  @characters = 0
end
# 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

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

# 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