aboutsummaryrefslogtreecommitdiffstats
path: root/markdown/inlinepatterns.py
diff options
context:
space:
mode:
authorWaylan Limberg <waylan@gmail.com>2013-02-27 09:10:47 -0500
committerWaylan Limberg <waylan@gmail.com>2013-02-27 09:10:47 -0500
commit579288c5eb684dd09d1ef298929a566f40151205 (patch)
treed10a50f91b8606cd1dea38764168abab92958b0f /markdown/inlinepatterns.py
parentb37ab16ba56ac6fe4e64f87521996bad323058f2 (diff)
downloadmarkdown-579288c5eb684dd09d1ef298929a566f40151205.tar.gz
markdown-579288c5eb684dd09d1ef298929a566f40151205.tar.bz2
markdown-579288c5eb684dd09d1ef298929a566f40151205.zip
Now using universal code for Python 2 & 3.
The most notable changes are the use of unicode_literals and absolute_imports. Actually, absolute_imports was the biggest deal as it gives us relative imports. For the first time extensions import markdown relative to themselves. This allows other packages to embed the markdown lib in a subdir of their project and still be able to use our extensions.
Diffstat (limited to 'markdown/inlinepatterns.py')
-rw-r--r--markdown/inlinepatterns.py30
1 files changed, 16 insertions, 14 deletions
diff --git a/markdown/inlinepatterns.py b/markdown/inlinepatterns.py
index f64aa58..e8ecaea 100644
--- a/markdown/inlinepatterns.py
+++ b/markdown/inlinepatterns.py
@@ -1,3 +1,4 @@
+from __future__ import unicode_literals
"""
INLINE PATTERNS
=============================================================================
@@ -41,17 +42,18 @@ So, we apply the expressions in the following order:
* finally we apply strong and emphasis
"""
-import util
-import odict
+from __future__ import absolute_import
+from . import util
+from . import odict
import re
-from urlparse import urlparse, urlunparse
-# If you see an ImportError for htmlentitydefs after using 2to3 to convert for
-# use by Python3, then you are probably using the buggy version from Python 3.0.
-# We recomend using the tool from Python 3.1 even if you will be running the
-# code on Python 3.0. The following line should be converted by the tool to:
-# `from html import entities` and later calls to `htmlentitydefs` should be
-# changed to call `entities`. Python 3.1's tool does this but 3.0's does not.
-import htmlentitydefs
+try:
+ from urllib.parse import urlparse, urlunparse
+except ImportError:
+ from urlparse import urlparse, urlunparse
+try:
+ from html import entities
+except ImportError:
+ import htmlentitydefs as entities
def build_inlinepatterns(md_instance, **kwargs):
@@ -141,7 +143,7 @@ The pattern classes
-----------------------------------------------------------------------------
"""
-class Pattern:
+class Pattern(object):
"""Base class that inline patterns subclass. """
def __init__(self, pattern, markdown_instance=None):
@@ -191,7 +193,7 @@ class Pattern:
def itertext(el):
' Reimplement Element.itertext for older python versions '
tag = el.tag
- if not isinstance(tag, basestring) and tag is not None:
+ if not isinstance(tag, util.string_type) and tag is not None:
return
if el.text:
yield el.text
@@ -204,7 +206,7 @@ class Pattern:
id = m.group(1)
if id in stash:
value = stash.get(id)
- if isinstance(value, basestring):
+ if isinstance(value, util.string_type):
return value
else:
# An etree Element - return text content only
@@ -464,7 +466,7 @@ class AutomailPattern(Pattern):
def codepoint2name(code):
"""Return entity definition by code, or the code if not defined."""
- entity = htmlentitydefs.codepoint2name.get(code)
+ entity = entities.codepoint2name.get(code)
if entity:
return "%s%s;" % (util.AMP_SUBSTITUTE, entity)
else: