Commits

Roger Haase committed b980a2e

fix some broken parts of transclusions in moinwiki, markdown, and rst parsers

  • Participants
  • Parent commits 00ac542

Comments (0)

Files changed (12)

File MoinMoin/converter/_tests/test_markdown_in.py

 
 import re
 
-from MoinMoin.util.tree import moin_page, xlink, xml
+from MoinMoin.util.tree import moin_page, xlink, xml, xinclude, html
 
 from ..markdown_in import Converter
 
         moin_page: '',
         xlink: 'xlink',
         xml: 'xml',
+        xinclude: 'xinclude',
+        html: 'html',
     }
 
     output_re = re.compile(r'\s+xmlns(:\S+)?="[^"]+"')
         for i in data:
             yield (self.do, ) + i
 
+    def test_image(self):
+        data = [
+            (u'![Alt text](png "Optional title")',
+                '<p><xinclude:include html:alt="Alt text" xinclude:href="wiki.local:png" /></p>'),
+            (u'![](png "Optional title")',
+                '<p><xinclude:include html:alt="" xinclude:href="wiki.local:png" /></p>'),
+            (u'![remote image](http://static.moinmo.in/logos/moinmoin.png)',
+                '<p><object xlink:href="http://static.moinmo.in/logos/moinmoin.png">remote image</object></p>'),
+            (u'![Alt text](http://test.moinmo.in/png)',
+                '<p><object xlink:href="http://test.moinmo.in/png">Alt text</object></p>'),
+            (u'![transclude local wiki item](someitem)',
+                '<p><xinclude:include html:alt="transclude local wiki item" xinclude:href="wiki.local:someitem" /></p>'),
+        ]
+        for i in data:
+            yield (self.do, ) + i
+
     def serialize(self, elem, **options):
         from StringIO import StringIO
         buffer = StringIO()

File MoinMoin/converter/_tests/test_moinwiki_in.py

                 '<page><body><p><a xlink:href="http://moinmo.in/">MoinMoin</a></p></body></page>'),
             (u'[[MoinMoin]]',
                 '<page><body><p><a xlink:href="wiki.local:MoinMoin">MoinMoin</a></p></body></page>'),
+            (u'{{somelocalimage|my alt text|width=10, height=10}}',
+                '<page><body><p><xinclude:include xhtml:alt="my alt text" xhtml:height="10" xhtml:width="10" xinclude:href="wiki.local:somelocalimage?" /></p></body></page>'),
             (u'{{somelocalimage||width=10, height=10}}',
                 '<page><body><p><xinclude:include xhtml:height="10" xhtml:width="10" xinclude:href="wiki.local:somelocalimage?" /></p></body></page>'),
             (u'{{somelocalimage||width=10, &h=10}}',
                 '<page><body><p><xinclude:include xhtml:width="10" xinclude:href="wiki.local:somelocalimage?h=10" /></p></body></page>'),
