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_regexp_handling_RDOCLINK, #add_regexp_handling_TIDYLINK, #add_tag, #annotate, #convert_flow, #convert_regexp_handling, #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



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

def initialize markup = nil
  super nil, markup

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

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

  @res = []
end

Instance Attribute Details

#resObject (readonly)

:nodoc:



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

def res
  @res
end

Instance Method Details

#convert(text) ⇒ Object

Converts text to an HTML-safe label



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

def convert text
  label = convert_flow @am.flow text

  CGI.escape(label).gsub('%', '-').sub(/^-/, '')
end

#handle_regexp_CROSSREF(target) ⇒ Object

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



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

def handle_regexp_CROSSREF target
  text = target.text

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

Converts the TIDYLINK target to just the text part



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

def handle_regexp_TIDYLINK target
  text = target.text

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

  $1
end