SmartType
What is SmartType?
SmartType is a ColdFusion Component which takes a string and converts all quotes, dashes, ellipses and multiplication signs into their typographic equivalents — e.g. smart quotes, em/en dashes.
All HTML tags are exempt from the conversion, as are HTML comment blocks. Also, all text inside some HTML elements (pre, code, kbd, script, math) is ignored.
SmartType will also pre-convert any MS Word-type entities before it begins. There is also the option to “stupefy” — that is, to remove all the typographic entities from the string and replace them with their “dumb” counterparts.
The component is influenced by the “SmartyPants” plug-in for Movable Type and Blosxom, written by John Gruber.
How to use SmartType
The only publicly accessible function in the component is SmartType. It takes two arguments:
Text (required) — The text string on which to perform the conversion operations.
Convert (optional) — String of characters specifying which conversions to perform.
If not specified, the default Convert operations are 'deqxw'. The available options are:
d — Educate dashes
A hyphen with a space around it will become an en-dash. Two or three hyphens will become an em-dash.
e — Educate ellipses
A series of three or four dots, with or without spaces in between, will become an ellipsis.
q — Educate quotes
All quote marks and apostrophes will be replaced by their “smart quote” equivalents.
x — Educate multiply sign
An ‘x’ with digits either side of it, optionally separated by white space, will become a typographic multiplication sign.
w — Convert MS Word entities
All MS Word-type smart quotes and entities will be converted to plain ASCII equivalents before the main processing is performed.
stupefy — Stupefy entities
All smart quotes and other HTML entities will be dumbed-down to their ASCII equivalents.
If you wish a character not to be smartened up, it can be escaped by preceding it with a backslash (\), e.g.:
"I am 6\'2\"," he said.
Would become:
“I am 6'2",” he said.
To call the component from a ColdFusion page, use the <cfinvoke> tag, e.g.:
<cfinvoke component="SmartType"
method="SmartType"
text="#myString#"
convert="qdexw"
returnvariable="myOutput" />
This would send the text contained in variable myString to the component, apply all operations to it, and return it to a variable named myOutput.
Download
Download SmartType [5 KB zip]
Requirements
SmartType has been tested in ColdFusion MX 7, but should work with other versions too. The only constraint is that SmartType makes extensive use of Java string operations, so any version of ColdFusion that supports these and CFCs should be fine.