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/20 19:45:38 UTC
[08/22] 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/851c962b
Tree: http://git-wip-us.apache.org/repos/asf/incubator-allura/tree/851c962b
Diff: http://git-wip-us.apache.org/repos/asf/incubator-allura/diff/851c962b
Branch: refs/heads/cj/6464
Commit: 851c962bc4bb89ac97d5d101f32cdbbd7b2fdcb8
Parents: acc9469
Author: Cory Johns <cj...@slashdotmedia.com>
Authored: Thu Aug 8 18:49:51 2013 +0000
Committer: Cory Johns <cj...@slashdotmedia.com>
Committed: Tue Aug 20 17:34:13 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/851c962b/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 { from becoming HTML entities
+ text = re_amp.sub('&', text)
+ # avoid accidental 4-space indentations creating code blocks
+ if preserve_multiple_spaces:
+ text = text.replace('\t', ' ' * 4)
+ text = re_preserve_spaces.sub(' ', 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('<', text)
+ text = re_angle_bracket_close.sub('>', text)
+ return text
http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/851c962b/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 { from becoming HTML entities
- text = re_amp.sub('&', text)
- # avoid accidental 4-space indentations creating code blocks
- if preserve_multiple_spaces:
- text = text.replace('\t', ' ' * 4)
- text = re_preserve_spaces.sub(' ', 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('<', text)
- text = re_angle_bracket_close.sub('>', text)
- return text
-
-
class RssFeedsCommand(base.BlogCommand):
summary = 'Rss feed client'
parser = base.BlogCommand.standard_parser(verbose=True)