diff --git a/readme_renderer/clean.py b/readme_renderer/clean.py index 74f6a93..76bf02c 100644 --- a/readme_renderer/clean.py +++ b/readme_renderer/clean.py @@ -27,9 +27,9 @@ "strong", "ul", # Custom Additions - "br", "cite", "col", "colgroup", "dd", "div", "dl", "dt", "h1", "h2", "h3", - "h4", "h5", "h6", "hr", "img", "p", "pre", "span", "sub", "sup", "table", - "tbody", "td", "th", "thead", "tr", "tt", "kbd", "var", + "br", "cite", "col", "colgroup", "dd", "del", "div", "dl", "dt", "h1", + "h2", "h3", "h4", "h5", "h6", "hr", "img", "p", "pre", "span", "sub", + "sup", "table", "tbody", "td", "th", "thead", "tr", "tt", "kbd", "var", ] ALLOWED_ATTRIBUTES = { @@ -43,6 +43,8 @@ "hr": ["class"], "img": ["src"], "span": ["class"], + "th": ["align"], + "td": ["align"], } ALLOWED_STYLES = [] diff --git a/readme_renderer/markdown.py b/readme_renderer/markdown.py index dae389e..cfdec80 100644 --- a/readme_renderer/markdown.py +++ b/readme_renderer/markdown.py @@ -18,9 +18,20 @@ from .clean import clean -variants = { - "CommonMark": commonmark, -} +try: + import cmarkgfm +except ImportError: + cmarkgfm = None + + +variants = {} + +if cmarkgfm is not None: + variants["gfm"] = cmarkgfm.github_flavored_markdown_to_html + # Preferentially use cmarkgfm for CommonMark. + variants["CommonMark"] = cmarkgfm.markdown_to_html +else: + variants["CommonMark"] = commonmark def render(raw, variant="CommonMark", **kwargs): diff --git a/setup.py b/setup.py index 08697e0..08ff708 100644 --- a/setup.py +++ b/setup.py @@ -67,6 +67,10 @@ "six", ], + extras_require={ + "gfm": "cmarkgfm>=0.1.0" + }, + entry_points={ "distutils.commands": [ "check = readme_renderer.integration.distutils:Check", diff --git a/tests/fixtures/test_gfm_001.html b/tests/fixtures/test_gfm_001.html new file mode 100644 index 0000000..822a121 --- /dev/null +++ b/tests/fixtures/test_gfm_001.html @@ -0,0 +1,12 @@ + + + + + + + + + + + +
foobar
bazbim
diff --git a/tests/fixtures/test_gfm_001.md b/tests/fixtures/test_gfm_001.md new file mode 100644 index 0000000..157376e --- /dev/null +++ b/tests/fixtures/test_gfm_001.md @@ -0,0 +1,3 @@ +| foo | bar | +| --- | --- | +| baz | bim | diff --git a/tests/fixtures/test_gfm_002.html b/tests/fixtures/test_gfm_002.html new file mode 100644 index 0000000..abb728a --- /dev/null +++ b/tests/fixtures/test_gfm_002.html @@ -0,0 +1,12 @@ + + + + + + + + + + + +
abcdefghi
barbaz
diff --git a/tests/fixtures/test_gfm_002.md b/tests/fixtures/test_gfm_002.md new file mode 100644 index 0000000..bbd53a9 --- /dev/null +++ b/tests/fixtures/test_gfm_002.md @@ -0,0 +1,3 @@ +| abc | defghi | +:-: | -----------: +bar | baz diff --git a/tests/fixtures/test_gfm_003.html b/tests/fixtures/test_gfm_003.html new file mode 100644 index 0000000..5ae5a82 --- /dev/null +++ b/tests/fixtures/test_gfm_003.html @@ -0,0 +1,13 @@ + + + + + + + + + + + + +
f|oo
b | az
b | im
diff --git a/tests/fixtures/test_gfm_003.md b/tests/fixtures/test_gfm_003.md new file mode 100644 index 0000000..b2ffaed --- /dev/null +++ b/tests/fixtures/test_gfm_003.md @@ -0,0 +1,4 @@ +| f\|oo | +| ------ | +| b `\|` az | +| b **\|** im | diff --git a/tests/fixtures/test_gfm_004.html b/tests/fixtures/test_gfm_004.html new file mode 100644 index 0000000..0cbffff --- /dev/null +++ b/tests/fixtures/test_gfm_004.html @@ -0,0 +1,15 @@ + + + + + + + + + + + +
abcdef
barbaz
+
+

bar

