You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by jo...@apache.org on 2013/11/14 18:36:27 UTC

[06/43] git commit: [#6622] ticket:435 Move github-markdown conversion methods to separate class

[#6622] ticket:435 Move github-markdown conversion methods to separate class


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

Branch: refs/heads/cj/6836
Commit: 44a7d0a11c2262fc2c46709f9f73df2a338c769b
Parents: 18b4e34
Author: Igor Bondarenko <je...@gmail.com>
Authored: Mon Oct 14 14:12:28 2013 +0300
Committer: Dave Brondsema <db...@slashdotmedia.com>
Committed: Wed Nov 13 17:16:55 2013 +0000

----------------------------------------------------------------------
 .../forgeimporters/github/tests/test_utils.py   | 39 ++++++++++++++++
 .../forgeimporters/github/tests/test_wiki.py    | 36 ---------------
 ForgeImporters/forgeimporters/github/utils.py   | 47 ++++++++++++++++++++
 ForgeImporters/forgeimporters/github/wiki.py    |  6 ++-
 4 files changed, 90 insertions(+), 38 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/44a7d0a1/ForgeImporters/forgeimporters/github/tests/test_utils.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/github/tests/test_utils.py b/ForgeImporters/forgeimporters/github/tests/test_utils.py
new file mode 100644
index 0000000..8711312
--- /dev/null
+++ b/ForgeImporters/forgeimporters/github/tests/test_utils.py
@@ -0,0 +1,39 @@
+from nose.tools import assert_equal
+
+from forgeimporters.github.utils import GitHubMarkdownConverter
+
+
+class TestGitHubMarkdownConverter(object):
+
+    def setUp(self):
+        self.conv = GitHubMarkdownConverter
+
+    def test_convert_sha_github_markup(self):
+        text = 'SHA: 16c999e8c71134401a78d4d46435517b2271d6ac'
+        result = self.conv.convert(text)
+        assert_equal(result, 'SHA: [16c999]')
+
+    def test_convert_user_sha_github_markup(self):
+        text = 'User@SHA: mojombo@16c999e8c71134401a78d4d46435517b2271d6ac'
+        result = self.conv.convert(text)
+        assert_equal(result, 'User@SHA:[16c999]')
+
+    def test_convert_user_repo_sha_github_markup(self):
+        text = 'User/Repository@SHA: mojombo/github-flavored-markdown@16c999e8c71134401a78d4d46435517b2271d6ac'
+        result = self.conv.convert(text)
+        assert_equal(result, 'User/Repository@SHA: [16c999]')
+
+    def test_convert_ticket_github_markup(self):
+        text = 'Ticket: #1'
+        result = self.conv.convert(text)
+        assert_equal(result, 'Ticket: [#1]')
+
+    def test_convert_username_ticket_github_markup(self):
+        text = 'User#Num: user#1'
+        result = self.conv.convert(text)
+        assert_equal(result, 'User#Num: [#1]')
+
+    def test_convert_username_repo_ticket_github_markup(self):
+        text = 'User/Repository#Num: user/repo#1'
+        result = self.conv.convert(text)
+        assert_equal(result, 'User/Repository#Num: [#1]')

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/44a7d0a1/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 bb2a383..50c905d 100644
--- a/ForgeImporters/forgeimporters/github/tests/test_wiki.py
+++ b/ForgeImporters/forgeimporters/github/tests/test_wiki.py
@@ -479,42 +479,6 @@ some text and **[Tips n' Tricks]**
         result = u'<p><strong>[[this checklist|Troubleshooting]]</strong></p>'
         assert_equal(f(source, 't.textile').strip(), result)
 
-    def test_convert_sha_github_markup(self):
-        html = 'SHA: 16c999e8c71134401a78d4d46435517b2271d6ac'
-        importer = GitHubWikiImporter()
-        result = importer.convert_github_markup(html)
-        assert_equal(result, 'SHA: [16c999]')
-
-    def test_convert_user_sha_github_markup(self):
-        html = 'User@SHA: mojombo@16c999e8c71134401a78d4d46435517b2271d6ac'
-        importer = GitHubWikiImporter()
-        result = importer.convert_github_markup(html)
-        assert_equal(result, 'User@SHA:[16c999]')
-
-    def test_convert_user_repo_sha_github_markup(self):
-        html = 'User/Repository@SHA: mojombo/github-flavored-markdown@16c999e8c71134401a78d4d46435517b2271d6ac'
-        importer = GitHubWikiImporter()
-        result = importer.convert_github_markup(html)
-        assert_equal(result, 'User/Repository@SHA: [16c999]')
-
-    def test_convert_ticket_github_markup(self):
-        html = 'Ticket: #1'
-        importer = GitHubWikiImporter()
-        result = importer.convert_github_markup(html)
-        assert_equal(result, 'Ticket: [#1]')
-
-    def test_convert_username_ticket_github_markup(self):
-        html = 'User#Num: user#1'
-        importer = GitHubWikiImporter()
-        result = importer.convert_github_markup(html)
-        assert_equal(result, 'User#Num: [#1]')
-
-    def test_convert_username_repo_ticket_github_markup(self):
-        html = 'User/Repository#Num: user/repo#1'
-        importer = GitHubWikiImporter()
-        result = importer.convert_github_markup(html)
-        assert_equal(result, 'User/Repository#Num: [#1]')
-
 
 class TestGitHubWikiImportController(TestController, TestCase):
 

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/44a7d0a1/ForgeImporters/forgeimporters/github/utils.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/github/utils.py b/ForgeImporters/forgeimporters/github/utils.py
new file mode 100644
index 0000000..c095f81
--- /dev/null
+++ b/ForgeImporters/forgeimporters/github/utils.py
@@ -0,0 +1,47 @@
+import re
+
+
+class GitHubMarkdownConverter(object):
+
+    @classmethod
+    def convert(cls, text):
+        _re = re.compile('\S*(#\d+)')
+        text = _re.sub(cls._convert_ticket, text)
+
+        _re = re.compile('\S*/\S*@(\w{40})')
+        text = _re.sub(cls._convert_user_repo_sha, text)
+
+        _re = re.compile('\s\S*@(\w{40})')
+        text = _re.sub(cls._convert_user_sha, text)
+
+        _re = re.compile(': (\w{40})')
+        text = _re.sub(cls._convert_sha, text)
+        _re = re.compile('~~(.*)~~',)
+        text = _re.sub(cls._convert_strikethrough, text)
+        _re = re.compile(r'```\w*(.*)```', re.DOTALL)
+        text = _re.sub(cls._convert_codeblock, text)
+        return text
+
+    @classmethod
+    def _convert_sha(cls, match):
+        return ': [%s]' % match.group(1)[:6]
+
+    @classmethod
+    def _convert_ticket(cls, match):
+        return '[%s]' % match.group(1)
+
+    @classmethod
+    def _convert_user_sha(cls, match):
+        return '[%s]' % (match.group(1)[:6])
+
+    @classmethod
+    def _convert_user_repo_sha(cls, match):
+        return '[%s]' % (match.group(1)[:6])
+
+    @classmethod
+    def _convert_strikethrough(cls, match):
+        return '<s>%s</s>' % match.group(1)
+
+    @classmethod
+    def _convert_codeblock(cls, match):
+        return '\n~~~~%s~~~~\n'% match.group(1)

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/44a7d0a1/ForgeImporters/forgeimporters/github/wiki.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/github/wiki.py b/ForgeImporters/forgeimporters/github/wiki.py
index f95ba44..4bf796c 100644
--- a/ForgeImporters/forgeimporters/github/wiki.py
+++ b/ForgeImporters/forgeimporters/github/wiki.py
@@ -51,6 +51,7 @@ from forgeimporters.base import (
         ToolImportForm,
         )
 from forgeimporters.github import GitHubProjectExtractor
+from forgeimporters.github.utils import GitHubMarkdownConverter
 from forgewiki import model as WM
 from forgewiki.converters import mediawiki2markdown
 
@@ -264,7 +265,9 @@ class GitHubWikiImporter(ToolImporter):
         """
         name, ext = os.path.splitext(filename)
         if ext in self.markdown_exts:
-            return self.convert_github_markup(text)
+            text = GitHubMarkdownConverter.convert(text)
+            return text
+            #return self.convert_gollum_tags(text)
 
         try:
             import html2text
@@ -394,7 +397,6 @@ class GitHubWikiImporter(ToolImporter):
                     a.setString(new_prefix + new_page)
         return unicode(soup)
 
-
     def _prepare_textile_text(self, text):
         # need to convert lists properly
         text_lines = text.splitlines()