incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From brond...@apache.org
Subject [25/44] git commit: [#6534] ticket:441 Convert gollum tags in one pass
Date Fri, 04 Oct 2013 14:43:13 GMT
[#6534] ticket:441 Convert gollum tags in one pass


Project: http://git-wip-us.apache.org/repos/asf/incubator-allura/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-allura/commit/ea7eb1e4
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/ea7eb1e4
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/ea7eb1e4

Branch: refs/heads/master
Commit: ea7eb1e4e6e79130c23d092fd3f914e426d279e8
Parents: 26fecec
Author: Igor Bondarenko <jetmind2@gmail.com>
Authored: Fri Sep 20 13:49:53 2013 +0300
Committer: Dave Brondsema <dbrondsema@slashdotmedia.com>
Committed: Fri Oct 4 14:21:22 2013 +0000

----------------------------------------------------------------------
 .../forgeimporters/github/tests/test_wiki.py    |  8 +-
 ForgeImporters/forgeimporters/github/wiki.py    | 91 ++++++++++----------
 2 files changed, 51 insertions(+), 48 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/ea7eb1e4/ForgeImporters/forgeimporters/github/tests/test_wiki.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/github/tests/test_wiki.py b/ForgeImporters/forgeimporters/github/tests/test_wiki.py
index 88e42d7..94fd139 100644
--- a/ForgeImporters/forgeimporters/github/tests/test_wiki.py
+++ b/ForgeImporters/forgeimporters/github/tests/test_wiki.py
@@ -135,7 +135,7 @@ class TestGitHubWikiImporter(TestCase):
         assert_equal(f('Page / Name'), 'Page   Name')
 
     def test_convert_gollum_page_links(self):
-        f = GitHubWikiImporter().convert_gollum_page_links
+        f = GitHubWikiImporter().convert_gollum_tags
         assert_equal(f(u'[[Page]]'), u'[Page]')
         assert_equal(f(u'[[Page Title|Page]]'), u'[Page Title](Page)')
         assert_equal(f(u'[[Pagê Nâme]]'), u'[Pagê Nâme]')
@@ -150,7 +150,7 @@ class TestGitHubWikiImporter(TestCase):
         assert_equal(f(u'[[go here|Page / 1]]'), u'[go here](Page   1)')
 
     def test_convert_gollum_page_links_escaped(self):
-        f = GitHubWikiImporter().convert_gollum_page_links
+        f = GitHubWikiImporter().convert_gollum_tags
         assert_equal(f(u"'[[Page]]"), u'[[Page]]')
         assert_equal(f(u"'[[Page Title|Page]]"), u'[[Page Title|Page]]')
         assert_equal(f(u"'[[Page With Spaces]]"), u'[[Page With Spaces]]')
@@ -161,13 +161,13 @@ class TestGitHubWikiImporter(TestCase):
         assert_equal(f(u"'[[go here|Page / 1]]"), u'[[go here|Page / 1]]')
 
     def test_convert_gollum_external_links(self):
-        f = GitHubWikiImporter().convert_gollum_external_links
+        f = GitHubWikiImporter().convert_gollum_tags
         assert_equal(f(u'[[http://sf.net]]'), u'<http://sf.net>')
         assert_equal(f(u'[[https://sf.net]]'), u'<https://sf.net>')
         assert_equal(f(u'[[SourceForge|http://sf.net]]'), u'[SourceForge](http://sf.net)')
 
     def test_convert_gollum_external_links_escaped(self):
-        f = GitHubWikiImporter().convert_gollum_external_links
+        f = GitHubWikiImporter().convert_gollum_tags
         assert_equal(f(u"'[[http://sf.net]]"), u'[[http://sf.net]]')
         assert_equal(f(u"'[[https://sf.net]]"), u'[[https://sf.net]]')
         assert_equal(f(u"'[[SourceForge|http://sf.net]]"), u'[[SourceForge|http://sf.net]]')

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/ea7eb1e4/ForgeImporters/forgeimporters/github/wiki.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/github/wiki.py b/ForgeImporters/forgeimporters/github/wiki.py
index bc82ac8..a3db81f 100644
--- a/ForgeImporters/forgeimporters/github/wiki.py
+++ b/ForgeImporters/forgeimporters/github/wiki.py
@@ -237,48 +237,51 @@ class GitHubWikiImporter(ToolImporter):
         return text
 
     def convert_gollum_tags(self, text):
-        # order is important
-        text = self.convert_gollum_external_links(text)
-        text = self.convert_gollum_page_links(text)
-        return text
+        tag_re = re.compile(r'''
+            (?P<quote>')?             # optional tag escaping
+            (?P<tag>\[\[              # tag start
+            (?P<link>[^]]+)           # title/link/filename with options
+            \]\])                     # tag end
+        ''', re.VERBOSE)
+        return tag_re.sub(self._gollum_tag_match, text)
+
+    def _gollum_tag_match(self, match):
+        available_options = [
+            'alt=',
+            'frame',
+            'align=',
+            'float',
+            'width=',
+            'height=',
+        ]
+        quote = match.groupdict().get('quote')
+        if quote:
+            # tag is escaped, return untouched
+            return match.group('tag')
+        link = match.group('link').split('|')
+        title = options = None
+        if len(link) == 1:
+            link = link[0]
+        elif any(map(lambda opt: link[1].startswith(opt), available_options)):
+            # second element is option -> first is the link
+            link, options = link[0], link[1:]
+        else:
+            title, link, options = link[0], link[1], link[2:]
 
-    def convert_gollum_page_links(self, text):
-        _re = re.compile(r'''(?P<quote>')?            # possible tag escaping
-                             (?P<tag>\[\[             # tag start
-                             (?:(?P<title>[^]|]*)\|)? # optional title
-                             (?P<page>[^]]+)          # page name
-                             \]\])                    # tag end''', re.VERBOSE)
-
-        def repl(match):
-            page = self._convert_page_name(match.group('page'))
-            title = match.groupdict().get('title')
-            quote = match.groupdict().get('quote')
-            if quote:
-                # tag is escaped, return untouched
-                return match.group('tag')
-            if title:
-                return u'[{}]({})'.format(title, page)
-            return u'[{}]'.format(page)
-
-        return _re.sub(repl, text)
-
-    def convert_gollum_external_links(self, text):
-        _re = re.compile(
-            r'''(?P<quote>')?                     # possible tag escaping
-                (?P<tag>\[\[                      # tag start
-                (?:(?P<title>[^]|]*)\|)?          # optional title
-                (?P<link>(?:http|https)://[^]]+)  # link
-                \]\])                             # tag end''', re.VERBOSE)
-
-        def repl(match):
-            link = match.group('link')
-            title = match.groupdict().get('title')
-            quote = match.groupdict().get('quote')
-            if quote:
-                # tag is escaped, return untouched
-                return match.group('tag')
-            if title:
-                return u'[{}]({})'.format(title, link)
-            return u'<{}>'.format(link)
-
-        return _re.sub(repl, text)
+        if link.startswith('http://') or link.startswith('https://'):
+            sub = self._gollum_external_link
+        # TODO: add embedded images and file links
+        else:
+            sub = self._gollum_page_link
+        return sub(link, title, options)
+
+    def _gollum_external_link(self, link, title, options):
+        if title:
+            return u'[{}]({})'.format(title, link)
+        return u'<{}>'.format(link)
+
+    def _gollum_page_link(self, link, title, options):
+        page = self._convert_page_name(link)
+        if title:
+            return u'[{}]({})'.format(title, page)
+        return u'[{}]'.format(page)


Mime
View raw message