Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for GitHub-flavored Markdown #67

Merged
merged 2 commits into from
Mar 19, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions readme_renderer/clean.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -43,6 +43,8 @@
"hr": ["class"],
"img": ["src"],
"span": ["class"],
"th": ["align"],
"td": ["align"],
}

ALLOWED_STYLES = []
Expand Down
17 changes: 14 additions & 3 deletions readme_renderer/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
4 changes: 4 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@
"six",
],

extras_require={
"gfm": "cmarkgfm>=0.1.0"
},

entry_points={
"distutils.commands": [
"check = readme_renderer.integration.distutils:Check",
Expand Down
12 changes: 12 additions & 0 deletions tests/fixtures/test_gfm_001.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<table>
<thead>
<tr>
<th>foo</th>
<th>bar</th>
</tr>
</thead>
<tbody>
<tr>
<td>baz</td>
<td>bim</td>
</tr></tbody></table>
3 changes: 3 additions & 0 deletions tests/fixtures/test_gfm_001.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| foo | bar |
| --- | --- |
| baz | bim |
12 changes: 12 additions & 0 deletions tests/fixtures/test_gfm_002.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<table>
<thead>
<tr>
<th align="center">abc</th>
<th align="right">defghi</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center">bar</td>
<td align="right">baz</td>
</tr></tbody></table>
3 changes: 3 additions & 0 deletions tests/fixtures/test_gfm_002.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| abc | defghi |
:-: | -----------:
bar | baz
13 changes: 13 additions & 0 deletions tests/fixtures/test_gfm_003.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<table>
<thead>
<tr>
<th>f|oo</th>
</tr>
</thead>
<tbody>
<tr>
<td>b <code>|</code> az</td>
</tr>
<tr>
<td>b <strong>|</strong> im</td>
</tr></tbody></table>
4 changes: 4 additions & 0 deletions tests/fixtures/test_gfm_003.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
| f\|oo |
| ------ |
| b `\|` az |
| b **\|** im |
15 changes: 15 additions & 0 deletions tests/fixtures/test_gfm_004.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td>baz</td>
</tr></tbody></table>
<blockquote>
<p>bar</p>
</blockquote>
4 changes: 4 additions & 0 deletions tests/fixtures/test_gfm_004.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
| abc | def |
| --- | --- |
| bar | baz |
> bar
17 changes: 17 additions & 0 deletions tests/fixtures/test_gfm_005.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td>baz</td>
</tr>
<tr>
<td>bar</td>
<td></td>
</tr></tbody></table>
<p>bar</p>
6 changes: 6 additions & 0 deletions tests/fixtures/test_gfm_005.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
| abc | def |
| --- | --- |
| bar | baz |
bar

bar
3 changes: 3 additions & 0 deletions tests/fixtures/test_gfm_006.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p>| abc | def |
| --- |
| bar |</p>
3 changes: 3 additions & 0 deletions tests/fixtures/test_gfm_006.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
| abc | def |
| --- |
| bar |
16 changes: 16 additions & 0 deletions tests/fixtures/test_gfm_007.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead>
<tbody>
<tr>
<td>bar</td>
<td></td>
</tr>
<tr>
<td>bar</td>
<td>baz</td>
</tr></tbody></table>
4 changes: 4 additions & 0 deletions tests/fixtures/test_gfm_007.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
| abc | def |
| --- | --- |
| bar |
| bar | baz | boo |
7 changes: 7 additions & 0 deletions tests/fixtures/test_gfm_008.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<table>
<thead>
<tr>
<th>abc</th>
<th>def</th>
</tr>
</thead></table>
2 changes: 2 additions & 0 deletions tests/fixtures/test_gfm_008.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
| abc | def |
| --- | --- |
1 change: 1 addition & 0 deletions tests/fixtures/test_gfm_009.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><del>Hi</del> Hello, world!</p>
1 change: 1 addition & 0 deletions tests/fixtures/test_gfm_009.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
~Hi~ Hello, world!
1 change: 1 addition & 0 deletions tests/fixtures/test_gfm_010.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>This <del>text</del> is <del>curious</del>.</p>
1 change: 1 addition & 0 deletions tests/fixtures/test_gfm_010.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This ~text~~~~ is ~~~~curious~.
2 changes: 2 additions & 0 deletions tests/fixtures/test_gfm_011.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p>This ~~has a</p>
<p>new paragraph~~.</p>
3 changes: 3 additions & 0 deletions tests/fixtures/test_gfm_011.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
This ~~has a

new paragraph~~.
1 change: 1 addition & 0 deletions tests/fixtures/test_gfm_012.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><a href="http://www.commonmark.org" rel="nofollow">www.commonmark.org</a></p>
1 change: 1 addition & 0 deletions tests/fixtures/test_gfm_012.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
www.commonmark.org
1 change: 1 addition & 0 deletions tests/fixtures/test_gfm_013.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Visit <a href="http://www.commonmark.org/help" rel="nofollow">www.commonmark.org/help</a> for more information.</p>
1 change: 1 addition & 0 deletions tests/fixtures/test_gfm_013.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Visit www.commonmark.org/help for more information.
2 changes: 2 additions & 0 deletions tests/fixtures/test_gfm_014.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p>Visit <a href="http://www.commonmark.org" rel="nofollow">www.commonmark.org</a>.</p>
<p>Visit <a href="http://www.commonmark.org/a.b" rel="nofollow">www.commonmark.org/a.b</a>.</p>
3 changes: 3 additions & 0 deletions tests/fixtures/test_gfm_014.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Visit www.commonmark.org.

Visit www.commonmark.org/a.b.
2 changes: 2 additions & 0 deletions tests/fixtures/test_gfm_015.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p><a href="http://www.google.com/search?q=Markup+(business)" rel="nofollow">www.google.com/search?q=Markup+(business)</a></p>
<p>(<a href="http://www.google.com/search?q=Markup+(business)" rel="nofollow">www.google.com/search?q=Markup+(business)</a>)</p>
3 changes: 3 additions & 0 deletions tests/fixtures/test_gfm_015.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
www.google.com/search?q=Markup+(business)

(www.google.com/search?q=Markup+(business))
1 change: 1 addition & 0 deletions tests/fixtures/test_gfm_016.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><a href="http://www.google.com/search?q=(business))+ok" rel="nofollow">www.google.com/search?q=(business))+ok</a></p>
1 change: 1 addition & 0 deletions tests/fixtures/test_gfm_016.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
www.google.com/search?q=(business))+ok
2 changes: 2 additions & 0 deletions tests/fixtures/test_gfm_017.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<p><a href="http://www.google.com/search?q=commonmark&amp;hl=en" rel="nofollow">www.google.com/search?q=commonmark&amp;hl=en</a></p>
<p><a href="http://www.google.com/search?q=commonmark" rel="nofollow">www.google.com/search?q=commonmark</a>&amp;hl;</p>
3 changes: 3 additions & 0 deletions tests/fixtures/test_gfm_017.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
www.google.com/search?q=commonmark&hl=en

