diff options
author | Yu-Jie Lin <livibetter@gmail.com> | 2013-08-16 20:22:43 +0800 |
---|---|---|
committer | Yu-Jie Lin <livibetter@gmail.com> | 2013-08-16 20:22:43 +0800 |
commit | e417f01e3f4a8821dc50259fa9dbcaad8141fd60 (patch) | |
tree | d8ae79310e40909bb71c2a33c875e037bef16b9b /smartypants.py | |
parent | 21f49de88218b1f4a3b9d2c7e48c9a6a0d7e592d (diff) | |
download | smartypants-e417f01e3f4a8821dc50259fa9dbcaad8141fd60.tar.gz |
deprecate str-type attr instead of direct removal, which will be removed at v2.0.0. Attr "-1" now is Attr.s (or "s")
a temporary function _str_attr_to_int is added for the transition before v2.0.0.
Diffstat (limited to 'smartypants.py')
-rwxr-xr-x | smartypants.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/smartypants.py b/smartypants.py index e210452..058c971 100755 --- a/smartypants.py +++ b/smartypants.py @@ -92,6 +92,35 @@ def cb_story(args): ### interal functions below here +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=default_smartypants_attr): """ SmartyPants function @@ -102,6 +131,13 @@ def smartyPants(text, attr=default_smartypants_attr): "foo" — bar """ skipped_tag_stack = [] + + 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 |