You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@allura.apache.org by je...@apache.org on 2014/10/30 12:20:54 UTC

[04/11] git commit: [#4771] ticket:635 Get rid of extra

's that markdown inserts

[#4771] ticket:635 Get rid of extra <p>'s that markdown inserts


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

Branch: refs/heads/ib/4771
Commit: 18dd3919f7b17eb4d59d5350d3077d309b2fef4a
Parents: 1c7d627
Author: Igor Bondarenko <je...@gmail.com>
Authored: Tue Aug 26 16:34:26 2014 +0300
Committer: Igor Bondarenko <je...@gmail.com>
Committed: Thu Oct 30 09:44:22 2014 +0000

----------------------------------------------------------------------
 Allura/allura/lib/markdown_extensions.py | 42 +++++++++++++++++++++++++++
 1 file changed, 42 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/18dd3919/Allura/allura/lib/markdown_extensions.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/markdown_extensions.py b/Allura/allura/lib/markdown_extensions.py
index f2f8871..024eca1 100644
--- a/Allura/allura/lib/markdown_extensions.py
+++ b/Allura/allura/lib/markdown_extensions.py
@@ -278,6 +278,8 @@ class ForgeExtension(markdown.Extension):
             'macro', ForgeMacroPattern(MACRO_PATTERN, md, ext=self), '<link')
         self.forge_link_tree_processor = ForgeLinkTreeProcessor(md)
         md.treeprocessors['links'] = self.forge_link_tree_processor
+        md.treeprocessors.add(
+            'macro_include', ForgeMacroIncludeTreeProcessor(md), '_end')
         # Sanitize HTML
         md.postprocessors['sanitize_html'] = HTMLSanitizer()
         # Rewrite all relative links that don't start with . to have a '../'
@@ -581,3 +583,43 @@ class ForgeMacroIncludePreprocessor(markdown.preprocessors.Preprocessor):
                     buf = []
                 result.append(line)
         return result
+
+
+class ForgeMacroIncludeTreeProcessor(markdown.treeprocessors.Treeprocessor):
+    '''Remove extra <p> in which included page is wrapped.
+
+    Because it adds extra space between parent page content and included page content,
+    which looks ugly.
+
+    Convert:
+    <p><div><div class="markdown_content">...</div></div></p>
+
+    To:
+    <div class="markdown_content">...</div>
+    '''
+
+    def _get_include(self, p):
+        children = p.getchildren()
+        if len(children) != 1 or children[0].tag != 'div':
+            return
+        children = children[0].getchildren()
+        if len(children) != 1 or children[0].tag != 'div':
+            return
+        if children[0].get('class') == 'markdown_content':
+            return children[0]
+
+    def run(self, root):
+        p_with_parent = [(tag, parent)
+                         for parent in root.getiterator()
+                         for tag in parent
+                         if tag.tag == 'p']
+        replace = []
+        for p, parent in p_with_parent:
+            include =  self._get_include(p)
+            if include:
+                idx = parent.getchildren().index(p)
+                replace.append((p, parent, idx, include))  # (what, parent, index in parent, replacement)
+        for what, parent, i, replacement in replace:
+            parent.remove(what)
+            parent.insert(i, replacement)
+        return root