incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From john...@apache.org
Subject [23/50] git commit: [#6534] ticket:441 Rewrite links
Date Fri, 04 Oct 2013 16:30:51 GMT
[#6534] ticket:441 Rewrite links


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

Branch: refs/heads/cj/5561
Commit: d2eedf4ea4577e03a0a8844de804279f4963b8b0
Parents: 34df819
Author: Igor Bondarenko <jetmind2@gmail.com>
Authored: Fri Sep 20 16:45:49 2013 +0300
Committer: Dave Brondsema <dbrondsema@slashdotmedia.com>
Committed: Fri Oct 4 14:21:22 2013 +0000

----------------------------------------------------------------------
 .../forgeimporters/github/tests/test_wiki.py    | 68 +++++++++++++++++---
 ForgeImporters/forgeimporters/github/wiki.py    | 37 ++++++++---
 2 files changed, 87 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/d2eedf4e/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 3947c53..80a4d72 100644
--- a/ForgeImporters/forgeimporters/github/tests/test_wiki.py
+++ b/ForgeImporters/forgeimporters/github/tests/test_wiki.py
@@ -85,7 +85,12 @@ class TestGitHubWikiImporter(TestCase):
     @patch('forgeimporters.github.wiki.h.render_any_markup')
     def test_without_history(self, render, upsert):
         upsert.text = Mock()
-        GitHubWikiImporter()._without_history(self.commit2)
+        importer = GitHubWikiImporter()
+        importer.github_wiki_url = 'https://github.com/a/b/wiki'
+        importer.app = Mock()
+        importer.app.url = '/p/test/wiki/'
+        importer.rewrite_links = Mock(return_value='')
+        importer._without_history(self.commit2)
         assert_equal(upsert.call_args_list, [call('Home'), call('Home2'), call('Home3')])
 
         assert_equal(render.call_args_list, [
@@ -104,7 +109,7 @@ class TestGitHubWikiImporter(TestCase):
     @patch('forgeimporters.github.wiki.git.Repo._clone')
     @patch('forgeimporters.github.wiki.GitHubWikiImporter._with_history')
     @patch('forgeimporters.github.wiki.GitHubWikiImporter._without_history')
-    def test_with_history(self, without_history, with_history, clone):
+    def test_import_with_history(self, without_history, with_history, clone):
         repo = clone.return_value
         repo.iter_commits.return_value = [self.commit1, self.commit2]
         GitHubWikiImporter().import_pages('wiki_url', history=True)
@@ -121,10 +126,15 @@ class TestGitHubWikiImporter(TestCase):
 
     @patch('forgeimporters.github.wiki.WM.Page.upsert')
     @patch('forgeimporters.github.wiki.h.render_any_markup')
-    def test_get_blobs_with_history(self, render, upsert):
+    def test_with_history(self, render, upsert):
         self.commit2.stats.files = {"Home.md": self.blob1}
         self.commit2.tree = {"Home.md": self.blob1}
-        GitHubWikiImporter()._with_history(self.commit2)
+        importer = GitHubWikiImporter()
+        importer.github_wiki_url = 'https://github.com/a/b/wiki'
+        importer.app = Mock()
+        importer.app.url = '/p/test/wiki/'
+        importer.rewrite_links = Mock(return_value='')
+        importer._with_history(self.commit2)
         assert_equal(upsert.call_args_list, [call('Home')])
         assert_equal(render.call_args_list, [call('Home.md', u'# test message')])
 
@@ -199,14 +209,22 @@ Our website is <http://sf.net>.
 
     @skipif(module_not_available('html2text'))
     def test_convert_markup(self):
-        f = GitHubWikiImporter().convert_markup
+        importer = GitHubWikiImporter()
+        importer.github_wiki_url = 'https://github.com/a/b/wiki'
+        importer.app = Mock()
+        importer.app.url = '/p/test/wiki/'
+        f = importer.convert_markup
         source = u'''Look at [[this page|Some Page]]
 
 More info at: [[MoreInfo]] [[Even More Info]]
 
 Our website is [[http://sf.net]].
 
-'[[Escaped Tag]]'''
+'[[Escaped Tag]]
+
+[External link to the wiki page](https://github.com/a/b/wiki/Page)
+
+[External link](https://github.com/a/b/issues/1)'''
 
         result = u'''Look at [this page](Some Page)
 
@@ -214,28 +232,58 @@ More info at: [MoreInfo] [Even More Info]
 
 Our website is <http://sf.net>.
 
-[[Escaped Tag]]\n\n'''
+[[Escaped Tag]]
+
+[External link to the wiki page](/p/test/wiki/Page)
+
+[External link](https://github.com/a/b/issues/1)\n\n'''
 
         assert_equal(f(source, 'test.md'), result)
 
     @without_module('html2text')
     def test_convert_markup_without_html2text(self):
-        f = GitHubWikiImporter().convert_markup
+        importer = GitHubWikiImporter()
+        importer.github_wiki_url = 'https://github.com/a/b/wiki'
+        importer.app = Mock()
+        importer.app.url = '/p/test/wiki/'
+        f = importer.convert_markup
         source = u'''Look at [[this page|Some Page]]
 
 More info at: [[MoreInfo]] [[Even More Info]]
 
 Our website is [[http://sf.net]].
 
-'[[Escaped Tag]]'''
+'[[Escaped Tag]]
+
+[External link to the wiki page](https://github.com/a/b/wiki/Page)
+
+[External link](https://github.com/a/b/issues/1)'''
 
         result = u'''<div class="markdown_content"><p>Look at [[this page|Some
Page]]</p>
 <p>More info at: [[MoreInfo]] [[Even More Info]]</p>
 <p>Our website is [[http://sf.net]].</p>
-<p>'[[Escaped Tag]]</p></div>'''
+<p>'[[Escaped Tag]]</p>
+<p><a class="" href="/p/test/wiki/Page" rel="nofollow">External link to the wiki
page</a></p>
+<p><a class="" href="https://github.com/a/b/issues/1" rel="nofollow">External
link</a></p></div>'''
 
         assert_equal(f(source, 'test.md'), result)
 
+    def test_rewrite_links(self):
+        f = GitHubWikiImporter().rewrite_links
+        prefix = 'https://github/a/b/wiki'
+        new = '/p/test/wiki/'
+        assert_equal(f(u'<a href="https://github/a/b/wiki/Test Page">Test Page</a>',
prefix, new),
+                     u'<a href="/p/test/wiki/Test Page">Test Page</a>')
+        assert_equal(f(u'<a href="https://github/a/b/wiki/Test-Page">Test-Page</a>',
prefix, new),
+                     u'<a href="/p/test/wiki/Test Page">Test Page</a>')
+        assert_equal(f(u'<a href="https://github/a/b/issues/1" class="1"></a>',
prefix, new),
+                     u'<a href="https://github/a/b/issues/1" class="1"></a>')
+        assert_equal(f(u'<a href="https://github/a/b/wiki/Test Page">https://github/a/b/wiki/Test
Page</a>', prefix, new),
+                     u'<a href="/p/test/wiki/Test Page">/p/test/wiki/Test Page</a>')
+        assert_equal(f(u'<a href="https://github/a/b/wiki/Test Page">Test <b>Page</b></a>',
prefix, new),
+                     u'<a href="/p/test/wiki/Test Page">Test <b>Page</b></a>')
+
+
 class TestGitHubWikiImportController(TestController, TestCase):
 
     url = '/p/%s/admin/ext/import/github-wiki/' % test_project_with_wiki

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/d2eedf4e/ForgeImporters/forgeimporters/github/wiki.py
----------------------------------------------------------------------
diff --git a/ForgeImporters/forgeimporters/github/wiki.py b/ForgeImporters/forgeimporters/github/wiki.py
index c61193d..629b0e9 100644
--- a/ForgeImporters/forgeimporters/github/wiki.py
+++ b/ForgeImporters/forgeimporters/github/wiki.py
@@ -20,6 +20,7 @@ from datetime import datetime
 from tempfile import mkdtemp
 from shutil import rmtree
 
+from BeautifulSoup import BeautifulSoup
 import git
 from pylons import app_globals as g
 from pylons import tmpl_context as c
@@ -145,7 +146,8 @@ class GitHubWikiImporter(ToolImporter):
         if not extractor.has_wiki():
             return
 
-        app = project.install_app(
+        self.github_wiki_url = extractor.get_page_url('wiki_url').replace('.wiki', '/wiki')
+        self.app = project.install_app(
             "Wiki",
             mount_point=mount_point or 'wiki',
             mount_label=mount_label or 'Wiki')
@@ -153,13 +155,13 @@ class GitHubWikiImporter(ToolImporter):
         ThreadLocalORMSession.flush_all()
         try:
             M.session.artifact_orm_session._get().skip_mod_date = True
-            with h.push_config(c, app=app):
+            with h.push_config(c, app=self.app):
                 self.import_pages(extractor.get_page_url('wiki_url'), history=with_history)
             ThreadLocalORMSession.flush_all()
             g.post_event('project_updated')
-            return app
+            return self.app
         except Exception as e:
-            h.make_app_admin_only(app)
+            h.make_app_admin_only(self.app)
             raise
         finally:
             M.session.artifact_orm_session._get().skip_mod_date = False
@@ -217,13 +219,14 @@ class GitHubWikiImporter(ToolImporter):
     def convert_markup(self, text, filename):
         """Convert any supported github markup into Allura-markdown.
 
-        Conversion happens in 3 phases:
+        Conversion happens in 4 phases:
 
         1. Convert source text to a html using h.render_any_markup
-        2. Convert resulting html to a markdown using html2text, if available.
-        3. Convert gollum tags
+        2. Rewrite links that match the wiki URL prefix with new location
+        3. Convert resulting html to a markdown using html2text, if available.
+        4. Convert gollum tags
 
-        If html2text module isn't available then only phase (1) will be executed.
+        If html2text module isn't available then only phases 1 and 2 will be executed.
         """
         try:
             import html2text
@@ -231,6 +234,7 @@ class GitHubWikiImporter(ToolImporter):
             html2text = None
 
         text = h.render_any_markup(filename, text)
+        text = self.rewrite_links(text, self.github_wiki_url, self.app.url)
         if html2text:
             text = html2text.html2text(text)
             text = self.convert_gollum_tags(text)
@@ -288,3 +292,20 @@ class GitHubWikiImporter(ToolImporter):
         if title:
             return u'[{}]({})'.format(title, page)
         return u'[{}]'.format(page)
+
+    def rewrite_links(self, html, prefix, new_prefix):
+        if not prefix.endswith('/'):
+            prefix += '/'
+        if not new_prefix.endswith('/'):
+            new_prefix += '/'
+        soup = BeautifulSoup(html)
+        for a in soup.findAll('a'):
+            if a.get('href').startswith(prefix):
+                page = a['href'].replace(prefix, '')
+                new_page = self._convert_page_name(page)
+                a['href'] = new_prefix + new_page
+                if a.text == page:
+                    a.setString(new_page)
+                elif a.text == prefix + page:
+                    a.setString(new_prefix + new_page)
+        return str(soup)


Mime
View raw message