Skip to content

Commit 40d59fb

Browse files
committed
Add Markdown support
Fixes: GH-1
1 parent 08e4dca commit 40d59fb

File tree

3 files changed

+131
-0
lines changed

3 files changed

+131
-0
lines changed

readme/markdown.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2014 Donald Stufft
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
from __future__ import absolute_import, division, print_function
15+
16+
import markdown
17+
18+
from .clean import clean
19+
20+
21+
def render(raw):
22+
rendered = markdown.markdown(raw)
23+
return clean(rendered or raw), bool(rendered)

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
install_requires=[
6363
"bleach",
6464
"docutils",
65+
"markdown",
6566
"Pygments",
6667
"six",
6768
],

tests/test_markdown.py

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import textwrap
2+
3+
from readme.markdown import render
4+
5+
6+
def test_markdown_001():
7+
markdown_markup = 'Hello'
8+
out, rendered = render(markdown_markup)
9+
assert rendered
10+
assert out == '<p>Hello</p>'
11+
12+
13+
def test_markdown_002():
14+
markdown_markup = textwrap.dedent("""\
15+
# Required packages
16+
To run the PyPI software, you need Python 2.5+ and PostgreSQL
17+
# Quick development setup
18+
Make sure you ...""")
19+
expected_html = textwrap.dedent("""\
20+
<h1>Required packages</h1>
21+
<p>To run the PyPI software, you need Python 2.5+ and PostgreSQL</p>
22+
<h1>Quick development setup</h1>
23+
<p>Make sure you ...</p>""")
24+
out, rendered = render(markdown_markup)
25+
assert rendered
26+
assert out == expected_html
27+
28+
29+
def test_markdown_003():
30+
markdown_markup = textwrap.dedent("""\
31+
Then, you can create a *development environment* like this,
32+
if you have **virtualenv** installed:
33+
34+
$ virtualenv --no-site-packages .
35+
$ pip install -r requirements.txt
36+
37+
Then you can launch the server using the `pypi.wsgi` script:
38+
39+
$ python pypi.wsgi
40+
Serving on port 8000...
41+
42+
PyPI will be available in your browser at http://localhost:8000
43+
""")
44+
expected_html = '\n'.join([
45+
'<p>Then, you can create a <em>development environment</em> '
46+
'like this,',
47+
'if you have <strong>virtualenv</strong> installed:</p>',
48+
'<pre><code>$ virtualenv --no-site-packages .',
49+
'$ pip install -r requirements.txt',
50+
'</code></pre>',
51+
'<p>Then you can launch the server using the '
52+
'<code>pypi.wsgi</code> script:</p>',
53+
'<pre><code>$ python pypi.wsgi',
54+
'Serving on port 8000...',
55+
'</code></pre>',
56+
'<p>PyPI will be available in your browser at '
57+
'http://localhost:8000</p>'])
58+
out, rendered = render(markdown_markup)
59+
assert rendered
60+
assert out == expected_html
61+
62+
63+
def test_markdown_004():
64+
markdown_markup = 'http://mymalicioussite.com/'
65+
out, rendered = render(markdown_markup)
66+
expected_html = '<p>http://mymalicioussite.com/</p>'
67+
assert rendered
68+
assert out == expected_html
69+
70+
71+
def test_markdown_005():
72+
markdown_markup = '<a href="http://mymalicioussite.com/">Click here</a>'
73+
out, rendered = render(markdown_markup)
74+
expected_htmls = [
75+
''.join(['<p><a rel="nofollow" href="http://mymalicioussite.com/">',
76+
'Click here</a></p>']),
77+
''.join(['<p><a href="http://mymalicioussite.com/" rel="nofollow">',
78+
'Click here</a></p>']),
79+
]
80+
assert rendered
81+
assert out in expected_htmls
82+
83+
84+
def test_markdown_006():
85+
markdown_markup = """\
86+
<iframe src="http://mymalicioussite.com/">Click here</iframe>
87+
""".strip()
88+
out, rendered = render(markdown_markup)
89+
expected_html = ''.join([
90+
'&lt;iframe src="http://mymalicioussite.com/"&gt;'
91+
'Click here&lt;/iframe&gt;'])
92+
assert rendered
93+
assert out == expected_html
94+
95+
96+
def test_markdown_007():
97+
markdown_markup = textwrap.dedent("""\
98+
<script>
99+
alert("Hello");
100+
</script>""")
101+
out, rendered = render(markdown_markup)
102+
expected_html = textwrap.dedent("""\
103+
&lt;script&gt;
104+
alert("Hello");
105+
&lt;/script&gt;""")
106+
assert rendered
107+
assert out == expected_html

0 commit comments

Comments
 (0)