Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
FND committed Sep 1, 2013
0 parents commit eb02043
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 0 deletions.
33 changes: 33 additions & 0 deletions Makefile
@@ -0,0 +1,33 @@
.PHONY: test lint coverage clean

readme:
python -c "import markdown_checklist as cl; print cl.__doc__.strip()" > README

test: clean
py.test -s --tb=short test

lint:
find . -name "*.py" -not -path "./venv/*" | while read filepath; do \
pep8 --ignore=E128,E261 $$filepath; \
done
#upyflakes $$filepath; \
#pylint --reports=n --include-ids=y $$filepath; \

coverage: clean
# option #1: figleaf
find . test -name "*.py" > coverage.lst
figleaf `which py.test` test
figleaf2html -f coverage.lst
# option #2: coverage
coverage run `which py.test` test
coverage html
# reports
coverage report
@echo "[INFO] additional reports in \`html/index.html\` (figleaf) and" \
"\`htmlcov/index.html\` (coverage)"

clean:
find . -name "*.pyc" -print0 | xargs -0 rm || true
rm -rf html .figleaf coverage.lst # figleaf
rm -rf htmlcov .coverage # coverage
rm -rf test/__pycache__ # pytest
7 changes: 7 additions & 0 deletions README
@@ -0,0 +1,7 @@
Markdown Checklist

a [Python Markdown](http://pythonhosted.org/Markdown/) extension for lists of
tasks with checkboxes

inspired by
[GitHub task lists](https://github.com/blog/1375-task-lists-in-gfm-issues-pulls-comments)
1 change: 1 addition & 0 deletions README.md
37 changes: 37 additions & 0 deletions markdown_checklist/__init__.py
@@ -0,0 +1,37 @@
"""
Markdown Checklist
a [Python Markdown](http://pythonhosted.org/Markdown/) extension for lists of
tasks with checkboxes
inspired by
[GitHub task lists](https://github.com/blog/1375-task-lists-in-gfm-issues-pulls-comments)
"""

import re

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


class ChecklistExtension(Extension):

def extendMarkdown(self, md, md_globals):
md.preprocessors.add('checklist', ChecklistPreprocessor(md),
'<reference')


class ChecklistPreprocessor(Preprocessor):

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

def run(self, lines):
return [self._transform_line(line) for line in lines]

def _transform_line(self, line):
return self.pattern.sub(self._replacer, line)

def _replacer(self, match):
list_prefix, state = match.groups()
checked = ' checked' if state != ' ' else ''
return '%s <input type="checkbox" readonly%s>' % (list_prefix, checked)
Empty file added test/__init__.py
Empty file.
38 changes: 38 additions & 0 deletions test/test_main.py
@@ -0,0 +1,38 @@
import markdown

from markdown_checklist import ChecklistExtension


def test_checkbox():
source = """
Hello World
===========
* [ ] foo
* [x] bar
* [ ] baz
lorem ipsum
""".strip()

html = markdown.markdown(source)
assert html == """
<h1>Hello World</h1>
<ul>
<li>[ ] foo</li>
<li>[x] bar</li>
<li>[ ] baz</li>
</ul>
<p>lorem ipsum</p>
""".strip()

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

0 comments on commit eb02043

Please sign in to comment.