10 # FOR A PARTICULAR PURPOSE. |
10 # FOR A PARTICULAR PURPOSE. |
11 # |
11 # |
12 |
12 |
13 __docformat__ = 'restructuredtext' |
13 __docformat__ = 'restructuredtext' |
14 |
14 |
|
15 from pyquery import PyQuery |
|
16 from pyramid.interfaces import IRequest |
15 |
17 |
16 # import standard library |
|
17 from pyquery import PyQuery |
|
18 |
|
19 # import interfaces |
|
20 from pyams_sequence.interfaces import ISequentialIntIds |
18 from pyams_sequence.interfaces import ISequentialIntIds |
|
19 from pyams_sequence.reference import get_reference_target |
|
20 from pyams_utils.adapter import ContextRequestAdapter, adapter_config |
21 from pyams_utils.interfaces.text import IHTMLRenderer |
21 from pyams_utils.interfaces.text import IHTMLRenderer |
22 from pyams_utils.interfaces.url import DISPLAY_CONTEXT |
22 from pyams_utils.interfaces.url import DISPLAY_CONTEXT |
|
23 from pyams_utils.registry import get_utility |
|
24 from pyams_utils.text import text_to_html |
|
25 from pyams_utils.url import relative_url |
23 from pyams_workflow.interfaces import IWorkflowPublicationInfo |
26 from pyams_workflow.interfaces import IWorkflowPublicationInfo |
24 from pyramid.interfaces import IRequest |
|
25 |
|
26 # import packages |
|
27 from pyams_sequence.reference 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 |
27 |
32 |
28 |
33 @adapter_config(name='oid_to_href', context=(str, IRequest), provides=IHTMLRenderer) |
29 @adapter_config(name='oid_to_href', context=(str, IRequest), provides=IHTMLRenderer) |
34 class OIDHTMLRenderer(ContextRequestAdapter): |
30 class OIDHTMLRenderer(ContextRequestAdapter): |
35 """An HTML renderer converting all "oid://" URLs to internal relative links""" |
31 """An HTML renderer converting all "oid://" URLs to internal relative links""" |
36 |
32 |
37 def render(self): |
33 def render(self): |
38 context = self.request.annotations.get(DISPLAY_CONTEXT, self.request.context) |
34 context = self.request.annotations.get(DISPLAY_CONTEXT, self.request.context) |
39 html = PyQuery('<div>{0}</div>'.format(self.context)) |
35 html = PyQuery('<div>{}</div>'.format(self.context)) |
40 sequence = get_utility(ISequentialIntIds) |
36 sequence = get_utility(ISequentialIntIds) |
41 for link in html('a[href]'): |
37 for link in html('a[href]'): |
42 href = link.attrib['href'] |
38 href = link.attrib['href'] |
43 if href.startswith('oid://'): |
39 if href.startswith('oid://'): |
44 oid = sequence.get_full_oid(href.split('//', 1)[1]) |
40 oid = sequence.get_full_oid(href.split('//', 1)[1]) |
49 link.attrib['href'] = relative_url(target, self.request, context) |
45 link.attrib['href'] = relative_url(target, self.request, context) |
50 continue |
46 continue |
51 # invalid link => remove href! |
47 # invalid link => remove href! |
52 del link.attrib['href'] |
48 del link.attrib['href'] |
53 return html.html() |
49 return html.html() |
|
50 |
|
51 |
|
52 @adapter_config(name='remove_tags', context=(str, IRequest), provides=IHTMLRenderer) |
|
53 class TagsRemoveHTMLRenderer(ContextRequestAdapter): |
|
54 """Remove all tags from HTML code""" |
|
55 |
|
56 def render(self): |
|
57 html = PyQuery('<div>{}</div>'.format(self.context)) |
|
58 return text_to_html(html.text()) |