www.google.com/search?q=commonmark&hl;
1 change: 1 addition & 0 deletions tests/fixtures/test_gfm_018.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><a href="http://www.commonmark.org/he" rel="nofollow">www.commonmark.org/he</a>&lt;lp</p>
1 change: 1 addition & 0 deletions tests/fixtures/test_gfm_018.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
www.commonmark.org/he<lp
3 changes: 3 additions & 0 deletions tests/fixtures/test_gfm_019.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p><a href="http://commonmark.org" rel="nofollow">http://commonmark.org</a></p>
<p>(Visit <a href="https://encrypted.google.com/search?q=Markup+(business)" rel="nofollow">https://encrypted.google.com/search?q=Markup+(business)</a>)</p>
<p>Anonymous FTP is available at <a>ftp://foo.bar.baz</a>.</p>
5 changes: 5 additions & 0 deletions tests/fixtures/test_gfm_019.md
Original file line number Diff line number Diff line change
@@ -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.
1 change: 1 addition & 0 deletions tests/fixtures/test_gfm_020.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p><a href="mailto:[email protected]">[email protected]</a></p>
1 change: 1 addition & 0 deletions tests/fixtures/test_gfm_020.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[email protected]
1 change: 1 addition & 0 deletions tests/fixtures/test_gfm_021.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>hello@mail+xyz.example isn't valid, but <a href="mailto:[email protected]">[email protected]</a> is.</p>
1 change: 1 addition & 0 deletions tests/fixtures/test_gfm_021.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hello@mail+xyz.example isn't valid, but [email protected] is.
4 changes: 4 additions & 0 deletions tests/fixtures/test_gfm_022.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<p><a href="mailto:[email protected]">[email protected]</a></p>
<p><a href="mailto:[email protected]">[email protected]</a>.</p>
<p>[email protected]</p>
<p>[email protected]_</p>
7 changes: 7 additions & 0 deletions tests/fixtures/test_gfm_022.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[email protected]

[email protected].

[email protected]

[email protected]_
5 changes: 5 additions & 0 deletions tests/fixtures/test_gfm_023.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<p><strong> &lt;title&gt; &lt;style&gt; <em></em></strong></p><strong><em>
<blockquote>
&lt;xmp&gt; is disallowed. &lt;XMP&gt; is also disallowed.
</blockquote>
</em></strong>
5 changes: 5 additions & 0 deletions tests/fixtures/test_gfm_023.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<strong> <title> <style> <em>

<blockquote>
<xmp> is disallowed. <XMP> is also disallowed.
</blockquote>
36 changes: 22 additions & 14 deletions tests/test_markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,44 @@
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
with io.open(md_filename, encoding='utf-8') as f:
md_markup = f.read()
md_markup = f.read().strip()

# Get our expected
with io.open(html_filename, encoding="utf-8") as f:
expected = f.read()
expected = f.read().strip()

assert render(md_markup, variant=variant) == expected
assert render(md_markup, variant=variant).strip() == expected
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is .strip() necessary here? Can we just add/remove newlines from the fixtures as necessary?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. The reason I added this is because bleach can occasionally consume the trailing newline from things- especially when it fixes broken HTML. This is mostly just affecting on particular fixture, so I can likely just do that. WDYT?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done in 93589c7



def test_missing_variant():
assert render('Hello', variant="InvalidVariant") is None


def test_cmarkgfm_is_preferred():
assert variants['CommonMark'] is cmarkgfm.markdown_to_html
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
envlist = py27,pypy,py34,py35,py36,pep8,py2pep8,packaging

[testenv]
extras = gfm
deps =
pytest
commands =
Expand Down