Skip to content

Commit

Permalink
added checklist class to list elements
Browse files Browse the repository at this point in the history
this makes it easier for external entities (e.g. CSS, JavaScript) to
identify checklists

using a postprocessor is a little crude, but pragmatic
  • Loading branch information
FND committed Sep 15, 2013
1 parent e32f634 commit e226c2a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 5 deletions.
2 changes: 1 addition & 1 deletion markdown_checklist/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,6 @@
See included `checklists.js` for details.
"""

__version__ = '0.2.0'
__version__ = '0.3.0'
__author__ = 'FND'
__license__ = 'MIT'
2 changes: 1 addition & 1 deletion markdown_checklist/checklists.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function Checklists(container, retriever, storer) {
this.retriever = retriever;
this.storer = storer;

var checklists = $("ul:has(" + this.checkboxSelector + ")", container); // XXX: imprecise, inefficient
var checklists = $(".checklist", container);
checklists.find(this.checkboxSelector).prop("disabled", false);
var self = this;
checklists.on("change", this.checkboxSelector, function() {
Expand Down
19 changes: 19 additions & 0 deletions markdown_checklist/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from markdown.extensions import Extension
from markdown.preprocessors import Preprocessor
from markdown.postprocessors import Postprocessor


def makeExtension(configs=None):
Expand All @@ -13,9 +14,14 @@ class ChecklistExtension(Extension):
def extendMarkdown(self, md, md_globals):
md.preprocessors.add('checklist', ChecklistPreprocessor(md),
'<reference')
md.postprocessors.add('checklist', ChecklistPostprocessor(md),
'>unescape')


class ChecklistPreprocessor(Preprocessor):
"""
injects checkbox elements
"""

pattern = re.compile(r'^([*-]) \[([ Xx])\]')

Expand All @@ -29,3 +35,16 @@ def _replacer(self, match):
list_prefix, state = match.groups()
checked = ' checked' if state != ' ' else ''
return '%s <input type="checkbox" disabled%s>' % (list_prefix, checked)


class ChecklistPostprocessor(Postprocessor):
"""
adds checklist class to list element
"""

pattern = re.compile(r'^([*-]) \[([ Xx])\]')

def run(self, html):
before = '<ul>\n<li><input type="checkbox"'
after = before.replace('<ul>', '<ul class="checklist">')
return html.replace(before, after)
35 changes: 32 additions & 3 deletions test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_checklists():

expected = """
<h1>Hello World</h1>
<ul>
<ul class="checklist">
<li><input type="checkbox" disabled> foo</li>
<li><input type="checkbox" disabled checked> bar</li>
<li><input type="checkbox" disabled> baz</li>
Expand All @@ -55,13 +55,42 @@ def test_syntax_variations():
lorem ipsum
""".strip()

html = markdown(source, extensions=['markdown_checklist.extension'])
html = markdown(source, extensions=[ChecklistExtension()])
assert html == """
<h1>Hello World</h1>
<ul>
<ul class="checklist">
<li><input type="checkbox" disabled checked> foo</li>
<li><input type="checkbox" disabled> bar</li>
<li><input type="checkbox" disabled checked> baz</li>
</ul>
<p>lorem ipsum</p>
""".strip()


def test_class():
source = """
* [x] foo
* [ ] bar
* [X] baz
----
* [ ] lorem
* [x] ipsum
* [ ] ...
""".strip()

html = markdown(source, extensions=[ChecklistExtension()])
assert html == """
<ul class="checklist">
<li><input type="checkbox" disabled checked> foo</li>
<li><input type="checkbox" disabled> bar</li>
<li><input type="checkbox" disabled checked> baz</li>
</ul>
<hr />
<ul class="checklist">
<li><input type="checkbox" disabled> lorem</li>
<li><input type="checkbox" disabled checked> ipsum</li>
<li><input type="checkbox" disabled> ...</li>
</ul>
""".strip()

0 comments on commit e226c2a

Please sign in to comment.