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, #annotate, #apply_regexp_handling, #convert_string, gen_relative_url, #handle_BOLD, #handle_BOLD_WORD, #handle_EM, #handle_EM_WORD, #handle_HARD_BREAK, #handle_STRIKE, #handle_TEXT, #handle_TIDYLINK, #handle_inline, #ignore, #parse_url, #traverse_inline_nodes, #tt?

Constructor Details

#initialize(markup = nil) ⇒ ToLabel

Creates a new formatter that will output HTML-safe labels



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

def initialize(markup = nil)
  super nil, markup

  @markup.add_regexp_handling RDoc::CrossReference::CROSSREF_REGEXP, :CROSSREF

  @res = []
end

Instance Attribute Details

#resObject (readonly)

:nodoc:



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

def res
  @res
end

Instance Method Details

#convert(text) ⇒ Object

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



46
47
48
49
50
# File 'lib/rdoc/markup/to_label.rb', line 46

def convert(text)
  label = extract_plaintext(text)

  RDoc::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.



56
57
58
59
60
# File 'lib/rdoc/markup/to_label.rb', line 56

def convert_legacy(text)
  label = extract_plaintext(text)

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

#extract_plaintext(text) ⇒ Object



37
38
39
40
41
# File 'lib/rdoc/markup/to_label.rb', line 37

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

#handle_PLAIN_TEXT(text) ⇒ Object



25
26
27
# File 'lib/rdoc/markup/to_label.rb', line 25

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



66
67
68
# File 'lib/rdoc/markup/to_label.rb', line 66

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

#handle_REGEXP_HANDLING_TEXT(text) ⇒ Object



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

def handle_REGEXP_HANDLING_TEXT(text)
  @res << text
end

#handle_TT(text) ⇒ Object



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

def handle_TT(text)
  @res << text
end