diff options
author | Waylan Limberg <waylan@gmail.com> | 2012-01-24 07:57:58 -0500 |
---|---|---|
committer | Waylan Limberg <waylan@gmail.com> | 2012-01-24 07:57:58 -0500 |
commit | a9cd895467b3caa08d585458fdcbcc5525356493 (patch) | |
tree | 2d99fb25c551217a6e59698f193b649ebdca65b8 | |
parent | acd09498fbd2b13e65038c96a118fd859a6df235 (diff) | |
download | markdown-a9cd895467b3caa08d585458fdcbcc5525356493.tar.gz markdown-a9cd895467b3caa08d585458fdcbcc5525356493.tar.bz2 markdown-a9cd895467b3caa08d585458fdcbcc5525356493.zip |
Fixes #64. Added sane_list extension.
This is an extension which alters the behavior of lists to be less surprising.
If turned out to be rather simple to code. I'm surprised no one had written it
yet.
I should note that this extension does not generate separate lists if different
unordered list markers are used (`*` verses `-` verses `+`) The way the parser
works that would be a little more tricky.
-rw-r--r-- | docs/extensions/index.md | 1 | ||||
-rw-r--r-- | docs/extensions/sane_lists.md | 75 | ||||
-rw-r--r-- | markdown/extensions/sane_lists.py | 49 | ||||
-rw-r--r-- | tests/extensions/sane_lists.html | 21 | ||||
-rw-r--r-- | tests/extensions/sane_lists.txt | 16 | ||||
-rw-r--r-- | tests/extensions/test.cfg | 3 |
6 files changed, 165 insertions, 0 deletions
diff --git a/docs/extensions/index.md b/docs/extensions/index.md index 7ab2414..82f00a4 100644 --- a/docs/extensions/index.md +++ b/docs/extensions/index.md @@ -40,6 +40,7 @@ available to you. * [Meta-Data](meta_data.html) * [New Line to Break](nl2br.html) * [RSS](rss.html) +* [Sane Lists](sane_lists.html) * [Table of Contents](toc.html) * [WikiLinks](wikilinks.html) diff --git a/docs/extensions/sane_lists.md b/docs/extensions/sane_lists.md new file mode 100644 index 0000000..b1c1d06 --- /dev/null +++ b/docs/extensions/sane_lists.md @@ -0,0 +1,75 @@ +Sane Lists +---------- + +Summary +------- + +The Sane Lists Extension alters the behavior of the Markdown List syntax +to be less surprising. + +This extension is included in the standard Markdown library. + +Syntax +------ + +Sane Lists do not allow the mixing of list types. In other words, an ordered +list will not continue when an unordered list item is encountered and +vice versa. For example: + + 1. Ordered item 1 + 2. Ordered item 2 + + * Unordered item 1 + * Unordered item 2 + +will result in the following output: + + <ol> + <li>Ordered item 1</li> + <li>Ordered item 2</li> + </ol> + + <ul> + <li>Unordered item 1</li> + <li>Unordered item 2</li> + </ul> + +Whereas the default Markdown behavior would be to generate an unordered list. + +Note that, unlike the default Markdown behavior, if a blank line is not +included between list items, the different list type is ignored completely. +This corresponds to the behavior of paragraphs. For example: + + A Paragraph. + * Not a list item. + + 1. Ordered list item. + * Not a separate list item. + +With this extension the above will result in the following output: + + <p>A Paragraph. + * Not a list item.</p> + + <ol> + <li>Ordered list item. + * Not a separate list item.</li> + </ol> + +In all other ways, Sane Lists should behave as normal Markdown lists. + +Usage +----- + +From the Python interpreter: + + >>> html = markdown.markdown(text, ['sane_lists']) + +To use with other extensions, just add them to the list, like this: + + >>> html = markdown.markdown(text, ['def_list', 'sane_lists']) + +The extension can also be called from the command line using Markdown's `-x` +parameter: + + python -m markdown -x sane_lists source.txt > output.html diff --git a/markdown/extensions/sane_lists.py b/markdown/extensions/sane_lists.py new file mode 100644 index 0000000..dce04ea --- /dev/null +++ b/markdown/extensions/sane_lists.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python +""" +Sane List Extension for Python-Markdown +======================================= + +Modify the behavior of Lists in Python-Markdown t act in a sane manor. + +In standard Markdown sytex, the following would constitute a single +ordered list. However, with this extension, the output would include +two lists, the first an ordered list and the second and unordered list. + + 1. ordered + 2. list + + * unordered + * list + +Copyright 2011 - [Waylan Limberg](http://achinghead.com) + +""" + +import re +import markdown + + +class SaneOListProcessor(markdown.blockprocessors.OListProcessor): + + CHILD_RE = re.compile(r'^[ ]{0,3}((\d+\.))[ ]+(.*)') + SIBLING_TAGS = ['ol'] + + +class SaneUListProcessor(markdown.blockprocessors.UListProcessor): + + CHILD_RE = re.compile(r'^[ ]{0,3}(([*+-]))[ ]+(.*)') + SIBLING_TAGS = ['ul'] + + +class SaneListExtension(markdown.Extension): + """ Add sane lists to Markdown. """ + + def extendMarkdown(self, md, md_globals): + """ Override existing Processors. """ + md.parser.blockprocessors['olist'] = SaneOListProcessor(md.parser) + md.parser.blockprocessors['ulist'] = SaneUListProcessor(md.parser) + + +def makeExtension(configs={}): + return SaneListExtension(configs=configs) + diff --git a/tests/extensions/sane_lists.html b/tests/extensions/sane_lists.html new file mode 100644 index 0000000..b9fe007 --- /dev/null +++ b/tests/extensions/sane_lists.html @@ -0,0 +1,21 @@ +<ol> +<li>Ordered</li> +<li>List</li> +</ol> +<ul> +<li>Unordered</li> +<li>List</li> +</ul> +<ol> +<li>Ordered again</li> +</ol> +<p>Paragraph +* not a list item</p> +<ol> +<li>More ordered +* not a list item</li> +</ol> +<ul> +<li>Unordered again +1. not a list item</li> +</ul>
\ No newline at end of file diff --git a/tests/extensions/sane_lists.txt b/tests/extensions/sane_lists.txt new file mode 100644 index 0000000..51981b3 --- /dev/null +++ b/tests/extensions/sane_lists.txt @@ -0,0 +1,16 @@ +1. Ordered +2. List + +* Unordered +* List + +1. Ordered again + +Paragraph +* not a list item + +1. More ordered +* not a list item + +* Unordered again +1. not a list item diff --git a/tests/extensions/test.cfg b/tests/extensions/test.cfg index 19721ce..ac8a747 100644 --- a/tests/extensions/test.cfg +++ b/tests/extensions/test.cfg @@ -26,3 +26,6 @@ extensions=fenced_code [github_flavored] extensions=fenced_code + +[sane_lists] +extensions=sane_lists |