Class: RDoc::Markup::ToHtmlSnippet

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

Overview

Outputs RDoc markup as paragraphs with inline markup only.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(characters = 100, paragraphs = 3) ⇒ 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.



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rdoc/markup/to_html_snippet.rb', line 39

def initialize(characters = 100, paragraphs = 3)
  super()

  @character_limit = characters
  @paragraph_limit = paragraphs

  @characters = 0
  @mask       = 0
  @paragraphs = 0

  @markup.add_regexp_handling CrossReference::CROSSREF_REGEXP, :CROSSREF
end

Instance Attribute Details

#character_limit ⇒ Object (readonly)

After this many characters the input will be cut off.



12
13
14
# File 'lib/rdoc/markup/to_html_snippet.rb', line 12

def character_limit
  @character_limit
end

#characters ⇒ Object (readonly)

The number of characters seen so far.



17
18
19
# File 'lib/rdoc/markup/to_html_snippet.rb', line 17

def characters
  @characters
end

#mask ⇒ Object (readonly)

The attribute bitmask



22
23
24
# File 'lib/rdoc/markup/to_html_snippet.rb', line 22

def mask
  @mask
end

#paragraph_limit ⇒ Object (readonly)

After this many paragraphs the input will be cut off.



27
28
29
# File 'lib/rdoc/markup/to_html_snippet.rb', line 27

def paragraph_limit
  @paragraph_limit
end

#paragraphs ⇒ Object (readonly)

Count of paragraphs found



32
33
34
# File 'lib/rdoc/markup/to_html_snippet.rb', line 32

def paragraphs
  @paragraphs
end

Instance Method Details

#accept_heading(heading) ⇒ Object

Adds heading to the output as a paragraph



55
56
57
58
59
# File 'lib/rdoc/markup/to_html_snippet.rb', line 55

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



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

def accept_list_item_end(list_item)
end

#accept_list_item_start(list_item) ⇒ Object

Prepares the visitor for consuming list_item



93
94
95
# File 'lib/rdoc/markup/to_html_snippet.rb', line 93

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



100
101
102
103
104
# File 'lib/rdoc/markup/to_html_snippet.rb', line 100

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



74
75
76
77
78
79
80
81
82
# File 'lib/rdoc/markup/to_html_snippet.rb', line 74

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



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rdoc/markup/to_html_snippet.rb', line 109

def accept_verbatim(verbatim)
  throw :done if @characters >= @character_limit
  input = verbatim.text.rstrip
  text = truncate(input, @character_limit - @characters)
  @characters += input.length
  text << " #{TO_HTML_CHARACTERS[text.encoding][:ellipsis]}" unless text == input

  super Markup::Verbatim.new text

  add_paragraph
end

#add_paragraph ⇒ Object

Throws :done when paragraph_limit paragraphs have been encountered



193
194
195
196
197
# File 'lib/rdoc/markup/to_html_snippet.rb', line 193

def add_paragraph
  @paragraphs += 1

  throw :done if @paragraphs >= @paragraph_limit
end

#convert(content) ⇒ Object

Marks up content



202
203
204
205
206
207
208
# File 'lib/rdoc/markup/to_html_snippet.rb', line 202

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.



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/rdoc/markup/to_html_snippet.rb', line 166

def gen_url(url, text)
  if url =~ /^rdoc-label:([^:]*)(?::(.*))?/
    type = "link"
  elsif url =~ /([A-Za-z]+):(.*)/
    type = $1
  else
    type = "http"
  end

  if (type == "http" or type == "https" or type == "link") and
     url =~ /\.(gif|png|jpg|jpeg|bmp)$/
    ''
  else
    text.sub(%r%^#{type}:/*%, '')
  end
end

#handle_BOLD(nodes) ⇒ Object



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

def handle_BOLD(nodes)
  super unless inline_limit_reached?
end

#handle_BOLD_WORD(word) ⇒ Object



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

def handle_BOLD_WORD(word)
  super unless inline_limit_reached?
end

#handle_EM(nodes) ⇒ Object



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

def handle_EM(nodes)
  super unless inline_limit_reached?
end

#handle_EM_WORD(word) ⇒ Object



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

def handle_EM_WORD(word)
  super unless inline_limit_reached?
end

#handle_HARD_BREAK ⇒ Object



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

def handle_HARD_BREAK
  super unless inline_limit_reached?
end

#handle_inline(text) ⇒ Object



263
264
265
266
267
268
269
270
271
# File 'lib/rdoc/markup/to_html_snippet.rb', line 263

def handle_inline(text)
  limit = @character_limit - @characters
  return ['', 0] if limit <= 0
  @inline_character_limit = limit
  res = super
  res << " #{TO_HTML_CHARACTERS[text.encoding][:ellipsis]}" if @inline_character_limit <= 0
  @characters += limit - @inline_character_limit
  res
end

#handle_PLAIN_TEXT(text) ⇒ Object

:nodoc:



210
211
212
213
214
215
216
# File 'lib/rdoc/markup/to_html_snippet.rb', line 210

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 and escapes HTML characters, since snippets don't link cross-references.



134
135
136
# File 'lib/rdoc/markup/to_html_snippet.rb', line 134

def handle_regexp_CROSSREF(text)
  convert_string(text.delete_prefix('\\'))
end

#handle_REGEXP_HANDLING_TEXT(text) ⇒ Object

:nodoc:



218
219
220
221
222
223
224
225
# File 'lib/rdoc/markup/to_html_snippet.rb', line 218

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



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

def handle_STRIKE(nodes)
  super unless inline_limit_reached?
end


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

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

#handle_TT(code) ⇒ Object



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

def handle_TT(code)
  super unless inline_limit_reached?
end

#html_list_name(list_type, open_tag) ⇒ Object

In snippets, there are no lists



186
187
188
# File 'lib/rdoc/markup/to_html_snippet.rb', line 186

def html_list_name(list_type, open_tag)
  ''
end

#inline_limit_reached? ⇒ Boolean

Returns:

  • (Boolean)


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

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/rdoc/markup/to_html_snippet.rb', line 141

def list_item_start(list_item, list_type)
  throw :done if @characters >= @character_limit

  case list_type
  when :BULLET, :LALPHA, :NUMBER, :UALPHA
    "<p>"
  when :LABEL, :NOTE
    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 Error, "Invalid list type: #{list_type.inspect}"
  end
end

#start_accepting ⇒ Object

Prepares the visitor for HTML snippet generation



124
125
126
127
128
# File 'lib/rdoc/markup/to_html_snippet.rb', line 124

def start_accepting
  super

  @characters = 0
end

#to_html(item) ⇒ Object



273
274
275
276
# File 'lib/rdoc/markup/to_html_snippet.rb', line 273

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

#truncate(text, limit) ⇒ Object

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



281
282
283
284
285
286
287
288
# File 'lib/rdoc/markup/to_html_snippet.rb', line 281

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