+
diff --git a/tests/fixtures/test_gfm_004.md b/tests/fixtures/test_gfm_004.md new file mode 100644 index 0000000..5862dd6 --- /dev/null +++ b/tests/fixtures/test_gfm_004.md @@ -0,0 +1,4 @@ +| abc | def | +| --- | --- | +| bar | baz | +> bar diff --git a/tests/fixtures/test_gfm_005.html b/tests/fixtures/test_gfm_005.html new file mode 100644 index 0000000..9428d7d --- /dev/null +++ b/tests/fixtures/test_gfm_005.html @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + +
abcdef
barbaz
bar
+

bar

diff --git a/tests/fixtures/test_gfm_005.md b/tests/fixtures/test_gfm_005.md new file mode 100644 index 0000000..3a131f1 --- /dev/null +++ b/tests/fixtures/test_gfm_005.md @@ -0,0 +1,6 @@ +| abc | def | +| --- | --- | +| bar | baz | +bar + +bar diff --git a/tests/fixtures/test_gfm_006.html b/tests/fixtures/test_gfm_006.html new file mode 100644 index 0000000..e767cc3 --- /dev/null +++ b/tests/fixtures/test_gfm_006.html @@ -0,0 +1,3 @@ +

| abc | def | +| --- | +| bar |

diff --git a/tests/fixtures/test_gfm_006.md b/tests/fixtures/test_gfm_006.md new file mode 100644 index 0000000..66abc49 --- /dev/null +++ b/tests/fixtures/test_gfm_006.md @@ -0,0 +1,3 @@ +| abc | def | +| --- | +| bar | diff --git a/tests/fixtures/test_gfm_007.html b/tests/fixtures/test_gfm_007.html new file mode 100644 index 0000000..60c90d4 --- /dev/null +++ b/tests/fixtures/test_gfm_007.html @@ -0,0 +1,16 @@ + + + + + + + + + + + + + + + +
abcdef
bar
barbaz
diff --git a/tests/fixtures/test_gfm_007.md b/tests/fixtures/test_gfm_007.md new file mode 100644 index 0000000..fe28c97 --- /dev/null +++ b/tests/fixtures/test_gfm_007.md @@ -0,0 +1,4 @@ +| abc | def | +| --- | --- | +| bar | +| bar | baz | boo | diff --git a/tests/fixtures/test_gfm_008.html b/tests/fixtures/test_gfm_008.html new file mode 100644 index 0000000..eb552af --- /dev/null +++ b/tests/fixtures/test_gfm_008.html @@ -0,0 +1,7 @@ + + + + + + +
abcdef
diff --git a/tests/fixtures/test_gfm_008.md b/tests/fixtures/test_gfm_008.md new file mode 100644 index 0000000..02f844f --- /dev/null +++ b/tests/fixtures/test_gfm_008.md @@ -0,0 +1,2 @@ +| abc | def | +| --- | --- | diff --git a/tests/fixtures/test_gfm_009.html b/tests/fixtures/test_gfm_009.html new file mode 100644 index 0000000..b143a3b --- /dev/null +++ b/tests/fixtures/test_gfm_009.html @@ -0,0 +1 @@ +

Hi Hello, world!

diff --git a/tests/fixtures/test_gfm_009.md b/tests/fixtures/test_gfm_009.md new file mode 100644 index 0000000..51f8c90 --- /dev/null +++ b/tests/fixtures/test_gfm_009.md @@ -0,0 +1 @@ +~Hi~ Hello, world! diff --git a/tests/fixtures/test_gfm_010.html b/tests/fixtures/test_gfm_010.html new file mode 100644 index 0000000..5d120fe --- /dev/null +++ b/tests/fixtures/test_gfm_010.html @@ -0,0 +1 @@ +

This text is curious.

diff --git a/tests/fixtures/test_gfm_010.md b/tests/fixtures/test_gfm_010.md new file mode 100644 index 0000000..e96f5bc --- /dev/null +++ b/tests/fixtures/test_gfm_010.md @@ -0,0 +1 @@ +This ~text~~~~ is ~~~~curious~. diff --git a/tests/fixtures/test_gfm_011.html b/tests/fixtures/test_gfm_011.html new file mode 100644 index 0000000..fa07aaf --- /dev/null +++ b/tests/fixtures/test_gfm_011.html @@ -0,0 +1,2 @@ +

This ~~has a

+

new paragraph~~.

diff --git a/tests/fixtures/test_gfm_011.md b/tests/fixtures/test_gfm_011.md new file mode 100644 index 0000000..001c5f0 --- /dev/null +++ b/tests/fixtures/test_gfm_011.md @@ -0,0 +1,3 @@ +This ~~has a + +new paragraph~~. diff --git a/tests/fixtures/test_gfm_012.html b/tests/fixtures/test_gfm_012.html new file mode 100644 index 0000000..61a6ddc --- /dev/null +++ b/tests/fixtures/test_gfm_012.html @@ -0,0 +1 @@ +

