4.4.1.1.12.6. smartquotes

4.4.1.1.12.6.1. Smart Quotes for Docutils

4.4.1.1.12.6.1.1. Synopsis

“SmartyPants” is a free web publishing plug-in for Movable Type, Blosxom, and BBEdit that easily translates plain ASCII punctuation characters into “smart” typographic punctuation characters.

smartquotes.py is an adaption of “SmartyPants” to Docutils.

  • Using Unicode instead of HTML entities for typographic punctuation characters, it works for any output format that supports Unicode.
  • Supports language specific quote characters.

4.4.1.1.12.6.1.2. Authors

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. Adapted to Docutils by Günter Milde.

4.4.1.1.12.6.1.3. Additional Credits

Portions of the SmartyPants original work are based on Brad Choate’s nifty MTRegex plug-in. Brad Choate also contributed a few bits of source code to this plug-in. Brad Choate is a fine hacker indeed.

Jeremy Hedley and Charles Wiltgen deserve mention for exemplary beta testing of the original SmartyPants.

Rael Dornfest ported SmartyPants to Blosxom.

4.4.1.1.12.6.1.5. Description

SmartyPants can perform the following transformations:

  • Straight quotes ( ” and ‘ ) into “curly” quote characters
  • Backticks-style quotes (``like this’‘) into “curly” quote characters
  • Dashes (-- and ---) into en- and em-dash entities
  • Three consecutive dots (... or . . .) into an ellipsis entity

This means you can write, edit, and save your posts using plain old 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.

4.4.1.1.12.6.1.6. Backslash Escapes

If you need to use literal straight quotes (or plain hyphens and periods), smartquotes accepts the following backslash escape sequences to force ASCII-punctuation. Mind, that you need two backslashes as Docutils expands it, too.

Escape Character
\\ \
\\" "
\\' '
\\. .
\\- -
\\` `

This is useful, for example, when you want to use straight quotes as foot and inch marks: 6'2" tall; a 17" iMac.

4.4.1.1.12.6.1.7. Caveats

4.4.1.1.12.6.1.7.1. Why You Might Not Want to Use Smart Quotes in Your Weblog

For one thing, you might not care.

Most normal, mentally stable individuals do not take notice of proper typographic punctuation. Many design and typography nerds, however, break out in a nasty rash when they encounter, say, a restaurant sign that uses a straight apostrophe to spell “Joe’s”.

If you’re the sort of person who just doesn’t care, you might well want to continue not caring. Using straight quotes – and sticking to the 7-bit ASCII character set in general – is certainly a simpler way to live.

Even if you do care about accurate typography, you still might want to think twice before educating the quote characters in your weblog. One side effect of publishing curly quote characters is that it makes your weblog a bit harder for others to quote from using copy-and-paste. What happens is that when someone copies text from your blog, the copied text contains the 8-bit curly quote characters (as well as the 8-bit characters for em-dashes and ellipses, if you use these options). These characters are not standard across different text encoding methods, which is why they need to be encoded as characters.

People copying text from your weblog, however, may not notice that you’re using curly quotes, and they’ll go ahead and paste the unencoded 8-bit characters copied from their browser into an email message or their own weblog. When pasted as raw “smart quotes”, these characters are likely to get mangled beyond recognition.

That said, my own opinion is that any decent text editor or email client makes it easy to stupefy smart quote characters into their 7-bit equivalents, and I don’t consider it my problem if you’re using an indecent text editor or email client.

4.4.1.1.12.6.1.7.2. Algorithmic Shortcomings

One situation in which quotes will get curled the wrong way is when apostrophes are used at the start of leading contractions. For example:

'Twas the night before Christmas.

In the case above, SmartyPants will turn the apostrophe into an opening single-quote, when in fact it should be the right single quotation mark character which is also “the preferred character to use for apostrophe” (Unicode). I don’t think this problem can be solved in the general case – every word processor I’ve tried gets this wrong as well. In such cases, it’s best to use the proper character for closing single-quotes (’) by hand.

In English, the same character is used for apostrophe and closing single quote (both plain and “smart” ones). For other locales (French, Italean, Swiss, …) “smart” single closing quotes differ from the curly apostrophe.

language-fr

Il dit : “C’est ‘super’ !”

