author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Tue, 20 Apr 2010 15:35:13 +0200 | |
branch | stable |
changeset 5351 | ecf07370e6db |
parent 4767 | 74b8e39d4825 |
child 5421 | 8167de96c523 |
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 |
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2312
diff
changeset
|
5 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
1977
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 |
||
4767 | 27 |
import os |
28 |
||
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
29 |
from logilab.mtconverter import xml_escape |
0 | 30 |
|
31 |
from docutils import nodes |
|
32 |
from docutils.writers.html4css1 import Writer as CSS1Writer |
|
33 |
from docutils.writers.html4css1 import HTMLTranslator as CSS1HTMLTranslator |
|
34 |
||
35 |
default_level = int(os.environ.get('STX_DEFAULT_LEVEL', 3)) |
|
36 |
||
37 |
class Writer(CSS1Writer): |
|
38 |
"""css writer using our html translator""" |
|
39 |
def __init__(self, base_url): |
|
40 |
CSS1Writer.__init__(self) |
|
41 |
self.translator_class = URLBinder(base_url, HTMLTranslator) |
|
42 |
||
43 |
def apply_template(self): |
|
44 |
"""overriding this is necessary with docutils >= 0.5""" |
|
45 |
return self.visitor.astext() |
|
46 |
||
47 |
class URLBinder: |
|
48 |
def __init__(self, url, klass): |
|
49 |
self.base_url = url |
|
50 |
self.translator_class = HTMLTranslator |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
704
diff
changeset
|
51 |
|
0 | 52 |
def __call__(self, document): |
53 |
translator = self.translator_class(document) |
|
54 |
translator.base_url = self.base_url |
|
55 |
return translator |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
704
diff
changeset
|
56 |
|
0 | 57 |
class HTMLTranslator(CSS1HTMLTranslator): |
58 |
"""ReST tree to html translator""" |
|
59 |
||
60 |
def astext(self): |
|
61 |
"""return the extracted html""" |
|
62 |
return ''.join(self.body) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
704
diff
changeset
|
63 |
|
0 | 64 |
def visit_title(self, node): |
65 |
"""Only 6 section levels are supported by HTML.""" |
|
66 |
if isinstance(node.parent, nodes.topic): |
|
67 |
self.body.append( |
|
68 |
self.starttag(node, 'p', '', CLASS='topic-title')) |
|
69 |
if node.parent.hasattr('id'): |
|
70 |
self.body.append( |
|
71 |
self.starttag({}, 'a', '', name=node.parent['id'])) |
|
72 |
self.context.append('</a></p>\n') |
|
73 |
else: |
|
74 |
self.context.append('</p>\n') |
|
75 |
elif self.section_level == 0: |
|
76 |
# document title |
|
77 |
self.head.append('<title>%s</title>\n' |
|
78 |
% self.encode(node.astext())) |
|
79 |
self.body.append(self.starttag(node, 'h%d' % default_level, '', |
|
80 |
CLASS='title')) |
|
81 |
self.context.append('</h%d>\n' % default_level) |
|
82 |
else: |
|
83 |
self.body.append( |
|
84 |
self.starttag(node, 'h%s' % ( |
|
85 |
default_level+self.section_level-1), '')) |
|
86 |
atts = {} |
|
87 |
if node.hasattr('refid'): |
|
88 |
atts['class'] = 'toc-backref' |
|
89 |
atts['href'] = '%s#%s' % (self.base_url, node['refid']) |
|
90 |
self.body.append(self.starttag({}, 'a', '', **atts)) |
|
91 |
self.context.append('</a></h%s>\n' % ( |
|
92 |
default_level+self.section_level-1)) |
|
93 |
||
94 |
def visit_subtitle(self, node): |
|
95 |
"""format a subtitle""" |
|
96 |
if isinstance(node.parent, nodes.sidebar): |
|
97 |
self.body.append(self.starttag(node, 'p', '', |
|
98 |
CLASS='sidebar-subtitle')) |
|
99 |
self.context.append('</p>\n') |
|
100 |
else: |
|
101 |
self.body.append( |
|
102 |
self.starttag(node, 'h%s' % (default_level+1), '', |
|
103 |
CLASS='subtitle')) |
|
104 |
self.context.append('</h%s>\n' % (default_level+1)) |
|
105 |
||
106 |
def visit_document(self, node): |
|
107 |
"""syt: i don't want the enclosing <div class="document">""" |
|
108 |
def depart_document(self, node): |
|
109 |
"""syt: i don't want the enclosing <div class="document">""" |
|
110 |
||
111 |
def visit_reference(self, node): |
|
112 |
"""syt: i want absolute urls""" |
|
113 |
if node.has_key('refuri'): |
|
114 |
href = node['refuri'] |
|
115 |
if ( self.settings.cloak_email_addresses |
|
116 |
and href.startswith('mailto:')): |
|
117 |
href = self.cloak_mailto(href) |
|
118 |
self.in_mailto = 1 |
|
119 |
else: |
|
120 |
assert node.has_key('refid'), \ |
|
121 |
'References must have "refuri" or "refid" attribute.' |
|
122 |
href = '%s#%s' % (self.base_url, node['refid']) |
|
123 |
atts = {'href': href, 'class': 'reference'} |
|
124 |
if not isinstance(node.parent, nodes.TextElement): |
|
125 |
assert len(node) == 1 and isinstance(node[0], nodes.image) |
|
126 |
atts['class'] += ' image-reference' |
|
127 |
self.body.append(self.starttag(node, 'a', '', **atts)) |
|
128 |
||
129 |
## override error messages to avoid XHTML problems ######################## |
|
130 |
def visit_problematic(self, node): |
|
131 |
pass |
|
132 |
||
133 |
def depart_problematic(self, node): |
|
134 |
pass |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
704
diff
changeset
|
135 |
|
0 | 136 |
def visit_system_message(self, node): |
137 |
backref_text = '' |
|
138 |
if len(node['backrefs']): |
|
139 |
backrefs = node['backrefs'] |
|
140 |
if len(backrefs) == 1: |
|
141 |
backref_text = '; <em>backlink</em>' |
|
142 |
else: |
|
143 |
i = 1 |
|
144 |
backlinks = [] |
|
145 |
for backref in backrefs: |
|
146 |
backlinks.append(str(i)) |
|
147 |
i += 1 |
|
148 |
backref_text = ('; <em>backlinks: %s</em>' |
|
149 |
% ', '.join(backlinks)) |
|
150 |
if node.hasattr('line'): |
|
151 |
line = ', line %s' % node['line'] |
|
152 |
else: |
|
153 |
line = '' |
|
154 |
a_start = a_end = '' |
|
155 |
error = u'System Message: %s%s/%s%s (%s %s)%s</p>\n' % ( |
|
156 |
a_start, node['type'], node['level'], a_end, |
|
157 |
self.encode(node['source']), line, backref_text) |
|
2312
af4d8f75c5db
use xml_escape
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
158 |
self.body.append(u'<div class="system-message"><b>ReST / HTML errors:</b>%s</div>' % xml_escape(error)) |
0 | 159 |
|
160 |
def depart_system_message(self, node): |
|
161 |
pass |