smartypants module

smartypants() is the core of smartypants module.

smartypants.Attr = <smartypants._Attr object at 0x7f004c510c50>

Processing attributes, which tells smartypants() what to convert

See also


class smartypants._Attr[source]

class for instantiation of module attribute Attr.

B = 6

flag for double quotes (``backticks'') and single quotes (`single') to curly ones.

D = 24

flag for old-school typewriter dashes (--) to en-dashes and dashes (---) to em-dashes.

b = 2

flag for double quotes (``backticks'') to curly ones.

d = 8

flag for dashes (--) to em-dashes.

See also



Default value of attributes, same value as set1

e = 64

flag for dashes (...) to ellipses.

h = 512

Output HTML named entities instead of numeric character references, for example, from &#8220; to &ldquo;.

i = 40

flag for inverted old-school typewriter dashes (--) to em-dashes and dashes (---) to en-dashes.

q = 1

flag for normal quotes (") and (') to curly ones.

See also


s = 768

Output ASCII equivalents instead of numeric character references, for example, from &#8212; to --.

set0 = 0

suppress all transformations. (Do nothing.)

set1 = 75

equivalent to q | b | d | e

set2 = 91

equivalent to q | b | D | e

For old school en- and em- dash.

set3 = 107

equivalent to q | b | i | e

For inverted old school en & em- dash.”

u = 256

Output Unicode characters instead of numeric character references, for example, from &#8220; to left double quotation mark () (U+201C).

w = 128

flag for dashes (&quot;) to ASCII double quotes (").

This should be of no interest to most people, but of particular interest to anyone who writes their posts using Dreamweaver, as Dreamweaver inexplicably uses this entity to represent a literal double-quote character. SmartyPants only educates normal quotes, not entities (because ordinarily, entities are used for the explicit purpose of representing the specific character they represent). The “w” option must be used in conjunction with one (or both) of the other quote options (“q” or “b”). Thus, if you wish to apply all SmartyPants transformations (quotes, en- and em-dashes, and ellipses) and also convert &quot; entities into regular quotes so SmartyPants can educate them.


Convert a list of skipped tags into regular expression

The default tags are tags_to_skip.

>>> f = _tags_to_skip_regex
>>> print(f(['foo', 'bar']).pattern)

Reference to an array of the tokens comprising the input string. Each token is either a tag (possibly with nested, tags contained therein, such as <a href="<MTFoo>">, or a run of text between tags. Each element of the array is a two-element array; the first is either ‘tag’ or ‘text’; the second is the actual value.

Based on the _tokenize() subroutine from Brad Choate’s MTRegex plugin.


Convert ``backticks''-style double quotes in text into HTML curly quote entities.

>>> print(convert_backticks("``Isn't this fun?''"))
&#8220;Isn't this fun?&#8221;

Convert -- in text into em-dash HTML entities.

>>> quote = 'Nothing endures but change. -- Heraclitus'
>>> print(convert_dashes(quote))
Nothing endures but change. &#8212; Heraclitus

Convert -- and --- in text into en-dash and em-dash HTML entities, respectively.

>>> quote = 'Life itself is the proper binge. --- Julia Child (1912--2004)'
>>> print(convert_dashes_oldschool(quote))
Life itself is the proper binge. &#8212; Julia Child (1912&#8211;2004)

Convert -- and --- in text into em-dash and en-dash HTML entities, respectively.

Two reasons why:

  • First, unlike the en- and em-dash syntax supported by convert_dashes_oldschool(), it’s compatible with existing entries written before SmartyPants 1.1, back when -- was only used for em-dashes.
  • Second, em-dashes are more common than en-dashes, and so it sort of makes sense that the shortcut should be shorter to type. (Thanks to Aaron Swartz for the idea.)
>>> quote = 'Dare to be naïve. -- Buckminster Fuller (1895---1983)'
>>> print(convert_dashes_oldschool_inverted(quote))
Dare to be naïve. &#8212; Buckminster Fuller (1895&#8211;1983)

Convert ... in text into ellipsis HTML entities

>>> print(convert_ellipses('Huh...?'))
smartypants.convert_entities(text, mode)[source]

Convert numeric character references to, if mode is

  • 0: Unicode characters
  • 1: HTML named entities
  • 2: ASCII equivalents
>>> print(convert_entities('&#8216;', 0))

>>> print(convert_entities('&#8216;SmartyPants&#8217;', 1))
>>> print(convert_entities('&#8220;Hello &#8212; world.&#8221;', 2))
"Hello -- world."

Convert quotes in text into HTML curly quote entities.

>>> print(convert_quotes('"Isn\'t this fun?"'))
&#8220;Isn&#8217;t this fun?&#8221;

Convert `backticks'-style single quotes in text into HTML curly quote entities.

>>> print(convert_single_backticks("`Isn't this fun?'"))
&#8216;Isn&#8217;t this fun?&#8217;

Processe the following backslash escape sequences in text. This is useful if you want to force a “dumb” quote or other character to appear.

Escape Value Character
\\ &#92; \
\" &#34; "
\' &#39; '
\. &#46; .
\- &#45; -
\` &#96; \`
>>> print(process_escapes(r'\\'))
>>> print(smartypants(r'"smarty" \"pants\"'))
&#8220;smarty&#8221; &#34;pants&#34;
smartypants.smartypants(text, attr=None)[source]

SmartyPants function

>>> print(smartypants('"foo" -- bar'))
&#8220;foo&#8221; &#8212; bar
>>> print(smartypants('"foo" -- bar', Attr.d))
"foo" &#8212; bar
smartypants.tags_to_skip = ['pre', 'samp', 'code', 'tt', 'kbd', 'script', 'style', 'math']

Skipped HTML elements

Table Of Contents

Previous topic


Next topic


This Page