Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add 'rel=nofollow' support

  • Loading branch information...
commit 1541e0f189030ebd6f124db80381e1a7f655f443 1 parent cf17142
@trentm authored
View
8 CHANGES.md
@@ -1,8 +1,12 @@
# python-markdown2 Changelog
-## python-markdown2 2.0.2 (not yet released)
+## python-markdown2 2.1.0 (not yet released)
-(nothing yet)
+- ["nofollow" extra, issue #74, pull #104] Add `rel="nofollow"` support
+ (mostly by https://github.com/cdman):
+
+ $ echo '[link](http://example)' | markdown2 -x nofollow
+ <p><a rel="nofollow" href="http://example">link</a></p>
## python-markdown2 2.0.1
View
11 lib/markdown2.py
@@ -61,6 +61,8 @@
some limitations.
* metadata: Extract metadata from a leading '---'-fenced block.
See <https://github.com/trentm/python-markdown2/issues/77> for details.
+* nofollow: Add `rel="nofollow"` to add `<a>` tags with an href. See
+ <http://en.wikipedia.org/wiki/Nofollow>.
* pyshell: Treats unindented Python interactive shell sessions as <code>
blocks.
* link-patterns: Auto-link given regex patterns in text (e.g. bug number
@@ -80,7 +82,7 @@
# not yet sure if there implications with this. Compare 'pydoc sre'
# and 'perldoc perlre'.
-__version_info__ = (2, 0, 2)
+__version_info__ = (2, 1, 0)
__version__ = '.'.join(map(str, __version_info__))
__author__ = "Trent Mick"
@@ -248,6 +250,10 @@ def reset(self):
if "metadata" in self.extras:
self.metadata = {}
+ # Per <https://developer.mozilla.org/en-US/docs/HTML/Element/a> "rel"
+ # should only be used in <a> tags with an "href" attribute.
+ _a_nofollow = re.compile(r"<(a)([^>]*href=)", re.IGNORECASE)
+
def convert(self, text):
"""Convert the given text."""
# Main function. The order in which other subs are called here is
@@ -328,6 +334,9 @@ def convert(self, text):
if self.safe_mode:
text = self._unhash_html_spans(text)
+ if "nofollow" in self.extras:
+ text = self._a_nofollow.sub(r'<\1 rel="nofollow"\2', text)
+
text += "\n"
rv = UnicodeWithAttrs(text)
View
17 test/tm-cases/link_nofollow.html
@@ -0,0 +1,17 @@
+<p><a rel="nofollow" href="http://example.com">link</a></p>
+
+<p>Pre-block:</p>
+
+<pre><code>Here is a raw link:
+&lt;a href="http://example.com/one"&gt;one&lt;/a&gt;
+</code></pre>
+
+<p>Raw HTML links:
+<a rel="nofollow" href="http://example.com/two">two</a>
+<a rel="nofollow" href="http://example.com/three" rel="nofollow">three</a>
+<a rel="nofollow" href="http://example.com/four" rel="foo">four</a>
+<a>five</a>
+<a rel="nofollow" href="//example.com/six">six</a>
+<A rel="nofollow" HREF="http://example.com/seven">seven</A>
+<a rel="nofollow" foo=bar href="http://example.com/eight">eight</a>
+</p>
View
1  test/tm-cases/link_nofollow.opts
@@ -0,0 +1 @@
+{"extras": ["nofollow"]}
View
1  test/tm-cases/link_nofollow.tags
@@ -0,0 +1 @@
+extras issue74 issue104
View
18 test/tm-cases/link_nofollow.text
@@ -0,0 +1,18 @@
+[link](http://example.com)
+
+Pre-block:
+
+ Here is a raw link:
+ <a href="http://example.com/one">one</a>
+
+<p>Raw HTML links:
+<a href="http://example.com/two">two</a>
+<a href="http://example.com/three" rel="nofollow">three</a>
+<a href="http://example.com/four" rel="foo">four</a>
+<a>five</a>
+<a href="//example.com/six">six</a>
+<A HREF="http://example.com/seven">seven</A>
+<a foo=bar href="http://example.com/eight">eight</a>
+</p>
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.