Skip to content

Commit

Permalink
added option to customize list class
Browse files Browse the repository at this point in the history
  • Loading branch information
FND committed Oct 1, 2016
1 parent 41a2418 commit 6f2e120
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
12 changes: 9 additions & 3 deletions markdown_checklist/extension.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@ class ChecklistExtension(Extension):

def __init__(self, **kwargs):
self.config = {
"list_class": ["checklist",
"class name to add to the list element"],
"render_item": [render_item, "custom function to render items"]
}
super(ChecklistExtension, self).__init__(**kwargs)

def extendMarkdown(self, md, md_globals):
postprocessor = ChecklistPostprocessor(self.getConfig("render_item"), md)
list_class = self.getConfig("list_class")
renderer = self.getConfig("render_item")
postprocessor = ChecklistPostprocessor(list_class, renderer, md)
md.postprocessors.add('checklist', postprocessor, '>raw_html')


Expand All @@ -33,7 +37,8 @@ class ChecklistPostprocessor(Postprocessor):
list_pattern = re.compile(r'(<ul>\n<li>\[[ Xx]\])')
item_pattern = re.compile(r'^<li>\[([ Xx])\](.*)</li>$', re.MULTILINE)

def __init__(self, render_item, *args, **kwargs):
def __init__(self, list_class, render_item, *args, **kwargs):
self.list_class = list_class
self.render_item = render_item
super(ChecklistPostprocessor, self).__init__(*args, **kwargs)

Expand All @@ -42,7 +47,8 @@ def run(self, html):
return re.sub(self.item_pattern, self._convert_item, html)

def _convert_list(self, match):
return match.group(1).replace("<ul>", '<ul class="checklist">')
return match.group(1).replace("<ul>",
'<ul class="%s">' % self.list_class)

def _convert_item(self, match):
state, caption = match.groups()
Expand Down
12 changes: 12 additions & 0 deletions test/test_customization.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,18 @@ def test_checklists():
* [ ] baz
""".strip()

expected = """
<ul class="check-list">
<li><input type="checkbox" disabled> foo</li>
<li><input type="checkbox" disabled checked> bar</li>
<li><input type="checkbox" disabled> baz</li>
</ul>
""".strip()

html = markdown(source,
extensions=[ChecklistExtension(list_class="check-list")])
assert html == expected

expected = """
<ul class="checklist">
<li><label><input type="checkbox" disabled> foo</label></li>
Expand Down

0 comments on commit 6f2e120

Please sign in to comment.