Posted in Code, Nerdiness, Projects, Wordpress
4 Comments

WP-Typogrify Hacked to Work With WP-Captions — Apr 11, 2009

cxvk5jbsbbdepyszjb

[Update 7/7/09: WP-Typogrify has now merged with WP-Hyphenate, and is com­pat­i­ble with cap­tions out of the box.]

Hamish Macpher­son’s WP-Typogrify is one of my favorite Word­Press plu­g­ins. I started using it espe­cially for the Smar­ty­Pants func­tion­al­ity, which fixes “dumb quotes” and poor man’s apos­tro­phes, among other things.[1]

How­ever, I was dis­ap­pointed to find that this func­tion­al­ity breaks Word­Press cap­tions (intro­duced in WP 2.6), which I’d rather not live with­out. Devel­op­ment on WP-Typogrify seems to have slowed[2] — there hasn’t been a new ver­sion in a while, so I’ve taken the lib­erty of hack­ing ver­sion 1.6 to fix this incom­pat­i­bil­ity, at least so I can use Smar­ty­Pants until an offi­cial fix comes out. The adjust­ments I made are sim­ple, and I have no idea whether they’re max­i­mally robust. But feel free to

and use at your own risk.

The Changes

It might be use­ful to some to know specif­i­cally how to fix the incom­pat­i­bil­ity. WP-Typogrify already ignores text inside a <code> or <pre> block. All that’s nec­es­sary is to make it rec­og­nize word­press tags like [caption] as things it should ignore too. All the changes are in the file smartypants.php:

  1. 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\]])';
    
  2. 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;
    
  3. 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 — noth­ing fancy. Happy typography!

Share and Enjoy:
  1. Of course, it won’t save you from these. []
  2. Though it’s appar­ently being taken over by the devel­op­ers of WP-Hyphenate. []

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

  1. Greg Price says: May 12, 2009 @ 12:09 am

    Cool. Have you sent your changes to the devel­oper? That’s a good way of mak­ing your fix easy for other peo­ple to get, and also of guar­an­tee­ing that the next ver­sion will have your fixes so you don’t have to repeat them.

    Some­times main­tain­ers are busy and it takes a while, and some­times they’re lame and never deal with a patch, but it’s gen­er­ally worth it.

  2. Jeff Byrnes says: May 13, 2009 @ 5:59 pm

    I’ve actu­ally had the same issue with my site & the Kim­ili Flash Embed code (uses [kml_flashembed] as a tag), and I couldn’t get your hack/fix to work. Added kml_flashembed to the list of tags in this bit:

    $sp_tags_to_skip = '(?:]|'.
    '\[(/?)(?:caption|code|php)[\s\]])';

    Any sug­ges­tions?

  3. davidsd says: May 13, 2009 @ 7:31 pm

    @Jeff, maybe the prob­lem is that the _TokenizeHTML func­tion as it’s writ­ten doesn’t rec­og­nize under­scores in tag names. Try replac­ing [-a-zA-Z0-9:] with [-a-zA-Z0-9:_] in line 10 of block (3) above.

  4. Jeff Byrnes says: May 13, 2009 @ 11:34 pm

    Looks like it works! Thanks!

Leave a Reply