Add <target> to one more testcase (see r8206).
[docutils.git] / sandbox / rst2chunkedhtml / html4css1-external-refs.patch
blob45b3652ff5ba6df1321d56546837af8b079c682d
1 Index: docutils/writers/html4css1/__init__.py
2 ===================================================================
3 --- docutils/writers/html4css1/__init__.py (revision 5506)
4 +++ docutils/writers/html4css1/__init__.py (working copy)
5 @@ -150,8 +150,20 @@
6 writers.Writer.__init__(self)
7 self.translator_class = HTMLTranslator
9 + def set_external_ids(self, extids):
10 + """Set a mapping of node IDs to URIs. Useful for replacing internal
11 + references with external ones (it adds support for chunked HTML).
12 + The mapping is not used by the writer itself, but the translator
13 + object: If it has a method called ``set_external_ids``, the mapping
14 + will be passed to it.
15 + """
16 + self.external_ids = extids
18 def translate(self):
19 self.visitor = visitor = self.translator_class(self.document)
20 + if callable(getattr(visitor, 'set_external_ids', None))\
21 + and hasattr(self, 'external_ids'):
22 + visitor.set_external_ids(self.external_ids)
23 self.document.walkabout(visitor)
24 for attr in self.visitor_attributes:
25 setattr(self, attr, getattr(visitor, attr))
26 @@ -298,6 +310,30 @@
27 self.in_mailto = 0
28 self.author_in_authors = None
30 + # Mapping of IDs to URIs, set by set_external_ids(), and used by
31 + # get_href().
32 + self.external_ids = {}
34 + def set_external_ids(self, extids):
35 + """Set a mapping of IDs to URIs. Useful for replacing internal
36 + references with external ones.
37 + """
38 + self.external_ids = extids
40 + def get_href(self, id):
41 + """Return the node ID as a string suitable for the "href" HTML
42 + attribute. This is usually just ``'#' + id``, but it can also be an
43 + URI if the ID is in the mapping set by the ``set_external_ids()``
44 + method. In any case, you don't need to add the '#' character to the
45 + string---this method takes care of it.
47 + *Use this method whenever you need to construct an "href" from an ID.*
48 + """
49 + try:
50 + return self.external_ids[id]
51 + except KeyError:
52 + return '#' + id
54 def astext(self):
55 return ''.join(self.head_prefix + self.head
56 + self.stylesheet + self.body_prefix
57 @@ -549,7 +585,7 @@
58 '</tbody>\n</table>\n')
60 def visit_citation_reference(self, node):
61 - href = '#' + node['refid']
62 + href = self.get_href(node['refid'])
63 self.body.append(self.starttag(
64 node, 'a', '[', CLASS='citation-reference', href=href))
66 @@ -879,13 +915,13 @@
67 if len(backrefs) == 1:
68 self.context.append('')
69 self.context.append('</a>')
70 - self.context.append('<a class="fn-backref" href="#%s">'
71 - % backrefs[0])
72 + self.context.append('<a class="fn-backref" href="%s">'
73 + % self.get_href(backrefs[0]))
74 else:
75 i = 1
76 for backref in backrefs:
77 - backlinks.append('<a class="fn-backref" href="#%s">%s</a>'
78 - % (backref, i))
79 + backlinks.append('<a class="fn-backref" href="%s">%s</a>'
80 + % (self.get_href(backref), i))
81 i += 1
82 self.context.append('<em>(%s)</em> ' % ', '.join(backlinks))
83 self.context += ['', '']
84 @@ -905,7 +941,7 @@
85 '</tbody>\n</table>\n')
87 def visit_footnote_reference(self, node):
88 - href = '#' + node['refid']
89 + href = self.get_href(node['refid'])
90 format = self.settings.footnote_references
91 if format == 'brackets':
92 suffix = '['
93 @@ -1187,7 +1223,7 @@
95 def visit_problematic(self, node):
96 if node.hasattr('refid'):
97 - self.body.append('<a href="#%s">' % node['refid'])
98 + self.body.append('<a href="%s">' % self.get_href(node['refid']))
99 self.context.append('</a>')
100 else:
101 self.context.append('')
102 @@ -1220,8 +1256,11 @@
103 else:
104 assert node.has_key('refid'), \
105 'References must have "refuri" or "refid" attribute.'
106 - atts['href'] = '#' + node['refid']
107 - atts['class'] += ' internal'
108 + atts['href'] = self.get_href(node['refid'])
109 + if atts['href'].startswith('#'):
110 + atts['class'] += ' internal'
111 + else:
112 + atts['class'] += ' external'
113 if not isinstance(node.parent, nodes.TextElement):
114 assert len(node) == 1 and isinstance(node[0], nodes.image)
115 atts['class'] += ' image-reference'
116 @@ -1334,13 +1373,14 @@
117 if len(node['backrefs']):
118 backrefs = node['backrefs']
119 if len(backrefs) == 1:
120 - backref_text = ('; <em><a href="#%s">backlink</a></em>'
121 - % backrefs[0])
122 + backref_text = ('; <em><a href="%s">backlink</a></em>'
123 + % self.get_href(backrefs[0]))
124 else:
125 i = 1
126 backlinks = []
127 for backref in backrefs:
128 - backlinks.append('<a href="#%s">%s</a>' % (backref, i))
129 + backlinks.append('<a href="%s">%s</a>'
130 + % (self.get_href(backref), i))
131 i += 1
132 backref_text = ('; <em>backlinks: %s</em>'
133 % ', '.join(backlinks))
134 @@ -1445,7 +1485,7 @@
135 atts = {}
136 if node.hasattr('refid'):
137 atts['class'] = 'toc-backref'
138 - atts['href'] = '#' + node['refid']
139 + atts['href'] = self.get_href(node['refid'])
140 if atts:
141 self.body.append(self.starttag({}, 'a', '', **atts))
142 close_tag = '</a></h%s>\n' % (h_level)