incubator-allura-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tvansteenbu...@apache.org
Subject [3/3] git commit: [#5290] ticket:220 prevent extra br tags inserting when including wiki pages
Date Tue, 18 Dec 2012 22:56:40 GMT
[#5290] ticket:220 prevent extra br tags inserting when including wiki pages


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

Branch: refs/heads/tv/4957
Commit: 8bc00663f6762488a17e30d283278bf20e34077e
Parents: 0a7b185
Author: Igor Bondarenko <jetmind2@gmail.com>
Authored: Tue Nov 27 12:24:59 2012 +0000
Committer: Tim Van Steenburgh <tvansteenburgh@gmail.com>
Committed: Tue Dec 18 14:56:30 2012 +0000

----------------------------------------------------------------------
 Allura/allura/lib/markdown_extensions.py |   27 +++++++++++++++++++++
 Allura/allura/tests/test_globals.py      |   32 +++++++++++++++++++++++++
 2 files changed, 59 insertions(+), 0 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/8bc00663/Allura/allura/lib/markdown_extensions.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/markdown_extensions.py b/Allura/allura/lib/markdown_extensions.py
index 0a5f75d..e7639a9 100644
--- a/Allura/allura/lib/markdown_extensions.py
+++ b/Allura/allura/lib/markdown_extensions.py
@@ -37,6 +37,7 @@ class ForgeExtension(markdown.Extension):
         md.preprocessors['html_block'].markdown_in_raw = True
         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%?&=+#;~:]+)')
         # replace the link pattern with our extended version
         md.inlinePatterns['link'] = ForgeLinkPattern(markdown.inlinepatterns.LINK_RE, md,
ext=self)
@@ -277,3 +278,29 @@ class AutolinkPattern(markdown.inlinepatterns.LinkPattern):
         result.text = old_link
         result.set('href', old_link)
         return result
+
+
+class ForgeMacroIncludePreprocessor(markdown.preprocessors.Preprocessor):
+    '''Join include statements to prevent extra <br>'s inserted by nl2br extension.
+
+    Converts:
+    [[include ref=some_ref]]
+    [[include ref=some_other_ref]]
+
+    To:
+    [[include ref=some_ref]][[include ref=some_other_ref]]
+    '''
+    pattern = re.compile(r'^\s*\[\[include ref=[^\]]*\]\]\s*$', re.IGNORECASE)
+
+    def run(self, lines):
+        buf = []
+        result = []
+        for line in lines:
+            if self.pattern.match(line):
+                buf.append(line)
+            else:
+                if buf:
+                    result.append(''.join(buf))
+                    buf = []
+                result.append(line)
+        return result

http://git-wip-us.apache.org/repos/asf/incubator-allura/blob/8bc00663/Allura/allura/tests/test_globals.py
----------------------------------------------------------------------
diff --git a/Allura/allura/tests/test_globals.py b/Allura/allura/tests/test_globals.py
index cbf5636..d3dbfab 100644
--- a/Allura/allura/tests/test_globals.py
+++ b/Allura/allura/tests/test_globals.py
@@ -159,6 +159,38 @@ def test_macro_project_admins_one_br():
     assert not '</a><br /><br /><a href=' in r, r
     assert '</a><br /><a href=' in r, r
 
+
+@td.with_wiki
+def test_macro_include_extra_br():
+    p_nbhd = M.Neighborhood.query.get(name='Projects')
+    p_test = M.Project.query.get(shortname='test', neighborhood_id=p_nbhd._id)
+    wiki = p_test.app_instance('wiki')
+    with h.push_context(p_test._id, app_config_id=wiki.config._id):
+        p = WM.Page.upsert(title='Include_1')
+        p.text = 'included page 1'
+        p.commit()
+        p = WM.Page.upsert(title='Include_2')
+        p.text = 'included page 2'
+        p.commit()
+        p = WM.Page.upsert(title='Include_3')
+        p.text = 'included page 3'
+        p.commit()
+        ThreadLocalORMSession.flush_all()
+        md = '[[include ref=Include_1]]\n[[include ref=Include_2]]\n[[include ref=Include_3]]'
+        html = g.markdown_wiki.convert(md)
+
+    expected_html = '''
+<div class="markdown_content">
+<p>
+<div><div class="markdown_content"><p>included page 1</p></div></div>
+<div><div class="markdown_content"><p>included page 2</p></div></div>
+<div><div class="markdown_content"><p>included page 3</p></div></div>
+</p>
+</div>
+'''.strip().replace('\n', '')
+    assert html.strip().replace('\n', '') == expected_html, html
+
+
 def test_markdown_toc():
     r = g.markdown_wiki.convert("""[TOC]
 


Mime
View raw message