author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 13 Oct 2009 16:00:09 +0200 | |
changeset 3658 | d8f2ec7e91fa |
parent 3655 | af86ab65a282 |
child 3733 | c3feb6a33f58 |
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 |
||
3495
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
18 |
from cubicweb.selectors import match_form_params, yes |
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 |
3495
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
22 |
from cubicweb.web import NotFound, action |
0 | 23 |
_ = unicode |
24 |
||
25 |
# table of content management ################################################# |
|
26 |
||
27 |
try: |
|
28 |
from xml.etree.ElementTree import parse |
|
29 |
except ImportError: |
|
30 |
from elementtree.ElementTree import parse |
|
31 |
||
32 |
def build_toc_index(node, index): |
|
33 |
try: |
|
34 |
nodeidx = node.attrib['resource'] |
|
35 |
assert not nodeidx in index, nodeidx |
|
36 |
index[nodeidx] = node |
|
37 |
except KeyError: |
|
38 |
pass |
|
39 |
for child in node: |
|
40 |
build_toc_index(child, index) |
|
41 |
child.parent = node |
|
42 |
||
43 |
def get_insertion_point(section, index): |
|
44 |
if section.attrib.get('insertafter'): |
|
45 |
snode = index[section.attrib['insertafter']] |
|
46 |
node = snode.parent |
|
47 |
idx = node.getchildren().index(snode) + 1 |
|
48 |
elif section.attrib.get('insertbefore'): |
|
49 |
snode = index[section.attrib['insertbefore']] |
|
50 |
node = snode.parent |
|
51 |
idx = node.getchildren().index(snode) |
|
52 |
else: |
|
53 |
node = index[section.attrib['appendto']] |
|
54 |
idx = None |
|
55 |
return node, idx |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
56 |
|
0 | 57 |
def build_toc(config): |
58 |
alltocfiles = reversed(tuple(config.locate_all_files('toc.xml'))) |
|
59 |
maintoc = parse(alltocfiles.next()).getroot() |
|
60 |
maintoc.parent = None |
|
61 |
index = {} |
|
62 |
build_toc_index(maintoc, index) |
|
63 |
# 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
|
64 |
# file |
0 | 65 |
for fpath in alltocfiles: |
66 |
toc = parse(fpath).getroot() |
|
67 |
for section in toc: |
|
68 |
node, idx = get_insertion_point(section, index) |
|
69 |
if idx is None: |
|
70 |
node.append(section) |
|
71 |
else: |
|
72 |
node.insert(idx, section) |
|
73 |
section.parent = node |
|
74 |
build_toc_index(section, index) |
|
75 |
return index |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
76 |
|
1149 | 77 |
def title_for_lang(node, lang): |
0 | 78 |
for title in node.findall('title'): |
79 |
if title.attrib['{http://www.w3.org/XML/1998/namespace}lang'] == lang: |
|
80 |
return unicode(title.text) |
|
81 |
||
82 |
def subsections(node): |
|
83 |
return [child for child in node if child.tag == 'section'] |
|
84 |
||
85 |
# help views ################################################################## |
|
86 |
||
87 |
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
|
88 |
__select__ = match_form_params('fid') |
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
89 |
__regid__ = 'wdoc' |
0 | 90 |
title = _('site documentation') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
91 |
|
0 | 92 |
def call(self): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
93 |
fid = self._cw.form['fid'] |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
94 |
for lang in chain((self._cw.lang, self._cw.vreg.property_value('ui.language')), |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
95 |
self._cw.config.available_languages()): |
0 | 96 |
rid = '%s_%s.rst' % (fid, lang) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
97 |
resourcedir = self._cw.config.locate_doc_file(rid) |
0 | 98 |
if resourcedir: |
99 |
break |
|
100 |
else: |
|
101 |
raise NotFound |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
102 |
self.tocindex = build_toc(self._cw.config) |
0 | 103 |
try: |
104 |
node = self.tocindex[fid] |
|
105 |
except KeyError: |
|
106 |
node = None |
|
107 |
else: |
|
108 |
self.navigation_links(node) |
|
109 |
self.w(u'<div class="hr"></div>') |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
110 |
self.w(u'<h1>%s</h1>' % (title_for_lang(node, self._cw.lang))) |
0 | 111 |
data = open(join(resourcedir, rid)).read() |
112 |
self.w(rest_publish(self, data)) |
|
113 |
if node is not None: |
|
114 |
self.subsections_links(node) |
|
115 |
self.w(u'<div class="hr"></div>') |
|
116 |
self.navigation_links(node) |
|
117 |
||
118 |
def navigation_links(self, node): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
119 |
req = self._cw |
0 | 120 |
parent = node.parent |
121 |
if parent is None: |
|
122 |
return |
|
123 |
brothers = subsections(parent) |
|
124 |
self.w(u'<div class="docnav">\n') |
|
125 |
previousidx = brothers.index(node) - 1 |
|
126 |
if previousidx >= 0: |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
127 |
self.navsection(brothers[previousidx], 'prev') |
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
128 |
self.navsection(parent, 'up') |
0 | 129 |
nextidx = brothers.index(node) + 1 |
130 |
if nextidx < len(brothers): |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
131 |
self.navsection(brothers[nextidx], 'next') |
0 | 132 |
self.w(u'</div>\n') |
133 |
||
134 |
navinfo = {'prev': ('', 'data/previous.png', _('i18nprevnext_previous')), |
|
135 |
'next': ('', 'data/next.png', _('i18nprevnext_next')), |
|
136 |
'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
|
137 |
|
0 | 138 |
def navsection(self, node, navtype): |
139 |
htmlclass, imgpath, msgid = self.navinfo[navtype] |
|
140 |
self.w(u'<span class="%s">' % htmlclass) |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
141 |
self.w(u'%s : ' % self._cw._(msgid)) |
0 | 142 |
self.w(u'<a href="%s">%s</a>' % ( |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
143 |
self._cw.build_url('doc/'+node.attrib['resource']), |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
144 |
title_for_lang(node, self._cw.lang))) |
0 | 145 |
self.w(u'</span>\n') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
146 |
|
0 | 147 |
def subsections_links(self, node, first=True): |
148 |
sub = subsections(node) |
|
149 |
if not sub: |
|
150 |
return |
|
151 |
if first: |
|
152 |
self.w(u'<div class="hr"></div>') |
|
153 |
self.w(u'<ul class="docsum">') |
|
154 |
for child in sub: |
|
155 |
self.w(u'<li><a href="%s">%s</a>' % ( |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
156 |
self._cw.build_url('doc/'+child.attrib['resource']), |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
157 |
title_for_lang(child, self._cw.lang))) |
0 | 158 |
self.subsections_links(child, False) |
159 |
self.w(u'</li>') |
|
160 |
self.w(u'</ul>\n') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
161 |
|
0 | 162 |
|
163 |
||
164 |
class InlineHelpImageView(StartupView): |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
165 |
__regid__ = 'wdocimages' |
731
ac4a94e50b60
some more s/__selectors__/__select__ but still more to come
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
692
diff
changeset
|
166 |
__select__ = match_form_params('fid') |
0 | 167 |
binary = True |
168 |
templatable = False |
|
169 |
content_type = 'image/png' |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
170 |
|
0 | 171 |
def call(self): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
172 |
fid = self._cw.form['fid'] |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
173 |
for lang in chain((self._cw.lang, self._cw.vreg.property_value('ui.language')), |
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
174 |
self._cw.config.available_languages()): |
0 | 175 |
rid = join('images', '%s_%s.png' % (fid, lang)) |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
176 |
resourcedir = self._cw.config.locate_doc_file(rid) |
0 | 177 |
if resourcedir: |
178 |
break |
|
179 |
else: |
|
180 |
raise NotFound |
|
181 |
self.w(open(join(resourcedir, rid)).read()) |
|
182 |
||
183 |
||
184 |
class ChangeLogView(StartupView): |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
185 |
__regid__ = 'changelog' |
0 | 186 |
title = _('What\'s new?') |
187 |
maxentries = 25 |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
188 |
|
0 | 189 |
def call(self): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
190 |
rid = 'ChangeLog_%s' % (self._cw.lang) |
0 | 191 |
allentries = [] |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
192 |
title = self._cw._(self.title) |
0 | 193 |
restdata = ['.. -*- coding: utf-8 -*-', '', title, '='*len(title), ''] |
194 |
w = restdata.append |
|
1364
3acc823121b6
can't compare date and datetime
sylvain.thenault@logilab.fr
parents:
1149
diff
changeset
|
195 |
today = date.today() |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
196 |
for fpath in self._cw.config.locate_all_files(rid): |
0 | 197 |
cl = ChangeLog(fpath) |
198 |
encoding = 'utf-8' |
|
199 |
# additional content may be found in title |
|
200 |
for line in (cl.title + cl.additional_content).splitlines(): |
|
201 |
m = CHARSET_DECL_RGX.search(line) |
|
202 |
if m is not None: |
|
203 |
encoding = m.group(1) |
|
204 |
continue |
|
205 |
elif line.startswith('.. '): |
|
206 |
w(unicode(line, encoding)) |
|
207 |
for entry in cl.entries: |
|
208 |
if entry.date: |
|
1364
3acc823121b6
can't compare date and datetime
sylvain.thenault@logilab.fr
parents:
1149
diff
changeset
|
209 |
edate = todate(strptime(entry.date, '%Y-%m-%d')) |
0 | 210 |
else: |
1364
3acc823121b6
can't compare date and datetime
sylvain.thenault@logilab.fr
parents:
1149
diff
changeset
|
211 |
edate = today |
0 | 212 |
messages = [] |
213 |
for msglines, submsgs in entry.messages: |
|
214 |
msgstr = unicode(' '.join(l.strip() for l in msglines), encoding) |
|
215 |
msgstr += u'\n\n' |
|
216 |
for submsglines in submsgs: |
|
217 |
msgstr += ' - ' + unicode(' '.join(l.strip() for l in submsglines), encoding) |
|
218 |
msgstr += u'\n' |
|
219 |
messages.append(msgstr) |
|
1132 | 220 |
entry = (edate, messages) |
0 | 221 |
allentries.insert(bisect_right(allentries, entry), entry) |
222 |
latestdate = None |
|
223 |
i = 0 |
|
1132 | 224 |
for edate, messages in reversed(allentries): |
225 |
if latestdate != edate: |
|
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
226 |
fdate = self._cw.format_date(edate) |
0 | 227 |
w(u'\n%s' % fdate) |
1132 | 228 |
w('~' * len(fdate)) |
1364
3acc823121b6
can't compare date and datetime
sylvain.thenault@logilab.fr
parents:
1149
diff
changeset
|
229 |
latestdate = edate |
0 | 230 |
for msg in messages: |
231 |
w(u'* %s' % msg) |
|
232 |
i += 1 |
|
233 |
if i > self.maxentries: |
|
234 |
break |
|
235 |
w('') # blank line |
|
236 |
self.w(rest_publish(self, '\n'.join(restdata))) |
|
3495
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
237 |
|
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
238 |
|
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
239 |
class ChangeLogAction(action.Action): |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
240 |
__regid__ = 'changelog' |
3495
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
241 |
__select__ = yes() |
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
242 |
|
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
243 |
category = 'footer' |
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
244 |
order = 1 |
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
245 |
title = ChangeLogView.title |
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
246 |
|
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
247 |
def url(self): |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
248 |
return self._cw.build_url('changelog') |
3495
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
249 |
|
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
250 |
|
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
251 |
class AboutAction(action.Action): |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
252 |
__regid__ = 'about' |
3495
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
253 |
__select__ = yes() |
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
254 |
|
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
255 |
category = 'footer' |
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
256 |
order = 2 |
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
257 |
title = _('about this site') |
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
258 |
|
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
259 |
def url(self): |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
260 |
return self._cw.build_url('doc/about') |
3495
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
261 |