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

[2/2] git commit: work in progress on autolink wiht escapes

Updated Branches:
  refs/heads/db/5358 [created] 658e1c434


work in progress on autolink wiht escapes


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

Branch: refs/heads/db/5358
Commit: 658e1c434351eca02bfde7d6cc280c05279653ca
Parents: fdba00c
Author: Dave Brondsema <db...@geek.net>
Authored: Tue Feb 5 20:38:47 2013 +0000
Committer: Dave Brondsema <db...@geek.net>
Committed: Wed Feb 13 23:50:27 2013 +0000

----------------------------------------------------------------------
 Allura/allura/lib/markdown_extensions.py |    6 +++++-
 Allura/allura/tests/test_globals.py      |   12 ++++++++----
 2 files changed, 13 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/658e1c43/Allura/allura/lib/markdown_extensions.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/markdown_extensions.py b/Allura/allura/lib/markdown_extensions.py
index c004db0..1f50876 100644
--- a/Allura/allura/lib/markdown_extensions.py
+++ b/Allura/allura/lib/markdown_extensions.py
@@ -38,7 +38,8 @@ class ForgeExtension(markdown.Extension):
         md.preprocessors['fenced-code'] = FencedCodeProcessor()
         md.preprocessors.add('plain_text_block', PlainTextPreprocessor(md), "_begin")
         md.preprocessors.add('macro_include', ForgeMacroIncludePreprocessor(md), '_end')
-        md.inlinePatterns['autolink_1'] = AutolinkPattern(r'(http(?:s?)://[a-zA-Z0-9./\-_0%?&=+#;~:]+)')
+        # this has to be before the 'escape' processor, otherwise weird placeholders are inserted for escaped chars within urls, and then the autolink can't match the whole url
+        md.inlinePatterns.add('autolink_without_brackets', AutolinkPattern(r'(http(?:s?)://[a-zA-Z0-9./\-\\_%?&=+#;~:]+)'), '<escape')
         # replace the link pattern with our extended version
         md.inlinePatterns['link'] = ForgeLinkPattern(markdown.inlinepatterns.LINK_RE, md, ext=self)
         md.inlinePatterns['short_reference'] = ForgeLinkPattern(markdown.inlinepatterns.SHORT_REF_RE, md, ext=self)
@@ -280,6 +281,9 @@ class AutolinkPattern(markdown.inlinepatterns.LinkPattern):
         old_link = mo.group(2)
         result = markdown.util.etree.Element('a')
         result.text = old_link
+        # since this is run before the builtin 'escape' processor, we have to do our own unescaping
+        for char in markdown.Markdown.ESCAPED_CHARS:
+            old_link = old_link.replace('\\' + char, char)
         result.set('href', old_link)
         return result
 

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/658e1c43/Allura/allura/tests/test_globals.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_globals.py b/Allura/allura/tests/test_globals.py
index cb53a0e..a11919c 100644
--- a/Allura/allura/tests/test_globals.py
+++ b/Allura/allura/tests/test_globals.py
@@ -259,14 +259,18 @@ def foo(): pass
 
 
 def test_markdown_autolink():
-    #with h.set_context('test', 'wiki', neighborhood='Projects')
-    text = g.markdown.convert('This is http://sf.net')
-    assert '<a href=' in text, text
+    assert '<a href=' in g.markdown.convert('This is http://sf.net')
     tgt = 'http://everything2.com/?node=nate+oostendorp'
     s = g.markdown.convert('This is %s' % tgt)
     assert_equal(
         s, '<div class="markdown_content"><p>This is <a href="%s" rel="nofollow">%s</a></p></div>' % (tgt, tgt))
-    assert '<a href=' in g.markdown.convert('This is http://sf.net')
+
+
+def test_markdown_autolink_with_escape():
+    # \_ is unnecessary but valid markdown escaping and should be considered as a regular underscore
+    # (it occurs during html2text conversion during project migrations)
+    r = g.markdown.convert('a http://www.phpmyadmin.net/home\_page/security/\#target b')
+    assert 'href="http://www.phpmyadmin.net/home_page/security/#target"' in r, r
 
 
 def test_macro_projects():