author | Adrien Di Mascio <Adrien.DiMascio@logilab.fr> |
Thu, 14 May 2009 11:38:40 +0200 | |
branch | tls-sprint |
changeset 1802 | d628defebc17 |
parent 1364 | 3acc823121b6 |
child 1977 | 606923dff11b |
permissions | -rw-r--r-- |
0 | 1 |
"""inline help system, using ReST file in products `wdoc` directory |
2 |
||
3 |
:organization: Logilab |
|
984 | 4 |
:copyright: 2008-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
6 |
""" |
|
7 |
__docformat__ = "restructuredtext en" |
|
8 |
||
9 |
from itertools import chain |
|
10 |
from os.path import join |
|
11 |
from bisect import bisect_right |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
984
diff
changeset
|
12 |
from datetime import date |
0 | 13 |
|
14 |
from logilab.common.changelog import ChangeLog |
|
15 |
from logilab.mtconverter import CHARSET_DECL_RGX |
|
16 |
||
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
|
17 |
from cubicweb.selectors import match_form_params |
984 | 18 |
from cubicweb.view import StartupView |
1364
3acc823121b6
can't compare date and datetime
sylvain.thenault@logilab.fr
parents:
1149
diff
changeset
|
19 |
from cubicweb.utils import strptime, todate |
0 | 20 |
from cubicweb.common.uilib import rest_publish |
21 |
from cubicweb.web import NotFound |
|
22 |
||
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') |
0 | 89 |
id = 'wdoc' |
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): |
93 |
fid = self.req.form['fid'] |
|
94 |
for lang in chain((self.req.lang, self.vreg.property_value('ui.language')), |
|
95 |
self.config.available_languages()): |
|
96 |
rid = '%s_%s.rst' % (fid, lang) |
|
97 |
resourcedir = self.config.locate_doc_file(rid) |
|
98 |
if resourcedir: |
|
99 |
break |
|
100 |
else: |
|
101 |
raise NotFound |
|
102 |
self.tocindex = build_toc(self.config) |
|
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>') |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1364
diff
changeset
|
110 |
self.w(u'<h1>%s</h1>' % (title_for_lang(node, self.req.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): |
|
119 |
req = self.req |
|
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) |
|
141 |
self.w(u'%s : ' % self.req._(msgid)) |
|
142 |
self.w(u'<a href="%s">%s</a>' % ( |
|
143 |
self.req.build_url('doc/'+node.attrib['resource']), |
|
1149 | 144 |
title_for_lang(node, self.req.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>' % ( |
|
156 |
self.req.build_url('doc/'+child.attrib['resource']), |
|
1149 | 157 |
title_for_lang(child, self.req.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): |
|
165 |
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
|
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): |
172 |
fid = self.req.form['fid'] |
|
173 |
for lang in chain((self.req.lang, self.vreg.property_value('ui.language')), |
|
174 |
self.config.available_languages()): |
|
175 |
rid = join('images', '%s_%s.png' % (fid, lang)) |
|
176 |
resourcedir = self.config.locate_doc_file(rid) |
|
177 |
if resourcedir: |
|
178 |
break |
|
179 |
else: |
|
180 |
raise NotFound |
|
181 |
self.w(open(join(resourcedir, rid)).read()) |
|
182 |
||
183 |
||
184 |
class ChangeLogView(StartupView): |
|
185 |
id = 'changelog' |
|
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): |
190 |
rid = 'ChangeLog_%s' % (self.req.lang) |
|
191 |
allentries = [] |
|
192 |
title = self.req._(self.title) |
|
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() |
0 | 196 |
for fpath in self.config.locate_all_files(rid): |
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: |
|
226 |
fdate = self.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))) |