www.commonmark.org

diff --git a/tests/fixtures/test_gfm_012.md b/tests/fixtures/test_gfm_012.md new file mode 100644 index 0000000..9456ada --- /dev/null +++ b/tests/fixtures/test_gfm_012.md @@ -0,0 +1 @@ +www.commonmark.org diff --git a/tests/fixtures/test_gfm_013.html b/tests/fixtures/test_gfm_013.html new file mode 100644 index 0000000..04753e9 --- /dev/null +++ b/tests/fixtures/test_gfm_013.html @@ -0,0 +1 @@ +

Visit www.commonmark.org/help for more information.

diff --git a/tests/fixtures/test_gfm_013.md b/tests/fixtures/test_gfm_013.md new file mode 100644 index 0000000..aaf18ce --- /dev/null +++ b/tests/fixtures/test_gfm_013.md @@ -0,0 +1 @@ +Visit www.commonmark.org/help for more information. diff --git a/tests/fixtures/test_gfm_014.html b/tests/fixtures/test_gfm_014.html new file mode 100644 index 0000000..6bac270 --- /dev/null +++ b/tests/fixtures/test_gfm_014.html @@ -0,0 +1,2 @@ +

Visit www.commonmark.org.

+

Visit www.commonmark.org/a.b.

diff --git a/tests/fixtures/test_gfm_014.md b/tests/fixtures/test_gfm_014.md new file mode 100644 index 0000000..ebac363 --- /dev/null +++ b/tests/fixtures/test_gfm_014.md @@ -0,0 +1,3 @@ +Visit www.commonmark.org. + +Visit www.commonmark.org/a.b. diff --git a/tests/fixtures/test_gfm_015.html b/tests/fixtures/test_gfm_015.html new file mode 100644 index 0000000..6534f61 --- /dev/null +++ b/tests/fixtures/test_gfm_015.html @@ -0,0 +1,2 @@ +

www.google.com/search?q=Markup+(business)

+

(www.google.com/search?q=Markup+(business))

diff --git a/tests/fixtures/test_gfm_015.md b/tests/fixtures/test_gfm_015.md new file mode 100644 index 0000000..7a4cad1 --- /dev/null +++ b/tests/fixtures/test_gfm_015.md @@ -0,0 +1,3 @@ +www.google.com/search?q=Markup+(business) + +(www.google.com/search?q=Markup+(business)) diff --git a/tests/fixtures/test_gfm_016.html b/tests/fixtures/test_gfm_016.html new file mode 100644 index 0000000..d76c1b1 --- /dev/null +++ b/tests/fixtures/test_gfm_016.html @@ -0,0 +1 @@ +

www.google.com/search?q=(business))+ok

diff --git a/tests/fixtures/test_gfm_016.md b/tests/fixtures/test_gfm_016.md new file mode 100644 index 0000000..d5509ce --- /dev/null +++ b/tests/fixtures/test_gfm_016.md @@ -0,0 +1 @@ +www.google.com/search?q=(business))+ok diff --git a/tests/fixtures/test_gfm_017.html b/tests/fixtures/test_gfm_017.html new file mode 100644 index 0000000..9e00a95 --- /dev/null +++ b/tests/fixtures/test_gfm_017.html @@ -0,0 +1,2 @@ +

www.google.com/search?q=commonmark&hl=en

+

www.google.com/search?q=commonmark&hl;

diff --git a/tests/fixtures/test_gfm_017.md b/tests/fixtures/test_gfm_017.md new file mode 100644 index 0000000..ff996c0 --- /dev/null +++ b/tests/fixtures/test_gfm_017.md @@ -0,0 +1,3 @@ +www.google.com/search?q=commonmark&hl=en + +www.google.com/search?q=commonmark&hl; diff --git a/tests/fixtures/test_gfm_018.html b/tests/fixtures/test_gfm_018.html new file mode 100644 index 0000000..a12ebb9 --- /dev/null +++ b/tests/fixtures/test_gfm_018.html @@ -0,0 +1 @@ +

www.commonmark.org/he<lp

diff --git a/tests/fixtures/test_gfm_018.md b/tests/fixtures/test_gfm_018.md new file mode 100644 index 0000000..2c63794 --- /dev/null +++ b/tests/fixtures/test_gfm_018.md @@ -0,0 +1 @@ +www.commonmark.org/hehttp://commonmark.org

+

