[532] | 1 | import re |
---|
| 2 | from xml.dom import minidom |
---|
| 3 | from types import ListType |
---|
| 4 | from types import TupleType |
---|
| 5 | |
---|
| 6 | from zope.interface import implements |
---|
| 7 | from Products.CMFPlone.Portal import PloneSite |
---|
| 8 | |
---|
| 9 | from quintagroup.transmogrifier.interfaces import IExportDataCorrector |
---|
| 10 | from quintagroup.transmogrifier.adapters.exporting import ReferenceExporter |
---|
| 11 | |
---|
| 12 | from quintagroup.transmogrifier.simpleblog2quills.interfaces import IExportItemManipulator, IBlog |
---|
| 13 | |
---|
| 14 | IMAGE_FOLDER = 'images' |
---|
| 15 | |
---|
| 16 | class BlogExporter(object): |
---|
| 17 | implements(IExportDataCorrector) |
---|
| 18 | |
---|
| 19 | def __init__(self, context): |
---|
| 20 | self.context = context |
---|
| 21 | |
---|
| 22 | def __call__(self, data): |
---|
| 23 | return data |
---|
| 24 | |
---|
| 25 | def recurseToInterface(item, ifaces): |
---|
| 26 | """Recurse up the aq_chain until an object providing `iface' is found, |
---|
| 27 | and return that. |
---|
| 28 | """ |
---|
| 29 | if not isinstance(ifaces, (ListType, TupleType)): |
---|
| 30 | ifaces = [ifaces] |
---|
| 31 | parent = item.aq_parent |
---|
| 32 | for iface in ifaces: |
---|
| 33 | if iface.providedBy(item): |
---|
| 34 | return item |
---|
| 35 | for iface in ifaces: |
---|
| 36 | if iface.providedBy(parent): |
---|
| 37 | return parent |
---|
| 38 | if isinstance(parent, PloneSite): |
---|
| 39 | # Stop when we get to the portal root. |
---|
| 40 | return None |
---|
| 41 | return recurseToInterface(parent, ifaces) |
---|
| 42 | |
---|
| 43 | class BlogEntryExporter(ReferenceExporter): |
---|
| 44 | implements(IExportDataCorrector) |
---|
| 45 | |
---|
| 46 | HREF = re.compile(r'href="([^"]+)"') |
---|
| 47 | SRC = re.compile(r'src="([^"]+)"') |
---|
| 48 | |
---|
| 49 | def __call__(self, data): |
---|
| 50 | data = super(BlogEntryExporter, self)(data) |
---|
| 51 | doc = minidom.parseString(data['data']) |
---|
| 52 | try: |
---|
| 53 | elem = [i for i in doc.getElementsByTagName('field') if i.getAttribute('name') == 'body'][0] |
---|
| 54 | except IndexError: |
---|
| 55 | return data |
---|
| 56 | |
---|
| 57 | text = elem.firstChild.nodeValue |
---|
| 58 | # urls = self.HREF.findall(text) |
---|
| 59 | urls = self.SRC.findall(text) |
---|
| 60 | blog = recurseToInterface(self.context, IBlog) |
---|
| 61 | blog_url = blog.absolute_url() |
---|
| 62 | blog_path = blog.getPhysicalPath() |
---|
| 63 | for url in urls: |
---|
| 64 | image_id = url.rsplit('/', 1)[-1] |
---|
| 65 | if url.startswith('http://'): |
---|
| 66 | new_url = '/'.join((blog_url, IMAGE_FOLDER, image_id)) |
---|
| 67 | else: |
---|
| 68 | path = self.context.getPhysicalPath() |
---|
| 69 | # /plone/blog 2 |
---|
| 70 | # /plone/blog/bloggins/entry 4 |
---|
| 71 | # ../../images |
---|
| 72 | level = len(path) - len(blog_path) |
---|
| 73 | new_url = '/'.join(['..' for i in range(level)]) |
---|
| 74 | new_url = '/'.join([new_url, IMAGE_FOLDER, image_id]) |
---|
| 75 | |
---|
| 76 | text = text.replace(url, new_url, 1) |
---|
| 77 | |
---|
| 78 | elem.firstChild.nodeValue = text |
---|
| 79 | data['data'] = doc.toxml('utf-8') |
---|
| 80 | return data |
---|
| 81 | |
---|
| 82 | class PathRewriter(object): |
---|
| 83 | implements(IExportItemManipulator) |
---|
| 84 | |
---|
| 85 | def __init__(self, context): |
---|
| 86 | self.context = context |
---|
| 87 | |
---|
| 88 | def __call__(self, item, **kw): |
---|
| 89 | pathkey = kw.get('path') |
---|
| 90 | if pathkey is None: |
---|
| 91 | return item |
---|
| 92 | |
---|
| 93 | path = item[pathkey] |
---|
| 94 | blog = recurseToInterface(self.context, IBlog) |
---|
| 95 | if blog is None: |
---|
| 96 | return item |
---|
| 97 | |
---|
| 98 | new_path = list(blog.getPhysicalPath())[2:] |
---|
| 99 | new_path.append(IMAGE_FOLDER) |
---|
| 100 | new_path.append(self.context.getId()) |
---|
| 101 | new_path = '/'.join(new_path) |
---|
| 102 | item[pathkey] = new_path |
---|
| 103 | |
---|
| 104 | return item |
---|