This commit is contained in:
Kovid Goyal 2014-02-27 09:19:08 +05:30
parent 2aa20b15c3
commit f9abbe9800

View File

@ -392,7 +392,7 @@ def cb_story(args):
try: try:
forbidden_flavours = args["entry"]["smartypants_forbidden_flavours"] forbidden_flavours = args["entry"]["smartypants_forbidden_flavours"]
except KeyError: except KeyError:
forbidden_flavours = [ "rss" ] forbidden_flavours = ["rss"]
try: try:
attributes = args["entry"]["smartypants_attributes"] attributes = args["entry"]["smartypants_attributes"]
@ -416,7 +416,7 @@ def cb_story(args):
args["entry"]["title"] = smartyPants(args["entry"]["title"], attributes) args["entry"]["title"] = smartyPants(args["entry"]["title"], attributes)
### interal functions below here # interal functions below here
def smartyPants(text, attr=default_smartypants_attr): def smartyPants(text, attr=default_smartypants_attr):
convert_quot = False # should we translate " entities into normal quotes? convert_quot = False # should we translate " entities into normal quotes?
@ -468,14 +468,22 @@ def smartyPants(text, attr=default_smartypants_attr):
do_stupefy = "1" do_stupefy = "1"
else: else:
for c in attr: for c in attr:
if c == "q": do_quotes = "1" if c == "q":
elif c == "b": do_backticks = "1" do_quotes = "1"
elif c == "B": do_backticks = "2" elif c == "b":
elif c == "d": do_dashes = "1" do_backticks = "1"
elif c == "D": do_dashes = "2" elif c == "B":
elif c == "i": do_dashes = "3" do_backticks = "2"
elif c == "e": do_ellipses = "1" elif c == "d":
elif c == "w": convert_quot = "1" 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: else:
pass pass
# ignore unknown option # ignore unknown option
@ -514,7 +522,7 @@ def smartyPants(text, attr=default_smartypants_attr):
in_pre = False in_pre = False
else: else:
t = cur_token[1] t = cur_token[1]
last_char = t[-1:] # Remember last char of this token before processing. last_char = t[-1:] # Remember last char of this token before processing.
if not in_pre: if not in_pre:
t = processEscapes(t) t = processEscapes(t)
@ -607,12 +615,12 @@ def educateQuotes(str):
# meaningful # meaningful
# Special case for Quotes at end of line with a preceeding space (may change just to end of line) # Special case for Quotes at end of line with a preceeding space (may change just to end of line)
#str = re.sub(r"""(?<=\s)"$""", r"""&#8221;""", str) # str = re.sub(r"""(?<=\s)"$""", r"""&#8221;""", str)
#str = re.sub(r"""(?<=\s)'$""", r"""&#8217;""", str) # str = re.sub(r"""(?<=\s)'$""", r"""&#8217;""", str)
# Special case for Quotes at beginning of line with a space - multiparagraph quoted text: # Special case for Quotes at beginning of line with a space - multiparagraph quoted text:
#str = re.sub(r"""^"(?=\s)""", r"""&#8220;""", str) # str = re.sub(r"""^"(?=\s)""", r"""&#8220;""", str)
#str = re.sub(r"""^'(?=\s)""", r"""&#8216;""", str) # str = re.sub(r"""^'(?=\s)""", r"""&#8216;""", str)
# Special case for decade abbreviations (the '80s): # Special case for decade abbreviations (the '80s):
str = re.sub(r"""\b'(?=\d{2}s)""", r"""&#8217;""", str) str = re.sub(r"""\b'(?=\d{2}s)""", r"""&#8217;""", str)
@ -724,8 +732,8 @@ def educateDashes(str):
an em-dash HTML entity. an em-dash HTML entity.
""" """
str = re.sub(r"""---""", r"""&#8211;""", str) # en (yes, backwards) str = re.sub(r"""---""", r"""&#8211;""", str) # en (yes, backwards)
str = re.sub(r"""--""", r"""&#8212;""", str) # em (yes, backwards) str = re.sub(r"""--""", r"""&#8212;""", str) # em (yes, backwards)
return str return str
@ -763,7 +771,6 @@ def educateDashesOldSchoolInverted(str):
return str return str
def educateEllipses(str): def educateEllipses(str):
""" """
Parameter: String. Parameter: String.
@ -790,7 +797,7 @@ def stupefyEntities(str):
""" """
str = re.sub(r"""&#8211;""", r"""-""", str) # en-dash str = re.sub(r"""&#8211;""", r"""-""", str) # en-dash
str = re.sub(r"""&#8212;""", r"""--""", str) # em-dash str = re.sub(r"""&#8212;""", r"""--""", str) # em-dash
str = re.sub(r"""&#8216;""", r"""'""", str) # open single quote str = re.sub(r"""&#8216;""", r"""'""", str) # open single quote
str = re.sub(r"""&#8217;""", r"""'""", str) # close single quote str = re.sub(r"""&#8217;""", r"""'""", str) # close single quote
@ -798,7 +805,7 @@ def stupefyEntities(str):
str = re.sub(r"""&#8220;""", r'''"''', str) # open double quote str = re.sub(r"""&#8220;""", r'''"''', str) # open double quote
str = re.sub(r"""&#8221;""", r'''"''', str) # close double quote str = re.sub(r"""&#8221;""", r'''"''', str) # close double quote
str = re.sub(r"""&#8230;""", r"""...""", str)# ellipsis str = re.sub(r"""&#8230;""", r"""...""", str) # ellipsis
return str return str
@ -847,9 +854,9 @@ def _tokenize(str):
#depth = 6 #depth = 6
#nested_tags = "|".join(['(?:<(?:[^<>]',] * depth) + (')*>)' * depth) #nested_tags = "|".join(['(?:<(?:[^<>]',] * depth) + (')*>)' * depth)
#match = r"""(?: <! ( -- .*? -- \s* )+ > ) | # comments # match = r"""(?: <! ( -- .*? -- \s* )+ > ) | # comments
# (?: <\? .*? \?> ) | # directives # (?: <\? .*? \?> ) | # directives
# %s # nested tags """ % (nested_tags,) # %s # nested tags """ % (nested_tags,)
tag_soup = re.compile(r"""([^<]*)(<[^>]*>)""") tag_soup = re.compile(r"""([^<]*)(<[^>]*>)""")
token_match = tag_soup.search(str) token_match = tag_soup.search(str)
@ -870,7 +877,6 @@ def _tokenize(str):
return tokens return tokens
if __name__ == "__main__": if __name__ == "__main__":
import locale import locale
@ -885,7 +891,6 @@ if __name__ == "__main__":
print docstring_html print docstring_html
# Unit test output goes out stderr. No worries. # Unit test output goes out stderr. No worries.
import unittest import unittest
sp = smartyPants sp = smartyPants
@ -904,17 +909,16 @@ if __name__ == "__main__":
def test_skip_tags(self): def test_skip_tags(self):
self.assertEqual( self.assertEqual(
sp("""<script type="text/javascript">\n<!--\nvar href = "http://www.google.com";\nvar linktext = "google";\ndocument.write('<a href="' + href + '">' + linktext + "</a>");\n//-->\n</script>"""), sp("""<script type="text/javascript">\n<!--\nvar href = "http://www.google.com";\nvar linktext = "google";\ndocument.write('<a href="' + href + '">' + linktext + "</a>");\n//-->\n</script>"""), # noqa
"""<script type="text/javascript">\n<!--\nvar href = "http://www.google.com";\nvar linktext = "google";\ndocument.write('<a href="' + href + '">' + linktext + "</a>");\n//-->\n</script>""") """<script type="text/javascript">\n<!--\nvar href = "http://www.google.com";\nvar linktext = "google";\ndocument.write('<a href="' + href + '">' + linktext + "</a>");\n//-->\n</script>""") # noqa
self.assertEqual( self.assertEqual(
sp("""<p>He said &quot;Let's write some code.&quot; This code here <code>if True:\n\tprint &quot;Okay&quot;</code> is python code.</p>"""), sp("""<p>He said &quot;Let's write some code.&quot; This code here <code>if True:\n\tprint &quot;Okay&quot;</code> is python code.</p>"""),
"""<p>He said &#8220;Let&#8217;s write some code.&#8221; This code here <code>if True:\n\tprint &quot;Okay&quot;</code> is python code.</p>""") """<p>He said &#8220;Let&#8217;s write some code.&#8221; This code here <code>if True:\n\tprint &quot;Okay&quot;</code> is python code.</p>""") # noqa
self.assertEqual( self.assertEqual(
sp('''<script/><p>It's ok</p>'''), sp('''<script/><p>It's ok</p>'''),
'''<script/><p>It&#8217;s ok</p>''') '''<script/><p>It&#8217;s ok</p>''')
def test_ordinal_numbers(self): def test_ordinal_numbers(self):
self.assertEqual(sp("21st century"), "21st century") # no effect. self.assertEqual(sp("21st century"), "21st century") # no effect.
self.assertEqual(sp("3rd"), "3rd") # no effect. self.assertEqual(sp("3rd"), "3rd") # no effect.
@ -925,8 +929,6 @@ if __name__ == "__main__":
unittest.main() unittest.main()
__author__ = "Chad Miller <smartypantspy@chad.org>" __author__ = "Chad Miller <smartypantspy@chad.org>"
__version__ = "1.5_1.6: Fri, 27 Jul 2007 07:06:40 -0400" __version__ = "1.5_1.6: Fri, 27 Jul 2007 07:06:40 -0400"
__url__ = "http://wiki.chad.org/SmartyPantsPy" __url__ = "http://wiki.chad.org/SmartyPantsPy"