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

Constants included from Text

Text::MARKUP_FORMAT, Text::TO_HTML_CHARACTERS

Instance Attribute Summary collapse

Attributes inherited from ToHtml

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

Instance Method Summary collapse

Methods inherited from ToHtml

#accept_blank_line, #accept_block_quote, #accept_list_end, #convert_string, #end_accepting, #handle_RDOCLINK, #handle_special_HYPERLINK, #handle_special_RDOCLINK, #handle_special_TIDYLINK, #init_tags, #list_end_for, #parseable?, #to_html

Methods included from Text

encode_fallback, #expand_tabs, #flush_left, #markup, #normalize_comment, #parse, #snippet, #strip_hashes, #strip_newlines, #strip_stars, #to_html, #wrap

Methods inherited from Formatter

#accept_document, #add_special_RDOCLINK, #add_special_TIDYLINK, #add_tag, #annotate, #convert_special, #convert_string, gen_relative_url, #ignore, #in_tt?, #parse_url, #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.



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/rdoc/markup/to_html_snippet.rb', line 36

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_special RDoc::CrossReference::CROSSREF_REGEXP, :CROSSREF
end

Instance Attribute Details

#character_limitObject (readonly)

After this many characters the input will be cut off.



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

def character_limit
  @character_limit
end

#charactersObject (readonly)

The number of characters seen so far.



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

def characters
  @characters
end

#maskObject (readonly)

The attribute bitmask



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

def mask
  @mask
end

#paragraph_limitObject (readonly)

After this many paragraphs the input will be cut off.



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

def paragraph_limit
  @paragraph_limit
end

#paragraphsObject (readonly)

Count of paragraphs found



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

def paragraphs
  @paragraphs
end

Instance Method Details

#accept_heading(heading) ⇒ Object

Adds heading to the output as a paragraph



52
53
54
55
56
# File 'lib/rdoc/markup/to_html_snippet.rb', line 52

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



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

def accept_list_item_end list_item
end

#accept_list_item_start(list_item) ⇒ Object

Prepares the visitor for consuming list_item



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

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



94
95
96
97
98
# File 'lib/rdoc/markup/to_html_snippet.rb', line 94

def accept_list_start list
  @list << list.type
  @res << html_list_name(list.type, true)
  @in_list_entry.push ''
end

#accept_paragraph(paragraph) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/rdoc/markup/to_html_snippet.rb', line 68

def accept_paragraph paragraph
  para = @in_list_entry.last || "<p>"

  text = paragraph.text @hard_break

  @res << "#{para}#{wrap to_html text}\n"

  add_paragraph
end

#accept_verbatim(verbatim) ⇒ Object

Adds verbatim to the output



103
104
105
106
107
108
109
110
111
112
113
# File 'lib/rdoc/markup/to_html_snippet.rb', line 103

def accept_verbatim verbatim
  throw :done if @characters >= @character_limit
  input = verbatim.text.rstrip

  text = truncate input
  text << ' ...' unless text == input

  super RDoc::Markup::Verbatim.new text

  add_paragraph
end

#add_paragraphObject

Throws :done when paragraph_limit paragraphs have been encountered



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

def add_paragraph
  @paragraphs += 1

  throw :done if @paragraphs >= @paragraph_limit
end

#convert(content) ⇒ Object

Marks up content



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

def convert content
  catch :done do
    return super
  end

  end_accepting
end

#convert_flow(flow) ⇒ Object

Converts flow items flow



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/rdoc/markup/to_html_snippet.rb', line 214

def convert_flow flow
  throw :done if @characters >= @character_limit

  res = []
  @mask = 0

  flow.each do |item|
    case item
    when RDoc::Markup::AttrChanger then
      off_tags res, item
      on_tags  res, item
    when String then
      text = convert_string item
      res << truncate(text)
    when RDoc::Markup::Special then
      text = convert_special item
      res << truncate(text)
    else
      raise "Unknown flow element: #{item.inspect}"
    end

    if @characters >= @character_limit then
      off_tags res, RDoc::Markup::AttrChanger.new(0, @mask)
      break
    end
  end

  res << ' ...' if @characters >= @character_limit

  res.join
end

#gen_url(url, text) ⇒ Object

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



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

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_special_CROSSREF(special) ⇒ Object

Removes escaping from the cross-references in special



127
128
129
# File 'lib/rdoc/markup/to_html_snippet.rb', line 127

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

#handle_special_HARD_BREAK(special) ⇒ Object

special is a <br>



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

def handle_special_HARD_BREAK special
  @characters -= 4
  '<br>'
end

#html_list_name(list_type, open_tag) ⇒ Object

In snippets, there are no lists



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

def html_list_name list_type, open_tag
  ''
end

#list_item_start(list_item, list_type) ⇒ Object

Lists are paragraphs, but notes and labels have a separator



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

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

#off_tags(res, item) ⇒ Object

Maintains a bitmask to allow HTML elements to be closed properly. See RDoc::Markup::Formatter.



260
261
262
263
264
# File 'lib/rdoc/markup/to_html_snippet.rb', line 260

def off_tags res, item
  @mask ^= item.turn_off

  super
end

#on_tags(res, item) ⇒ Object

Maintains a bitmask to allow HTML elements to be closed properly. See RDoc::Markup::Formatter.



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

def on_tags res, item
  @mask ^= item.turn_on

  super
end

#start_acceptingObject

Prepares the visitor for HTML snippet generation



118
119
120
121
122
# File 'lib/rdoc/markup/to_html_snippet.rb', line 118

def start_accepting
  super

  @characters = 0
end

#truncate(text) ⇒ Object

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



269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/rdoc/markup/to_html_snippet.rb', line 269

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