diff options
Diffstat (limited to 'markdown/extensions/smarty.py')
-rw-r--r-- | markdown/extensions/smarty.py | 76 |
1 files changed, 44 insertions, 32 deletions
diff --git a/markdown/extensions/smarty.py b/markdown/extensions/smarty.py index 00c330f..3d79061 100644 --- a/markdown/extensions/smarty.py +++ b/markdown/extensions/smarty.py @@ -3,17 +3,17 @@ Smarty extension for Python-Markdown ==================================== -Adds conversion of ASCII dashes, quotes and ellipses to their HTML +Adds conversion of ASCII dashes, quotes and ellipses to their HTML entity equivalents. -See <https://pythonhosted.org/Markdown/extensions/smarty.html> +See <https://pythonhosted.org/Markdown/extensions/smarty.html> for documentation. Author: 2013, Dmitry Shachnev <mitya57@gmail.com> All changes Copyright 2013-2014 The Python Markdown Project -License: [BSD](http://www.opensource.org/licenses/bsd-license.php) +License: [BSD](http://www.opensource.org/licenses/bsd-license.php) SmartyPants license: @@ -32,7 +32,7 @@ SmartyPants license: the documentation and/or other materials provided with the distribution. - * Neither the name "SmartyPants" nor the names of its contributors + * Neither the name "SmartyPants" nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. @@ -86,7 +86,7 @@ from . import Extension from ..inlinepatterns import HtmlPattern from ..odict import OrderedDict from ..treeprocessors import InlineProcessor -from ..util import parseBoolValue + # Constants for quote education. punctClass = r"""[!"#\$\%'()*+,-.\/:;<=>?\@\[\\\]\^_`{|}~]""" @@ -94,14 +94,14 @@ endOfWordClass = r"[\s.,;:!?)]" closeClass = "[^\ \t\r\n\[\{\(\-\u0002\u0003]" openingQuotesBase = ( - '(\s' # a whitespace char - '| ' # or a non-breaking space entity - '|--' # or dashes - '|–|—' # or unicode - '|&[mn]dash;' # or named dash entities - '|–|—' # or decimal entities - ')' -) + '(\s' # a whitespace char + '| ' # or a non-breaking space entity + '|--' # or dashes + '|–|—' # or unicode + '|&[mn]dash;' # or named dash entities + '|–|—' # or decimal entities + ')' +) substitutions = { 'mdash': '—', @@ -137,13 +137,14 @@ closingDoubleQuotesRegex2 = '(?<=%s)"' % closeClass openingSingleQuotesRegex = r"%s'(?=\w)" % openingQuotesBase # Single closing quotes: -closingSingleQuotesRegex = r"(?<=%s)'(?!\s|s\b|\d)" % closeClass +closingSingleQuotesRegex = r"(?<=%s)'(?!\s|s\b|\d)" % closeClass closingSingleQuotesRegex2 = r"(?<=%s)'(\s|s\b)" % closeClass # All remaining quotes should be opening ones remainingSingleQuotesRegex = "'" remainingDoubleQuotesRegex = '"' + class SubstituteTextPattern(HtmlPattern): def __init__(self, pattern, replace, markdown_instance): """ Replaces matches with some text. """ @@ -160,6 +161,7 @@ class SubstituteTextPattern(HtmlPattern): result += self.markdown.htmlStash.store(part, safe=True) return result + class SmartyExtension(Extension): def __init__(self, *args, **kwargs): self.config = { @@ -167,7 +169,7 @@ class SmartyExtension(Extension): 'smart_angled_quotes': [False, 'Educate angled quotes'], 'smart_dashes': [True, 'Educate dashes'], 'smart_ellipses': [True, 'Educate ellipses'], - 'substitutions' : [{}, 'Overwrite default substitutions'], + 'substitutions': [{}, 'Overwrite default substitutions'], } super(SmartyExtension, self).__init__(*args, **kwargs) self.substitutions = dict(substitutions) @@ -182,31 +184,40 @@ class SmartyExtension(Extension): self.inlinePatterns.add(name, pattern, after) def educateDashes(self, md): - emDashesPattern = SubstituteTextPattern(r'(?<!-)---(?!-)', - (self.substitutions['mdash'],), md) - enDashesPattern = SubstituteTextPattern(r'(?<!-)--(?!-)', - (self.substitutions['ndash'],), md) + emDashesPattern = SubstituteTextPattern( + r'(?<!-)---(?!-)', (self.substitutions['mdash'],), md + ) + enDashesPattern = SubstituteTextPattern( + r'(?<!-)--(?!-)', (self.substitutions['ndash'],), md + ) self.inlinePatterns.add('smarty-em-dashes', emDashesPattern, '_begin') - self.inlinePatterns.add('smarty-en-dashes', enDashesPattern, - '>smarty-em-dashes') + self.inlinePatterns.add( + 'smarty-en-dashes', enDashesPattern, '>smarty-em-dashes' + ) def educateEllipses(self, md): - ellipsesPattern = SubstituteTextPattern(r'(?<!\.)\.{3}(?!\.)', - (self.substitutions['ellipsis'],), md) + ellipsesPattern = SubstituteTextPattern( + r'(?<!\.)\.{3}(?!\.)', (self.substitutions['ellipsis'],), md + ) self.inlinePatterns.add('smarty-ellipses', ellipsesPattern, '_begin') def educateAngledQuotes(self, md): - leftAngledQuotePattern = SubstituteTextPattern(r'\<\<', - (self.substitutions['left-angle-quote'],), md) - rightAngledQuotePattern = SubstituteTextPattern(r'\>\>', - (self.substitutions['right-angle-quote'],), md) - self.inlinePatterns.add('smarty-left-angle-quotes', - leftAngledQuotePattern, '_begin') - self.inlinePatterns.add('smarty-right-angle-quotes', - rightAngledQuotePattern, '>smarty-left-angle-quotes') + leftAngledQuotePattern = SubstituteTextPattern( + r'\<\<', (self.substitutions['left-angle-quote'],), md + ) + rightAngledQuotePattern = SubstituteTextPattern( + r'\>\>', (self.substitutions['right-angle-quote'],), md + ) + self.inlinePatterns.add( + 'smarty-left-angle-quotes', leftAngledQuotePattern, '_begin' + ) + self.inlinePatterns.add( + 'smarty-right-angle-quotes', + rightAngledQuotePattern, + '>smarty-left-angle-quotes' + ) def educateQuotes(self, md): - configs = self.getConfigs() lsquo = self.substitutions['left-single-quote'] rsquo = self.substitutions['right-single-quote'] ldquo = self.substitutions['left-double-quote'] @@ -243,5 +254,6 @@ class SmartyExtension(Extension): md.treeprocessors.add('smarty', inlineProcessor, '_end') md.ESCAPED_CHARS.extend(['"', "'"]) + def makeExtension(*args, **kwargs): return SmartyExtension(*args, **kwargs) |