1require 'cgi'
2
3##
4# Creates HTML-safe labels suitable for use in id attributes.  Tidylinks are
5# converted to their link part and cross-reference links have the suppression
6# marks removed (\\SomeClass is converted to SomeClass).
7
8class RDoc::Markup::ToLabel < RDoc::Markup::Formatter
9
10  attr_reader :res # :nodoc:
11
12  ##
13  # Creates a new formatter that will output HTML-safe labels
14
15  def initialize markup = nil
16    super nil, markup
17
18    @markup.add_special RDoc::CrossReference::CROSSREF_REGEXP, :CROSSREF
19    @markup.add_special(/(((\{.*?\})|\b\S+?)\[\S+?\])/, :TIDYLINK)
20
21    add_tag :BOLD, '', ''
22    add_tag :TT,   '', ''
23    add_tag :EM,   '', ''
24
25    @res = []
26  end
27
28  ##
29  # Converts +text+ to an HTML-safe label
30
31  def convert text
32    label = convert_flow @am.flow text
33
34    CGI.escape label
35  end
36
37  ##
38  # Converts the CROSSREF +special+ to plain text, removing the suppression
39  # marker, if any
40
41  def handle_special_CROSSREF special
42    text = special.text
43
44    text.sub(/^\\/, '')
45  end
46
47  ##
48  # Converts the TIDYLINK +special+ to just the text part
49
50  def handle_special_TIDYLINK special
51    text = special.text
52
53    return text unless text =~ /\{(.*?)\}\[(.*?)\]/ or text =~ /(\S+)\[(.*?)\]/
54
55    $1
56  end
57
58  alias accept_blank_line         ignore
59  alias accept_block_quote        ignore
60  alias accept_heading            ignore
61  alias accept_list_end           ignore
62  alias accept_list_item_end      ignore
63  alias accept_list_item_start    ignore
64  alias accept_list_start         ignore
65  alias accept_paragraph          ignore
66  alias accept_raw                ignore
67  alias accept_rule               ignore
68  alias accept_verbatim           ignore
69  alias end_accepting             ignore
70  alias handle_special_HARD_BREAK ignore
71  alias start_accepting           ignore
72
73end
74
75