|
1 # |
|
2 # Copyright (c) 2008-2018 Thierry Florac <tflorac AT ulthar.net> |
|
3 # All Rights Reserved. |
|
4 # |
|
5 # This software is subject to the provisions of the Zope Public License, |
|
6 # Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. |
|
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|
10 # FOR A PARTICULAR PURPOSE. |
|
11 # |
|
12 |
|
13 __docformat__ = 'restructuredtext' |
|
14 |
|
15 |
|
16 # import standard library |
|
17 from pyquery import PyQuery |
|
18 |
|
19 # import interfaces |
|
20 from pyams_sequence.interfaces import ISequentialIntIds |
|
21 from pyams_utils.interfaces.text import IHTMLRenderer |
|
22 from pyams_utils.interfaces.url import DISPLAY_CONTEXT |
|
23 from pyams_workflow.interfaces import IWorkflowPublicationInfo |
|
24 from pyramid.interfaces import IRequest |
|
25 |
|
26 # import packages |
|
27 from pyams_sequence.utility import get_reference_target |
|
28 from pyams_utils.adapter import adapter_config, ContextRequestAdapter |
|
29 from pyams_utils.registry import get_utility |
|
30 from pyams_utils.url import relative_url |
|
31 |
|
32 |
|
33 @adapter_config(name='oid_to_href', context=(str, IRequest), provides=IHTMLRenderer) |
|
34 class OIDHTMLRenderer(ContextRequestAdapter): |
|
35 """An HTML renderer converting all "oid://" URLs to internal relative links""" |
|
36 |
|
37 def render(self): |
|
38 context = self.request.annotations.get(DISPLAY_CONTEXT, self.request.context) |
|
39 html = PyQuery('<div>{0}</div>'.format(self.context)) |
|
40 sequence = get_utility(ISequentialIntIds) |
|
41 for link in html('a[href]'): |
|
42 href = link.attrib['href'] |
|
43 if href.startswith('oid://'): |
|
44 oid = sequence.get_full_oid(href.split('//', 1)[1]) |
|
45 target = get_reference_target(oid) |
|
46 if target is not None: |
|
47 publication_info = IWorkflowPublicationInfo(target, None) |
|
48 if (publication_info is not None) and publication_info.is_visible(self.request): |
|
49 link.attrib['href'] = relative_url(target, self.request, context) |
|
50 continue |
|
51 # invalid link => remove href! |
|
52 del link.attrib['href'] |
|
53 return html.html() |