If the apostrophe is used at the end of a word, it cannot be distinguished from a single quote by the algorithm. Therefore, a text like:

.. class:: language-de-CH

"Er sagt: 'Ich fass' es nicht.'"

will get a single closing guillemet instead of an apostrophe.

This can be prevented by use use of the curly apostrophe character (’) in the source:

-  "Er sagt: 'Ich fass' es nicht.'"
+  "Er sagt: 'Ich fass’ es nicht.'"

4.4.1.1.12.6.1.8. Version History

1.8: 2017-04-24
  • Command line front-end.
1.7.1: 2017-03-19
  • Update and extend language-dependent quotes.
  • Differentiate apostrophe from single quote.
1.7: 2012-11-19
  • Internationalization: language-dependent quotes.
1.6.1: 2012-11-06
  • Refactor code, code cleanup,
  • educate_tokens() generator as interface for Docutils.
1.6: 2010-08-26
  • Adaption to Docutils: - Use Unicode instead of HTML entities, - Remove code special to pyblosxom.
1.5_1.6: Fri, 27 Jul 2007 07:06:40 -0400
  • Fixed bug where blocks of precious unalterable text was instead interpreted. Thanks to Le Roux and Dirk van Oosterbosch.
1.5_1.5: Sat, 13 Aug 2005 15:50:24 -0400
  • Fix bogus magical quotation when there is no hint that the user wants it, e.g., in “21st century”. Thanks to Nathan Hamblen.
  • Be smarter about quotes before terminating numbers in an en-dash’ed range.
1.5_1.4: Thu, 10 Feb 2005 20:24:36 -0500
  • Fix a date-processing bug, as reported by jacob childress.
  • Begin a test-suite for ensuring correct output.
  • Removed import of “string”, since I didn’t really need it. (This was my first every Python program. Sue me!)
1.5_1.3: Wed, 15 Sep 2004 18:25:58 -0400
  • Abort processing if the flavour is in forbidden-list. Default of [ “rss” ] (Idea of Wolfgang SCHNERRING.)
  • Remove stray virgules from en-dashes. Patch by Wolfgang SCHNERRING.
1.5_1.2: Mon, 24 May 2004 08:14:54 -0400
  • Some single quotes weren’t replaced properly. Diff-tesuji played by Benjamin GEIGER.
1.5_1.1: Sun, 14 Mar 2004 14:38:28 -0500
  • Support upcoming pyblosxom 0.9 plugin verification feature.
1.5_1.0: Tue, 09 Mar 2004 08:08:35 -0500
  • Initial release
docutils.utils.smartquotes.educateBackticks(text, language='en')[source]

Parameter: String (unicode or bytes). Returns: The text, with ``backticks’’ -style double quotes

translated into HTML curly quote entities.

Example input: ``Isn’t this fun?’’ Example output: “Isn’t this fun?“;

docutils.utils.smartquotes.educateDashes(text)[source]

Parameter: String (unicode or bytes). Returns: The text, with each instance of “–” translated to

an em-dash character.
docutils.utils.smartquotes.educateDashesOldSchool(text)[source]

Parameter: String (unicode or bytes). Returns: The text, with each instance of “–” translated to

an en-dash character, and each “—” translated to an em-dash character.
docutils.utils.smartquotes.educateDashesOldSchoolInverted(text)[source]

Parameter: String (unicode or bytes). Returns: The text, with each instance of “–” translated to

an em-dash character, and each “—” translated to an en-dash character. Two reasons why: First, unlike the en- and em-dash syntax supported by EducateDashesOldSchool(), 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.)
docutils.utils.smartquotes.educateEllipses(text)[source]

Parameter: String (unicode or bytes). Returns: The text, with each instance of “…” translated to

an ellipsis character.

Example input: Huh…? Example output: Huh&#8230;?

docutils.utils.smartquotes.educateQuotes(text, language='en')[source]
Parameter: - text string (unicode or bytes).
  • language (BCP 47 language tag.)

Returns: The text, with “educated” curly quote characters.

Example input: “Isn’t this fun?” Example output: “Isn’t this fun?“;

docutils.utils.smartquotes.educateSingleBackticks(text, language='en')[source]

Parameter: String (unicode or bytes). Returns: The text, with `backticks’ -style single quotes

translated into HTML curly quote entities.

Example input: `Isn’t this fun?’ Example output: ‘Isn’t this fun?’

