Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 5 additions & 7 deletions docs/extensions/extra.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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:
```
Expand Down
12 changes: 5 additions & 7 deletions markdown/extensions/extra.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ def _process_nests(self, element, block):
# 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 = \
Expand All @@ -85,11 +85,9 @@ def _process_nests(self, element, block):
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

Expand Down
22 changes: 14 additions & 8 deletions markdown/preprocessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,20 +154,19 @@ def _stringindex_to_listindex(self, stringindex, items):

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(
left_tag, left_index, ''.join(items[i:]))
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][
Expand All @@ -179,7 +178,6 @@ def _nested_markdown_in_html(self, items):
items[i:right_listindex]))
del items[i:right_listindex]
items.insert(i, placeholder)
i += 1
return items

def run(self, lines):
Expand Down Expand Up @@ -272,8 +270,12 @@ def run(self, lines):
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))
Expand All @@ -290,9 +292,13 @@ def run(self, lines):
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
Expand Down