author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 18 Aug 2009 09:25:26 +0200 | |
changeset 2901 | 8f4d495c94c2 |
parent 2312 | af4d8f75c5db |
child 4212 | ab6573088b4a |
permissions | -rw-r--r-- |
0 | 1 |
# Author: David Goodger |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
2 |
""" |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
3 |
|
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:organization: Logilab |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
5 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
8 |
""" |
0 | 9 |
# Contact: goodger@users.sourceforge.net |
10 |
# Revision: $Revision: 1.2 $ |
|
11 |
# Date: $Date: 2005-07-04 16:36:50 $ |
|
12 |
# Copyright: This module has been placed in the public domain. |
|
13 |
||
14 |
""" |
|
15 |
Simple HyperText Markup Language document tree Writer. |
|
16 |
||
17 |
The output conforms to the HTML 4.01 Transitional DTD and to the Extensible |
|
18 |
HTML version 1.0 Transitional DTD (*almost* strict). The output contains a |
|
19 |
minimum of formatting information. A cascading style sheet ("default.css" by |
|
20 |
default) is required for proper viewing with a modern graphical browser. |
|
21 |
||
22 |
http://cvs.zope.org/Zope/lib/python/docutils/writers/Attic/html4zope.py?rev=1.1.2.2&only_with_tag=ajung-restructuredtext-integration-branch&content-type=text/vnd.viewcvs-markup |
|
23 |
""" |
|
24 |
||
25 |
__docformat__ = 'reStructuredText' |
|
26 |
||
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
27 |
from logilab.mtconverter import xml_escape |
0 | 28 |
|
29 |
from docutils import nodes |
|
30 |
from docutils.writers.html4css1 import Writer as CSS1Writer |
|
31 |
from docutils.writers.html4css1 import HTMLTranslator as CSS1HTMLTranslator |
|
32 |
import os |
|
33 |
||
34 |
default_level = int(os.environ.get('STX_DEFAULT_LEVEL', 3)) |
|
35 |
||
36 |
class Writer(CSS1Writer): |
|
37 |
"""css writer using our html translator""" |
|
38 |
def __init__(self, base_url): |
|
39 |
CSS1Writer.__init__(self) |
|
40 |
self.translator_class = URLBinder(base_url, HTMLTranslator) |
|
41 |
||
42 |
def apply_template(self): |
|
43 |
"""overriding this is necessary with docutils >= 0.5""" |
|
44 |
return self.visitor.astext() |
|
45 |
||
46 |
class URLBinder: |
|
47 |
def __init__(self, url, klass): |
|
48 |
self.base_url = url |
|
49 |
self.translator_class = HTMLTranslator |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
704
diff
changeset
|
50 |
|
0 | 51 |
def __call__(self, document): |
52 |
translator = self.translator_class(document) |
|
53 |
translator.base_url = self.base_url |
|
54 |
return translator |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
704
diff
changeset
|
55 |
|
0 | 56 |
class HTMLTranslator(CSS1HTMLTranslator): |
57 |
"""ReST tree to html translator""" |
|
58 |
||
59 |
def astext(self): |
|
60 |
"""return the extracted html""" |
|
61 |
return ''.join(self.body) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
704
diff
changeset
|
62 |
|
0 | 63 |
def visit_title(self, node): |
64 |
"""Only 6 section levels are supported by HTML.""" |
|
65 |
if isinstance(node.parent, nodes.topic): |
|
66 |
self.body.append( |
|
67 |
self.starttag(node, 'p', '', CLASS='topic-title')) |
|
68 |
if node.parent.hasattr('id'): |
|
69 |
self.body.append( |
|
70 |
self.starttag({}, 'a', '', name=node.parent['id'])) |
|
71 |
self.context.append('</a></p>\n') |
|
72 |
else: |
|
73 |
self.context.append('</p>\n') |
|
74 |
elif self.section_level == 0: |
|
75 |
# document title |
|
76 |
self.head.append('<title>%s</title>\n' |
|
77 |
% self.encode(node.astext())) |
|
78 |
self.body.append(self.starttag(node, 'h%d' % default_level, '', |
|
79 |
CLASS='title')) |
|
80 |
self.context.append('</h%d>\n' % default_level) |
|
81 |
else: |
|
82 |
self.body.append( |
|
83 |
self.starttag(node, 'h%s' % ( |
|
84 |
default_level+self.section_level-1), '')) |
|
85 |
atts = {} |
|
86 |
if node.hasattr('refid'): |
|
87 |
atts['class'] = 'toc-backref' |
|
88 |
atts['href'] = '%s#%s' % (self.base_url, node['refid']) |
|
89 |
self.body.append(self.starttag({}, 'a', '', **atts)) |
|
90 |
self.context.append('</a></h%s>\n' % ( |
|
91 |
default_level+self.section_level-1)) |
|
92 |
||
93 |
def visit_subtitle(self, node): |
|
94 |
"""format a subtitle""" |
|
95 |
if isinstance(node.parent, nodes.sidebar): |
|
96 |
self.body.append(self.starttag(node, 'p', '', |
|
97 |
CLASS='sidebar-subtitle')) |
|
98 |
self.context.append('</p>\n') |
|
99 |
else: |
|
100 |
self.body.append( |
|
101 |
self.starttag(node, 'h%s' % (default_level+1), '', |
|
102 |
CLASS='subtitle')) |
|
103 |
self.context.append('</h%s>\n' % (default_level+1)) |
|
104 |
||
105 |
def visit_document(self, node): |
|
106 |
"""syt: i don't want the enclosing <div class="document">""" |
|
107 |
def depart_document(self, node): |
|
108 |
"""syt: i don't want the enclosing <div class="document">""" |
|
109 |
||
110 |
def visit_reference(self, node): |
|
111 |
"""syt: i want absolute urls""" |
|
112 |
if node.has_key('refuri'): |
|
113 |
href = node['refuri'] |
|
114 |
if ( self.settings.cloak_email_addresses |
|
115 |
and href.startswith('mailto:')): |
|
116 |
href = self.cloak_mailto(href) |
|
117 |
self.in_mailto = 1 |
|
118 |
else: |
|
119 |
assert node.has_key('refid'), \ |
|
120 |
'References must have "refuri" or "refid" attribute.' |
|
121 |
href = '%s#%s' % (self.base_url, node['refid']) |
|
122 |
atts = {'href': href, 'class': 'reference'} |
|
123 |
if not isinstance(node.parent, nodes.TextElement): |
|
124 |
assert len(node) == 1 and isinstance(node[0], nodes.image) |
|
125 |
atts['class'] += ' image-reference' |
|
126 |
self.body.append(self.starttag(node, 'a', '', **atts)) |
|
127 |
||
128 |
## override error messages to avoid XHTML problems ######################## |
|
129 |
def visit_problematic(self, node): |
|
130 |
pass |
|
131 |
||
132 |
def depart_problematic(self, node): |
|
133 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
704
diff
changeset
|
134 |
|
0 | 135 |
def visit_system_message(self, node): |
136 |
backref_text = '' |
|
137 |
if len(node['backrefs']): |
|
138 |
backrefs = node['backrefs'] |
|
139 |
if len(backrefs) == 1: |
|
140 |
backref_text = '; <em>backlink</em>' |
|
141 |
else: |
|
142 |
i = 1 |
|
143 |
backlinks = [] |
|
144 |
for backref in backrefs: |
|
145 |
backlinks.append(str(i)) |
|
146 |
i += 1 |
|
147 |
backref_text = ('; <em>backlinks: %s</em>' |
|
148 |
% ', '.join(backlinks)) |
|
149 |
if node.hasattr('line'): |
|
150 |
line = ', line %s' % node['line'] |
|
151 |
else: |
|
152 |
line = '' |
|
153 |
a_start = a_end = '' |
|
154 |
error = u'System Message: %s%s/%s%s (%s %s)%s</p>\n' % ( |
|
155 |
a_start, node['type'], node['level'], a_end, |
|
156 |
self.encode(node['source']), line, backref_text) |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
157 |
self.body.append(u'<div class="system-message"><b>ReST / HTML errors:</b>%s</div>' % xml_escape(error)) |
0 | 158 |
|
159 |
def depart_system_message(self, node): |
|
160 |
pass |