-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpage.py
384 lines (325 loc) · 13 KB
/
page.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
# -*- coding: UTF-8 -*-
# Copyright (C) 2007 Sylvain Taverne <[email protected]>
# Copyright (C) 2007-2008 Henry Obein <[email protected]>
# Copyright (C) 2007-2008 Juan David Ibáñez Palomar <[email protected]>
# Copyright (C) 2007-2008, 2010 Hervé Cauwelier <[email protected]>
# Copyright (C) 2008 Gautier Hayoun <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import from the Standard Library
from re import compile
from urllib import urlencode
# Import from docutils
from docutils import nodes
from docutils.core import publish_doctree
from docutils.languages.en import labels
from docutils.readers import get_reader_class
from docutils.parsers.rst import directives, Directive
from docutils.parsers.rst.directives import register_directive
from docutils.utils import SystemMessage
# Import from itools
from itools.gettext import MSG
from itools.handlers import checkid
from itools.uri import get_reference
from itools.web import get_context
# Import from ikaaro
from ikaaro.text import Text
from ikaaro.resource_ import DBResource
from page_views import WikiPage_View, WikiPage_Edit, WikiPage_Help
from page_views import WikiPage_ToPDF, WikiPage_ToODT, WikiPage_HelpODT
from page_views import is_external, BacklinksMenu
StandaloneReader = get_reader_class('standalone')
def language(argument):
try:
return argument.encode()
except UnicodeEncodeError:
raise ValueError('expected "xx-YY" language-COUNTRY code')
def yesno(argument):
return directives.choice(argument, ('yes', 'no'))
# Class name gives the DOM element name
class book(nodes.Admonition, nodes.Element):
pass
class Book(Directive):
required_arguments = 0
optional_arguments = 1
final_argument_whitespace = True
option_spec = {
'cover': directives.uri,
'template': directives.unchanged,
'ignore-missing-pages': yesno,
'toc-depth': directives.positive_int,
'title': directives.unchanged,
'comments': directives.unchanged,
'subject': directives.unchanged,
'language': language,
'keywords': directives.unchanged,
'filename': directives.unchanged}
has_content = True
def run(self):
self.assert_has_content()
# Default values
options = self.options
for option in ('title', 'comments', 'subject', 'keywords'):
if options.get(option) is None:
options[option] = u""
if options.get('language') is None:
# The website language, not the content language
# because the wiki is not multilingual anyway
context = get_context()
languages = context.site_root.get_property('website_languages')
language = context.accept_language.select_language(languages)
options['language'] = language
# Cover page
if self.arguments:
# Push cover as an option
cover_uri = checkid(self.arguments[0][1:-2])
options['cover'] = directives.uri(cover_uri)
# Metadata
metadata = ['Book:']
for key in ('toc-depth', 'ignore-missing-pages', 'title', 'comments',
'subject', 'keywords', 'language', 'filename'):
value = options.get(key)
if not value:
continue
metadata.append(' %s: %s' % (key, value))
template = options.get('template')
if template is not None:
metadata.append(' template: ')
meta_node = nodes.literal_block('Book Metadata',
'\n'.join(metadata))
meta_node.append(nodes.reference(refuri=template, text=template,
name=template,
wiki_template=True))
else:
meta_node = nodes.literal_block('Book Metadata',
'\n'.join(metadata))
book_node = book(self.block_text, **options)
if self.arguments:
# Display the cover
cover_text = self.arguments.pop(0)
textnodes, messages = self.state.inline_text(cover_text,
self.lineno)
book_node += nodes.title(cover_text, '', *textnodes)
book_node += messages
# Parse inner list
self.state.nested_parse(self.content, self.content_offset, book_node)
# Automatically number pages
for bullet_list in book_node.traverse(condition=nodes.bullet_list):
bullet_list.__class__ = nodes.enumerated_list
bullet_list.tagname = 'enumerated_list'
return [meta_node, book_node]
class WikiPage(Text):
class_id = 'WikiPage'
class_version = '20090123'
class_title = MSG(u"Wiki Page")
class_description = MSG(u"Wiki contents")
class_icon16 = '/ui/wiki/WikiPage16.png'
class_icon48 = '/ui/wiki/WikiPage48.png'
class_views = ['view', 'edit', 'externaledit', 'backlinks', 'commit_log',
'help', 'to_odt', 'help_odt']
overrides = {
# Security
'file_insertion_enabled': 0,
'raw_enabled': 0,
# Encodings
'input_encoding': 'utf-8',
'output_encoding': 'utf-8',
}
# Views
new_instance = DBResource.new_instance
view = WikiPage_View
to_pdf = WikiPage_ToPDF
edit = WikiPage_Edit
to_odt = WikiPage_ToODT
help = WikiPage_Help
help_odt = WikiPage_HelpODT
def get_text(self):
handler = self.get_value('data')
return handler.to_str() if handler else ''
#######################################################################
# Ikaaro API
#######################################################################
def get_context_menus(self):
return [BacklinksMenu()] + self.parent.get_context_menus()
def get_links(self):
base = self.abspath
try:
doctree = self.get_doctree()
except SystemMessage:
# The doctree is in a incoherent state
return set()
# Links
links = set()
for node in doctree.traverse(condition=nodes.reference):
refname = node.get('wiki_name')
if refname is False:
# Wiki link not found
title = node['name']
path = checkid(title) or title
path = base.resolve(path)
elif refname:
# Wiki link found, "refname" is the path
path = base.resolve2(refname)
else:
# Regular link, include internal ones
refuri = node.get('refuri')
if refuri is None:
continue
reference = get_reference(refuri.encode('utf_8'))
# Skip external
if is_external(reference):
continue
path = base.resolve2(reference.path)
path = str(path)
links.add(path)
# Images
for node in doctree.traverse(condition=nodes.image):
reference = get_reference(node['uri'].encode('utf_8'))
# Skip external image
if is_external(reference):
continue
# Resolve the path
path = base.resolve(reference.path)
path = str(path)
links.add(path)
return links
def update_links(self, source, target,
links_re = compile(r'(\.\. .*?: )(\S*)')):
old_data = self.get_text()
new_data = []
not_uri = 0
base = self.parent.abspath
for segment in links_re.split(old_data):
not_uri = (not_uri + 1) % 3
if not not_uri:
reference = get_reference(segment)
# Skip external link
if is_external(reference):
new_data.append(segment)
continue
# Strip the view
path = reference.path
if path and path[-1] == ';download':
path = path[:-1]
view = '/;download'
else:
view = ''
# Resolve the path
path = base.resolve(path)
# Match ?
if path == source:
segment = str(base.get_pathto(target)) + view
new_data.append(segment)
new_data = ''.join(new_data)
self.get_value('data').load_state_from_string(new_data)
get_context().database.change_resource(self)
#######################################################################
# API
#######################################################################
def resolve_link(self, title):
parent = self.parent
# Try regular resource name or path
try:
name = str(title)
except UnicodeEncodeError:
pass
else:
resource = parent.get_resource(name, soft=True)
if resource is not None:
return resource
# Convert wiki name to resource name
name = checkid(title)
if name is None:
return None
return parent.get_resource(name, soft=True)
def set_new_resource_link(self, node):
node['classes'].append('nowiki')
prefix = self.get_pathto(self.parent)
title = node['name']
title_encoded = title.encode('utf_8')
if node.attributes.get('wiki_template'):
new_type = 'application/vnd.oasis.opendocument.text'
else:
new_type = self.__class__.__name__
params = {'type': new_type,
'title': title_encoded,
'name': checkid(title) or title_encoded}
refuri = "%s/;new_resource?%s" % (prefix,
urlencode(params))
node['refuri'] = refuri
def get_doctree(self):
parent = self.parent
# Override dandling links handling
class WikiReader(StandaloneReader):
supported = ('wiki',)
def wiki_reference_resolver(target):
title = target['name']
resource = self.resolve_link(title)
if resource is None:
# Not Found
target['wiki_name'] = False
else:
# Found
target['wiki_name'] = str(resource.abspath)
return True
wiki_reference_resolver.priority = 851
unknown_reference_resolvers = [wiki_reference_resolver]
# Publish!
reader = WikiReader(parser_name='restructuredtext')
doctree = publish_doctree(self.get_text(), reader=reader,
settings_overrides=self.overrides)
# Assume internal paths are relative to the container
for node in doctree.traverse(condition=nodes.reference):
refuri = node.get('refuri')
# Skip wiki or fragment link
if node.get('wiki_name') or not refuri:
continue
reference = get_reference(refuri.encode('utf_8'))
# Skip external
if is_external(reference):
continue
# Resolve absolute path
resource = self.get_resource(reference.path, soft=True)
if resource is None:
resource = parent.get_resource(reference.path, soft=True)
if resource is None:
continue
refuri = str(resource.abspath)
# Restore fragment
if reference.fragment:
refuri = "%s#%s" % (refuri, reference.fragment)
node['refuri'] = refuri
# Assume image paths are relative to the container
for node in doctree.traverse(condition=nodes.image):
reference = get_reference(node['uri'].encode('utf_8'))
# Skip external
if is_external(reference):
continue
# Strip the view
path = reference.path
if path[-1][0] == ';':
path = path[:-1]
# Resolve absolute path
resource = parent.get_resource(path, soft=True)
if resource is not None:
node['uri'] = str(resource.abspath)
return doctree
def get_book(self):
doctree = self.get_doctree()
return doctree.next_node(condition=nodes.book)
# Register dummy book directive for ODT export
nodes._add_node_class_names(['book'])
nodes.book = book
register_directive('book', Book)
labels['book'] = ''