docutils.utils.smartquotes.educate_tokens(text_tokens, attr='1', language='en')[source]

Return iterator that “educates” the items of text_tokens.

docutils.utils.smartquotes.processEscapes(text, restore=False)[source]

Parameter: String (unicode or bytes). Returns: The text, with after processing the following backslash

escape sequences. This is useful if you want to force a “dumb” quote or other character to appear.

Escape Value —— —– \ \ ” " ‘ ' . . - - ` `

class docutils.utils.smartquotes.smartchars(language='en')[source]

Bases: object

Smart quotes and dashes

apostrophe = '’'
ellipsis = '…'
emdash = '—'
endash = '–'
quotes = {'af': '“”‘’', 'af-x-altquot': '„”‚’', 'bg': '„“‚‘', 'ca': '«»“”', 'ca-x-altquot': '“”‘’', 'cs': '„“‚‘', 'cs-x-altquot': '»«›‹', 'da': '»«›‹', 'da-x-altquot': '„“‚‘', 'de': '„“‚‘', 'de-x-altquot': '»«›‹', 'de-ch': '«»‹›', 'el': '«»“”', 'en': '“”‘’', 'en-uk-x-altquot': '‘’“”', 'eo': '“”‘’', 'es': '«»“”', 'es-x-altquot': '“”‘’', 'et': '„“‚‘', 'et-x-altquot': '«»‹›', 'eu': '«»‹›', 'fi': '””’’', 'fi-x-altquot': '»»››', 'fr': ('«\xa0', '\xa0»', '“', '”'), 'fr-x-altquot': ('«\u202f', '\u202f»', '“', '”'), 'fr-ch': '«»‹›', 'fr-ch-x-altquot': ('«\u202f', '\u202f»', '‹\u202f', '\u202f›'), 'gl': '«»“”', 'he': '”“»«', 'he-x-altquot': '„”‚’', 'hr': '„”‘’', 'hr-x-altquot': '»«›‹', 'hsb': '„“‚‘', 'hsb-x-altquot': '»«›‹', 'hu': '„”«»', 'is': '„“‚‘', 'it': '«»“”', 'it-ch': '«»‹›', 'it-x-altquot': '“”‘’', 'ja': '「」『』', 'lt': '„“‚‘', 'lv': '„“‚‘', 'mk': '„“‚‘', 'nl': '“”‘’', 'nl-x-altquot': '„”‚’', 'nb': '«»’’', 'nn': '«»’’', 'nn-x-altquot': '«»‘’', 'no': '«»’’', 'no-x-altquot': '«»‘’', 'pl': '„”«»', 'pl-x-altquot': '«»‚’', 'pt': '«»“”', 'pt-br': '“”‘’', 'ro': '„”«»', 'ru': '«»„“', 'sh': '„”‚’', 'sh-x-altquot': '»«›‹', 'sk': '„“‚‘', 'sk-x-altquot': '»«›‹', 'sl': '„“‚‘', 'sl-x-altquot': '»«›‹', 'sq': '«»‹›', 'sq-x-altquot': '“„‘‚', 'sr': '„”’’', 'sr-x-altquot': '»«›‹', 'sv': '””’’', 'sv-x-altquot': '»»››', 'tr': '“”‘’', 'tr-x-altquot': '«»‹›', 'uk': '«»„“', 'uk-x-altquot': '„“‚‘', 'zh-cn': '“”‘’', 'zh-tw': '「」『』'}
docutils.utils.smartquotes.smartyPants(text, attr='1', language='en')[source]

Main function for “traditional” use.

docutils.utils.smartquotes.stupefyEntities(text, language='en')[source]

Parameter: String (unicode or bytes). Returns: The text, with each SmartyPants character translated to

its ASCII counterpart.

Example input: “Hello — world.” Example output: “Hello – world.”

docutils.utils.smartquotes.tokenize(text)[source]

Parameter: String containing HTML markup. Returns: An iterator that yields 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 yielded element is a two-element tuple; the first is either ‘tag’ or ‘text’; the second is the actual value.
Based on the _tokenize() subroutine from Brad Choate’s MTRegex plugin.
<http://www.bradchoate.com/past/mtregex.php>