diff options
author | Yu-Jie Lin <livibetter@gmail.com> | 2013-08-15 04:03:33 +0800 |
---|---|---|
committer | Yu-Jie Lin <livibetter@gmail.com> | 2013-08-15 04:03:33 +0800 |
commit | 443532b1c8d5a56537f9ffd5b8a97b6c898a5cd4 (patch) | |
tree | 20e38983ed82361990153e478248a5a3e3a9312a | |
parent | 846cef1b173cda09ca64d154fd7d3e38f9e22f8b (diff) | |
download | smartypants-443532b1c8d5a56537f9ffd5b8a97b6c898a5cd4.tar.gz |
redesign attr input with new Attr class
-rw-r--r-- | CHANGES.rst | 2 | ||||
-rw-r--r-- | README.rst | 106 | ||||
-rwxr-xr-x | smartypants | 17 | ||||
-rwxr-xr-x | smartypants.py | 124 | ||||
-rw-r--r-- | tests/test.py | 6 |
5 files changed, 124 insertions, 131 deletions
diff --git a/CHANGES.rst b/CHANGES.rst index 988b268..310aefd 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -5,10 +5,12 @@ Versions without timestamps mean they are future releases. 2.0.0: - drop Pyblosxom support + - use ``Attr.default`` instead of ``default_smartypants_attr`` development: - add ``--version`` - add ``install_test`` target for checking package installation + - redesign attr input with new ``Attr`` class 1.7.1: 2013-08-14T06:45:59Z - fix README-PyPI.rst missing while installing @@ -93,71 +93,33 @@ foot and inch marks: 6'2" tall; a 17" iMac. Options ======= -smartypants.py only accepts SmartyPants attributes, which are represented by -a string and can be inputed like: +smartypants.py only accepts SmartyPants attributes, which are accessible via +``smartypants.Attr``: -.. code:: python - - smartypants.smartyPants(text, attrs) - -In CLI: - -.. code:: sh - - echo "$text" | smartypants -a "$attrs" - -The attribute value can be: - -``"0"`` - Suppress all transformations. (Do nothing.) -``"1"`` (Default) - Performs default SmartyPants transformations: quotes (including - \`\`backticks''-style), em-dashes, and ellipses. ``--`` (dash dash) - is used to signify an em-dash; there is no support for en-dashes. - -``"2"`` - Same as ``"1"``, except that it uses the old-school typewriter shorthand - for dashes: ``--`` (dash dash) for en-dashes, ``---`` (dash dash dash) for - em-dashes. - -``"3"`` - Same as ``"2"``, but inverts the shorthand for dashes: ``--`` (dash dash) - for em-dashes, and ``---`` (dash dash dash) for en-dashes. - -``"-1"`` - Stupefy mode. Reverses the SmartyPants transformation process, turning - the HTML entities produced by SmartyPants into their ASCII equivalents. - E.g. ``“`` is turned into a simple double-quote ("), ``—`` is - turned into two dashes, etc. - -Besides the single-character attribute as listed above. The following -single-character attribute values can be combined to toggle individual -transformations from within the SmartyPants attributes: - -``"q"`` +``Attr.q`` Educates normal quote characters: (") and ('). -``"b"`` +``Attr.b`` Educates \`\`backticks''-style double quotes. -``"B"`` +``Attr.B`` Educates \`\`backticks''-style double quotes and \`single' quotes. -``"d"`` +``Attr.d`` Educates em-dashes. -``"D"`` +``Attr.D`` Educates em-dashes and en-dashes, using old-school typewriter shorthand: (dash dash) for en-dashes, (dash dash dash) for em-dashes. -``"i"`` +``Attr.i`` Educates em-dashes and en-dashes, using inverted old-school typewriter shorthand: (dash dash) for em-dashes, (dash dash dash) for en-dashes. -``"e"`` +``Attr.e`` Educates ellipses. -``"w"`` +``Attr.w`` Translates any instance of ``"`` into a normal double-quote character. This should be of no interest to most people, but of particular interest to anyone who writes their posts using Dreamweaver, as Dreamweaver @@ -170,6 +132,54 @@ transformations from within the SmartyPants attributes: and em-dashes, and ellipses) and also translate ``"`` entities into regular quotes so SmartyPants can educate them. +``Attr.s`` + Stupefy mode. Reverses the SmartyPants transformation process, turning + the HTML entities produced by SmartyPants into their ASCII equivalents. + E.g. ``“`` is turned into a simple double-quote ("), ``—`` is + turned into two dashes, etc. + +``Attr.set0`` + Suppress all transformations. (Do nothing.) + +``Attr.set1`` = ``Attr.q | Attr.b | Attr.d | Attr.e`` (Default) + Performs default SmartyPants transformations: quotes (including + \`\`backticks''-style), em-dashes, and ellipses. ``--`` (dash dash) + is used to signify an em-dash; there is no support for en-dashes. + +``Attr.set2`` = ``Attr.q | Attr.b | Attr.D | Attr.e`` + Same as ``Attr.set1``, except that it uses the old-school typewriter shorthand + for dashes: ``--`` (dash dash) for en-dashes, ``---`` (dash dash dash) for + em-dashes. + +``Attr.set3`` = ``Attr.q | Attr.b | Attr.i | Attr.e`` + Same as ``Attr.set2``, but inverts the shorthand for dashes: ``--`` (dash dash) + 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. + +To use these attributes, simply using bitwise or: + +.. code:: python + + from smartypants import Attr + + attrs = Attr.q | Attr.d + smartypants.smartyPants(text, attrs) + + attrs = Attr.set1 | Attr.w + smartypants.smartyPants(text, attrs) + +When using in command-line, use only the attribute names and drop ``set``: + +.. code:: sh + + attrs="qd" + echo "$text" | smartypants -a "$attrs" + + attrs="1w" + echo "$text" | smartypants -a "$attrs" + Bugs ==== diff --git a/smartypants b/smartypants index f02a785..6d949c1 100755 --- a/smartypants +++ b/smartypants @@ -7,24 +7,33 @@ import argparse import sys import smartypants +from smartypants import Attr def main(): parser = argparse.ArgumentParser(description=smartypants.__description__, version=smartypants.__version__) - parser.add_argument('-a', '--attr', - default=smartypants.default_smartypants_attr, + parser.add_argument('-a', '--attr', default='1', help='processing attributes (Default: %(default)s)') parser.add_argument('files', metavar='FILE', type=argparse.FileType('r'), nargs='*', help='files to be processed ') args = parser.parse_args() + attr = 0 + for c in args.attr: + if '0' <= c <= '3': + c = 'set' + c + if not hasattr(Attr, c): + print('Unknown attribute: %s' % c, file=sys.stderr) + sys.exit(1) + attr |= getattr(Attr, c) + if args.files: for f in args.files: - print(smartypants.smartyPants(f.read(), args.attr), end='') + print(smartypants.smartyPants(f.read(), attr), end='') else: - print(smartypants.smartyPants(sys.stdin.read(), args.attr), end='') + print(smartypants.smartyPants(sys.stdin.read(), attr), end='') if __name__ == '__main__': diff --git a/smartypants.py b/smartypants.py index 911d5d6..e210452 100755 --- a/smartypants.py +++ b/smartypants.py @@ -11,11 +11,38 @@ __license__ = 'BSD License' __url__ = 'https://bitbucket.org/livibetter/smartypants.py' __description__ = 'Python with the SmartyPants' -default_smartypants_attr = "1" - import re import warnings + +class Attr: + + q = 0b000000001 # quotes + + b = 0b000000010 # backtick quotes (``double'' only) + B = 0b000000110 # backtick quotes (``double'' and `single') + mask_b = b | B + + d = 0b000001000 # dashes + D = 0b000011000 # old school dashes + i = 0b000101000 # inverted old school dashes + mask_d = d | D | i + + e = 0b001000000 # ellipses + w = 0b010000000 # convert " entities to " + + s = 0b100000000 # special "stupefy" mode. + + set0 = 0 # do nothing + set1 = q | b | d | e # set all + set2 = q | b | D | e # set all, using old school en- and em- dash + # shortcuts + set3 = q | b | i | e # set all, using inverted old school en & em- dash + # shortcuts + default = set1 + +default_smartypants_attr = Attr.default + tags_to_skip_regex = re.compile('<(/)?(pre|code|kbd|script|math)[^>]*>', re.I) @@ -66,76 +93,21 @@ def cb_story(args): ### interal functions below here def smartyPants(text, attr=default_smartypants_attr): - # should we translate " entities into normal quotes? - convert_quot = 0 - - # Parse attributes: - # 0 : do nothing - # 1 : set all - # 2 : set all, using old school en- and em- dash shortcuts - # 3 : set all, using inverted old school en and em- dash shortcuts - # - # q : quotes - # b : backtick quotes (``double'' only) - # B : backtick quotes (``double'' and `single') - # d : dashes - # D : old school dashes - # i : inverted old school dashes - # e : ellipses - # w : convert " entities to " for Dreamweaver users + """ + SmartyPants function + >>> print(smartyPants('"foo" -- bar')) + “foo” — bar + >>> print(smartyPants('"foo" -- bar', Attr.d)) + "foo" — bar + """ skipped_tag_stack = [] - do_dashes = 0 - do_backticks = 0 - do_quotes = 0 - do_ellipses = 0 - do_stupefy = 0 - - if attr == "0": - # Do nothing. - 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_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 + convert_quot = attr & Attr.w tokens = _tokenize(text) result = [] @@ -181,21 +153,21 @@ def smartyPants(text, attr=default_smartypants_attr): t = re.sub('"', '"', t) if do_dashes: - if do_dashes == 1: + if do_dashes == Attr.d: t = educateDashes(t) - if do_dashes == 2: + if do_dashes == Attr.D: t = educateDashesOldSchool(t) - if do_dashes == 3: + if do_dashes == Attr.i: t = educateDashesOldSchoolInverted(t) if do_ellipses: t = educateEllipses(t) # Note: backticks need to be processed before quotes. - if do_backticks: + if do_backticks == Attr.b: t = educateBackticks(t) - if do_backticks == 2: + if do_backticks == Attr.B: t = educateSingleBackticks(t) if do_quotes: @@ -216,7 +188,7 @@ def smartyPants(text, attr=default_smartypants_attr): # Normal case: t = educateQuotes(t) - if do_stupefy == 1: + if do_stupefy: t = stupefyEntities(t) prev_token_last_char = last_char diff --git a/tests/test.py b/tests/test.py index 0f36e0d..c03d647 100644 --- a/tests/test.py +++ b/tests/test.py @@ -2,7 +2,7 @@ import doctest import unittest import smartypants -from smartypants import smartyPants as sp +from smartypants import Attr, smartyPants as sp class TestSmartypantsAllAttributes(unittest.TestCase): @@ -49,13 +49,13 @@ document.write('<a href="' + href + '">' + linktext + "</a>"); "is python code.</p>") self.assertEqual(T, E) - T = sp(TEXT, 'w') + T = sp(TEXT, Attr.w) E = ('<p>He said "Let\'s write some code." ' "This code here <code>if True:\n\tprint "Okay"</code> " "is python code.</p>") self.assertEqual(T, E) - T = sp(TEXT, 'qw') + T = sp(TEXT, Attr.q | Attr.w) E = ("<p>He said “Let’s write some code.” " "This code here <code>if True:\n\tprint "Okay"</code> " "is python code.</p>") |