2024-03-04 01:15:23 +08:00
|
|
|
|
# Smarty extension for Python-Markdown
|
|
|
|
|
# ====================================
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
# Adds conversion of ASCII dashes, quotes and ellipses to their HTML
|
|
|
|
|
# entity equivalents.
|
|
|
|
|
|
|
|
|
|
# See https://Python-Markdown.github.io/extensions/smarty
|
|
|
|
|
# for documentation.
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
# Author: 2013, Dmitry Shachnev <mitya57@gmail.com>
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
# All changes Copyright 2013-2014 The Python Markdown Project
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
# License: [BSD](https://opensource.org/licenses/bsd-license.php)
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
# SmartyPants license:
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
# Copyright (c) 2003 John Gruber <https://daringfireball.net/>
|
|
|
|
|
# All rights reserved.
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
|
# modification, are permitted provided that the following conditions are
|
|
|
|
|
# met:
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
# * Redistributions of source code must retain the above copyright
|
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
# * 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.
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
# * 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.
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
# This software is provided by the copyright holders and contributors "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 the copyright
|
|
|
|
|
# owner or contributors 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.
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
# `smartypants.py` license:
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
# `smartypants.py` is a derivative work of SmartyPants.
|
|
|
|
|
# Copyright (c) 2004, 2007 Chad Miller <http://web.chad.org/>
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
# Redistribution and use in source and binary forms, with or without
|
|
|
|
|
# modification, are permitted provided that the following conditions are
|
|
|
|
|
# met:
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
# * Redistributions of source code must retain the above copyright
|
|
|
|
|
# notice, this list of conditions and the following disclaimer.
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
# * 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.
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
# This software is provided by the copyright holders and contributors "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 the copyright
|
|
|
|
|
# owner or contributors 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.
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
"""
|
|
|
|
|
Adds conversion of ASCII dashes, quotes and ellipses to their HTML
|
|
|
|
|
entity equivalents.
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
See the [documentation](https://Python-Markdown.github.io/extensions/smarty)
|
|
|
|
|
for details.
|
|
|
|
|
"""
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
from __future__ import annotations
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
|
|
|
|
from . import Extension
|
2022-01-24 12:07:52 +08:00
|
|
|
|
from ..inlinepatterns import HtmlInlineProcessor, HTML_RE
|
2018-09-17 08:27:00 +08:00
|
|
|
|
from ..treeprocessors import InlineProcessor
|
2022-11-08 02:06:49 +08:00
|
|
|
|
from ..util import Registry
|
2024-03-04 01:15:23 +08:00
|
|
|
|
from typing import TYPE_CHECKING, Sequence
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
if TYPE_CHECKING: # pragma: no cover
|
|
|
|
|
from markdown import Markdown
|
|
|
|
|
from .. import inlinepatterns
|
|
|
|
|
import re
|
|
|
|
|
import xml.etree.ElementTree as etree
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
|
|
|
|
# Constants for quote education.
|
|
|
|
|
punctClass = r"""[!"#\$\%'()*+,-.\/:;<=>?\@\[\\\]\^_`{|}~]"""
|
|
|
|
|
endOfWordClass = r"[\s.,;:!?)]"
|
|
|
|
|
closeClass = r"[^\ \t\r\n\[\{\(\-\u0002\u0003]"
|
|
|
|
|
|
|
|
|
|
openingQuotesBase = (
|
|
|
|
|
r'(\s' # a whitespace char
|
|
|
|
|
r'| ' # or a non-breaking space entity
|
|
|
|
|
r'|--' # or dashes
|
2024-03-04 01:15:23 +08:00
|
|
|
|
r'|–|—' # or Unicode
|
2018-09-17 08:27:00 +08:00
|
|
|
|
r'|&[mn]dash;' # or named dash entities
|
|
|
|
|
r'|–|—' # or decimal entities
|
|
|
|
|
r')'
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
substitutions = {
|
|
|
|
|
'mdash': '—',
|
|
|
|
|
'ndash': '–',
|
|
|
|
|
'ellipsis': '…',
|
|
|
|
|
'left-angle-quote': '«',
|
|
|
|
|
'right-angle-quote': '»',
|
|
|
|
|
'left-single-quote': '‘',
|
|
|
|
|
'right-single-quote': '’',
|
|
|
|
|
'left-double-quote': '“',
|
|
|
|
|
'right-double-quote': '”',
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Special case if the very first character is a quote
|
|
|
|
|
# followed by punctuation at a non-word-break. Close the quotes by brute force:
|
|
|
|
|
singleQuoteStartRe = r"^'(?=%s\B)" % punctClass
|
|
|
|
|
doubleQuoteStartRe = r'^"(?=%s\B)' % punctClass
|
|
|
|
|
|
|
|
|
|
# Special case for double sets of quotes, e.g.:
|
|
|
|
|
# <p>He said, "'Quoted' words in a larger quote."</p>
|
|
|
|
|
doubleQuoteSetsRe = r""""'(?=\w)"""
|
|
|
|
|
singleQuoteSetsRe = r"""'"(?=\w)"""
|
|
|
|
|
|
|
|
|
|
# Special case for decade abbreviations (the '80s):
|
|
|
|
|
decadeAbbrRe = r"(?<!\w)'(?=\d{2}s)"
|
|
|
|
|
|
|
|
|
|
# Get most opening double quotes:
|
|
|
|
|
openingDoubleQuotesRegex = r'%s"(?=\w)' % openingQuotesBase
|
|
|
|
|
|
|
|
|
|
# Double closing quotes:
|
|
|
|
|
closingDoubleQuotesRegex = r'"(?=\s)'
|
|
|
|
|
closingDoubleQuotesRegex2 = '(?<=%s)"' % closeClass
|
|
|
|
|
|
|
|
|
|
# Get most opening single quotes:
|
|
|
|
|
openingSingleQuotesRegex = r"%s'(?=\w)" % openingQuotesBase
|
|
|
|
|
|
|
|
|
|
# Single closing quotes:
|
|
|
|
|
closingSingleQuotesRegex = r"(?<=%s)'(?!\s|s\b|\d)" % closeClass
|
2024-03-04 01:15:23 +08:00
|
|
|
|
closingSingleQuotesRegex2 = r"'(\s|s\b)"
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
|
|
|
|
# All remaining quotes should be opening ones
|
|
|
|
|
remainingSingleQuotesRegex = r"'"
|
|
|
|
|
remainingDoubleQuotesRegex = r'"'
|
|
|
|
|
|
|
|
|
|
HTML_STRICT_RE = HTML_RE + r'(?!\>)'
|
|
|
|
|
|
|
|
|
|
|
2022-01-24 12:07:52 +08:00
|
|
|
|
class SubstituteTextPattern(HtmlInlineProcessor):
|
2024-03-04 01:15:23 +08:00
|
|
|
|
def __init__(self, pattern: str, replace: Sequence[int | str | etree.Element], md: Markdown):
|
2018-09-17 08:27:00 +08:00
|
|
|
|
""" Replaces matches with some text. """
|
2022-01-24 12:07:52 +08:00
|
|
|
|
HtmlInlineProcessor.__init__(self, pattern)
|
2018-09-17 08:27:00 +08:00
|
|
|
|
self.replace = replace
|
2022-01-24 12:07:52 +08:00
|
|
|
|
self.md = md
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
def handleMatch(self, m: re.Match[str], data: str) -> tuple[str, int, int]:
|
2018-09-17 08:27:00 +08:00
|
|
|
|
result = ''
|
|
|
|
|
for part in self.replace:
|
|
|
|
|
if isinstance(part, int):
|
|
|
|
|
result += m.group(part)
|
|
|
|
|
else:
|
2022-01-24 12:07:52 +08:00
|
|
|
|
result += self.md.htmlStash.store(part)
|
|
|
|
|
return result, m.start(0), m.end(0)
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SmartyExtension(Extension):
|
2024-03-04 01:15:23 +08:00
|
|
|
|
""" Add Smarty to Markdown. """
|
2022-01-24 12:07:52 +08:00
|
|
|
|
def __init__(self, **kwargs):
|
2018-09-17 08:27:00 +08:00
|
|
|
|
self.config = {
|
|
|
|
|
'smart_quotes': [True, 'Educate quotes'],
|
|
|
|
|
'smart_angled_quotes': [False, 'Educate angled quotes'],
|
|
|
|
|
'smart_dashes': [True, 'Educate dashes'],
|
|
|
|
|
'smart_ellipses': [True, 'Educate ellipses'],
|
|
|
|
|
'substitutions': [{}, 'Overwrite default substitutions'],
|
|
|
|
|
}
|
2024-03-04 01:15:23 +08:00
|
|
|
|
""" Default configuration options. """
|
2022-01-24 12:07:52 +08:00
|
|
|
|
super().__init__(**kwargs)
|
2024-03-04 01:15:23 +08:00
|
|
|
|
self.substitutions: dict[str, str] = dict(substitutions)
|
2018-09-17 08:27:00 +08:00
|
|
|
|
self.substitutions.update(self.getConfig('substitutions', default={}))
|
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
def _addPatterns(
|
|
|
|
|
self,
|
|
|
|
|
md: Markdown,
|
|
|
|
|
patterns: Sequence[tuple[str, Sequence[int | str | etree.Element]]],
|
|
|
|
|
serie: str,
|
|
|
|
|
priority: int,
|
|
|
|
|
):
|
2018-09-17 08:27:00 +08:00
|
|
|
|
for ind, pattern in enumerate(patterns):
|
|
|
|
|
pattern += (md,)
|
|
|
|
|
pattern = SubstituteTextPattern(*pattern)
|
|
|
|
|
name = 'smarty-%s-%d' % (serie, ind)
|
2022-01-24 12:07:52 +08:00
|
|
|
|
self.inlinePatterns.register(pattern, name, priority-ind)
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
def educateDashes(self, md: Markdown) -> None:
|
2018-09-17 08:27:00 +08:00
|
|
|
|
emDashesPattern = SubstituteTextPattern(
|
|
|
|
|
r'(?<!-)---(?!-)', (self.substitutions['mdash'],), md
|
|
|
|
|
)
|
|
|
|
|
enDashesPattern = SubstituteTextPattern(
|
|
|
|
|
r'(?<!-)--(?!-)', (self.substitutions['ndash'],), md
|
|
|
|
|
)
|
2022-01-24 12:07:52 +08:00
|
|
|
|
self.inlinePatterns.register(emDashesPattern, 'smarty-em-dashes', 50)
|
|
|
|
|
self.inlinePatterns.register(enDashesPattern, 'smarty-en-dashes', 45)
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
def educateEllipses(self, md: Markdown) -> None:
|
2018-09-17 08:27:00 +08:00
|
|
|
|
ellipsesPattern = SubstituteTextPattern(
|
|
|
|
|
r'(?<!\.)\.{3}(?!\.)', (self.substitutions['ellipsis'],), md
|
|
|
|
|
)
|
2022-01-24 12:07:52 +08:00
|
|
|
|
self.inlinePatterns.register(ellipsesPattern, 'smarty-ellipses', 10)
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
def educateAngledQuotes(self, md: Markdown) -> None:
|
2018-09-17 08:27:00 +08:00
|
|
|
|
leftAngledQuotePattern = SubstituteTextPattern(
|
|
|
|
|
r'\<\<', (self.substitutions['left-angle-quote'],), md
|
|
|
|
|
)
|
|
|
|
|
rightAngledQuotePattern = SubstituteTextPattern(
|
|
|
|
|
r'\>\>', (self.substitutions['right-angle-quote'],), md
|
|
|
|
|
)
|
2022-01-24 12:07:52 +08:00
|
|
|
|
self.inlinePatterns.register(leftAngledQuotePattern, 'smarty-left-angle-quotes', 40)
|
|
|
|
|
self.inlinePatterns.register(rightAngledQuotePattern, 'smarty-right-angle-quotes', 35)
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
def educateQuotes(self, md: Markdown) -> None:
|
2018-09-17 08:27:00 +08:00
|
|
|
|
lsquo = self.substitutions['left-single-quote']
|
|
|
|
|
rsquo = self.substitutions['right-single-quote']
|
|
|
|
|
ldquo = self.substitutions['left-double-quote']
|
|
|
|
|
rdquo = self.substitutions['right-double-quote']
|
|
|
|
|
patterns = (
|
|
|
|
|
(singleQuoteStartRe, (rsquo,)),
|
|
|
|
|
(doubleQuoteStartRe, (rdquo,)),
|
|
|
|
|
(doubleQuoteSetsRe, (ldquo + lsquo,)),
|
|
|
|
|
(singleQuoteSetsRe, (lsquo + ldquo,)),
|
|
|
|
|
(decadeAbbrRe, (rsquo,)),
|
2022-01-24 12:07:52 +08:00
|
|
|
|
(openingSingleQuotesRegex, (1, lsquo)),
|
2018-09-17 08:27:00 +08:00
|
|
|
|
(closingSingleQuotesRegex, (rsquo,)),
|
2022-01-24 12:07:52 +08:00
|
|
|
|
(closingSingleQuotesRegex2, (rsquo, 1)),
|
2018-09-17 08:27:00 +08:00
|
|
|
|
(remainingSingleQuotesRegex, (lsquo,)),
|
2022-01-24 12:07:52 +08:00
|
|
|
|
(openingDoubleQuotesRegex, (1, ldquo)),
|
2018-09-17 08:27:00 +08:00
|
|
|
|
(closingDoubleQuotesRegex, (rdquo,)),
|
|
|
|
|
(closingDoubleQuotesRegex2, (rdquo,)),
|
|
|
|
|
(remainingDoubleQuotesRegex, (ldquo,))
|
|
|
|
|
)
|
2022-01-24 12:07:52 +08:00
|
|
|
|
self._addPatterns(md, patterns, 'quotes', 30)
|
2018-09-17 08:27:00 +08:00
|
|
|
|
|
2022-01-24 12:07:52 +08:00
|
|
|
|
def extendMarkdown(self, md):
|
2018-09-17 08:27:00 +08:00
|
|
|
|
configs = self.getConfigs()
|
2024-03-04 01:15:23 +08:00
|
|
|
|
self.inlinePatterns: Registry[inlinepatterns.InlineProcessor] = Registry()
|
2018-09-17 08:27:00 +08:00
|
|
|
|
if configs['smart_ellipses']:
|
|
|
|
|
self.educateEllipses(md)
|
|
|
|
|
if configs['smart_quotes']:
|
|
|
|
|
self.educateQuotes(md)
|
|
|
|
|
if configs['smart_angled_quotes']:
|
|
|
|
|
self.educateAngledQuotes(md)
|
2024-03-04 01:15:23 +08:00
|
|
|
|
# Override `HTML_RE` from `inlinepatterns.py` so that it does not
|
2018-09-17 08:27:00 +08:00
|
|
|
|
# process tags with duplicate closing quotes.
|
2022-01-24 12:07:52 +08:00
|
|
|
|
md.inlinePatterns.register(HtmlInlineProcessor(HTML_STRICT_RE, md), 'html', 90)
|
2018-09-17 08:27:00 +08:00
|
|
|
|
if configs['smart_dashes']:
|
|
|
|
|
self.educateDashes(md)
|
|
|
|
|
inlineProcessor = InlineProcessor(md)
|
|
|
|
|
inlineProcessor.inlinePatterns = self.inlinePatterns
|
2022-01-24 12:07:52 +08:00
|
|
|
|
md.treeprocessors.register(inlineProcessor, 'smarty', 2)
|
2018-09-17 08:27:00 +08:00
|
|
|
|
md.ESCAPED_CHARS.extend(['"', "'"])
|
|
|
|
|
|
|
|
|
|
|
2022-01-24 12:07:52 +08:00
|
|
|
|
def makeExtension(**kwargs): # pragma: no cover
|
|
|
|
|
return SmartyExtension(**kwargs)
|