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:59 UTC

[09/11] git commit: [#4771] ticket:672 Fix case when file from the repo included directly

[#4771] ticket:672 Fix case when file from the repo included directly


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

Branch: refs/heads/ib/4771
Commit: 296de69e10d9ed4b6f20d9361284967aa91b961e
Parents: f1b8958
Author: Igor Bondarenko <je...@gmail.com>
Authored: Thu Oct 23 10:20:23 2014 +0000
Committer: Igor Bondarenko <je...@gmail.com>
Committed: Thu Oct 30 09:44:23 2014 +0000

----------------------------------------------------------------------
 Allura/allura/lib/markdown_extensions.py | 39 ++++++++++++++++++++-------
 1 file changed, 29 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/allura/blob/296de69e/Allura/allura/lib/markdown_extensions.py
----------------------------------------------------------------------
diff --git a/Allura/allura/lib/markdown_extensions.py b/Allura/allura/lib/markdown_extensions.py
index 5b0542d..f93663e 100644
--- a/Allura/allura/lib/markdown_extensions.py
+++ b/Allura/allura/lib/markdown_extensions.py
@@ -419,19 +419,38 @@ class ForgeMacroPattern(markdown.inlinepatterns.Pattern):
         self.macro = macro.parse(self.ext._macro_context)
         markdown.inlinepatterns.Pattern.__init__(self, *args, **kwargs)
 
+    def _contains_headers(self, html):
+        tags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6']
+        html = BeautifulSoup(html)
+        return len(html.findAll(tags)) > 0
+
     def handleMatch(self, m):
         html = self.macro(m.group(2))
         if m.group(2).startswith('include'):
-            # etree.fromstring parses html with newlines into empty div somehow
-            html = [l.strip() for l in html.splitlines() if l.strip()]
-            html = ''.join(html)
-            try:
-                html = markdown.util.etree.fromstring(html)
-            except Exception:
-                # perhaps it is something like macro error which isn't parsable to html
-                # (e.g. "[[include: you don't have a read permission for...")
-                pass
-            return html
+            # To enable TOC extension to generate TOC with headers from
+            # included pages we need to make html, generated by [[include]]
+            # macro, available to TOC in etree form. Default
+            # "put-html-into-stash" approach does not work here, because stuff
+            # in the stash will be retrieved after all of extensions are
+            # processed and there is no way to change it.
+
+            # We're using the following workaround here:
+            # 1. Check if html contains headers (h1-h6). If it is then
+            #    transform html string to etree and return that.
+            # 2. If not then there's nothing that should go into TOC, thus we
+            #    can fall back to default approach.
+
+            if self._contains_headers(html):
+                # etree.fromstring parses html with newlines into empty div somehow
+                html = [l.strip() for l in html.splitlines() if l.strip()]
+                html = ''.join(html)
+                try:
+                    html = markdown.util.etree.fromstring(html)
+                except Exception:
+                    # perhaps it is something like macro error which isn't parsable to html
+                    # (e.g. "[[include: you don't have a read permission for...")
+                    pass
+                return html
 
         placeholder = self.markdown.htmlStash.store(html)
         return placeholder