Reference¶
smartypants module¶
smartypants()
is the core of smartypants module.
- smartypants.Attr = <smartypants._Attr object>¶
Processing attributes, which tells
smartypants()
what to convertSee 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.See also
convert_backticks()
andconvert_single_backticks()
- D = 24¶
flag for old-school typewriter dashes (
--
) to en-dashes and dashes (---
) to em-dashes.See also
- b = 2¶
flag for double quotes (
``backticks''
) to curly ones.See also
- d = 8¶
flag for dashes (
--
) to em-dashes.See also
- e = 64¶
flag for dashes (
...
) to ellipses.See also
- h = 512¶
Output HTML named entities instead of numeric character references, for example, from
“
to“
.See also
- i = 40¶
flag for inverted old-school typewriter dashes (
--
) to em-dashes and dashes (---
) to en-dashes.See also
convert_dashes_oldschool_inverted()
- 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
—
to--
.See also
- set0 = 0¶
suppress all transformations. (Do nothing.)
- u = 256¶
Output Unicode characters instead of numeric character references, for example, from
“
to left double quotation mark (“
) (U+201C).See also
- w = 128¶
flag for dashes (
"
) 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
"
entities into regular quotes so SmartyPants can educate them.
- smartypants._tags_to_skip_regex(tags=None)[source]¶
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) <(/)?(foo|bar)[^>]*>
- smartypants._tokenize(text)[source]¶
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.
- smartypants.convert_backticks(text)[source]¶
Convert
``backticks''
-style double quotes in text into HTML curly quote entities.>>> print(convert_backticks("``Isn't this fun?''")) “Isn't this fun?”
- smartypants.convert_dashes(text)[source]¶
Convert
--
in text into em-dash HTML entities.>>> quote = 'Nothing endures but change. -- Heraclitus' >>> print(convert_dashes(quote)) Nothing endures but change. — Heraclitus
- smartypants.convert_dashes_oldschool(text)[source]¶
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. — Julia Child (1912–2004)
- smartypants.convert_dashes_oldschool_inverted(text)[source]¶
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. — Buckminster Fuller (1895–1983)
- smartypants.convert_ellipses(text)[source]¶
Convert
...
in text into ellipsis HTML entities>>> print(convert_ellipses('Huh...?')) 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('‘', 0)) ‘ >>> print(convert_entities('‘SmartyPants’', 1)) ‘SmartyPants’ >>> print(convert_entities('“Hello — world.”', 2)) "Hello -- world."
- smartypants.convert_quotes(text)[source]¶
Convert quotes in text into HTML curly quote entities.
>>> print(convert_quotes('"Isn\'t this fun?"')) “Isn’t this fun?”
- smartypants.convert_single_backticks(text)[source]¶
Convert
`backticks'
-style single quotes in text into HTML curly quote entities.>>> print(convert_single_backticks("`Isn't this fun?'")) ‘Isn’t this fun?’
- smartypants.process_escapes(text)[source]¶
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
\\
\
\
\"
"
"
\'
'
'
\.
.
.
\-
-
-
\`
`
\`
>>> print(process_escapes(r'\\')) \ >>> print(smartypants(r'"smarty" \"pants\"')) “smarty” "pants"
- smartypants.smartypants(text, attr=None)[source]¶
SmartyPants function
>>> print(smartypants('"foo" -- bar')) “foo” — bar >>> print(smartypants('"foo" -- bar', Attr.d)) "foo" — bar
- smartypants.tags_to_skip = ['pre', 'samp', 'code', 'tt', 'kbd', 'script', 'style', 'math']¶
Skipped HTML elements
See also