+            # object tags do not have alt attributes, instead it is placed before the </object>
             (u'{{http://moinmo.in/|test|width=10, height=10}}',
-                '<page><body><p><object alt="test" xhtml:height="10" xhtml:width="10" xlink:href="http://moinmo.in/">test</object></p></body></page>'),
+                '<page><body><p><object xhtml:height="10" xhtml:width="10" xlink:href="http://moinmo.in/">test</object></p></body></page>'),
             (u'{{http://moinmo.in/}}',
                 '<page><body><p><object xlink:href="http://moinmo.in/" /></p></body></page>', None, 'unknown'),
             (u'{{http://moinmo.in/|MoinMoin}}',

File MoinMoin/converter/_tests/test_moinwiki_in_out.py

 
     def test_object(self):
         data = [
+            # (u'{{png}}', '{{png}}\n'),
+            # (u'{{png|png}}', '{{png|png}}\n'),
+            # (u'{{png|my png}}', '{{png|my png}}\n'),
+            # (u'{{png|my png|width=100}}', '{{png|my png|width=100}}\n'),
+            # (u'{{png||width=100}}', '{{png||width=100}}\n'),
             # (u"{{drawing:anywikitest.adraw}}", '{{drawing:anywikitest.adraw}}\n'),
             (u"{{http://static.moinmo.in/logos/moinmoin.png}}\n", '{{http://static.moinmo.in/logos/moinmoin.png}}\n'),
-            (u'{{http://static.moinmo.in/logos/moinmoin.png|alt text}}', '{{http://static.moinmo.in/logos/moinmoin.png|alt text}}\n'),
+            # (u'{{http://static.moinmo.in/logos/moinmoin.png|alt text}}', '{{http://static.moinmo.in/logos/moinmoin.png|alt text}}\n'),
             # (u'{{http://static.moinmo.in/logos/moinmoin.png|alt text|width=100 height=150 align=right}}', '{{http://static.moinmo.in/logos/moinmoin.png|alt text|width=100 height=150 align=right}}\n'),
+            # (u'{{http://static.moinmo.in/logos/moinmoin.png|alt text|width=100}}', '{{http://static.moinmo.in/logos/moinmoin.png|alt text|width=100}}\n'),
+            # (u'{{http://static.moinmo.in/logos/moinmoin.png|alt text|width=100 height=150 style="float: right"}}', '{{http://static.moinmo.in/logos/moinmoin.png|alt text|width=100 height=150 style="float: right"}}\n'),
             # (u'{{attachment:image.png}}', '{{attachment:image.png}}\n'),
             # (u'{{attachment:image.png|alt text}}', '{{attachment:image.png|alt text}}\n'),
             # (u'{{attachment:image.png|alt text|width=100 align=left height=150}}', '{{attachment:image.png|alt text|width=100 align=left height=150}}\n'),

File MoinMoin/converter/_tests/test_moinwiki_out.py

 
     def test_object(self):
         data = [
-            (u"<page:object xlink:href=\"drawing:anywikitest.adraw\">{{drawing:anywikitest.adraw</page:object>", '{{drawing:anywikitest.adraw}}'),
+            (u"<page:object xlink:href=\"drawing:anywikitest.adraw\">drawing:anywikitest.adraw</page:object>", '{{drawing:anywikitest.adraw}}'),
             (u"<page:object xlink:href=\"http://static.moinmo.in/logos/moinmoin.png\" />", '{{http://static.moinmo.in/logos/moinmoin.png}}'),
             (u'<page:object page:alt="alt text" xlink:href="http://static.moinmo.in/logos/moinmoin.png">alt text</page:object>', '{{http://static.moinmo.in/logos/moinmoin.png|alt text}}'),
             (u'<page:object xlink:href="attachment:image.png" />', '{{attachment:image.png}}'),
+            # TODO: review following 2 lines, alt is invalid within object tag, align is not valid in html5
             (u'<page:object page:alt="alt text" xlink:href="attachment:image.png">alt text</page:object>', '{{attachment:image.png|alt text}}'),
             (u'<page:object page:alt="alt text" xlink:href="attachment:image.png?width=100&amp;height=150&amp;align=left" />', '{{attachment:image.png|alt text|width=100 height=150 align=left}}'),
-
         ]
         for i in data:
             yield (self.do, ) + i

File MoinMoin/converter/_tests/test_rst_in.py

         moin_page.namespace: '',
         xlink.namespace: 'xlink',
         html: 'xhtml',
+        xinclude: 'xinclude',
     }
 
     output_re = re.compile(r'\s+xmlns(:\S+)?="[^"]+"')
 
     def test_image(self):
         data = [
-            (u'.. image:: images/biohazard.png', '<page><body><object xlink:href="wiki.local:images/biohazard.png?do=get" /></body></page>'),
+            (u'.. image:: images/biohazard.png', '<page><body><xinclude:include xinclude:href="wiki.local:images/biohazard.png" /></body></page>'),
             (u""".. image:: images/biohazard.png
    :height: 100
    :width: 200
    :scale: 50
-   :alt: alternate text""", '<page><body><object xhtml:alt="alternate text" xhtml:height="50" xhtml:width="100" xlink:href="wiki.local:images/biohazard.png?do=get" /></body></page>'),
-            (u'abc |test| cba\n\n.. |test| image:: test.png', '<page><body><p>abc <object xhtml:alt="test" xlink:href="wiki.local:test.png?do=get" /> cba</p></body></page>'),
+   :alt: alternate text""", '<page><body><xinclude:include xhtml:alt="alternate text" xhtml:height="50" xhtml:width="100" xinclude:href="wiki.local:images/biohazard.png" /></body></page>'),
+            (u'abc |test| cba\n\n.. |test| image:: test.png', '<page><body><p>abc <xinclude:include xhtml:alt="test" xinclude:href="wiki.local:test.png" /> cba</p></body></page>'),
         ]
         for i in data:
             yield (self.do, ) + i

File MoinMoin/converter/html_out.py

             return "object"
 
     def visit_moinpage_object(self, elem):
-        # TODO: maybe IE8 would display transcluded external pages if we could do <object... type="text/html" ...>
+        """
+        elem of type img are converted to img tags here, others are left as object tags
+        """
         href = elem.get(xlink.href, None)
         attrib = {}
         whitelist = ['width', 'height']
 
         if obj_type == "img":
             # Images have alt text
-            alt = ''.join(unicode(e) for e in elem)  # XXX handle non-text e
-            if alt:
-                attrib[html.alt] = alt
+            if not attrib.get(html.alt):
+                alt = ''.join(unicode(e) for e in elem)  # XXX handle non-text e
+                if alt:
+                    attrib[html.alt] = alt
             new_elem = html.img(attrib=attrib)
 
         else:
                 attrib[html.controls] = 'controls'
             new_elem = self.new_copy(getattr(html, obj_type), elem, attrib)
 
-        if obj_type == "object" and href.scheme:
+        if obj_type == "object" and getattr(href, 'scheme', None):
             # items similar to {{http://moinmo.in}} are marked here, other objects are marked in include.py
             return mark_item_as_transclusion(new_elem, href)
         return new_elem

File MoinMoin/converter/image_in.py

         attrib = {}
         if arguments:
             query = arguments.keyword.get(xinclude.href).query
-            query_keys.update(url_decode(query))
+            if query:
+                query_keys.update(url_decode(query))
             attrib = arguments.keyword
 
         query = url_encode(query_keys, charset=CHARSET, encode_keys=True)

File MoinMoin/converter/link.py

 from MoinMoin.util.interwiki import is_known_wiki, url_for_item
 from MoinMoin.util.iri import Iri
 from MoinMoin.util.mime import Type, type_moin_document
-from MoinMoin.util.tree import moin_page, xlink, xinclude
+from MoinMoin.util.tree import moin_page, xlink, xinclude, html
 from MoinMoin.wikiutil import AbsItemName
 
 
 
     def handle_external_links(self, elem, input):
         elem.set(self._tag_xlink_href, input)
-        elem.set(moin_page.class_, 'moin-' + input.scheme)
+        elem.set(html.class_, elem.attrib.get(html.class_, '') + ' moin-' + input.scheme)
 
 
 class ConverterItemRefs(ConverterBase):

File MoinMoin/converter/markdown_in.py

 import htmlentitydefs
 from collections import deque
 
-from MoinMoin.util.tree import moin_page, xml, html, xlink
+from MoinMoin.util.tree import moin_page, xml, html, xlink, xinclude
 from ._util import allowed_uri_scheme, decode_data
+from MoinMoin.util.iri import Iri
 
 from MoinMoin import log
 logging = log.getLogger(__name__)
         """
         <img src="URI" /> --> <object xlink:href="URI />
         """
-        key = xlink('href')
         attrib = {}
-        attrib[key] = element.attrib.get("src")
-        return moin_page.object(attrib)
+        url = Iri(element.attrib.get('src'))
+        if url.scheme is None:
+            # img tag
+            attrib[html.alt] = element.attrib.get('alt', '')
+            target = Iri(scheme='wiki.local', path=element.attrib.get("src"), fragment=None)
+            attrib[xinclude.href] = target
+            new_node = xinclude.include(attrib=attrib)
+        else:
+            # object tag
+            attrib[xlink.href] = url
+            new_node = moin_page.object(attrib, children=[element.attrib.get('alt', '')])
+        return new_node
 
     def visit_object(self, element):
         """

File MoinMoin/converter/moinwiki_in.py

                 attrib[html(attr)] = value
 
         if object_item is not None:
+            # img tag
             query = url_encode(query_keys, charset=CHARSET, encode_keys=True)
             att = 'attachment:'  # moin 1.9 needed this for an attached file
             if object_item.startswith(att):
                 object_item = '/' + object_item[len(att):]  # now we have a subitem
             target = Iri(scheme='wiki.local', path=object_item, query=query, fragment=None)
-            text = object_item
-
             attrib[xinclude.href] = target
-
+            if object_text:
+                attrib[html.alt] = object_text
             element = xinclude.include(attrib=attrib)
             stack.top_append(element)
         else:
+            # object tag
             target = Iri(object_url)
             text = object_url
-
             attrib[xlink.href] = target
-
-            if object_text is not None:
-                attrib[moin_page.alt] = object_text
-
             element = moin_page.object(attrib)
             stack.push(element)
             if object_text:

File MoinMoin/converter/moinwiki_out.py

         ret += href
         alt = elem.get(moin_page.alt, u'')
         if alt and alt != href:
+            # TODO: this will fail on: {{png||width=100}}
             ret += u'|' + alt
             if args:
                 ret += u'|' + args

File MoinMoin/converter/rst_in.py

         pass
 
     def visit_image(self, node):
-        whitelist = ['width', 'height', 'align', 'alt', ]
-        attr = {}
+        """
+        Processes images and other transcluded objects.
+        """
+        # TODO: ReST also defines "align" as a parameter, but it is invalid in HTML5.  Depending upon value
+        # of align, we could add a style attribute of either:
+        #   vertical-align: middle | top | bottom
+        #   float: left | right
+        whitelist = ['width', 'height', 'alt', ]
+        attrib = {}
         for key in whitelist:
             if node.get(key):
-                attr[html(key)] = node.get(key)
+                attrib[html(key)] = node.get(key)
 
         # there is no 'scale' attribute, hence absent from whitelist, handled separately
-        # TODO: Error reporting in case of bad input, currently prints on terminal
-        # TODO: mark_item_as_transclusion in html_out conflicts 'align' by adding item-wrapper
-
         if node.get('scale'):
             scaling_factor = int(node.get('scale')) / 100.0
             for key in ('width', 'height'):
-                if html(key) in attr:
-                    attr[html(key)] = int(int(attr[html(key)]) * scaling_factor)
+                if html(key) in attrib:
+                    attrib[html(key)] = int(int(attrib[html(key)]) * scaling_factor)
 
-        new_node = moin_page.object(attr)
         url = Iri(node['uri'])
         if url.scheme is None:
-            url.scheme = u'wiki.local'
-            query_keys = url_decode(url.query or '')
-            query_keys['do'] = 'get'
-            url.query = url_encode(query_keys, charset=CHARSET, encode_keys=True)
-        new_node.set(xlink.href, url)
+            # img
+            target = Iri(scheme='wiki.local', path=node['uri'], fragment=None)
+            attrib[xinclude.href] = target
+            new_node = xinclude.include(attrib=attrib)
+        else:
+            # obj
+            # TODO: alt is set above, OK on img tags, but invalid here on object tags, it needs to be a text child of object tag for present code in html_out.py
+            # this should be handled consistently for rest, moinwiki, markdown, mediawiki...
+            new_node = moin_page.object(attrib)
+            new_node.set(xlink.href, url)
 
         self.open_moin_page_node(new_node)