Class: RDoc::Markup::ToHtmlSnippet

Inherits:
ToHtml show all
Defined in:
lib/rdoc/markup/to_html_snippet.rb

Overview

Outputs RDoc markup as paragraphs with inline markup only.

Constant Summary

Constants inherited from ToHtml

RDoc::Markup::ToHtml::LIST_TYPE_TO_HTML, RDoc::Markup::ToHtml::URL_CHARACTERS_REGEXP_STR

Constants included from Text

Text::MARKUP_FORMAT, Text::SPACE_SEPARATED_LETTER_CLASS, Text::TO_HTML_CHARACTERS

Instance Attribute Summary collapse

Attributes inherited from ToHtml

#code_object, #from_path, #in_list_entry, #list, #res

Attributes included from Text

#language

Instance Method Summary collapse

Methods inherited from ToHtml

#accept_blank_line, #accept_block_quote, #accept_list_end, #accept_table, #apply_tidylink_label_special_handling, #convert_string, #deduplicate_heading_id, #emit_inline, #end_accepting, #handle_RDOCLINK, #handle_regexp_HYPERLINK, #handle_regexp_RDOCLINK, #handle_regexp_SUPPRESSED_CROSSREF, #in_tidylink_label?, #init_link_notation_regexp_handlings, #init_regexp_handlings, #list_end_for, #parseable?

Methods included from Text

decode_legacy_label, encode_fallback, expand_tabs, #flush_left, #markup, #normalize_comment, #parse, #snippet, #strip_hashes, #strip_newlines, #strip_stars, to_anchor, #to_html_characters, #wrap

Methods inherited from Formatter

#accept_document, #add_regexp_handling_RDOCLINK, #annotate, #apply_regexp_handling, #convert_string, gen_relative_url, #handle_TEXT, #ignore, #parse_url, #traverse_inline_nodes, #tt?

Constructor Details

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



37
38
39
40
41
42
43
44
45
46
47
48
# 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_limitObject (readonly)

After this many characters the input will be cut off.



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

def character_limit
  @character_limit
end

#charactersObject (readonly)

The number of characters seen so far.



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

def characters
  @characters
end

#maskObject (readonly)

The attribute bitmask



20
21
22
# File 'lib/rdoc/markup/to_html_snippet.rb', line 20

def mask
  @mask
end

#paragraph_limitObject (readonly)

After this many paragraphs the input will be cut off.



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

def paragraph_limit
  @paragraph_limit
end

#paragraphsObject (readonly)

Count of paragraphs found



30
31
32
# File 'lib/rdoc/markup/to_html_snippet.rb', line 30

def paragraphs
  @paragraphs
end

Instance Method Details

#accept_heading(heading) ⇒ Object

Adds heading to the output as a paragraph



53
54
55
56
57
# 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) ⇒ Object

Finishes consumption of list_item



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

def accept_list_item_end(list_item)
end

#accept_list_item_start(list_item) ⇒ Object

Prepares the visitor for consuming list_item



91
92
93
# 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) ⇒ Object

Prepares the visitor for consuming list



98
99
100
101
102
# 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) ⇒ Object

Adds paragraph to the output



72
73
74
75
76
77
78
79
80
# 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_verbatim(verbatim) ⇒ Object

Adds verbatim to the output



107
108
109
110
111
112
113
114
115
116
117
# 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_paragraphObject

Throws :done when paragraph_limit paragraphs have been encountered



190
191
192
193
194
# File 'lib/rdoc/markup/to_html_snippet.rb', line 190

def add_paragraph
  @paragraphs += 1

  throw :done if @paragraphs >= @paragraph_limit
end

#convert(content) ⇒ Object

Marks up content



199
200
201
202
203
204
205
# 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) ⇒ Object

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



163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# 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) ⇒ Object



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

def handle_BOLD(nodes)
  super unless inline_limit_reached?
end

#handle_BOLD_WORD(word) ⇒ Object



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

def handle_BOLD_WORD(word)
  super unless inline_limit_reached?
end

#handle_EM(nodes) ⇒ Object



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

def handle_EM(nodes)
  super unless inline_limit_reached?
end

#handle_EM_WORD(word) ⇒ Object



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

def handle_EM_WORD(word)
  super unless inline_limit_reached?
end

#handle_HARD_BREAKObject



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

def handle_HARD_BREAK
  super unless inline_limit_reached?
end

#handle_inline(text) ⇒ Object



260
261
262
263
264
265
266
267
268
# 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) ⇒ Object

:nodoc:



207
208
209
210
211
212
213
# 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) ⇒ Object

Removes escaping from the cross-references in target



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

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

#handle_REGEXP_HANDLING_TEXT(text) ⇒ Object

:nodoc:



215
216
217
218
219
220
221
222
# 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) ⇒ Object



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

def handle_STRIKE(nodes)
  super unless inline_limit_reached?
end


252
253
254
# File 'lib/rdoc/markup/to_html_snippet.rb', line 252

def handle_TIDYLINK(label_part, url)
  traverse_inline_nodes(label_part) unless inline_limit_reached?
end

#handle_TT(code) ⇒ Object



240
241
242
# 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) ⇒ Object

In snippets, there are no lists



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

def html_list_name(list_type, open_tag)
  ''
end

#inline_limit_reached?Boolean



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

def inline_limit_reached?
  @inline_character_limit <= 0
end

#list_item_start(list_item, list_type) ⇒ Object

Lists are paragraphs, but notes and labels have a separator



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# 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_acceptingObject

Prepares the visitor for HTML snippet generation



122
123
124
125
126
# File 'lib/rdoc/markup/to_html_snippet.rb', line 122

def start_accepting
  super

  @characters = 0
end

#to_html(item) ⇒ Object



270
271
272
273
# 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) ⇒ Object

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



278
279
280
281
282
283
284
285
# 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