Usage

Typically, you import smartypants and call the smartypants.smartypants() to convert a text, for example:

import smartypants

text = '"SmartyPants" is smart, so is <code>smartypants.py</code> -- a Python port'
print(smartypants.smartypants(text))

It results:

&#8220;SmartyPants&#8221; is smart, so is <code>smartypants.py</code> &#8212; a Python port

Attributes

smartypants.smartypants() accepts processing attributes, which tells it what need to be converted.

See also

smartypants.Attr for a list of attributes.

To use these attributes, simply using bitwise OR operator, that is A | B:

from smartypants import Attr

attrs = Attr.q | Attr.d
smartypants.smartypants(text, attrs)

attrs = Attr.set1 | Attr.w
smartypants.smartypants(text, attrs)

Skipped HTML elements

Elements

By default, there are a few HTML elements that smartypants.smartypants() do not try to be smart with them:

tags_to_skip = ['pre', 'samp', 'code', 'tt', 'kbd', 'script', 'style', 'math']

If you need to change, for example, adding additional tags and remove one of them:

>>> from smartypants import tags_to_skip as tags
>>> tags.append('a')
>>> tags.remove('code')
>>> tags
['pre', 'samp', 'tt', 'kbd', 'script', 'style', 'math', 'a']

The smartypants.tags_to_skip is compiled into a regular expression for being used by smartypants.smartypants(). You could actually overwrite smartypants._tags_to_skip_regex() and return with your own regular expression.

Comments

HTML comments are always skipped since they are not rendered in browsers.

>>> from smartypants import smartypants as sp
>>> print(sp('<!-- <span>"foobar"</span> -->'))
<!-- <span>"foobar"</span> -->

Important

Beware of --, which should not or must not be in a HTML comment.

>>> from smartypants import smartypants as sp
>>> print(sp('<!-- <span>"foo--bar"</span> -->'))
<!&#8212; <span>&#8221;foo&#8212;bar&#8221;</span> &#8212;>

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\".

See also

smartypants.process_escapes() for a complete list of backslash escapes.

Table Of Contents

Previous topic

Introduction

Next topic

Command-line

This Page