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/08/19 20:00:24 UTC

[06/17] git commit: [#6464] Refactored plain2markdown to Allura helpers

[#6464] Refactored plain2markdown to Allura helpers

Signed-off-by: Cory Johns <cj...@slashdotmedia.com>


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

Branch: refs/heads/cj/6464
Commit: ab52ce7da539f78e67a69927633427e2040e7658
Parents: df25d75
Author: Cory Johns <cj...@slashdotmedia.com>
Authored: Thu Aug 8 18:49:51 2013 +0000
Committer: Cory Johns <cj...@slashdotmedia.com>
Committed: Mon Aug 19 18:00:09 2013 +0000

----------------------------------------------------------------------
 Allura/allura/lib/helpers.py            | 41 ++++++++++++++++++++++++++++
 ForgeBlog/forgeblog/command/rssfeeds.py | 36 +-----------------------
 2 files changed, 42 insertions(+), 35 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/ab52ce7d/Allura/allura/lib/helpers.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/helpers.py b/Allura/allura/lib/helpers.py
index 0511dc7..99a5d30 100644
--- a/Allura/allura/lib/helpers.py
+++ b/Allura/allura/lib/helpers.py
@@ -80,6 +80,26 @@ re_clean_vardec_key = re.compile(r'''\A
 )+
 \Z''', re.VERBOSE)
 
+# markdown escaping regexps
+re_amp = re.compile(r'''
+    [&]          # amp
+    (?=          # look ahead for:
+      ([a-zA-Z0-9]+;)  # named HTML entity
+      |
+      (\#[0-9]+;)      # decimal entity
+      |
+      (\#x[0-9A-F]+;)  # hex entity
+    )
+    ''', re.VERBOSE)
+re_leading_spaces = re.compile(r'^[\t ]+', re.MULTILINE)
+re_preserve_spaces = re.compile(r'''
+    [ ]           # space
+    (?=[ ])       # lookahead for a space
+    ''', re.VERBOSE)
+re_angle_bracket_open = re.compile('<')
+re_angle_bracket_close = re.compile('>')
+md_chars_matcher_all = re.compile(r"([`\*_{}\[\]\(\)#!\\.+-])")
+
 def make_safe_path_portion(ustr, relaxed=True):
     """Return an ascii representation of ``ustr`` that conforms to mount point
     naming :attr:`rules <re_tool_mount_point_fragment>`.
@@ -926,3 +946,24 @@ def urlopen(url, retries=3, codes=(408,)):
             else:
                 log.exception('Failed after %s retries: %s', retries, e)
                 raise
+
+
+def plain2markdown(text, preserve_multiple_spaces=False, has_html_entities=False):
+    if not has_html_entities:
+        # prevent &foo; and &#123; from becoming HTML entities
+        text = re_amp.sub('&amp;', text)
+    # avoid accidental 4-space indentations creating code blocks
+    if preserve_multiple_spaces:
+        text = text.replace('\t', ' ' * 4)
+        text = re_preserve_spaces.sub('&nbsp;', text)
+        # try to use html2text for most of the escaping
+        import html2text
+        html2text.BODY_WIDTH = 0
+        text = html2text.escape_md_section(text, snob=True)
+    except ImportError:
+        # fall back to just escaping any MD-special chars
+        text = md_chars_matcher.sub(r"\\\\1", text)
+    # prevent < and > from becoming tags
+    text = re_angle_bracket_open.sub('&lt;', text)
+    text = re_angle_bracket_close.sub('&gt;', text)
+    return text

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/ab52ce7d/ForgeBlog/forgeblog/command/rssfeeds.py
----------------------------------------------------------------------
diff --git a/ForgeBlog/forgeblog/command/rssfeeds.py b/ForgeBlog/forgeblog/command/rssfeeds.py
index 305bc5a..f4b5c75 100644
--- a/ForgeBlog/forgeblog/command/rssfeeds.py
+++ b/ForgeBlog/forgeblog/command/rssfeeds.py
@@ -34,6 +34,7 @@ from forgeblog import version
 from forgeblog.main import ForgeBlogApp
 from allura.lib import exceptions
 from allura.lib.helpers import exceptionless
+from allura.lib.helpers import plain2markdown
 
 ## Everything in this file depends on html2text,
 ## so import attempt is placed in global scope.
@@ -45,41 +46,6 @@ except ImportError:
 
 html2text.BODY_WIDTH = 0
 
-re_amp = re.compile(r'''
-    [&]          # amp
-    (?=          # look ahead for:
-      ([a-zA-Z0-9]+;)  # named HTML entity
-      |
-      (\#[0-9]+;)      # decimal entity
-      |
-      (\#x[0-9A-F]+;)  # hex entity
-    )
-    ''', re.VERBOSE)
-re_leading_spaces = re.compile(r'^[\t ]+', re.MULTILINE)
-re_preserve_spaces = re.compile(r'''
-    [ ]           # space
-    (?=[ ])       # lookahead for a space
-    ''', re.VERBOSE)
-re_angle_bracket_open = re.compile('<')
-re_angle_bracket_close = re.compile('>')
-def plain2markdown(text, preserve_multiple_spaces=False, has_html_entities=False):
-    if not has_html_entities:
-        # prevent &foo; and &#123; from becoming HTML entities
-        text = re_amp.sub('&amp;', text)
-    # avoid accidental 4-space indentations creating code blocks
-    if preserve_multiple_spaces:
-        text = text.replace('\t', ' ' * 4)
-        text = re_preserve_spaces.sub('&nbsp;', text)
-    else:
-        text = re_leading_spaces.sub('', text)
-    # use html2text for most of the escaping
-    text = html2text.escape_md_section(text, snob=True)
-    # prevent < and > from becoming tags
-    text = re_angle_bracket_open.sub('&lt;', text)
-    text = re_angle_bracket_close.sub('&gt;', text)
-    return text
-
-
 class RssFeedsCommand(base.BlogCommand):
     summary = 'Rss feed client'
     parser = base.BlogCommand.standard_parser(verbose=True)