Return-Path: X-Original-To: apmail-incubator-allura-commits-archive@minotaur.apache.org Delivered-To: apmail-incubator-allura-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 9A58AF46E for ; Wed, 21 Aug 2013 16:06:01 +0000 (UTC) Received: (qmail 10382 invoked by uid 500); 21 Aug 2013 16:05:58 -0000 Delivered-To: apmail-incubator-allura-commits-archive@incubator.apache.org Received: (qmail 10318 invoked by uid 500); 21 Aug 2013 16:05:58 -0000 Mailing-List: contact allura-commits-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: allura-dev@incubator.apache.org Delivered-To: mailing list allura-commits@incubator.apache.org Received: (qmail 9821 invoked by uid 99); 21 Aug 2013 16:05:54 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 21 Aug 2013 16:05:54 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 1D04F8C1A88; Wed, 21 Aug 2013 16:05:54 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: brondsem@apache.org To: allura-commits@incubator.apache.org Date: Wed, 21 Aug 2013 16:05:59 -0000 Message-Id: <3903ac4f124c49babe5221e75901e9e2@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [07/25] git commit: [#6464] Refactored plain2markdown to Allura helpers [#6464] Refactored plain2markdown to Allura helpers Signed-off-by: Cory Johns 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/master Commit: 851c962bc4bb89ac97d5d101f32cdbbd7b2fdcb8 Parents: acc9469 Author: Cory Johns Authored: Thu Aug 8 18:49:51 2013 +0000 Committer: Cory Johns 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 `. @@ -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)