Commit 0378c29a authored by Xiaowu Zhang's avatar Xiaowu Zhang

erp5_web: extend asStrippedHTML to replace relative url by absolute url

parent 6ef450aa
import re
from AccessControl import ClassSecurityInfo
from Products.ERP5Type import Permissions
from erp5.component.document.TextDocument import TextDocument
class ReplaceRelativeUrlTextDocument(TextDocument, object):
"""
XXX Necessairy
"""
# Declarative security
security = ClassSecurityInfo()
security.declareProtected(Permissions.View, 'asStrippedHTML')
def asStrippedHTML(self, **kw):
match_link_tag = '<a.*?>'
match_link = 'href="(.*)"'
match_img_tag = '<img.*?/>'
match_img_src = 'src="(.*)"'
html = super(ReplaceRelativeUrlTextDocument, self).asStrippedHTML(**kw)
web_section = self.getWebSectionValue()
# call directly from web page
if not web_section:
return html
web_section_url = web_section.getAbsoluteUrl()
for link_tag in re.findall(match_link_tag, html):
link = re.search(match_link, link_tag)
if link:
link = link.group(1)
# link inside page
if link and not link.startswith('http') and not link.startswith('#') and not link.startswith('mailto'):
if link.startswith('./'):
new_link = link[2:]
else:
new_link = link
# By conversion, first letter of reference should be upper case
# To be more sure of what to replace
# erp5-Howto
if new_link[0].isupper() or new_link.startswith('erp5-') or new_link.startswith('user-') or \
new_link.startswith('slapos-') or new_link.startswith('vifib-') or new_link.startswith('osoe-') or \
new_link.startswith('developer-') or new_link.startswith('slapOS'):
transformed_link_tag = link_tag.replace(link, '%s/%s' % (web_section_url, new_link), 1)
html = html.replace(link_tag, transformed_link_tag)
for img_tag in re.findall(match_img_tag, html):
src = re.search(match_img_src, img_tag)
if src:
src = src.group(1)
# consider src = './xxxx' is a local file inside web page
if src and not src.startswith('http') and not src.startswith('./'):
if src[0].isupper():
transformed_img_tag = img_tag.replace(src, '%s/%s' % (web_section_url, src), 1)
html = html.replace(img_tag, transformed_img_tag)
return html
<?xml version="1.0"?>
<ZopeData>
<record id="1" aka="AAAAAAAAAAE=">
<pickle>
<global name="Document Component" module="erp5.portal_type"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_recorded_property_dict</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAI=</string> </persistent>
</value>
</item>
<item>
<key> <string>default_reference</string> </key>
<value> <string>ReplaceRelativeUrlTextDocument</string> </value>
</item>
<item>
<key> <string>description</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>document.erp5.ReplaceRelativeUrlTextDocument</string> </value>
</item>
<item>
<key> <string>portal_type</string> </key>
<value> <string>Document Component</string> </value>
</item>
<item>
<key> <string>sid</string> </key>
<value>
<none/>
</value>
</item>
<item>
<key> <string>text_content_error_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>text_content_warning_message</string> </key>
<value>
<tuple/>
</value>
</item>
<item>
<key> <string>version</string> </key>
<value> <string>erp5</string> </value>
</item>
<item>
<key> <string>workflow_history</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAM=</string> </persistent>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="2" aka="AAAAAAAAAAI=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary/>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="3" aka="AAAAAAAAAAM=">
<pickle>
<global name="PersistentMapping" module="Persistence.mapping"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>data</string> </key>
<value>
<dictionary>
<item>
<key> <string>component_validation_workflow</string> </key>
<value>
<persistent> <string encoding="base64">AAAAAAAAAAQ=</string> </persistent>
</value>
</item>
</dictionary>
</value>
</item>
</dictionary>
</pickle>
</record>
<record id="4" aka="AAAAAAAAAAQ=">
<pickle>
<global name="WorkflowHistoryList" module="Products.ERP5Type.Workflow"/>
</pickle>
<pickle>
<dictionary>
<item>
<key> <string>_log</string> </key>
<value>
<list>
<dictionary>
<item>
<key> <string>action</string> </key>
<value> <string>validate</string> </value>
</item>
<item>
<key> <string>validation_state</string> </key>
<value> <string>validated</string> </value>
</item>
</dictionary>
</list>
</value>
</item>
</dictionary>
</pickle>
</record>
</ZopeData>
...@@ -102,7 +102,7 @@ ...@@ -102,7 +102,7 @@
</item> </item>
<item> <item>
<key> <string>type_class</string> </key> <key> <string>type_class</string> </key>
<value> <string>TextDocument</string> </value> <value> <string>ReplaceRelativeUrlTextDocument</string> </value>
</item> </item>
<item> <item>
<key> <string>type_interface</string> </key> <key> <string>type_interface</string> </key>
......
...@@ -2,3 +2,4 @@ document.erp5.StaticWebSection ...@@ -2,3 +2,4 @@ document.erp5.StaticWebSection
document.erp5.StaticWebSite document.erp5.StaticWebSite
document.erp5.WebSection document.erp5.WebSection
document.erp5.WebSite document.erp5.WebSite
document.erp5.ReplaceRelativeUrlTextDocument
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment