Class: RDoc::Markup::ToLabel

Inherits:
Markup::Formatter
  • Object
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

Constructor Details

#initialize ⇒ ToLabel

Creates a new formatter that will output HTML-safe labels



19
20
21
22
23
24
25
# File 'lib/rdoc/markup/to_label.rb', line 19

def initialize
  super

  @markup.add_regexp_handling CrossReference::CROSSREF_REGEXP, :CROSSREF

  @res = []
end

Instance Attribute Details

#res ⇒ Object (readonly)

:nodoc:



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

def res
  @res
end

Instance Method Details

#convert(text) ⇒ Object

Converts text to an HTML-safe label using GitHub-style anchor formatting.



48
49
50
51
52
# File 'lib/rdoc/markup/to_label.rb', line 48

def convert(text)
  label = extract_plaintext(text)

  Text.to_anchor(label)
end

#convert_legacy(text) ⇒ Object

Converts text to an HTML-safe label using legacy RDoc formatting. Used for generating backward-compatible anchor aliases.



58
59
60
61
62
# File 'lib/rdoc/markup/to_label.rb', line 58

def convert_legacy(text)
  label = extract_plaintext(text)

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

#extract_plaintext(text) ⇒ Object



39
40
41
42
43
# File 'lib/rdoc/markup/to_label.rb', line 39

def extract_plaintext(text)
  @res = []
  handle_inline(text)
  @res.join
end

#handle_PLAIN_TEXT(text) ⇒ Object



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

def handle_PLAIN_TEXT(text)
  @res << text
end

#handle_regexp_CROSSREF(text) ⇒ Object

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



68
69
70
# File 'lib/rdoc/markup/to_label.rb', line 68

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

#handle_REGEXP_HANDLING_TEXT(text) ⇒ Object



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

def handle_REGEXP_HANDLING_TEXT(text)
  @res << text
end

#handle_TT(text) ⇒ Object



35
36
37
# File 'lib/rdoc/markup/to_label.rb', line 35

def handle_TT(text)
  @res << text
end