summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYu-Jie Lin <livibetter@gmail.com>2016-12-28 13:57:19 +0800
committerYu-Jie Lin <livibetter@gmail.com>2016-12-28 13:57:19 +0800
commit039c16111c3bbb0cdd2627f1e6d2c5ae68f4256e (patch)
tree498a86d833475e02d1958c1fd8569248bfa9ff63
parent431d4dd9bde33c3980ebadf6dc4a0f9756f58ced (diff)
parentd9a25abd73eaf809f836f240d7874eacd6af3fdc (diff)
downloadsmartypants-git-039c16111c3bbb0cdd2627f1e6d2c5ae68f4256e.tar.gz
merge v2.0.0dev
-rw-r--r--CHANGES.rst14
-rw-r--r--Makefile4
-rw-r--r--README.rst5
-rw-r--r--docs/index.rst6
-rw-r--r--docs/usage.rst4
-rwxr-xr-xsmartypants31
-rwxr-xr-xsmartypants.py260
-rw-r--r--tests/test.py16
-rw-r--r--tests/test_deprecated.py49
9 files changed, 133 insertions, 256 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 3042a92..6b7125a 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -46,6 +46,9 @@ Releases 1.7 and greater
Release 2.0.0
-------------
+Development
+-----------
+
- drop Pyblosxom support
- drop str-type ``attr``
@@ -55,12 +58,17 @@ Release 2.0.0
- drop fooBarXyz functions, such as ``smartyPants``, ``educateQuotes``,
and ``processEscapes``
-Development
------------
++ add ``Attr.u`` and ``Attr.h`` for Unicode characters and HTML named entities
+ outputs, respectively. The ``stupefy_entities`` has become
+ ``convert_entities`` to support all three types of conversions. (#6)
* Makefile
+ - do not build ``bdist_wininst --plat-name win32`` per
+ :pep:`527#bdist-dmg-bdist-msi-and-bdist-wininst`
+
+ test packages build in ``test_setup`` target
+
* rename target ``install_test`` to ``test_setup``
@@ -70,6 +78,7 @@ Release 1.8.6: 2014-07-19T11:20:52Z
* Makefile
+ add ``LC_ALL=C`` test for locale setting on ``setup.py`` wrt #5
+
* change virtualenv invocation method in ``install_test`` target
* fix UnicodeDecodeError on opening ``smartypants.py``, which includes Unicode
@@ -89,6 +98,7 @@ Release 1.8.4: 2014-06-29T04:39:59Z
+ add missing ``COPYING`` and ``CHANGES.rst`` to package (#3)
+ add ``bdist_wheel`` to the building process for Python Wheel format
+ add ``test_doc8`` target
+
* fix ``install_test`` on missing of Wheel package
* fix argparse version option breaks CLI on Python 3
diff --git a/Makefile b/Makefile
index 7b43989..7789ef2 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-# Copyright (c) 2013, 2014 Yu-Jie Lin
+# Copyright (c) 2013, 2014, 2016 Yu-Jie Lin
# Licensed under the BSD License, for detailed license information, see COPYING
PACKAGE=smartypants
@@ -8,7 +8,7 @@ PY2_CMD=python2
PY3_CMD=python3
INSTALL_TEST_DIR=/tmp/$(PACKAGE)_install_test
-BUILD_CMD=./setup.py sdist --formats gztar,zip bdist_wheel bdist_wininst --plat-name win32
+BUILD_CMD=./setup.py sdist --formats gztar,zip bdist_wheel
DOC_FILES = CHANGES.rst COPYING docs/conf.py $(wildcard docs/*.rst)
diff --git a/README.rst b/README.rst
index 9eb9afe..fb752f2 100644
--- a/README.rst
+++ b/README.rst
@@ -7,6 +7,11 @@ smartypants_ is a Python fork of SmartyPants__.
__ SmartyPantsPerl_
.. _SmartyPantsPerl: http://daringfireball.net/projects/smartypants/
+.. important::
+
+ As of 2016-12-28, smartypants is looking for new maintainer to take over,
+ please contact project owner on Bitbucket.
+
Installation
------------
diff --git a/docs/index.rst b/docs/index.rst
index f614fe5..75fff8d 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -6,6 +6,12 @@
Welcome to smartypants documentation!
=====================================
+.. important::
+
+ As of 2016-12-28, smartypants is looking for new maintainer to take over,
+ please contact project owner on Bitbucket.
+
+
Contents:
.. toctree::
diff --git a/docs/usage.rst b/docs/usage.rst
index 18f716e..9edea34 100644
--- a/docs/usage.rst
+++ b/docs/usage.rst
@@ -97,7 +97,7 @@ Backslash escapes
=================
If you need to use literal straight quotes (or plain hyphens and periods), for
-example, text like ``6'2"`` may become ``6‘2”``. To avoid such situation, you
-can use backslash escapes like ``6\'2\"``.
+example, text like ``6'2"`` may become ``6‘2”``. To avoid such situation,
+you can use backslash escapes like ``6\'2\"``.
.. seealso:: :func:`smartypants.process_escapes` for a complete list of backslash escapes.
diff --git a/smartypants b/smartypants
index 0b822df..189adf5 100755
--- a/smartypants
+++ b/smartypants
@@ -73,6 +73,35 @@ import warnings
import smartypants
+def _str_attr_to_int(str_attr):
+ """
+ Convert str-type attr into int
+
+ >>> f = _str_attr_to_int
+ >>> f('q') == Attr.q
+ True
+ >>> f('1') == Attr.set1
+ True
+ >>> with warnings.catch_warnings(record=True) as w:
+ ... f('bz')
+ ... len(w)
+ ... print(w[-1].message)
+ 2
+ 1
+ Unknown attribute: z
+ """
+ attr = 0
+ for c in str_attr:
+ if '0' <= c <= '3':
+ c = 'set' + c
+ if not hasattr(smartypants.Attr, c):
+ warnings.warn('Unknown attribute: %s' % c, Warning)
+ continue
+ attr |= getattr(smartypants.Attr, c)
+
+ return attr
+
+
def main():
parser = argparse.ArgumentParser(description=smartypants.__description__)
@@ -88,7 +117,7 @@ def main():
args = parser.parse_args()
with warnings.catch_warnings(record=True) as w:
- attr = smartypants._str_attr_to_int(args.attr)
+ attr = _str_attr_to_int(args.attr)
if len(w):
print(w[-1].message)
sys.exit(1)
diff --git a/smartypants.py b/smartypants.py
index 3202e4d..7078547 100755
--- a/smartypants.py
+++ b/smartypants.py
@@ -1,6 +1,6 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
-# Copyright (c) 2013, 2014 Yu-Jie Lin
+# Copyright (c) 2013, 2014, 2016 Yu-Jie Lin
# Copyright (c) 2004, 2005, 2007, 2013 Chad Miller
# Copyright (c) 2003 John Gruber
# Licensed under the BSD License, for detailed license information, see COPYING
@@ -14,33 +14,32 @@ smartypants module
__author__ = 'Yu-Jie Lin'
__author_email__ = 'livibetter@gmail.com'
-__version__ = '1.8.6'
+__version__ = '2.0.0dev'
__license__ = 'BSD License'
__url__ = 'https://bitbucket.org/livibetter/smartypants.py'
__description__ = 'Python with the SmartyPants'
import re
-import warnings
class _Attr(object):
"""
class for instantiation of module attribute :attr:`Attr`.
"""
- q = 0b000000001
+ q = 1 << 0
"""
flag for normal quotes (``"``) and (``'``) to curly ones.
.. seealso:: :func:`convert_quotes`
"""
- b = 0b000000010
+ b = 1 << 1
"""
flag for double quotes (````backticks''``) to curly ones.
.. seealso:: :func:`convert_backticks`
"""
- B = 0b000000110
+ B = 1 << 2 | b
"""
flag for double quotes (````backticks''``) and single quotes
(```single'``) to curly ones.
@@ -49,20 +48,20 @@ class _Attr(object):
"""
mask_b = b | B
- d = 0b000001000
+ d = 1 << 3
"""
flag for dashes (``--``) to em-dashes.
.. seealso:: :func:`convert_dashes`
"""
- D = 0b000011000
+ D = 1 << 4 | d
"""
flag for old-school typewriter dashes (``--``) to en-dashes and dashes
(``---``) to em-dashes.
.. seealso:: :func:`convert_dashes_oldschool`
"""
- i = 0b000101000
+ i = 1 << 5 | d
"""
flag for inverted old-school typewriter dashes (``--``) to em-dashes and
dashes (``---``) to en-dashes.
@@ -71,13 +70,13 @@ class _Attr(object):
"""
mask_d = d | D | i
- e = 0b001000000
+ e = 1 << 6
"""
flag for dashes (``...``) to ellipses.
.. seealso:: :func:`convert_ellipses`
"""
- w = 0b010000000
+ w = 1 << 7
"""
flag for dashes (``&quot;``) to ASCII double quotes (``"``).
@@ -93,13 +92,28 @@ class _Attr(object):
regular quotes so SmartyPants can educate them.
"""
- s = 0b100000000
+ u = 0 << 9 | 1 << 8
"""
- Stupefy mode. Reverses the SmartyPants transformation process, turning
- the HTML entities produced by SmartyPants into their ASCII equivalents.
- E.g. ``&#8220;`` is turned into a simple double-quote ("), ``&#8212;`` is
- turned into two dashes, etc.
+ Output Unicode characters instead of numeric character references, for
+ example, from ``&#8220;`` to left double quotation mark (``“``) (U+201C).
+
+ .. seealso:: :func:`convert_entities`
+ """
+ h = 1 << 9 | 0 << 8
+ """
+ Output HTML named entities instead of numeric character references, for
+ example, from ``&#8220;`` to ``&ldquo;``.
+
+ .. seealso:: :func:`convert_entities`
+ """
+ s = 1 << 9 | 1 << 8
"""
+ Output ASCII equivalents instead of numeric character references, for
+ example, from ``&#8212;`` to ``--``.
+
+ .. seealso:: :func:`convert_entities`
+ """
+ mask_o = u | h | s
set0 = 0
"suppress all transformations. (Do nothing.)"
@@ -166,89 +180,6 @@ def _tags_to_skip_regex(tags=None):
return re.compile('<(/)?(%s)[^>]*>' % tags, re.I)
-def verify_installation(request):
-
- msg = 'Pyblosxom support will be removed at Version 2.0.0'
- warnings.filterwarnings('once', msg, DeprecationWarning)
- warnings.warn(msg, DeprecationWarning)
- return 1
- # assert the plugin is functional
-
-
-def cb_story(args):
-
- msg = 'Pyblosxom support will be removed at Version 2.0.0'
- warnings.filterwarnings('once', msg, DeprecationWarning)
- warnings.warn(msg, DeprecationWarning)
-
- global default_smartypants_attr
-
- try:
- forbidden_flavours = args["entry"]["smartypants_forbidden_flavours"]
- except KeyError:
- forbidden_flavours = ["rss"]
-
- try:
- attributes = args["entry"]["smartypants_attributes"]
- except KeyError:
- attributes = default_smartypants_attr
-
- if attributes is None:
- attributes = default_smartypants_attr
-
- entryData = args["entry"].getData()
-
- try:
- if args["request"]["flavour"] in forbidden_flavours:
- return
- except KeyError:
- if "&lt;" in args["entry"]["body"][0:15]: # sniff the stream
- return # abort if it looks like escaped HTML. FIXME
-
- # FIXME: make these configurable, perhaps?
- args["entry"]["body"] = smartypants(entryData, attributes)
- args["entry"]["title"] = smartypants(args["entry"]["title"], attributes)
-
-
-def _str_attr_to_int(str_attr):
- """
- Convert deprecated str-type attr into int
-
- >>> f = _str_attr_to_int
- >>> f('q') == Attr.q
- True
- >>> f('1') == Attr.set1
- True
- >>> with warnings.catch_warnings(record=True) as w:
- ... f('bz')
- ... len(w)
- ... print(w[-1].message)
- 2
- 1
- Unknown attribute: z
- """
- attr = 0
- for c in str_attr:
- if '0' <= c <= '3':
- c = 'set' + c
- if not hasattr(Attr, c):
- warnings.warn('Unknown attribute: %s' % c, Warning)
- continue
- attr |= getattr(Attr, c)
-
- return attr
-
-
-def smartyPants(text, attr=None):
-
- msg = ('smartyPants function will be removed at Version 2.0.0, '
- 'use smartypants, instead')
- warnings.filterwarnings('once', msg, DeprecationWarning)
- warnings.warn(msg, DeprecationWarning)
-
- return smartypants(text, attr)
-
-
def smartypants(text, attr=None):
"""
SmartyPants function
@@ -263,17 +194,11 @@ def smartypants(text, attr=None):
if attr is None:
attr = Attr.default
- if isinstance(attr, str):
- msg = 'str-type attr will be removed at Version 2.0.0'
- warnings.filterwarnings('once', msg, DeprecationWarning)
- warnings.warn(msg, DeprecationWarning)
- attr = _str_attr_to_int(attr)
-
do_quotes = attr & Attr.q
do_backticks = attr & Attr.mask_b
do_dashes = attr & Attr.mask_d
do_ellipses = attr & Attr.e
- do_stupefy = attr & Attr.s
+ do_entities = attr & Attr.mask_o
convert_quot = attr & Attr.w
tokens = _tokenize(text)
@@ -357,8 +282,12 @@ def smartypants(text, attr=None):
# Normal case:
t = convert_quotes(t)
- if do_stupefy:
- t = stupefy_entities(t)
+ if do_entities:
+ mode = (0 if do_entities == Attr.u else
+ 1 if do_entities == Attr.h else
+ 2 if do_entities == Attr.s else
+ 3) # would result in key error
+ t = convert_entities(t, mode)
prev_token_last_char = last_char
result.append(t)
@@ -366,15 +295,6 @@ def smartypants(text, attr=None):
return "".join(result)
-def educateQuotes(text):
-
- msg = 'educateQuotes will be removed at Version 2.0.0'
- warnings.filterwarnings('once', msg, DeprecationWarning)
- warnings.warn(msg, DeprecationWarning)
-
- return convert_quotes(text)
-
-
def convert_quotes(text):
"""
Convert quotes in *text* into HTML curly quote entities.
@@ -469,15 +389,6 @@ def convert_quotes(text):
return text
-def educateBackticks(text):
-
- msg = 'educateBackticks will be removed at Version 2.0.0'
- warnings.filterwarnings('once', msg, DeprecationWarning)
- warnings.warn(msg, DeprecationWarning)
-
- return convert_backticks(text)
-
-
def convert_backticks(text):
"""
Convert ````backticks''``-style double quotes in *text* into HTML curly
@@ -492,15 +403,6 @@ def convert_backticks(text):
return text
-def educateSingleBackticks(text):
-
- msg = 'educateSingleBackticks will be removed at Version 2.0.0'
- warnings.filterwarnings('once', msg, DeprecationWarning)
- warnings.warn(msg, DeprecationWarning)
-
- return convert_single_backticks(text)
-
-
def convert_single_backticks(text):
"""
Convert ```backticks'``-style single quotes in *text* into HTML curly
@@ -515,15 +417,6 @@ def convert_single_backticks(text):
return text
-def educateDashes(text):
-
- msg = 'educateDashes will be removed at Version 2.0.0'
- warnings.filterwarnings('once', msg, DeprecationWarning)
- warnings.warn(msg, DeprecationWarning)
-
- return convert_dashes(text)
-
-
def convert_dashes(text):
"""
Convert ``--`` in *text* into em-dash HTML entities.
@@ -537,15 +430,6 @@ def convert_dashes(text):
return text
-def educateDashesOldSchool(text):
-
- msg = 'educateDashesOldSchool will be removed at Version 2.0.0'
- warnings.filterwarnings('once', msg, DeprecationWarning)
- warnings.warn(msg, DeprecationWarning)
-
- return convert_dashes_oldschool(text)
-
-
def convert_dashes_oldschool(text):
"""
Convert ``--`` and ``---`` in *text* into en-dash and em-dash HTML
@@ -561,15 +445,6 @@ def convert_dashes_oldschool(text):
return text
-def educateDashesOldSchoolInverted(text):
-
- msg = 'educateDashesOldSchoolInverted will be removed at Version 2.0.0'
- warnings.filterwarnings('once', msg, DeprecationWarning)
- warnings.warn(msg, DeprecationWarning)
-
- return convert_dashes_oldschool_inverted(text)
-
-
def convert_dashes_oldschool_inverted(text):
"""
Convert ``--`` and ``---`` in *text* into em-dash and en-dash HTML
@@ -595,15 +470,6 @@ def convert_dashes_oldschool_inverted(text):
return text
-def educateEllipses(text):
-
- msg = 'educateEllipses will be removed at Version 2.0.0'
- warnings.filterwarnings('once', msg, DeprecationWarning)
- warnings.warn(msg, DeprecationWarning)
-
- return convert_ellipses(text)
-
-
def convert_ellipses(text):
"""
Convert ``...`` in *text* into ellipsis HTML entities
@@ -617,46 +483,38 @@ def convert_ellipses(text):
return text
-def stupefyEntities(text):
-
- msg = 'stupefyEntities will be removed at Version 2.0.0'
- warnings.filterwarnings('once', msg, DeprecationWarning)
- warnings.warn(msg, DeprecationWarning)
-
- return stupefy_entities(text)
-
-
-def stupefy_entities(text):
+def convert_entities(text, mode):
"""
- Convert SmartyPants HTML entities in *text* into their ASCII counterparts.
+ Convert numeric character references to, if *mode* is
+
+ - *0*: Unicode characters
+ - *1*: HTML named entities
+ - *2*: ASCII equivalents
- >>> print(stupefy_entities('&#8220;Hello &#8212; world.&#8221;'))
+ >>> print(convert_entities('&#8216;', 0))
+ ‘
+ >>> print(convert_entities('&#8216;SmartyPants&#8217;', 1))
+ &lsquo;SmartyPants&rsquo;
+ >>> print(convert_entities('&#8220;Hello &#8212; world.&#8221;', 2))
"Hello -- world."
"""
- text = re.sub('&#8211;', '-', text) # en-dash
- text = re.sub('&#8212;', '--', text) # em-dash
-
- text = re.sub('&#8216;', "'", text) # open single quote
- text = re.sub('&#8217;', "'", text) # close single quote
-
- text = re.sub('&#8220;', '"', text) # open double quote
- text = re.sub('&#8221;', '"', text) # close double quote
+ CTBL = {
+ '&#8211;': ('–', '&ndash;', '-'),
+ '&#8212;': ('—', '&mdash;', '--'),
+ '&#8216;': ('‘', '&lsquo;', "'"),
+ '&#8217;': ('’', '&rsquo;', "'"),
+ '&#8220;': ('“', '&ldquo;', '"'),
+ '&#8221;': ('”', '&rdquo;', '"'),
+ '&#8230;': ('…', '&hellip;', '...'),
+ }
- text = re.sub('&#8230;', '...', text) # ellipsis
+ for k, v in CTBL.items():
+ text = text.replace(k, v[mode])
return text
-def processEscapes(text):
-
- msg = 'processEscapes will be removed at Version 2.0.0'
- warnings.filterwarnings('once', msg, DeprecationWarning)
- warnings.warn(msg, DeprecationWarning)
-
- return process_escapes(text)
-
-
def process_escapes(text):
r"""
Processe the following backslash escape sequences in *text*. This is useful
diff --git a/tests/test.py b/tests/test.py
index c9451ac..a16178b 100644
--- a/tests/test.py
+++ b/tests/test.py
@@ -1,5 +1,6 @@
#!/usr/bin/env python
-# Copyright (c) 2013 Yu-Jie Lin
+# -*- coding: utf-8 -*-
+# Copyright (c) 2013, 2016 Yu-Jie Lin
# Licensed under the BSD License, for detailed license information, see COPYING
import doctest
@@ -133,6 +134,19 @@ document.write('<a href="' + href + '">' + linktext + "</a>");
self.assertEqual(sp('"Isn\'t this fun?"'),
'&#8220;Isn&#8217;t this fun?&#8221;')
+ def test_convert_entities(self):
+
+ self.assertEqual(sp('"quote here"', Attr.set1 | Attr.u),
+ '“quote here”')
+ self.assertEqual(sp('"quote&ndash;here"', Attr.set1 | Attr.u),
+ '“quote&ndash;here”')
+
+ self.assertEqual(sp('"quote here"', Attr.set1 | Attr.h),
+ '&ldquo;quote here&rdquo;')
+
+ self.assertEqual(sp('"quote here"', Attr.set1 | Attr.s),
+ '"quote here"')
+
def load_tests(loader, tests, pattern):
diff --git a/tests/test_deprecated.py b/tests/test_deprecated.py
index 32e2fbd..8d4fc7e 100644
--- a/tests/test_deprecated.py
+++ b/tests/test_deprecated.py
@@ -1,55 +1,10 @@
#!/usr/bin/env python
-# Copyright (c) 2013 Yu-Jie Lin
+# Copyright (c) 2013, 2016 Yu-Jie Lin
# Licensed under the BSD License, for detailed license information, see COPYING
import unittest
-import warnings
-
-import smartypants as sps
-from smartypants import smartypants as sp
-from smartypants import smartyPants as sP
-from smartypants import Attr
class SmartyPantsDeprecatedTestCase(unittest.TestCase):
- def test_str_attr(self):
-
- TEXT = '"foo" -- bar'
-
- with warnings.catch_warnings(record=True) as w:
-
- T = sp(TEXT, 'q')
- E = '&#8220;foo&#8221; -- bar'
- self.assertEquals(T, E)
-
- T = sp(TEXT, 'qd')
- E = '&#8220;foo&#8221; &#8212; bar'
- self.assertEquals(T, E)
-
- # should only get warning 'once'
- self.assertEquals(len(w), 1)
-
- def test_smartyPants(self):
-
- TEXT = '"foo" -- bar'
-
- with warnings.catch_warnings(record=True) as w:
-
- T = sP(TEXT, Attr.q)
- E = '&#8220;foo&#8221; -- bar'
- self.assertEquals(T, E)
-
- self.assertEquals(len(w), 1)
-
- def test_educateQuotes(self):
-
- TEXT = '"foo" -- bar'
-
- with warnings.catch_warnings(record=True) as w:
-
- T = sps.educateQuotes(TEXT)
- E = '&#8220;foo&#8221; -- bar'
- self.assertEquals(T, E)
-
- self.assertEquals(len(w), 1)
+ pass