author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 18 Jun 2009 09:38:32 +0200 | |
branch | stable |
changeset 2115 | c48ff1d0acc4 |
parent 1977 | 606923dff11b |
child 3377 | dd9d292b6a6d |
child 3495 | 438576c5b1d1 |
permissions | -rw-r--r-- |
0 | 1 |
"""inline help system, using ReST file in products `wdoc` directory |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
4 |
:copyright: 2008-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1802
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
from itertools import chain |
|
11 |
from os.path import join |
|
12 |
from bisect import bisect_right |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
984
diff
changeset
|
13 |
from datetime import date |
0 | 14 |
|
15 |
from logilab.common.changelog import ChangeLog |
|
16 |
from logilab.mtconverter import CHARSET_DECL_RGX |
|
17 |
||
692
800592b8d39b
replace deprecated cubicweb.common.selectors by its new module path (cubicweb.selectors)
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
431
diff
changeset
|
18 |
from cubicweb.selectors import match_form_params |
984 | 19 |
from cubicweb.view import StartupView |
1364
3acc823121b6
can't compare date and datetime
sylvain.thenault@logilab.fr
parents:
1149
diff
changeset
|
20 |
from cubicweb.utils import strptime, todate |
0 | 21 |
from cubicweb.common.uilib import rest_publish |
22 |
from cubicweb.web import NotFound |
|
23 |
||
24 |
_ = unicode |
|
25 |
||
26 |
# table of content management ################################################# |
|
27 |
||
28 |
try: |
|
29 |
from xml.etree.ElementTree import parse |
|
30 |
except ImportError: |
|
31 |
from elementtree.ElementTree import parse |
|
32 |
||
33 |
def build_toc_index(node, index): |
|
34 |
try: |
|
35 |
nodeidx = node.attrib['resource'] |
|
36 |
assert not nodeidx in index, nodeidx |
|
37 |
index[nodeidx] = node |
|
38 |
except KeyError: |
|
39 |
pass |
|
40 |
for child in node: |
|
41 |
build_toc_index(child, index) |
|
42 |
child.parent = node |
|
43 |
||
44 |
def get_insertion_point(section, index): |
|
45 |
if section.attrib.get('insertafter'): |
|
46 |
snode = index[section.attrib['insertafter']] |
|
47 |
node = snode.parent |
|
48 |
idx = node.getchildren().index(snode) + 1 |
|
49 |
elif section.attrib.get('insertbefore'): |
|
50 |
snode = index[section.attrib['insertbefore']] |
|
51 |
node = snode.parent |
|
52 |
idx = node.getchildren().index(snode) |
|
53 |
else: |
|
54 |
node = index[section.attrib['appendto']] |
|
55 |
idx = None |
|
56 |
return node, idx |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
57 |
|
0 | 58 |
def build_toc(config): |
59 |
alltocfiles = reversed(tuple(config.locate_all_files('toc.xml'))) |
|
60 |
maintoc = parse(alltocfiles.next()).getroot() |
|
61 |
maintoc.parent = None |
|
62 |
index = {} |
|
63 |
build_toc_index(maintoc, index) |
|
64 |
# insert component documentation into the tree according to their toc.xml |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
65 |
# file |
0 | 66 |
for fpath in alltocfiles: |
67 |
toc = parse(fpath).getroot() |
|
68 |
for section in toc: |
|
69 |
node, idx = get_insertion_point(section, index) |
|
70 |
if idx is None: |
|
71 |
node.append(section) |
|
72 |
else: |
|
73 |
node.insert(idx, section) |
|
74 |
section.parent = node |
|
75 |
build_toc_index(section, index) |
|
76 |
return index |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
77 |
|
1149 | 78 |
def title_for_lang(node, lang): |
0 | 79 |
for title in node.findall('title'): |
80 |
if title.attrib['{http://www.w3.org/XML/1998/namespace}lang'] == lang: |
|
81 |
return unicode(title.text) |
|
82 |
||
83 |
def subsections(node): |
|
84 |
return [child for child in node if child.tag == 'section'] |
|
85 |
||
86 |
# help views ################################################################## |
|
87 |
||
88 |
class InlineHelpView(StartupView): |
|
731
ac4a94e50b60
some more s/__selectors__/__select__ but still more to come
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
89 |
__select__ = match_form_params('fid') |
0 | 90 |
id = 'wdoc' |
91 |
title = _('site documentation') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
92 |
|
0 | 93 |
def call(self): |
94 |
fid = self.req.form['fid'] |
|
95 |
for lang in chain((self.req.lang, self.vreg.property_value('ui.language')), |
|
96 |
self.config.available_languages()): |
|
97 |
rid = '%s_%s.rst' % (fid, lang) |
|
98 |
resourcedir = self.config.locate_doc_file(rid) |
|
99 |
if resourcedir: |
|
100 |
break |
|
101 |
else: |
|
102 |
raise NotFound |
|
103 |
self.tocindex = build_toc(self.config) |
|
104 |
try: |
|
105 |
node = self.tocindex[fid] |
|
106 |
except KeyError: |
|
107 |
node = None |
|
108 |
else: |
|
109 |
self.navigation_links(node) |
|
110 |
self.w(u'<div class="hr"></div>') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
111 |
self.w(u'<h1>%s</h1>' % (title_for_lang(node, self.req.lang))) |
0 | 112 |
data = open(join(resourcedir, rid)).read() |
113 |
self.w(rest_publish(self, data)) |
|
114 |
if node is not None: |
|
115 |
self.subsections_links(node) |
|
116 |
self.w(u'<div class="hr"></div>') |
|
117 |
self.navigation_links(node) |
|
118 |
||
119 |
def navigation_links(self, node): |
|
120 |
req = self.req |
|
121 |
parent = node.parent |
|
122 |
if parent is None: |
|
123 |
return |
|
124 |
brothers = subsections(parent) |
|
125 |
self.w(u'<div class="docnav">\n') |
|
126 |
previousidx = brothers.index(node) - 1 |
|
127 |
if previousidx >= 0: |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
128 |
self.navsection(brothers[previousidx], 'prev') |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
129 |
self.navsection(parent, 'up') |
0 | 130 |
nextidx = brothers.index(node) + 1 |
131 |
if nextidx < len(brothers): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
132 |
self.navsection(brothers[nextidx], 'next') |
0 | 133 |
self.w(u'</div>\n') |
134 |
||
135 |
navinfo = {'prev': ('', 'data/previous.png', _('i18nprevnext_previous')), |
|
136 |
'next': ('', 'data/next.png', _('i18nprevnext_next')), |
|
137 |
'up': ('', 'data/up.png', _('i18nprevnext_up'))} |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
138 |
|
0 | 139 |
def navsection(self, node, navtype): |
140 |
htmlclass, imgpath, msgid = self.navinfo[navtype] |
|
141 |
self.w(u'<span class="%s">' % htmlclass) |
|
142 |
self.w(u'%s : ' % self.req._(msgid)) |
|
143 |
self.w(u'<a href="%s">%s</a>' % ( |
|
144 |
self.req.build_url('doc/'+node.attrib['resource']), |
|
1149 | 145 |
title_for_lang(node, self.req.lang))) |
0 | 146 |
self.w(u'</span>\n') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
147 |
|
0 | 148 |
def subsections_links(self, node, first=True): |
149 |
sub = subsections(node) |
|
150 |
if not sub: |
|
151 |
return |
|
152 |
if first: |
|
153 |
self.w(u'<div class="hr"></div>') |
|
154 |
self.w(u'<ul class="docsum">') |
|
155 |
for child in sub: |
|
156 |
self.w(u'<li><a href="%s">%s</a>' % ( |
|
157 |
self.req.build_url('doc/'+child.attrib['resource']), |
|
1149 | 158 |
title_for_lang(child, self.req.lang))) |
0 | 159 |
self.subsections_links(child, False) |
160 |
self.w(u'</li>') |
|
161 |
self.w(u'</ul>\n') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
162 |
|
0 | 163 |
|
164 |
||
165 |
class InlineHelpImageView(StartupView): |
|
166 |
id = 'wdocimages' |
|
731
ac4a94e50b60
some more s/__selectors__/__select__ but still more to come
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
167 |
__select__ = match_form_params('fid') |
0 | 168 |
binary = True |
169 |
templatable = False |
|
170 |
content_type = 'image/png' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
171 |
|
0 | 172 |
def call(self): |
173 |
fid = self.req.form['fid'] |
|
174 |
for lang in chain((self.req.lang, self.vreg.property_value('ui.language')), |
|
175 |
self.config.available_languages()): |
|
176 |
rid = join('images', '%s_%s.png' % (fid, lang)) |
|
177 |
resourcedir = self.config.locate_doc_file(rid) |
|
178 |
if resourcedir: |
|
179 |
break |
|
180 |
else: |
|
181 |
raise NotFound |
|
182 |
self.w(open(join(resourcedir, rid)).read()) |
|
183 |
||
184 |
||
185 |
class ChangeLogView(StartupView): |
|
186 |
id = 'changelog' |
|
187 |
title = _('What\'s new?') |
|
188 |
maxentries = 25 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
189 |
|
0 | 190 |
def call(self): |
191 |
rid = 'ChangeLog_%s' % (self.req.lang) |
|
192 |
allentries = [] |
|
193 |
title = self.req._(self.title) |
|
194 |
restdata = ['.. -*- coding: utf-8 -*-', '', title, '='*len(title), ''] |
|
195 |
w = restdata.append |
|
1364
3acc823121b6
can't compare date and datetime
sylvain.thenault@logilab.fr
parents:
1149
diff
changeset
|
196 |
today = date.today() |
0 | 197 |
for fpath in self.config.locate_all_files(rid): |
198 |
cl = ChangeLog(fpath) |
|
199 |
encoding = 'utf-8' |
|
200 |
# additional content may be found in title |
|
201 |
for line in (cl.title + cl.additional_content).splitlines(): |
|
202 |
m = CHARSET_DECL_RGX.search(line) |
|
203 |
if m is not None: |
|
204 |
encoding = m.group(1) |
|
205 |
continue |
|
206 |
elif line.startswith('.. '): |
|
207 |
w(unicode(line, encoding)) |
|
208 |
for entry in cl.entries: |
|
209 |
if entry.date: |
|
1364
3acc823121b6
can't compare date and datetime
sylvain.thenault@logilab.fr
parents:
1149
diff
changeset
|
210 |
edate = todate(strptime(entry.date, '%Y-%m-%d')) |
0 | 211 |
else: |
1364
3acc823121b6
can't compare date and datetime
sylvain.thenault@logilab.fr
parents:
1149
diff
changeset
|
212 |
edate = today |
0 | 213 |
messages = [] |
214 |
for msglines, submsgs in entry.messages: |
|
215 |
msgstr = unicode(' '.join(l.strip() for l in msglines), encoding) |
|
216 |
msgstr += u'\n\n' |
|
217 |
for submsglines in submsgs: |
|
218 |
msgstr += ' - ' + unicode(' '.join(l.strip() for l in submsglines), encoding) |
|
219 |
msgstr += u'\n' |
|
220 |
messages.append(msgstr) |
|
1132 | 221 |
entry = (edate, messages) |
0 | 222 |
allentries.insert(bisect_right(allentries, entry), entry) |
223 |
latestdate = None |
|
224 |
i = 0 |
|
1132 | 225 |
for edate, messages in reversed(allentries): |
226 |
if latestdate != edate: |
|
227 |
fdate = self.format_date(edate) |
|
0 | 228 |
w(u'\n%s' % fdate) |
1132 | 229 |
w('~' * len(fdate)) |
1364
3acc823121b6
can't compare date and datetime
sylvain.thenault@logilab.fr
parents:
1149
diff
changeset
|
230 |
latestdate = edate |
0 | 231 |
for msg in messages: |
232 |
w(u'* %s' % msg) |
|
233 |
i += 1 |
|
234 |
if i > self.maxentries: |
|
235 |
break |
|
236 |
w('') # blank line |
|
237 |
self.w(rest_publish(self, '\n'.join(restdata))) |