Index: third_party/Python-Markdown/markdown/__init__.py |
diff --git a/third_party/markdown/__init__.py b/third_party/Python-Markdown/markdown/__init__.py |
similarity index 63% |
copy from third_party/markdown/__init__.py |
copy to third_party/Python-Markdown/markdown/__init__.py |
index 0aa15a7e8f950a7b8789b8cbf100850ce9074d84..1b865531363cb8dd3912ee81aa8f2f767bf68f98 100644 |
--- a/third_party/markdown/__init__.py |
+++ b/third_party/Python-Markdown/markdown/__init__.py |
@@ -1,35 +1,3 @@ |
-# markdown is released under the BSD license |
-# Copyright 2007, 2008 The Python Markdown Project (v. 1.7 and later) |
-# Copyright 2004, 2005, 2006 Yuri Takhteyev (v. 0.2-1.6b) |
-# Copyright 2004 Manfred Stienstra (the original version) |
-# |
-# All rights reserved. |
-# |
-# Redistribution and use in source and binary forms, with or without |
-# modification, are permitted provided that the following conditions are met: |
-# |
-# * Redistributions of source code must retain the above copyright |
-# notice, this list of conditions and the following disclaimer. |
-# * Redistributions in binary form must reproduce the above copyright |
-# notice, this list of conditions and the following disclaimer in the |
-# documentation and/or other materials provided with the distribution. |
-# * Neither the name of the <organization> nor the |
-# names of its contributors may be used to endorse or promote products |
-# derived from this software without specific prior written permission. |
-# |
-# THIS SOFTWARE IS PROVIDED BY THE PYTHON MARKDOWN PROJECT ''AS IS'' AND ANY |
-# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
-# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
-# DISCLAIMED. IN NO EVENT SHALL ANY CONTRIBUTORS TO THE PYTHON MARKDOWN PROJECT |
-# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
-# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
-# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
-# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
-# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
-# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
-# POSSIBILITY OF SUCH DAMAGE. |
- |
- |
""" |
Python Markdown |
=============== |
@@ -42,7 +10,7 @@ called from the command line. |
import markdown |
html = markdown.markdown(your_text_string) |
-See <http://packages.python.org/Markdown/> for more |
+See <https://pythonhosted.org/Markdown/> for more |
information and instructions on how to extend the functionality of |
Python Markdown. Read that before you try modifying this file. |
@@ -64,11 +32,12 @@ License: BSD (see LICENSE for details). |
from __future__ import absolute_import |
from __future__ import unicode_literals |
-from .__version__ import version, version_info |
-import re |
+from .__version__ import version, version_info # noqa |
import codecs |
import sys |
import logging |
+import warnings |
+import importlib |
from . import util |
from .preprocessors import build_preprocessors |
from .blockprocessors import build_block_parser |
@@ -80,6 +49,7 @@ from .serializers import to_html_string, to_xhtml_string |
__all__ = ['Markdown', 'markdown', 'markdownFromFile'] |
+ |
logger = logging.getLogger('MARKDOWN') |
@@ -89,24 +59,24 @@ class Markdown(object): |
doc_tag = "div" # Element used to wrap document - later removed |
option_defaults = { |
- 'html_replacement_text' : '[HTML_REMOVED]', |
- 'tab_length' : 4, |
- 'enable_attributes' : True, |
- 'smart_emphasis' : True, |
- 'lazy_ol' : True, |
+ 'html_replacement_text': '[HTML_REMOVED]', |
+ 'tab_length': 4, |
+ 'enable_attributes': True, |
+ 'smart_emphasis': True, |
+ 'lazy_ol': True, |
} |
output_formats = { |
- 'html' : to_html_string, |
- 'html4' : to_html_string, |
- 'html5' : to_html_string, |
- 'xhtml' : to_xhtml_string, |
+ 'html': to_html_string, |
+ 'html4': to_html_string, |
+ 'html5': to_html_string, |
+ 'xhtml': to_xhtml_string, |
'xhtml1': to_xhtml_string, |
'xhtml5': to_xhtml_string, |
} |
ESCAPED_CHARS = ['\\', '`', '*', '_', '{', '}', '[', ']', |
- '(', ')', '>', '#', '+', '-', '.', '!'] |
+ '(', ')', '>', '#', '+', '-', '.', '!'] |
def __init__(self, *args, **kwargs): |
""" |
@@ -118,36 +88,42 @@ class Markdown(object): |
If they are of type string, the module mdx_name.py will be loaded. |
If they are a subclass of markdown.Extension, they will be used |
as-is. |
- * extension_configs: Configuration settingis for extensions. |
+ * extension_configs: Configuration settings for extensions. |
* output_format: Format of output. Supported formats are: |
* "xhtml1": Outputs XHTML 1.x. Default. |
* "xhtml5": Outputs XHTML style tags of HTML 5 |
- * "xhtml": Outputs latest supported version of XHTML (currently XHTML 1.1). |
+ * "xhtml": Outputs latest supported version of XHTML |
+ (currently XHTML 1.1). |
* "html4": Outputs HTML 4 |
* "html5": Outputs HTML style tags of HTML 5 |
- * "html": Outputs latest supported version of HTML (currently HTML 4). |
+ * "html": Outputs latest supported version of HTML |
+ (currently HTML 4). |
Note that it is suggested that the more specific formats ("xhtml1" |
and "html4") be used as "xhtml" or "html" may change in the future |
if it makes sense at that time. |
- * safe_mode: Disallow raw html. One of "remove", "replace" or "escape". |
- * html_replacement_text: Text used when safe_mode is set to "replace". |
+ * safe_mode: Deprecated! Disallow raw html. One of "remove", "replace" |
+ or "escape". |
+ * html_replacement_text: Deprecated! Text used when safe_mode is set |
+ to "replace". |
* tab_length: Length of tabs in the source. Default: 4 |
* enable_attributes: Enable the conversion of attributes. Default: True |
- * smart_emphasis: Treat `_connected_words_` intelegently Default: True |
+ * smart_emphasis: Treat `_connected_words_` intelligently Default: True |
* lazy_ol: Ignore number of first item of ordered lists. Default: True |
""" |
# For backward compatibility, loop through old positional args |
pos = ['extensions', 'extension_configs', 'safe_mode', 'output_format'] |
- c = 0 |
- for arg in args: |
+ for c, arg in enumerate(args): |
if pos[c] not in kwargs: |
kwargs[pos[c]] = arg |
- c += 1 |
- if c == len(pos): |
+ if c+1 == len(pos): # pragma: no cover |
# ignore any additional args |
break |
+ if len(args): |
+ warnings.warn('Positional arguments are deprecated in Markdown. ' |
+ 'Use keyword arguments only.', |
+ DeprecationWarning) |
# Loop through kwargs and assign defaults |
for option, default in self.option_defaults.items(): |
@@ -158,6 +134,19 @@ class Markdown(object): |
# Disable attributes in safeMode when not explicitly set |
self.enable_attributes = False |
+ if 'safe_mode' in kwargs: |
+ warnings.warn('"safe_mode" is deprecated in Python-Markdown. ' |
+ 'Use an HTML sanitizer (like ' |
+ 'Bleach http://bleach.readthedocs.org/) ' |
+ 'if you are parsing untrusted markdown text. ' |
+ 'See the 2.6 release notes for more info', |
+ DeprecationWarning) |
+ |
+ if 'html_replacement_text' in kwargs: |
+ warnings.warn('The "html_replacement_text" keyword is ' |
+ 'deprecated along with "safe_mode".', |
+ DeprecationWarning) |
+ |
self.registeredExtensions = [] |
self.docType = "" |
self.stripTopLevelTags = True |
@@ -166,9 +155,9 @@ class Markdown(object): |
self.references = {} |
self.htmlStash = util.HtmlStash() |
- self.set_output_format(kwargs.get('output_format', 'xhtml1')) |
self.registerExtensions(extensions=kwargs.get('extensions', []), |
configs=kwargs.get('extension_configs', {})) |
+ self.set_output_format(kwargs.get('output_format', 'xhtml1')) |
self.reset() |
def build_parser(self): |
@@ -193,9 +182,13 @@ class Markdown(object): |
""" |
for ext in extensions: |
if isinstance(ext, util.string_type): |
- ext = self.build_extension(ext, configs.get(ext, [])) |
+ ext = self.build_extension(ext, configs.get(ext, {})) |
if isinstance(ext, Extension): |
ext.extendMarkdown(self, globals()) |
+ logger.debug( |
+ 'Successfully loaded extension "%s.%s".' |
+ % (ext.__class__.__module__, ext.__class__.__name__) |
+ ) |
elif ext is not None: |
raise TypeError( |
'Extension "%s.%s" must be of type: "markdown.Extension"' |
@@ -203,7 +196,7 @@ class Markdown(object): |
return self |
- def build_extension(self, ext_name, configs = []): |
+ def build_extension(self, ext_name, configs): |
"""Build extension by name, then return the module. |
The extension name may contain arguments as part of the string in the |
@@ -211,44 +204,98 @@ class Markdown(object): |
""" |
- # Parse extensions config params (ignore the order) |
configs = dict(configs) |
- pos = ext_name.find("(") # find the first "(" |
+ |
+ # Parse extensions config params (ignore the order) |
+ pos = ext_name.find("(") # find the first "(" |
if pos > 0: |
ext_args = ext_name[pos+1:-1] |
ext_name = ext_name[:pos] |
pairs = [x.split("=") for x in ext_args.split(",")] |
configs.update([(x.strip(), y.strip()) for (x, y) in pairs]) |
- |
- # Setup the module name |
- module_name = ext_name |
- if '.' not in ext_name: |
- module_name = '.'.join(['third_party.markdown.extensions', ext_name]) |
+ warnings.warn('Setting configs in the Named Extension string is ' |
+ 'deprecated. It is recommended that you ' |
+ 'pass an instance of the extension class to ' |
+ 'Markdown or use the "extension_configs" keyword. ' |
+ 'The current behavior will raise an error in version 2.7. ' |
+ 'See the Release Notes for Python-Markdown version ' |
+ '2.6 for more info.', DeprecationWarning) |
+ |
+ # Get class name (if provided): `path.to.module:ClassName` |
+ ext_name, class_name = ext_name.split(':', 1) \ |
+ if ':' in ext_name else (ext_name, '') |
# Try loading the extension first from one place, then another |
- try: # New style (markdown.extensons.<extension>) |
- module = __import__(module_name, {}, {}, [module_name.rpartition('.')[0]]) |
+ try: |
+ # Assume string uses dot syntax (`path.to.some.module`) |
+ module = importlib.import_module(ext_name) |
+ logger.debug( |
+ 'Successfuly imported extension module "%s".' % ext_name |
+ ) |
+ # For backward compat (until deprecation) |
+ # check that this is an extension. |
+ if ('.' not in ext_name and not (hasattr(module, 'makeExtension') or |
+ (class_name and hasattr(module, class_name)))): |
+ # We have a name conflict |
+ # eg: extensions=['tables'] and PyTables is installed |
+ raise ImportError |
except ImportError: |
- module_name_old_style = '_'.join(['mdx', ext_name]) |
- try: # Old style (mdx_<extension>) |
- module = __import__(module_name_old_style) |
- except ImportError as e: |
- message = "Failed loading extension '%s' from '%s' or '%s'" \ |
- % (ext_name, module_name, module_name_old_style) |
+ # Preppend `markdown.extensions.` to name |
+ module_name = '.'.join(['markdown.extensions', ext_name]) |
+ try: |
+ module = importlib.import_module(module_name) |
+ logger.debug( |
+ 'Successfuly imported extension module "%s".' % |
+ module_name |
+ ) |
+ warnings.warn('Using short names for Markdown\'s builtin ' |
+ 'extensions is deprecated. Use the ' |
+ 'full path to the extension with Python\'s dot ' |
+ 'notation (eg: "%s" instead of "%s"). The ' |
+ 'current behavior will raise an error in version ' |
+ '2.7. See the Release Notes for ' |
+ 'Python-Markdown version 2.6 for more info.' % |
+ (module_name, ext_name), |
+ DeprecationWarning) |
+ except ImportError: |
+ # Preppend `mdx_` to name |
+ module_name_old_style = '_'.join(['mdx', ext_name]) |
+ try: |
+ module = importlib.import_module(module_name_old_style) |
+ logger.debug( |
+ 'Successfuly imported extension module "%s".' % |
+ module_name_old_style) |
+ warnings.warn('Markdown\'s behavior of prepending "mdx_" ' |
+ 'to an extension name is deprecated. ' |
+ 'Use the full path to the ' |
+ 'extension with Python\'s dot notation ' |
+ '(eg: "%s" instead of "%s"). The current ' |
+ 'behavior will raise an error in version 2.7. ' |
+ 'See the Release Notes for Python-Markdown ' |
+ 'version 2.6 for more info.' % |
+ (module_name_old_style, ext_name), |
+ DeprecationWarning) |
+ except ImportError as e: |
+ message = "Failed loading extension '%s' from '%s', '%s' " \ |
+ "or '%s'" % (ext_name, ext_name, module_name, |
+ module_name_old_style) |
+ e.args = (message,) + e.args[1:] |
+ raise |
+ |
+ if class_name: |
+ # Load given class name from module. |
+ return getattr(module, class_name)(**configs) |
+ else: |
+ # Expect makeExtension() function to return a class. |
+ try: |
+ return module.makeExtension(**configs) |
+ except AttributeError as e: |
+ message = e.args[0] |
+ message = "Failed to initiate extension " \ |
+ "'%s': %s" % (ext_name, message) |
e.args = (message,) + e.args[1:] |
raise |
- # If the module is loaded successfully, we expect it to define a |
- # function called makeExtension() |
- try: |
- return module.makeExtension(configs.items()) |
- except AttributeError as e: |
- message = e.args[0] |
- message = "Failed to initiate extension " \ |
- "'%s': %s" % (ext_name, message) |
- e.args = (message,) + e.args[1:] |
- raise |
- |
def registerExtension(self, extension): |
""" This gets called by the extension """ |
self.registeredExtensions.append(extension) |
@@ -276,8 +323,8 @@ class Markdown(object): |
valid_formats = list(self.output_formats.keys()) |
valid_formats.sort() |
message = 'Invalid Output Format: "%s". Use one of %s.' \ |
- % (self.output_format, |
- '"' + '", "'.join(valid_formats) + '"') |
+ % (self.output_format, |
+ '"' + '", "'.join(valid_formats) + '"') |
e.args = (message,) + e.args[1:] |
raise |
return self |
@@ -326,23 +373,25 @@ class Markdown(object): |
# Run the tree-processors |
for treeprocessor in self.treeprocessors.values(): |
newRoot = treeprocessor.run(root) |
- if newRoot: |
+ if newRoot is not None: |
root = newRoot |
# Serialize _properly_. Strip top-level tags. |
output = self.serializer(root) |
if self.stripTopLevelTags: |
try: |
- start = output.index('<%s>'%self.doc_tag)+len(self.doc_tag)+2 |
- end = output.rindex('</%s>'%self.doc_tag) |
+ start = output.index( |
+ '<%s>' % self.doc_tag) + len(self.doc_tag) + 2 |
+ end = output.rindex('</%s>' % self.doc_tag) |
output = output[start:end].strip() |
- except ValueError: |
- if output.strip().endswith('<%s />'%self.doc_tag): |
+ except ValueError: # pragma: no cover |
+ if output.strip().endswith('<%s />' % self.doc_tag): |
# We have an empty document |
output = '' |
else: |
# We have a serious problem |
- raise ValueError('Markdown failed to strip top-level tags. Document=%r' % output.strip()) |
+ raise ValueError('Markdown failed to strip top-level ' |
+ 'tags. Document=%r' % output.strip()) |
# Run the text post-processors |
for pp in self.postprocessors.values(): |
@@ -351,7 +400,7 @@ class Markdown(object): |
return output.strip() |
def convertFile(self, input=None, output=None, encoding=None): |
- """Converts a markdown file and returns the HTML as a unicode string. |
+ """Converts a Markdown file and returns the HTML as a Unicode string. |
Decodes the file using the provided encoding (defaults to utf-8), |
passes the file content to markdown, and outputs the html to either |
@@ -359,9 +408,9 @@ class Markdown(object): |
encoding as the source file. The 'xmlcharrefreplace' error handler is |
used when encoding the output. |
- **Note:** This is the only place that decoding and encoding of unicode |
- takes place in Python-Markdown. (All other code is unicode-in / |
- unicode-out.) |
+ **Note:** This is the only place that decoding and encoding of Unicode |
+ takes place in Python-Markdown. (All other code is Unicode-in / |
+ Unicode-out.) |
Keyword arguments: |
@@ -386,7 +435,7 @@ class Markdown(object): |
if not isinstance(text, util.text_type): |
text = text.decode(encoding) |
- text = text.lstrip('\ufeff') # remove the byte-order mark |
+ text = text.lstrip('\ufeff') # remove the byte-order mark |
# Convert |
html = self.convert(text) |
@@ -405,7 +454,7 @@ class Markdown(object): |
output_file.write(html) |
# Don't close here. User may want to write more. |
else: |
- # Encode manually and write bytes to stdout. |
+ # Encode manually and write bytes to stdout. |
html = html.encode(encoding, "xmlcharrefreplace") |
try: |
# Write bytes directly to buffer (Python 3). |
@@ -425,8 +474,9 @@ Those are the two functions we really mean to export: markdown() and |
markdownFromFile(). |
""" |
+ |
def markdown(text, *args, **kwargs): |
- """Convert a markdown string to HTML and return HTML as a unicode string. |
+ """Convert a Markdown string to HTML and return HTML as a Unicode string. |
This is a shortcut function for `Markdown` class to cover the most |
basic use case. It initializes an instance of Markdown, loads the |
@@ -467,9 +517,13 @@ def markdownFromFile(*args, **kwargs): |
c += 1 |
if c == len(pos): |
break |
+ if len(args): |
+ warnings.warn('Positional arguments are depreacted in ' |
+ 'Markdown and will raise an error in version 2.7. ' |
+ 'Use keyword arguments only.', |
+ DeprecationWarning) |
md = Markdown(**kwargs) |
md.convertFile(kwargs.get('input', None), |
kwargs.get('output', None), |
kwargs.get('encoding', None)) |
- |