(Visit https://encrypted.google.com/search?q=Markup+(business))

+

Anonymous FTP is available at ftp://foo.bar.baz.

diff --git a/tests/fixtures/test_gfm_019.md b/tests/fixtures/test_gfm_019.md new file mode 100644 index 0000000..b6f1242 --- /dev/null +++ b/tests/fixtures/test_gfm_019.md @@ -0,0 +1,5 @@ +http://commonmark.org + +(Visit https://encrypted.google.com/search?q=Markup+(business)) + +Anonymous FTP is available at ftp://foo.bar.baz. diff --git a/tests/fixtures/test_gfm_020.html b/tests/fixtures/test_gfm_020.html new file mode 100644 index 0000000..5904880 --- /dev/null +++ b/tests/fixtures/test_gfm_020.html @@ -0,0 +1 @@ +

foo@bar.baz

diff --git a/tests/fixtures/test_gfm_020.md b/tests/fixtures/test_gfm_020.md new file mode 100644 index 0000000..df200e5 --- /dev/null +++ b/tests/fixtures/test_gfm_020.md @@ -0,0 +1 @@ +foo@bar.baz diff --git a/tests/fixtures/test_gfm_021.html b/tests/fixtures/test_gfm_021.html new file mode 100644 index 0000000..85ecf64 --- /dev/null +++ b/tests/fixtures/test_gfm_021.html @@ -0,0 +1 @@ +

hello@mail+xyz.example isn't valid, but hello+xyz@mail.example is.

diff --git a/tests/fixtures/test_gfm_021.md b/tests/fixtures/test_gfm_021.md new file mode 100644 index 0000000..50923a1 --- /dev/null +++ b/tests/fixtures/test_gfm_021.md @@ -0,0 +1 @@ +hello@mail+xyz.example isn't valid, but hello+xyz@mail.example is. diff --git a/tests/fixtures/test_gfm_022.html b/tests/fixtures/test_gfm_022.html new file mode 100644 index 0000000..470265d --- /dev/null +++ b/tests/fixtures/test_gfm_022.html @@ -0,0 +1,4 @@ +

a.b-c_d@a.b

+

a.b-c_d@a.b.

+

a.b-c_d@a.b-

+

a.b-c_d@a.b_

diff --git a/tests/fixtures/test_gfm_022.md b/tests/fixtures/test_gfm_022.md new file mode 100644 index 0000000..b3bbdd0 --- /dev/null +++ b/tests/fixtures/test_gfm_022.md @@ -0,0 +1,7 @@ +a.b-c_d@a.b + +a.b-c_d@a.b. + +a.b-c_d@a.b- + +a.b-c_d@a.b_ diff --git a/tests/fixtures/test_gfm_023.html b/tests/fixtures/test_gfm_023.html new file mode 100644 index 0000000..21f976a --- /dev/null +++ b/tests/fixtures/test_gfm_023.html @@ -0,0 +1,5 @@ +

<title> <style>

+
+ <xmp> is disallowed. <XMP> is also disallowed. +
+
\ No newline at end of file diff --git a/tests/fixtures/test_gfm_023.md b/tests/fixtures/test_gfm_023.md new file mode 100644 index 0000000..7d53b14 --- /dev/null +++ b/tests/fixtures/test_gfm_023.md @@ -0,0 +1,5 @@ + <style> <em> + +<blockquote> + <xmp> is disallowed. <XMP> is also disallowed. +</blockquote> diff --git a/tests/test_markdown.py b/tests/test_markdown.py index 0b87b84..cc92236 100755 --- a/tests/test_markdown.py +++ b/tests/test_markdown.py @@ -2,24 +2,28 @@ import glob import os +import cmarkgfm import pytest from readme_renderer.markdown import render, variants +MD_FIXTURES = [ + (fn, os.path.splitext(fn)[0] + ".html", variant) + for variant in variants + for fn in glob.glob( + os.path.join( + os.path.dirname(__file__), + "fixtures", + "test_" + variant + "*.md" + ) + ) +] + + @pytest.mark.parametrize( ("md_filename", "html_filename", "variant"), - [ - (fn, os.path.splitext(fn)[0] + ".html", variant) - for variant in variants - for fn in glob.glob( - os.path.join( - os.path.dirname(__file__), - "fixtures", - "test_" + variant + "*.md" - ) - ) - ], + MD_FIXTURES, ) def test_md_fixtures(md_filename, html_filename, variant): # Get our Markup @@ -35,3 +39,7 @@ def test_md_fixtures(md_filename, html_filename, variant): def test_missing_variant(): assert render('Hello', variant="InvalidVariant") is None + + +def test_cmarkgfm_is_preferred(): + assert variants['CommonMark'] is cmarkgfm.markdown_to_html diff --git a/tox.ini b/tox.ini index b4b313e..a98369f 100644 --- a/tox.ini +++ b/tox.ini @@ -2,6 +2,7 @@ envlist = py27,pypy,py34,py35,py36,pep8,py2pep8,packaging [testenv] +extras = gfm deps = pytest commands =