- Posted in Code, Nerdiness, Projects, Wordpress
- 4 Comments
WP-Typogrify Hacked to Work With WP-Captions
[Update 7/7/09: WP-Typogrify has now merged with WP-Hyphenate, and is compatible with captions out of the box.]
Hamish Macpherson’s WP-Typogrify is one of my favorite WordPress plugins. I started using it especially for the SmartyPants functionality, which fixes “dumb quotes” and poor man’s apostrophes, among other things.[1]
However, I was disappointed to find that this functionality breaks WordPress captions (introduced in WP 2.6), which I’d rather not live without. Development on WP-Typogrify seems to have slowed[2] — there hasn’t been a new version in a while, so I’ve taken the liberty of hacking version 1.6 to fix this incompatibility, at least so I can use SmartyPants until an official fix comes out. The adjustments I made are simple, and I have no idea whether they’re maximally robust. But feel free to
and use at your own risk.
The Changes
It might be useful to some to know specifically how to fix the incompatibility. WP-Typogrify already ignores text inside a <code> or <pre> block. All that’s necessary is to make it recognize wordpress tags like [caption] as things it should ignore too. All the changes are in the file smartypants.php:
-
At the top, where it says “
Globals:,” replace/* $sp_tags_to_skip = '<(/?)(?:pre|code|kbd|script|math)[\s>]'; */ $sp_tags_to_skip = '(?:<(/?)(?:pre|code|kbd|script|math)[\s>]|'. '\[(/?)(?:caption|code|php)[\s\]])'; -
In the “
function SmartyPants” block, in the loop “foreach ($tokens as $cur_token),” replace/* $in_pre = isset($matches[2]) && $matches[2] == '/' ? 0 : 1; */ $in_pre = (isset($matches[1]) && $matches[1] == '/') || (isset($matches[2]) && $matches[2] == '/') ? 0 : 1; -
Finally, in the “
function _TokenizeHTML($str)” block, replace/* $match = '(?s:<!(?:--.*?--\s*)+>)|'. # comment '(?s:<\?.*?\?>)|'. # processing instruction # regular tags '(?:<[/!$]?[-a-zA-Z0-9:]+\b(?>[^"\'>]+|"[^"]*"|\'[^\']*\')*>)'; */ $match = '(?s:<!(?:--.*?--\s*)+>)|'. # comment '(?s:<\?.*?\?>)|'. # processing instruction '(?:<[/!$]?[-a-zA-Z0-9:]+\b(?>[^"\'>]+|"[^"]*"|\'[^\']*\')*>)|'. '(?:\[[/!$]?[-a-zA-Z0-9:]+\b(?>[^"\'>]+|"[^"]*"|\'[^\']*\')*\])';
That’s all — nothing fancy. Happy typography!
- Of course, it won’t save you from these. [↩]
- Though it’s apparently being taken over by the developers of WP-Hyphenate. [↩]

4 Responses to “WP-Typogrify Hacked to Work With WP-Captions” Comment Feed, Trackback.
Leave a Reply