author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Mon, 19 Oct 2009 18:15:45 +0200 | |
changeset 3735 | 80fb4083c29b |
parent 3733 | c3feb6a33f58 |
child 3890 | d7a270f50f54 |
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'] |
3733
c3feb6a33f58
remove some warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3655
diff
changeset
|
94 |
vreg = self._cw.vreg |
c3feb6a33f58
remove some warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3655
diff
changeset
|
95 |
for lang in chain((self._cw.lang, vreg.property_value('ui.language')), |
c3feb6a33f58
remove some warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3655
diff
changeset
|
96 |
vreg.config.available_languages()): |
0 | 97 |
rid = '%s_%s.rst' % (fid, lang) |
3733
c3feb6a33f58
remove some warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3655
diff
changeset
|
98 |
resourcedir = vreg.config.locate_doc_file(rid) |
0 | 99 |
if resourcedir: |
100 |
break |
|
101 |
else: |
|
102 |
raise NotFound |
|
3733
c3feb6a33f58
remove some warnings
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3655
diff
changeset
|
103 |
self.tocindex = build_toc(vreg.config) |
0 | 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>') |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
111 |
self.w(u'<h1>%s</h1>' % (title_for_lang(node, self._cw.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): |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
120 |
req = self._cw |
0 | 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) |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
142 |
self.w(u'%s : ' % self._cw._(msgid)) |
0 | 143 |
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
|
144 |
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
|
145 |
title_for_lang(node, self._cw.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>' % ( |
|
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
157 |
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
|
158 |
title_for_lang(child, self._cw.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): |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
166 |
__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
|
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): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
173 |
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
|
174 |
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
|
175 |
self._cw.config.available_languages()): |
0 | 176 |
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
|
177 |
resourcedir = self._cw.config.locate_doc_file(rid) |
0 | 178 |
if resourcedir: |
179 |
break |
|
180 |
else: |
|
181 |
raise NotFound |
|
182 |
self.w(open(join(resourcedir, rid)).read()) |
|
183 |
||
184 |
||
185 |
class ChangeLogView(StartupView): |
|
3377
dd9d292b6a6d
use __regid__ instead of id on appobject classes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
186 |
__regid__ = 'changelog' |
0 | 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): |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
191 |
rid = 'ChangeLog_%s' % (self._cw.lang) |
0 | 192 |
allentries = [] |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
193 |
title = self._cw._(self.title) |
0 | 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() |
3451
6b46d73823f5
[api] work in progress, use __regid__, cw_*, etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3377
diff
changeset
|
197 |
for fpath in self._cw.config.locate_all_files(rid): |
0 | 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: |
|
3460
e4843535db25
[api] some more _cw / __regid__, automatic tests now pass again
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
3451
diff
changeset
|
227 |
fdate = self._cw.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))) |
|
3495
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 |
|
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
240 |
class ChangeLogAction(action.Action): |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
241 |
__regid__ = 'changelog' |
3495
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
242 |
__select__ = yes() |
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
243 |
|
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
244 |
category = 'footer' |
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
245 |
order = 1 |
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
246 |
title = ChangeLogView.title |
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
247 |
|
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
248 |
def url(self): |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
249 |
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
|
250 |
|
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
251 |
|
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
252 |
class AboutAction(action.Action): |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
253 |
__regid__ = 'about' |
3495
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
254 |
__select__ = yes() |
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
255 |
|
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
256 |
category = 'footer' |
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
257 |
order = 2 |
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
258 |
title = _('about this site') |
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
259 |
|
438576c5b1d1
[ui] turn footer items into actions
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
260 |
def url(self): |
3655
af86ab65a282
3.6 updates
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3503
diff
changeset
|
261 |
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
|
262 |