mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Make the smartypants inner loop a little more efficient
This commit is contained in:
parent
3ebc847f48
commit
2aaef2f34c
@ -376,8 +376,6 @@ smartypants.py license::
|
|||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
default_smartypants_attr = "1"
|
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
|
||||||
# style added by Kovid
|
# style added by Kovid
|
||||||
@ -387,9 +385,52 @@ self_closing_regex = re.compile(r'/\s*>$')
|
|||||||
|
|
||||||
# interal functions below here
|
# interal functions below here
|
||||||
|
|
||||||
def smartyPants(text, attr=default_smartypants_attr):
|
def parse_attr(attr):
|
||||||
convert_quot = False # should we translate " entities into normal quotes?
|
do_dashes = do_backticks = do_quotes = do_ellipses = do_stupefy = 0
|
||||||
|
|
||||||
|
if attr == "1":
|
||||||
|
do_quotes = 1
|
||||||
|
do_backticks = 1
|
||||||
|
do_dashes = 1
|
||||||
|
do_ellipses = 1
|
||||||
|
elif attr == "2":
|
||||||
|
# Do everything, turn all options on, use old school dash shorthand.
|
||||||
|
do_quotes = 1
|
||||||
|
do_backticks = 1
|
||||||
|
do_dashes = 2
|
||||||
|
do_ellipses = 1
|
||||||
|
elif attr == "3":
|
||||||
|
# Do everything, turn all options on, use inverted old school dash shorthand.
|
||||||
|
do_quotes = 1
|
||||||
|
do_backticks = 1
|
||||||
|
do_dashes = 3
|
||||||
|
do_ellipses = 1
|
||||||
|
elif attr == "-1":
|
||||||
|
# Special "stupefy" mode.
|
||||||
|
do_stupefy = 1
|
||||||
|
else:
|
||||||
|
for c in attr:
|
||||||
|
if c == "q":
|
||||||
|
do_quotes = 1
|
||||||
|
elif c == "b":
|
||||||
|
do_backticks = 1
|
||||||
|
elif c == "B":
|
||||||
|
do_backticks = 2
|
||||||
|
elif c == "d":
|
||||||
|
do_dashes = 1
|
||||||
|
elif c == "D":
|
||||||
|
do_dashes = 2
|
||||||
|
elif c == "i":
|
||||||
|
do_dashes = 3
|
||||||
|
elif c == "e":
|
||||||
|
do_ellipses = 1
|
||||||
|
else:
|
||||||
|
pass
|
||||||
|
# ignore unknown option
|
||||||
|
return do_dashes, do_backticks, do_quotes, do_ellipses, do_stupefy
|
||||||
|
|
||||||
|
|
||||||
|
def smartyPants(text, attr='1'):
|
||||||
# Parse attributes:
|
# Parse attributes:
|
||||||
# 0 : do nothing
|
# 0 : do nothing
|
||||||
# 1 : set all
|
# 1 : set all
|
||||||
@ -403,60 +444,17 @@ def smartyPants(text, attr=default_smartypants_attr):
|
|||||||
# D : old school dashes
|
# D : old school dashes
|
||||||
# i : inverted old school dashes
|
# i : inverted old school dashes
|
||||||
# e : ellipses
|
# e : ellipses
|
||||||
# w : convert " entities to " for Dreamweaver users
|
|
||||||
|
|
||||||
skipped_tag_stack = []
|
|
||||||
do_dashes = "0"
|
|
||||||
do_backticks = "0"
|
|
||||||
do_quotes = "0"
|
|
||||||
do_ellipses = "0"
|
|
||||||
do_stupefy = "0"
|
|
||||||
|
|
||||||
if attr == "0":
|
if attr == "0":
|
||||||
# Do nothing.
|
# Do nothing.
|
||||||
return text
|
return text
|
||||||
elif attr == "1":
|
|
||||||
do_quotes = "1"
|
|
||||||
do_backticks = "1"
|
|
||||||
do_dashes = "1"
|
|
||||||
do_ellipses = "1"
|
|
||||||
elif attr == "2":
|
|
||||||
# Do everything, turn all options on, use old school dash shorthand.
|
|
||||||
do_quotes = "1"
|
|
||||||
do_backticks = "1"
|
|
||||||
do_dashes = "2"
|
|
||||||
do_ellipses = "1"
|
|
||||||
elif attr == "3":
|
|
||||||
# Do everything, turn all options on, use inverted old school dash shorthand.
|
|
||||||
do_quotes = "1"
|
|
||||||
do_backticks = "1"
|
|
||||||
do_dashes = "3"
|
|
||||||
do_ellipses = "1"
|
|
||||||
elif attr == "-1":
|
|
||||||
# Special "stupefy" mode.
|
|
||||||
do_stupefy = "1"
|
|
||||||
else:
|
|
||||||
for c in attr:
|
|
||||||
if c == "q":
|
|
||||||
do_quotes = "1"
|
|
||||||
elif c == "b":
|
|
||||||
do_backticks = "1"
|
|
||||||
elif c == "B":
|
|
||||||
do_backticks = "2"
|
|
||||||
elif c == "d":
|
|
||||||
do_dashes = "1"
|
|
||||||
elif c == "D":
|
|
||||||
do_dashes = "2"
|
|
||||||
elif c == "i":
|
|
||||||
do_dashes = "3"
|
|
||||||
elif c == "e":
|
|
||||||
do_ellipses = "1"
|
|
||||||
elif c == "w":
|
|
||||||
convert_quot = "1"
|
|
||||||
else:
|
|
||||||
pass
|
|
||||||
# ignore unknown option
|
|
||||||
|
|
||||||
|
do_dashes, do_backticks, do_quotes, do_ellipses, do_stupefy = parse_attr(attr)
|
||||||
|
dashes_func = {1: educateDashes, 2: educateDashesOldSchool, 3: educateDashesOldSchoolInverted}.get(do_dashes, lambda x: x)
|
||||||
|
backticks_func = {1: educateBackticks, 2: lambda x: educateSingleBackticks(educateBackticks(x))}.get(do_backticks, lambda x: x)
|
||||||
|
ellipses_func = {1: educateEllipses}.get(do_ellipses, lambda x: x)
|
||||||
|
stupefy_func = {1: stupefyEntities}.get(do_stupefy, lambda x: x)
|
||||||
|
skipped_tag_stack = []
|
||||||
tokens = _tokenize(text)
|
tokens = _tokenize(text)
|
||||||
result = []
|
result = []
|
||||||
in_pre = False
|
in_pre = False
|
||||||
@ -495,28 +493,13 @@ def smartyPants(text, attr=default_smartypants_attr):
|
|||||||
if not in_pre:
|
if not in_pre:
|
||||||
t = processEscapes(t)
|
t = processEscapes(t)
|
||||||
|
|
||||||
if convert_quot != "0":
|
t = re.sub('"', '"', t)
|
||||||
t = re.sub('"', '"', t)
|
t = dashes_func(t)
|
||||||
|
t = ellipses_func(t)
|
||||||
if do_dashes != "0":
|
|
||||||
if do_dashes == "1":
|
|
||||||
t = educateDashes(t)
|
|
||||||
if do_dashes == "2":
|
|
||||||
t = educateDashesOldSchool(t)
|
|
||||||
if do_dashes == "3":
|
|
||||||
t = educateDashesOldSchoolInverted(t)
|
|
||||||
|
|
||||||
if do_ellipses != "0":
|
|
||||||
t = educateEllipses(t)
|
|
||||||
|
|
||||||
# Note: backticks need to be processed before quotes.
|
# Note: backticks need to be processed before quotes.
|
||||||
if do_backticks != "0":
|
t = backticks_func(t)
|
||||||
t = educateBackticks(t)
|
|
||||||
|
|
||||||
if do_backticks == "2":
|
if do_quotes is not 0:
|
||||||
t = educateSingleBackticks(t)
|
|
||||||
|
|
||||||
if do_quotes != "0":
|
|
||||||
if t == "'":
|
if t == "'":
|
||||||
# Special case: single-character ' token
|
# Special case: single-character ' token
|
||||||
if re.match("\S", prev_token_last_char):
|
if re.match("\S", prev_token_last_char):
|
||||||
@ -534,8 +517,7 @@ def smartyPants(text, attr=default_smartypants_attr):
|
|||||||
# Normal case:
|
# Normal case:
|
||||||
t = educateQuotes(t)
|
t = educateQuotes(t)
|
||||||
|
|
||||||
if do_stupefy == "1":
|
t = stupefy_func(t)
|
||||||
t = stupefyEntities(t)
|
|
||||||
|
|
||||||
prev_token_last_char = last_char
|
prev_token_last_char = last_char
|
||||||
result.append(t)
|
result.append(t)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user