And so appeared SmartyPants Typographer

Somewhat as an imitation of what I’ve done with PHP Markdown Extra, today I unveil a special extension of PHP SmartyPants which adds customizable quote characters and automatic conversion of spaces into unbreakable spaces where appropriate, avoiding undesirable line breaks. Let me present you SmartyPants Typographer and its little story.

In French it is usual to have a space before some punctuation marks, like the colon “ :” and inside « guillemets ». It is also common to have a space before other punctuation like the exclamation and question marks. The problem is that people don’t know, or understand, what an unbreakable space is. It looks the same as a normal space and unless someone sees his colon fall on the next line he won’t realize something is wrong. That is, until the browser rewrap the text to fit the page.

Typographer, as a project, began some years ago when I wanted to avoid such kind of bad line breaks on my website. A little while after porting John’s Gruber’s SmartyPants to PHP, which became PHP SmartyPants, I started a SmartyPants-like project called Cosmos which was all about space, about making the space character smarter. And thus I built regular expressions that would change spaces with “smart” ones around punctuation, inside numbers when the space is used as a thousand separator (as in many parts of the world) and around common unit symbols (like kg, °C, Kb).

I also added a way to force the space to be added where there was none. The idea was to use the script to enforce spacing typographic rules, especially French typographic rules. But since there is not set of rules in French that would please everyone I was forced to add a configuration for each of the characters.

Cosmos was integrated into my website rapidly, as easily as PHP SmartyPants and PHP Markdown are, and I have been ready since that time to ship it as a separate stript that would have worked in WordPress and as a Smarty text modifier, just like PHP SmartyPants. There was only one problems: I wanted to have a suitable-for-everyone default configuration. Another downside was that it added an extra parsing layer of HTML tokenization over SmartyPants, not a show stopper, but still something I wasn’t happy with.

Cosmos stagnated for a year or two in that almost-ready state. Then, in october 2005 came a discussion on the Markdown mailing list that made me think. This particular discussion was about adding support for new types of quotes in SmartyPants. I came with the conclusion that for SmartyPants to support almost any kind of quote used in western languages, it needed only a small additions.

All this interested me much. It also came to my mind that I could integrate Cosmos in that new international version of SmartyPants. When I suggested the idea to the list, John Gruber answered this:

Could be a good addition. More or less fits as a SmartyPants feature if you think of SmartyPants as a system for controlling all things related to web typography and layout, not just punctuation.

(Emphasis mine)

This became my source of inspiration for the new name: SmartyPants Typographer.

In the middle of May, I finally decided I’d make an object-oriented version of PHP Markdown. The idea was to be able to use both PHP Markdown and PHP Markdown Extra inside one PHP script, something I’ll need in a near future. I did the same too with PHP SmartyPants with the more secretive goal of creating Typographer. Writing it as an extension to an object-oriented SmartyPants makes multiple spacing configurations possible within the same PHP script by creating multiple parser objects.

So I created Typographer based on the object-oriented PHP SmartyPants I made. Then I decided it needed some more real-world testing. Cosmos was running on my website for ages, but only for what I wrote as a way to enforce my custom spacing rules around French punctuation. So I asked Rémi Prévost for some help: I wanted him to install Typographer on his WordPress-based French weblog. He agreed, and I begun to track how it converted spaces in the comment he received. I’d be surprised anyone commenting there noticed anything, but by looking at the source I could check that the unbreakable spaces were placed correctly. The conclusion is that beside two minor bugs, everything seems perfect. Thank you Rémi for the testing.

The bugs are now corrected. Now that I’m satisfied of the result, now that it has been tested and I know it works well, I’m releasing it as PHP SmartyPants Typographer, which you can download from the PHP SmartyPants main page. And the PHP Markdown Dingus has been updated so you can test-drive Typographer.

Typographer, in its default configuration, doesn’t add or remove any space to your text; it will only make spaces unbreakable where appropriate, so it should be suitable for any language, any spacing-style, and should be pretty unnoticeable, except for the absence of some ugly line breaks when people add space around their punctuation.

  • © 2003–2023 Michel Fortin.