summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYu-Jie Lin <livibetter@gmail.com>2013-08-18 14:17:50 +0800
committerYu-Jie Lin <livibetter@gmail.com>2013-08-18 14:17:50 +0800
commit59bf54f05302880f612604b090971e22b0cd975c (patch)
tree46cd04f71e16f9551368a02dc47a202cc5f913ea
parent2df89c8ca06e662e20547d201ef438f9ac86fff8 (diff)
downloadsmartypants-59bf54f05302880f612604b090971e22b0cd975c.tar.gz
samp and tt as skipped elements, compile regular expression from tags_to_skip list
-rw-r--r--CHANGES.rst3
-rw-r--r--README.rst46
-rwxr-xr-xsmartypants.py27
3 files changed, 65 insertions, 11 deletions
diff --git a/CHANGES.rst b/CHANGES.rst
index 201a7c5..07c0b3f 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -13,6 +13,9 @@ development:
- add ``--version``
- add ``install_test`` target for checking package installation
- add ``test`` target for all tests
+ - add ``samp`` and ``tt`` to be skipped HTML elements
+ - remove ``tags_to_skip_regex`` and add ``tags_to_skip`` as a list of
+ skipped HTML with a helper function to compile a regular expreesion
- deprecate str-type ``attr`` with:
- redesign attr input with new ``Attr`` object
- ``"-1"`` now is ``Attr.s`` (``"s"``)
diff --git a/README.rst b/README.rst
index cb5cc23..2a46a32 100644
--- a/README.rst
+++ b/README.rst
@@ -7,6 +7,8 @@ smartypants.py is a Python port of SmartyPants_.
.. _SmartyPants: http://daringfireball.net/projects/smartypants/
.. contents:: **Contents**
+ :local:
+ :backlinks: top
Description
@@ -24,10 +26,10 @@ ASCII straight quotes, plain dashes, and plain dots, but your published
posts (and final HTML output) will appear with smart quotes, em-dashes,
and proper ellipses.
-SmartyPants does not modify characters within ``<pre>``, ``<code>``, ``<kbd>``,
-``<math>`` or ``<script>`` tag blocks. Typically, these tags are used to
-display text where smart quotes and other "smart punctuation" would not be
-appropriate, such as source code or example markup.
+SmartyPants does not modify contents in some HTML element, see `Skipped HTML
+Elements`_. Typically, these tags are used to display text where smart quotes
+and other "smart punctuation" would not be appropriate, such as source code or
+example markup.
Installation
@@ -93,7 +95,10 @@ foot and inch marks: 6'2" tall; a 17" iMac.
Options
=======
-smartypants.py only accepts SmartyPants attributes, which are accessible via
+Attributes
+----------
+
+``smartypants()`` only accepts SmartyPants attributes, which are accessible via
``smartypants.Attr``:
``Attr.q``
@@ -181,6 +186,31 @@ When using in command-line, use only the attribute names and drop ``set``:
echo "$text" | smartypants -a "$attrs"
+Skipped HTML elements
+---------------------
+
+By default, there are a few HTML elements that ``smartypants()`` do not try to
+be smart with them:
+
+.. code:: python
+
+ tags_to_skip = ['pre', 'samp', 'code', 'tt', 'kbd', 'script', 'math']
+
+If you need to change, for example, adding additional tags and remove one of
+them:
+
+.. code:: python
+
+ >>> from smartypants import tags_to_skip as tags
+ >>> tags.append('a')
+ >>> tags.remove('code')
+ >>> tags
+ ['pre', 'samp', 'tt', 'kbd', 'script', 'math', 'a']
+
+The ``tags_to_skip`` is compiled into a regular expression for being used by
+``smartypants()``. You could actually overwrite ``_tags_to_skip_regex()`` and
+return your own regular expression.
+
Bugs
====
@@ -195,6 +225,8 @@ to illustrate.
History
=======
+*For changelog, please see CHANGES.rst*
+
`John Gruber`_ did all of the hard work of writing this software in Perl for
`Movable Type`_ and almost all of this useful documentation. `Chad Miller`_
ported it to Python to use with Pyblosxom_.
@@ -227,8 +259,8 @@ package ownership on PyPI. It has also officially supported Python 3.
.. _Yu-Jie Lin: http://yjl.im/
-Copyright and License
-=====================
+Copyright
+=========
::
diff --git a/smartypants.py b/smartypants.py
index fdee3b4..9b0df5b 100755
--- a/smartypants.py
+++ b/smartypants.py
@@ -54,10 +54,29 @@ class _Attr(object):
Attr = _Attr()
-
default_smartypants_attr = Attr.set1
-tags_to_skip_regex = re.compile('<(/)?(pre|code|kbd|script|math)[^>]*>', re.I)
+
+tags_to_skip = ['pre', 'samp', 'code', 'tt', 'kbd', 'script', 'math']
+
+
+def _tags_to_skip_regex(tags=None):
+ """
+ Convert a list of skipped tags into regular expression
+
+ >>> f = _tags_to_skip_regex
+ >>> print(f().pattern)
+ <(/)?(pre|code|tt|kbd|script|math)[^>]*>
+ >>> print(f(['foo', 'bar']).pattern)
+ <(/)?(foo|bar)[^>]*>
+ """
+ if tags is None:
+ tags = tags_to_skip
+
+ if isinstance(tags, (list, tuple)):
+ tags = '|'.join(tags)
+
+ return re.compile('<(/)?(%s)[^>]*>' % tags, re.I)
def verify_installation(request):
@@ -104,8 +123,6 @@ def cb_story(args):
args["entry"]["title"] = smartypants(args["entry"]["title"], attributes)
-### interal functions below here
-
def _str_attr_to_int(str_attr):
"""
Convert deprecated str-type attr into int
@@ -183,6 +200,8 @@ def smartypants(text, attr=None):
# token, to use as context to curl single-
# character quote tokens correctly.
+ tags_to_skip_regex = _tags_to_skip_regex()
+
for cur_token in tokens:
if cur_token[0] == "tag":
# Don't mess with quotes inside some tags. This does not handle