From b11926de44429e8a17df5ea74db7a1edcc6b2dcb Mon Sep 17 00:00:00 2001 From: ryneeverett Date: Thu, 3 Oct 2013 16:51:07 -0400 Subject: Miscellaneous improvements and bug fixes. --- docs/extensions/extra.txt | 12 +++++------- markdown/extensions/extra.py | 12 +++++------- markdown/preprocessors.py | 22 ++++++++++++++-------- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/docs/extensions/extra.txt b/docs/extensions/extra.txt index 6140647..0507e8f 100644 --- a/docs/extensions/extra.txt +++ b/docs/extensions/extra.txt @@ -47,12 +47,10 @@ Markdown Inside HTML Blocks Unlike the other Extra features, this feature is build into the markdown core and is turned on when `extra` is enabled. -The content of any block-level element can be Markdown-formatted simply by adding a `markdown` attribute to the opening tag. The markdown attribute will be stripped from the output, but all other attributes will be preserved. +The content of any raw html block element can be Markdown-formatted simply by adding a `markdown` attribute to the opening tag. The markdown attribute will be stripped from the output, but all other attributes will be preserved. If the markdown value is set to `1` (recommended) or any value other than `span` or `block`, the default behavior will be executed: `p`,`h[1-6]`,`li`,`dd`,`dt`,`td`,`th`,`legend`, and `address` elements skip block parsing while others do not. If the default is overrident by a value of `span`, *block parsing will be skipped* regardless of tag. If the default is overriden by a value of `block`, *block parsing will occur* regardless of tag. -*An opening tag with the markdown attribute must start immediately on a line following a blank line.* - #### Simple Example: ``` This is *true* markdown text. @@ -70,10 +68,10 @@ This is *true* markdown text. ``` ### Nested Markdown Inside HTML BLocks -Nested elements are more sensitive and must be used cautiously. Violation of the following will lead to unexpected behavior or unhandled exceptions. - * Only block mode elements may have further elements nested within them. - * The closing tag of inner elements must be followed by a blank line. - * More than one level of nesting is not supported (i.e., elements nested within elements nested within elements). This feature is not an alternative to templating. +Nested elements are more sensitive and must be used cautiously. To avoid unexpected results: + * Only nest elements within block mode elements. + * Follow the closing tag of inner elements with a blank line. + * Only have one level of nesting. #### Complex Example: ``` diff --git a/markdown/extensions/extra.py b/markdown/extensions/extra.py index dd70305..c4a0a97 100644 --- a/markdown/extensions/extra.py +++ b/markdown/extensions/extra.py @@ -75,8 +75,8 @@ class MarkdownInHtmlProcessor(BlockProcessor): # Build list of indexes of each nest within the parent element. nest_index = [] # a list of tuples: (left index, right index) i = self.parser.blockprocessors.tag_counter + 1 - while len(self.parser.markdown.htmlStash.tag_data) > i and self.\ - parser.markdown.htmlStash.tag_data[i]['left_index']: + is_nest = self.parser.markdown.htmlStash.tag_data[i]['left_index'] + while len(self.parser.markdown.htmlStash.tag_data) > i and is_nest: left_child_index = \ self.parser.markdown.htmlStash.tag_data[i]['left_index'] right_child_index = \ @@ -85,11 +85,9 @@ class MarkdownInHtmlProcessor(BlockProcessor): i += 1 # Create each nest subelement. - i = 0 - for n in nest_index[:-1]: - self.run(element, block[n[0]:n[1]], - block[n[1]:nest_index[i + 1][0]], True) - i += 1 + for i, (left_index, right_index) in enumerate(nest_index[:-1]): + self.run(element, block[left_index:right_index], + block[right_index:nest_index[i + 1][0]], True) self.run(element, block[nest_index[-1][0]:nest_index[-1][1]], # last block[nest_index[-1][1]:], True) # nest diff --git a/markdown/preprocessors.py b/markdown/preprocessors.py index c532702..1b5bc7e 100644 --- a/markdown/preprocessors.py +++ b/markdown/preprocessors.py @@ -154,9 +154,8 @@ class HtmlBlockPreprocessor(Preprocessor): def _nested_markdown_in_html(self, items): """Find and process html child elements of the given element block.""" - i = 0 - while i < len(items): - if self.left_tag_re.match(items[i]): + for i, item in enumerate(items): + if self.left_tag_re.match(item): left_tag, left_index, attrs = \ self._get_left_tag(''.join(items[i:])) right_tag, data_index = self._get_right_tag( @@ -164,10 +163,10 @@ class HtmlBlockPreprocessor(Preprocessor): right_listindex = \ self._stringindex_to_listindex(data_index, items[i:]) + i if 'markdown' in attrs.keys(): + items[i] = items[i][left_index:] # remove opening tag placeholder = self.markdown.htmlStash.store_tag( left_tag, attrs, i + 1, right_listindex + 1) - items = items[:i] + [placeholder] + \ - [items[i][left_index:]] + items[i + 1:] + items.insert(i, placeholder) if len(items) - right_listindex <= 1: # last nest, no tail right_listindex -= 1 items[right_listindex] = items[right_listindex][ @@ -179,7 +178,6 @@ class HtmlBlockPreprocessor(Preprocessor): items[i:right_listindex])) del items[i:right_listindex] items.insert(i, placeholder) - i += 1 return items def run(self, lines): @@ -272,8 +270,12 @@ class HtmlBlockPreprocessor(Preprocessor): if self.markdown_in_raw and 'markdown' in attrs.keys(): items[0] = items[0][left_index:] items[-1] = items[-1][:-len(right_tag) - 2] + if items[len(items) - 1]: # not a newline/empty string + right_index = len(items) + 3 + else: + right_index = len(items) + 2 new_blocks.append(self.markdown.htmlStash.store_tag( - left_tag, attrs, 0, len(items) + 2)) + left_tag, attrs, 0, right_index)) placeholderslen = len(self.markdown.htmlStash.tag_data) new_blocks.extend( self._nested_markdown_in_html(items)) @@ -290,9 +292,13 @@ class HtmlBlockPreprocessor(Preprocessor): if self.markdown_in_raw and 'markdown' in attrs.keys(): items[0] = items[0][left_index:] items[-1] = items[-1][:-len(right_tag) - 2] + if items[len(items) - 1]: # not a newline/empty string + right_index = len(items) + 3 + else: + right_index = len(items) + 2 new_blocks.append( self.markdown.htmlStash.store_tag( - left_tag, attrs, 0, len(items) + 2)) + left_tag, attrs, 0, right_index)) placeholderslen = len(self.markdown.htmlStash.tag_data) new_blocks.extend(self._nested_markdown_in_html(items)) nests = len(self.markdown.htmlStash.tag_data) - placeholderslen -- cgit v1.2.3