Typogrify for Textpattern
A couple of weeks ago, Jeff Croft got Web typography geeks like me very excited with his announcement of Typogrify. Made for the Python-based Django framework, this set of text filters provides widow control similar to Shaun Inman’s Widon’t and adds a handful of useful styling hooks for improving the presentation of online text. A few days later, Typogrify was ported to PHP with a WordPress plugin by Hamish Macpherson.
When I released a version of Widon’t for Textpattern last week, it didn’t take long to find out that what everybody really wanted was Typogrify. Well, I aim to please… so here you go.
Download aam_typogrify-0.1.zip (10.9KB, 1007 downloads)
About “Typogrify for Textpattern”
The aam_typogrify plugin brings the typographical goodies of Django’s Typogrify filters to Textpattern. More specifically, it provides new template tags that perform the following actions:
- Prevent typographical widows (words that appear all by themselves on the last line of a paragraph or title).
- Wrap ampersands (“&”) in a span with a class of “amp” for easier selective styling.
- Wrap initial quotes—at the beginning of a paragraph or heading—in a span with a class of “dquo” for double quotes or “quo” for single quotes. This allows some additional CSS tricks, like hanging quotation marks.
Basically, this is candy for Web typography geeks. If you care about how text is presented online, Typogrify can help you make your text look better.
If you have any problems with aam_typogrify, please let me know by posting a message in the plugin’s support thread on the Textpattern forum.
Usage
Typogrify for Textpattern provides four container tags, all of which can be used around other Textpattern tags or around blocks of static text.
aam_widont
Wrap this tag around text to prevent typographical widows:
<txp:aam_widont>
<h2><txp:custom_field name="my_field" /></h2>
</txp:aam_widont>
With the inclusion of this feature, aam_typogrify supersedes my previous plugin aam_widont.
aam_amplify
Wrap this tag around text to markup ampersands with a classed span:
<txp:aam_amplify>
<p>The Law Offices of Dewy, Cheatham & Howe</p>
</txp:aam_amplify>
aam_quotify
Wrap this tag around text to markup initial quotation marks with a classed span:
<txp:aam_quotify><txp:title /></txp:aam_quotify>
aam_typogrify
This is the big daddy mega-filter. It runs text through all of the manipulations listed above:
<txp:aam_typogrify>
<txp:title />
<txp:body />
</txp:aam_typogrify>
Nesting Tags
You can nest tags to selectively use Typogrify’s features. For example, if you only want to prevent widows and wrap ampersands in spans:
<txp:aam_amplify><txp:aam_widont>
<h2><txp:title /></h2>
</txp:aam_widont></txp:aam_amplify>
The order in which you nest tags doesn’t matter. Just make sure that you close them in the same order that you open them.
What this Plugin Doesn’t Do
There are a couple of filters from the original Typogrify that weren’t incorporated into this plugin, mostly because they duplicate existing features of Textpattern’s Textile formatting system:
- SmartyPants — This was omitted for its duplication of Textile features and possible compatibility problems.
- span-wrapping of capitalized strings — The original Typogrify also wrapped strings of two or more capital letters in a span with class “caps.” It even did this if there were periods or numbers in between the letters. Textile does something similar, but isn’t as aggressive. If there’s a call for the more comprehensive cap-wrapping of Typogrify, I’ll include it in a future release.
Compatibility Notes and Technical Stuff
Compatibility with aam_widont
Typogrify for Textpattern should not be used together with my previous plugin, aam_widont. They both use the same tag for widow prevention, but they use different PHP functions to accomplish this goal.
The previous plugin used the Inman / Mullenweg implementation from Widon’t 2.1 for WordPress. It worked well for one-liners like headings, but when used with <txp:excerpt /> or <txp:article /> the white space (tabs or spaces) between blocks of XHTML seemed to trip it up.
The aam_typogrify plugin uses the Widon’t function from Hamish Macpherson’s PHP port of Typogrify. This handles white space better, but I don’t know if it has the internationalization support of Windon’t 2.1. If you write in a non-English language and find that the widow prevention feature of this plugin causes trouble, please let me know.
Compatibility with upm_textile
You shouldn’t have any trouble using aam_typogrify together with Mary’s upm_textile, as long as you nest Mary’s plugin inside mine:
<txp:aam_typogrify><txp:upm_textile>
<txp:excerpt />
</txp:upm_textile></txp:aam_typogrify>
Textpattern evaluates plugins from the inside out, and you’ll want to have Textile applied to your text before it gets Typogrified.
A Few Words about Performance
This plugin uses a feature of PHP called regular expressions (regex) to do its thing. Regular expressions are very powerful, but can also cause a drain on performance if you go overboard.
One of the reasons that Textpattern is so fast is that Textile’s formatting is applied when an article is saved, not whenever a page is viewed. Plugins like mine don’t have that luxury, and this plugin’s extensive regex use might cause some websites to perform more slowly.
If you notice that your site seems slower when using this plugin:
- Try using only the features that you need. If you only want to apply special styles to ampersands, then only use the
aam_amplifytag. - Use the plugin only on selected areas of your Textpattern templates, where typographical niceties are most noticeable. Titles are an especially good candidate.
- If all else fails, try a chaching plugin like zem_cache or asy_jpcache.
License
This plugin is provided under the terms of the BSD license. See the “LICENSE.txt” file, included in the download, for details.
Credits and Thanks
Typogrify started out as a set of Django text filters developed by Christian Metts. This Textpattern plugin is based on the PHP port by Hamish Macpherson.
Comments (11 so far)
-
Awesome!
by Sven on Jun 11, 02:54 am
-
Wow thanks again for the plugins.
Actually I using aam_widont now.Thanks for your great work.
PS : Wow Now I can make a comment here, thanks.
by kus on Jun 11, 07:16 pm
-
Thanks bro, its really helpful!!
by suray on Jun 11, 11:13 pm
-
Can’t wait to try it out. Thanks a lot! :)
by evalottchen on Jun 13, 03:07 pm
-
Woohoo very nice plugin for “typonatics”
by khttw on Jul 4, 10:06 am
-
Just found this now. But great job!
I’ve recently updated my plugin, so feel free to check it out if you want.
by Hamish M on Oct 23, 11:07 pm
-
I’ve been playing around with aam_typogrify for a few days now as I really wanted the amplify function and wondering why on earth I nothing was happening. Then I realized that Textpattern includes Widon’t by default as of January of this year (http://textpattern.com/faq/306/widows-and-no_widow). This was throwing of the regexp.
I found a solution, and it’s dead easy. Textpattern uses   to prevent widows. In your plugin I replaced the with   and now aam_amplify and Textpattern’s native widon’t get along like chocolate and peanut butter.






