Class: RDoc::Markup::ToLabel

Inherits:
Formatter show all
Defined in:
lib/rdoc/markup/to_label.rb

Overview

Creates HTML-safe labels suitable for use in id attributes. Tidylinks are converted to their link part and cross-reference links have the suppression marks removed (\SomeClass is converted to SomeClass).

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Formatter

#accept_document, #add_special_RDOCLINK, #add_special_TIDYLINK, #add_tag, #annotate, #convert_flow, #convert_special, #convert_string, gen_relative_url, #ignore, #in_tt?, #off_tags, #on_tags, #parse_url, #tt?

Constructor Details

#initialize(markup = nil) ⇒ ToLabel

Creates a new formatter that will output HTML-safe labels



15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/rdoc/markup/to_label.rb', line 15

def initialize markup = nil
  super nil, markup

  @markup.add_special RDoc::CrossReference::CROSSREF_REGEXP, :CROSSREF
  @markup.add_special(/(((\{.*?\})|\b\S+?)\[\S+?\])/, :TIDYLINK)

  add_tag :BOLD, '', ''
  add_tag :TT,   '', ''
  add_tag :EM,   '', ''

  @res = []
end

Instance Attribute Details

#resObject (readonly)

:nodoc:



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

def res
  @res
end

Instance Method Details

#convert(text) ⇒ Object

Converts text to an HTML-safe label



31
32
33
34
35
# File 'lib/rdoc/markup/to_label.rb', line 31

def convert text
  label = convert_flow @am.flow text

  CGI.escape label
end

#handle_special_CROSSREF(special) ⇒ Object

Converts the CROSSREF special to plain text, removing the suppression marker, if any



41
42
43
44
45
# File 'lib/rdoc/markup/to_label.rb', line 41

def handle_special_CROSSREF special
  text = special.text

  text.sub(/^\\/, '')
end

Converts the TIDYLINK special to just the text part



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

def handle_special_TIDYLINK special
  text = special.text

  return text unless text =~ /\{(.*?)\}\[(.*?)\]/ or text =~ /(\S+)\[(.*?)\]/

  $1
end