summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYu-Jie Lin <livibetter@gmail.com>2013-08-18 13:36:51 +0800
committerYu-Jie Lin <livibetter@gmail.com>2013-08-18 13:36:51 +0800
commitd281de63c7f21b8ae962b626f30d28410b957da2 (patch)
tree643b2fa7ace35345150c7e15b8c03e53d9fa1d0c
parente417f01e3f4a8821dc50259fa9dbcaad8141fd60 (diff)
downloadsmartypants-d281de63c7f21b8ae962b626f30d28410b957da2.tar.gz
Properly deprecate default_smartypants_attr
b4d164b6a497 uses new class, but it's still using the variable. Now it will use Attr.default property, although it's now still get the value from that variable.
-rw-r--r--CHANGES.rst2
-rw-r--r--README.rst2
-rwxr-xr-xsmartypants.py25
-rw-r--r--tests/test.py22
4 files changed, 44 insertions, 7 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index e94e74f..55a827c 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -13,7 +13,7 @@ development:
- add ``install_test`` target for checking package installation
- add ``test`` target for all tests
- deprecate str-type ``attr`` with:
- - redesign attr input with new ``Attr`` class
+ - redesign attr input with new ``Attr`` object
- ``"-1"`` now is ``Attr.s`` (``"s"``)
- ``_str_attr_to_int()`` to handle str-type before the removal
diff --git a/README.rst b/README.rst
index 7f9a856..e819060 100644
--- a/README.rst
+++ b/README.rst
@@ -156,7 +156,7 @@ smartypants.py only accepts SmartyPants attributes, which are accessible via
for em-dashes, and ``---`` (dash dash dash) for en-dashes.
``Attr.default`` = ``Attr.set1``
- Will be the default argument value of ``smartyPants()`` once ``default_smartypants_attr`` removed at Version 2.0.0.
+ Default attributes.
To use these attributes, simply using bitwise or:
diff --git a/smartypants.py b/smartypants.py
index 058c971..169afa3 100755
--- a/smartypants.py
+++ b/smartypants.py
@@ -15,7 +15,7 @@ import re
import warnings
-class Attr:
+class _Attr(object):
q = 0b000000001 # quotes
@@ -39,9 +39,23 @@ class Attr:
# shortcuts
set3 = q | b | i | e # set all, using inverted old school en & em- dash
# shortcuts
- default = set1
-default_smartypants_attr = Attr.default
+ @property
+ def default(self):
+
+ global default_smartypants_attr
+ return default_smartypants_attr
+
+ @default.setter
+ def default(self, attr):
+
+ global default_smartypants_attr
+ default_smartypants_attr = attr
+
+
+Attr = _Attr()
+
+default_smartypants_attr = Attr.set1
tags_to_skip_regex = re.compile('<(/)?(pre|code|kbd|script|math)[^>]*>', re.I)
@@ -121,7 +135,7 @@ def _str_attr_to_int(str_attr):
return attr
-def smartyPants(text, attr=default_smartypants_attr):
+def smartyPants(text, attr=None):
"""
SmartyPants function
@@ -132,6 +146,9 @@ def smartyPants(text, attr=default_smartypants_attr):
"""
skipped_tag_stack = []
+ 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)
diff --git a/tests/test.py b/tests/test.py
index e2a28c9..2905427 100644
--- a/tests/test.py
+++ b/tests/test.py
@@ -9,7 +9,27 @@ import smartypants
from smartypants import Attr, smartyPants as sp
-class TestSmartypantsAllAttributes(unittest.TestCase):
+class SmartyPantsTestCase(unittest.TestCase):
+
+ def test_change_default_attr(self):
+
+ TEXT = '"foo" -- bar'
+
+ attr = Attr.q
+ Attr.default = attr
+ self.assertEqual(Attr.default, attr)
+
+ T = sp(TEXT)
+ E = '&#8220;foo&#8221; -- bar'
+ self.assertEquals(T, E)
+
+ attr = Attr.q | Attr.d
+ Attr.default = attr
+ self.assertEqual(Attr.default, attr)
+
+ T = sp(TEXT)
+ E = '&#8220;foo&#8221; &#8212; bar'
+ self.assertEquals(T, E)
def test_dates(self):