PHP SmartyPants Typographer
Typographer is an extension to SmartyPants adding support for different quotation marks as well as for converting normal spaces to unbreakable spaces and thus preventing inappropriate line breaks. Typographer is contained in its own parser class
SmartyPantsTypographer in the regular Lib version, and is a separate download in the classic version.
SmartyPants will change straight quotes with curled quotes in an intelligent way. SmartyPants Typographer adds options to support other quotation marks as used in many languages.
You can now transform "straight" double-quotes in whatever mark you want by changing the appropriate configuration variables of the parser object:
// Opening and closing smart double-quotes. $sp->smart_doublequote_open = "“"; $sp->smart_doublequote_close = "”";
which by default are set to “english-style” quotes. English ‘single quotes’ are not configurable in this way because it would cause problem with apostrophes.
'' are not affected by this setting.
SmartyPants Typographer also support
,, quotation marks, which can be used to create
,, style curly quotes. It will also convert
>> quotes written in
<< to typographic angle quote characters, but only when they are unambiguous with HTML tags.
You can add “
c” to SmartyPants attributes to activate double-comma quotes and “
g” for angle quotes. These two options are enabled by default when using a preset configuration.
The space normalization performed by SmartyPants Typographer should adapt to any language. Defaults are set so that no space is inserted or removed: normal breakable spaces are replaced by unbreakable spaces where it is not appropriate for a line break to occur.
If you write a number followed by a unit symbol (say 12 kg), SmartyPants Typographer will change the space between the number and the unit to make it unbreakable. If someone put a space before a question mark, for instance, the space will be preserved as intended by the author, but it will be made unbreakable since it is inadvisable to break a line just before a question mark. Replacement with unbreakable space characters should pass pretty much unnoticed, except for the fact that there won’t be wrong line line breaks anymore.
SmartyPants Typographer makes spaces unbreakable in the following cases (unbreakable spaces are shown as underbars “_” to make them visible):
- before a colon_:
- before a semicolon_;
- before an exclamation mark_! and a question mark_?
- inside «_french quotes_»
- thousand separator 100_000
- between a value and common unit symbols 12_kg 10_˚C
Space normalization also makes sure that spaces around em- and en- dashes are breakable (normal) spaces:
- autour du tiret cadratin_–_comme ça
- autour du tiret demi-cadratin_–_comme ça
You can also configure SmartyPants Typographer to force the insertion or the removal of spaces in all the previously mentioned cases to enforce specific typographic rules.
Particular attention has been given to some special cases to make sure they work well:
- multiple exclamation or question marks_!?!
- ¿¡_inverted_!? exclamation and question marks.
- »german-« and »sweden-/finnish-» style quotation marks.
You can control what transformations will apply (replace, insert, remove space) for each of the space categories by adding the right characters to SmartyPants attributes. Each of these categories is represented by one character which can optionally be followed by a plus (
+) or a minus (
-) sign to respectively force the insertion or the removal of the space. Characters categories are:
||question and exclamation marks|
||value and unit|
(Note: thousand separator space cannot be added automatically.)
For example, to force the insertion of a space inside french quotes and before the colon, make unbreakable already-existing spaces before a unit and used as a thousand separator, and to remove any space before a semicolon as well as the question and exclamation marks, use the following configuration string:
You can also choose, for each one of these categories, which replacement character will be used by changing the appropriate configuration variables near the start of the SmartyPants file.
See the configuration page for more details.