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

Attributes included from Text

#language

Instance Method Summary collapse

Methods inherited from ToHtml

#accept_blank_line, #accept_block_quote, #accept_list_end, #convert_string, #end_accepting, #handle_RDOCLINK, #handle_regexp_HYPERLINK, #handle_regexp_RDOCLINK, #handle_regexp_TIDYLINK, #init_link_notation_regexp_handlings, #init_regexp_handlings, #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_regexp_handling_RDOCLINK, #add_regexp_handling_TIDYLINK, #add_tag, #annotate, #convert_regexp_handling, #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.


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


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

def accept_list_item_end list_item
end

#accept_list_item_start(list_item) ⇒ Object

Prepares the visitor for consuming list_item


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

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


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

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

#accept_paragraph(paragraph) ⇒ Object


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

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


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

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


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

def add_paragraph
  @paragraphs += 1

  throw :done if @paragraphs >= @paragraph_limit
end

#convert(content) ⇒ Object

Marks up content


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

def convert content
  catch :done do
    return super
  end

  end_accepting
end

#convert_flow(flow) ⇒ Object

Converts flow items flow


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
245
# File 'lib/rdoc/markup/to_html_snippet.rb', line 215

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::RegexpHandling then
      text = convert_regexp_handling 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.


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

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_regexp_CROSSREF(target) ⇒ Object

Removes escaping from the cross-references in target


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

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

#handle_regexp_HARD_BREAK(target) ⇒ Object

target is a <br>


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

def handle_regexp_HARD_BREAK target
  @characters -= 4
  '<br>'
end

#html_list_name(list_type, open_tag) ⇒ Object

In snippets, there are no lists


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

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


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

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.


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

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.


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

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

  super
end

#start_acceptingObject

Prepares the visitor for HTML snippet generation


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

def start_accepting
  super

  @characters = 0
end

#truncate(text) ⇒ Object

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


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

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