/usr/lib/python2.7/dist-packages/sphinx/util/smartypants.py is in python-sphinx 1.6.7-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 | # -*- coding: utf-8 -*-
"""
sphinx.util.smartypants
~~~~~~~~~~~~~~~~~~~~~~~
This is extracted (with minor adaptations for flake8 compliance) from
docutils’ docutils/utils/smartquotes.py as of revision 8097 (30 May 2017),
in order to backport for Sphinx usage with Docutils < 0.14 extra language
configurations and fixes. Replaces earlier smartypants version as used up
to Sphinx 1.5.6.
:copyright: © 2010 Günter Milde,
original `SmartyPants`_: © 2003 John Gruber
smartypants.py: © 2004, 2007 Chad Miller
:license: Released under the terms of the `2-Clause BSD license`_, in short:
Copying and distribution of this file, with or without modification,
are permitted in any medium without royalty provided the copyright
notices and this notice are preserved.
This file is offered as-is, without any warranty.
.. _SmartyPants: http://daringfireball.net/projects/smartypants/
.. _2-Clause BSD license: http://www.spdx.org/licenses/BSD-2-Clause
See the LICENSE file and the original docutils code for details.
"""
from __future__ import absolute_import, unicode_literals
import re
from docutils.utils import smartquotes
from sphinx.util.docutils import __version_info__ as docutils_version
if False: # For type annotation
from typing import Iterable, Iterator, Tuple # NOQA
langquotes = {'af': u'“”‘’',
'af-x-altquot': u'„”‚’',
'bg': u'„“‚‘', # Bulgarian, https://bg.wikipedia.org/wiki/Кавички
'ca': u'«»“”',
'ca-x-altquot': u'“”‘’',
'cs': u'„“‚‘',
'cs-x-altquot': u'»«›‹',
'da': u'»«›‹',
'da-x-altquot': u'„“‚‘',
# 'da-x-altquot2': u'””’’',
'de': u'„“‚‘',
'de-x-altquot': u'»«›‹',
'de-ch': u'«»‹›',
'el': u'«»“”',
'en': u'“”‘’',
'en-uk-x-altquot': u'‘’“”', # Attention: " → ‘ and ' → “ !
'eo': u'“”‘’',
'es': u'«»“”',
'es-x-altquot': u'“”‘’',
'et': u'„“‚‘', # no secondary quote listed in
'et-x-altquot': u'«»‹›', # the sources above (wikipedia.org)
'eu': u'«»‹›',
'fi': u'””’’',
'fi-x-altquot': u'»»››',
'fr': (u'« ', u' »', u'“', u'”'), # full no-break space
'fr-x-altquot': (u'« ', u' »', u'“', u'”'), # narrow no-break space
'fr-ch': u'«»‹›',
'fr-ch-x-altquot': (u'« ', u' »', u'‹ ', u' ›'), # narrow no-break space
# http://typoguide.ch/
'gl': u'«»“”',
'he': u'”“»«', # Hebrew is RTL, test position:
'he-x-altquot': u'„”‚’', # low quotation marks are opening.
# 'he-x-altquot': u'“„‘‚', # RTL: low quotation marks opening
'hr': u'„”‘’', # http://hrvatska-tipografija.com/polunavodnici/
'hr-x-altquot': u'»«›‹',
'hsb': u'„“‚‘',
'hsb-x-altquot': u'»«›‹',
'hu': u'„”«»',
'is': u'„“‚‘',
'it': u'«»“”',
'it-ch': u'«»‹›',
'it-x-altquot': u'“”‘’',
# 'it-x-altquot2': u'“„‘‚', # [7] in headlines
'ja': u'「」『』',
'lt': u'„“‚‘',
'lv': u'„“‚‘',
'mk': u'„“‚‘', # Macedonian,
# https://mk.wikipedia.org/wiki/Правопис_и_правоговор_на_македонскиот_јазик
'nl': u'“”‘’',
'nl-x-altquot': u'„”‚’',
# 'nl-x-altquot2': u'””’’',
'nb': u'«»’’', # Norsk bokmål (canonical form 'no')
'nn': u'«»’’', # Nynorsk [10]
'nn-x-altquot': u'«»‘’', # [8], [10]
# 'nn-x-altquot2': u'«»«»', # [9], [10]
# 'nn-x-altquot3': u'„“‚‘', # [10]
'no': u'«»’’', # Norsk bokmål [10]
'no-x-altquot': u'«»‘’', # [8], [10]
# 'no-x-altquot2': u'«»«»', # [9], [10]
# 'no-x-altquot3': u'„“‚‘', # [10]
'pl': u'„”«»',
'pl-x-altquot': u'«»‚’',
# 'pl-x-altquot2': u'„”‚’',
# https://pl.wikipedia.org/wiki/Cudzys%C5%82%C3%B3w
'pt': u'«»“”',
'pt-br': u'“”‘’',
'ro': u'„”«»',
'ru': u'«»„“',
'sh': u'„”‚’', # Serbo-Croatian
'sh-x-altquot': u'»«›‹',
'sk': u'„“‚‘', # Slovak
'sk-x-altquot': u'»«›‹',
'sl': u'„“‚‘', # Slovenian
'sl-x-altquot': u'»«›‹',
'sq': u'«»‹›', # Albanian
'sq-x-altquot': u'“„‘‚',
'sr': u'„”’’',
'sr-x-altquot': u'»«›‹',
'sv': u'””’’',
'sv-x-altquot': u'»»››',
'tr': u'“”‘’',
'tr-x-altquot': u'«»‹›',
# 'tr-x-altquot2': u'“„‘‚', # [7] antiquated?
'uk': u'«»„“',
'uk-x-altquot': u'„“‚‘',
'zh-cn': u'“”‘’',
'zh-tw': u'「」『』',
}
def educateQuotes(text, language='en'):
# type: (unicode, unicode) -> unicode
"""
Parameter: - text string (unicode or bytes).
- language (`BCP 47` language tag.)
Returns: The `text`, with "educated" curly quote characters.
Example input: "Isn't this fun?"
Example output: “Isn’t this fun?“;
"""
smart = smartquotes.smartchars(language)
try:
apostrophe = smart.apostrophe
except Exception:
apostrophe = u'’'
# oldtext = text
punct_class = r"""[!"#\$\%'()*+,-.\/:;<=>?\@\[\\\]\^_`{|}~]"""
# Special case if the very first character is a quote
# followed by punctuation at a non-word-break.
# Close the quotes by brute force:
text = re.sub(r"""^'(?=%s\\B)""" % (punct_class,), smart.csquote, text)
text = re.sub(r"""^"(?=%s\\B)""" % (punct_class,), smart.cpquote, text)
# Special case for double sets of quotes, e.g.:
# <p>He said, "'Quoted' words in a larger quote."</p>
text = re.sub(r""""'(?=\w)""", smart.opquote + smart.osquote, text)
text = re.sub(r"""'"(?=\w)""", smart.osquote + smart.opquote, text)
# Special case for decade abbreviations (the '80s):
if language.startswith('en'): # TODO similar cases in other languages?
text = re.sub(r"""'(?=\d{2}s)""", apostrophe, text, re.UNICODE)
close_class = r"""[^\ \t\r\n\[\{\(\-]"""
dec_dashes = r"""–|—"""
# Get most opening single quotes:
opening_single_quotes_regex = re.compile(r"""
(
\s | # a whitespace char, or
| # a non-breaking space entity, or
-- | # dashes, or
&[mn]dash; | # named dash entities
%s | # or decimal entities
&\#x201[34]; # or hex
)
' # the quote
(?=\w) # followed by a word character
""" % (dec_dashes,), re.VERBOSE | re.UNICODE)
text = opening_single_quotes_regex.sub(r'\1' + smart.osquote, text)
# In many locales, single closing quotes are different from apostrophe:
if smart.csquote != apostrophe:
apostrophe_regex = re.compile(r"(?<=(\w|\d))'(?=\w)", re.UNICODE)
text = apostrophe_regex.sub(apostrophe, text)
# TODO: keep track of quoting level to recognize apostrophe in, e.g.,
# "Ich fass' es nicht."
closing_single_quotes_regex = re.compile(r"""
(%s)
'
(?!\s | # whitespace
s\b |
\d # digits ('80s)
)
""" % (close_class,), re.VERBOSE | re.UNICODE)
text = closing_single_quotes_regex.sub(r'\1' + smart.csquote, text)
closing_single_quotes_regex = re.compile(r"""
(%s)
'
(\s | s\b)
""" % (close_class,), re.VERBOSE | re.UNICODE)
text = closing_single_quotes_regex.sub(r'\1%s\2' % smart.csquote, text)
# Any remaining single quotes should be opening ones:
text = re.sub(r"""'""", smart.osquote, text)
# Get most opening double quotes:
opening_double_quotes_regex = re.compile(r"""
(
\s | # a whitespace char, or
| # a non-breaking space entity, or
-- | # dashes, or
&[mn]dash; | # named dash entities
%s | # or decimal entities
&\#x201[34]; # or hex
)
" # the quote
(?=\w) # followed by a word character
""" % (dec_dashes,), re.VERBOSE)
text = opening_double_quotes_regex.sub(r'\1' + smart.opquote, text)
# Double closing quotes:
closing_double_quotes_regex = re.compile(r"""
#(%s)? # character that indicates the quote should be closing
"
(?=\s)
""" % (close_class,), re.VERBOSE)
text = closing_double_quotes_regex.sub(smart.cpquote, text)
closing_double_quotes_regex = re.compile(r"""
(%s) # character that indicates the quote should be closing
"
""" % (close_class,), re.VERBOSE)
text = closing_double_quotes_regex.sub(r'\1' + smart.cpquote, text)
# Any remaining quotes should be opening ones.
text = re.sub(r'"', smart.opquote, text)
return text
def educate_tokens(text_tokens, attr=smartquotes.default_smartypants_attr, language='en'):
# type: (Iterable[Tuple[str, unicode]], unicode, unicode) -> Iterator
"""Return iterator that "educates" the items of `text_tokens`.
This is modified to intercept the ``attr='2'`` as it was used by the
Docutils 0.13.1 SmartQuotes transform in a hard coded way. Docutils 0.14
uses ``'qDe'``` and is configurable, and its choice is backported here
for use by Sphinx with earlier Docutils releases. Similarly ``'1'`` is
replaced by ``'qde'``.
Use ``attr='qDbe'``, resp. ``'qdbe'`` to recover Docutils effect of ``'2'``,
resp. ``'1'``.
refs: https://sourceforge.net/p/docutils/mailman/message/35869025/
"""
# Parse attributes:
# 0 : do nothing
# 1 : set all (but backticks)
# 2 : set all (but backticks), using old school en- and em- dash shortcuts
# 3 : set all, using inverted old school en and em- dash shortcuts
#
# q : quotes
# b : backtick quotes (``double'' only)
# B : backtick quotes (``double'' and `single')
# d : dashes
# D : old school dashes
# i : inverted old school dashes
# e : ellipses
# w : convert " entities to " for Dreamweaver users
convert_quot = False # translate " entities into normal quotes?
do_dashes = 0
do_backticks = 0
do_quotes = False
do_ellipses = False
do_stupefy = False
if attr == "1": # Do everything, turn all options on.
do_quotes = True
# do_backticks = 1
do_dashes = 1
do_ellipses = True
elif attr == "2":
# Do everything, turn all options on, use old school dash shorthand.
do_quotes = True
# do_backticks = 1
do_dashes = 2
do_ellipses = True
elif attr == "3":
# Do everything, use inverted old school dash shorthand.
do_quotes = True
do_backticks = 1
do_dashes = 3
do_ellipses = True
elif attr == "-1": # Special "stupefy" mode.
do_stupefy = True
else:
if "q" in attr:
do_quotes = True
if "b" in attr:
do_backticks = 1
if "B" in attr:
do_backticks = 2
if "d" in attr:
do_dashes = 1
if "D" in attr:
do_dashes = 2
if "i" in attr:
do_dashes = 3
if "e" in attr:
do_ellipses = True
if "w" in attr:
convert_quot = True
prev_token_last_char = " "
# Last character of the previous text token. Used as
# context to curl leading quote characters correctly.
for (ttype, text) in text_tokens:
# skip HTML and/or XML tags as well as emtpy text tokens
# without updating the last character
if ttype == 'tag' or not text:
yield text
continue
# skip literal text (math, literal, raw, ...)
if ttype == 'literal':
prev_token_last_char = text[-1:]
yield text
continue
last_char = text[-1:] # Remember last char before processing.
text = smartquotes.processEscapes(text)
if convert_quot:
text = re.sub('"', '"', text)
if do_dashes == 1:
text = smartquotes.educateDashes(text)
elif do_dashes == 2:
text = smartquotes.educateDashesOldSchool(text)
elif do_dashes == 3:
text = smartquotes.educateDashesOldSchoolInverted(text)
if do_ellipses:
text = smartquotes.educateEllipses(text)
# Note: backticks need to be processed before quotes.
if do_backticks:
text = smartquotes.educateBackticks(text, language)
if do_backticks == 2:
text = smartquotes.educateSingleBackticks(text, language)
if do_quotes:
# Replace plain quotes to prevent converstion to
# 2-character sequence in French.
context = prev_token_last_char.replace('"', ';').replace("'", ';')
text = educateQuotes(context + text, language)[1:]
if do_stupefy:
text = smartquotes.stupefyEntities(text, language)
# Remember last char as context for the next token
prev_token_last_char = last_char
text = smartquotes.processEscapes(text, restore=True)
yield text
if docutils_version < (0, 13, 2):
# Monkey patch the old docutils versions to fix the issues mentioned
# at https://sourceforge.net/p/docutils/bugs/313/
# at https://sourceforge.net/p/docutils/bugs/317/
# and more
smartquotes.educateQuotes = educateQuotes
smartquotes.educate_tokens = educate_tokens
# Fix the issue with French quotes mentioned at
# https://sourceforge.net/p/docutils/mailman/message/35760696/
# Add/fix other languages as well
smartquotes.smartchars.quotes = langquotes
|