| 1 |
<?php
|
| 2 |
// $Id: typogrify.module,v 1.9 2009/05/30 12:55:19 mikl Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Typogrify: Brings typographical refinemnts to drupal
|
| 7 |
*
|
| 8 |
* Hook together all the typogrify components.
|
| 9 |
*/
|
| 10 |
|
| 11 |
/**
|
| 12 |
* Implementation of hook_filter_info().
|
| 13 |
*/
|
| 14 |
function typogrify_filter_info() {
|
| 15 |
return array('typogrify' => array(
|
| 16 |
'title' => t('Typogrify'),
|
| 17 |
'description' => t('Adds typographic refinements.'),
|
| 18 |
'process callback' => '_typogrify_process',
|
| 19 |
'settings callback' => '_typogrify_settings',
|
| 20 |
'default settings' => array(
|
| 21 |
'smartypants_enabled' => 1,
|
| 22 |
'smartypants_hyphens' => 3,
|
| 23 |
'wrap_ampersand' => 1,
|
| 24 |
'widont_enabled' => 1,
|
| 25 |
'wrap_caps' => 1,
|
| 26 |
'wrap_initial_quotes' => 1,
|
| 27 |
'long_s' => 0,
|
| 28 |
'ligatures' => array(),
|
| 29 |
'arrows' => array(),
|
| 30 |
),
|
| 31 |
'tips callback' => '_typogrify_filter_tips',
|
| 32 |
));
|
| 33 |
}
|
| 34 |
|
| 35 |
/**
|
| 36 |
* Filter tips callback for Typogrify filter.
|
| 37 |
*/
|
| 38 |
function _typogrify_filter_tips($filter, $format, $long) {
|
| 39 |
if ($long) {
|
| 40 |
$output = t('Typogrify.module brings the typographic refinements of Typogrify to Drupal.<ul>
|
| 41 |
<li>Wraps ampersands (the $ldquo;&” character) with <span class=\"amp\">&</span>.</li>
|
| 42 |
<li>Prevents single words from wrapping onto their own line using Shaun Inman\'s Widont technique.</li>
|
| 43 |
<li>Converts straight quotation marks to typographer\'s quotation marks, using SmartyPants.</li>
|
| 44 |
<li>Converts multiple hyphens to en dashes and em dashes (according to your preferences), using SmartyPants.</li>
|
| 45 |
<li>Wraps multiple capital letters with <span class=\"caps\">CAPS</span>.</li>
|
| 46 |
<li>Wraps initial quotation marks with <span class=\"quo\"></span> or <span class=\"dquo\"></span>.</li>
|
| 47 |
<li>Adds a css style sheet that uses the <span> tags to substitute a showy ampersand in headlines, switch caps to small caps, and hang initial quotation marks.</li></ul>');
|
| 48 |
}
|
| 49 |
else {
|
| 50 |
$output = t('Adds typographic refinements.');
|
| 51 |
}
|
| 52 |
|
| 53 |
return $output;
|
| 54 |
}
|
| 55 |
|
| 56 |
/**
|
| 57 |
* Implementation of hook_init().
|
| 58 |
*/
|
| 59 |
function typogrify_init() {
|
| 60 |
drupal_add_css(drupal_get_path('module', 'typogrify') . '/typogrify.css');
|
| 61 |
}
|
| 62 |
|
| 63 |
/**
|
| 64 |
* Processing function to apply the Typogrify filters
|
| 65 |
*
|
| 66 |
* @param string $text
|
| 67 |
* The text to apply the filter on.
|
| 68 |
* @param integer $format
|
| 69 |
* ID if the input format whose settings to use when applying the filters.
|
| 70 |
* @return string
|
| 71 |
* The filtered text.
|
| 72 |
*/
|
| 73 |
function _typogrify_process($text, $filter, $format, $langcode, $cache, $cache_id) {
|
| 74 |
$characters_to_convert = array();
|
| 75 |
// Load Helpers.
|
| 76 |
module_load_include('class.php', 'typogrify');
|
| 77 |
module_load_include('php', 'typogrify', 'unicode-conversion');
|
| 78 |
module_load_include('php', 'typogrify', 'smartypants');
|
| 79 |
|
| 80 |
// Wrap ampersands.
|
| 81 |
if ($filter->settings['wrap_ampersand']) {
|
| 82 |
$text = Typogrify::amp($text);
|
| 83 |
}
|
| 84 |
|
| 85 |
// Remove widows.
|
| 86 |
if ($filter->settings['widont_enabled']) {
|
| 87 |
$text = Typogrify::widont($text);
|
| 88 |
}
|
| 89 |
|
| 90 |
// Smartypants formatting.
|
| 91 |
if ($filter->settings['smartypants_enabled']) {
|
| 92 |
global $_typogrify_smartypants_attr;
|
| 93 |
$_typogrify_smartypants_attr = $filter->settings['smartypants_hyphens'];
|
| 94 |
$text = SmartyPants($text);
|
| 95 |
}
|
| 96 |
|
| 97 |
// Wrap caps.
|
| 98 |
if ($filter->settings['wrap_caps']) {
|
| 99 |
$text = Typogrify::caps($text);
|
| 100 |
}
|
| 101 |
|
| 102 |
// Wrap initial quotes.
|
| 103 |
if ($filter->settings['wrap_initial_quotes']) {
|
| 104 |
$text = Typogrify::initial_quotes($text);
|
| 105 |
}
|
| 106 |
|
| 107 |
// Build a list of ligatures to convert.
|
| 108 |
foreach (unicode_conversion_map('ligature') as $ascii => $unicode) {
|
| 109 |
if ($filter->settings['ligatures'][$ascii]) {
|
| 110 |
$characters_to_convert[] = $ascii;
|
| 111 |
}
|
| 112 |
}
|
| 113 |
|
| 114 |
// Build a list of arrows to convert.
|
| 115 |
foreach (unicode_conversion_map('arrow') as $ascii => $unicode) {
|
| 116 |
if ($filter->settings['arrows'][$ascii]) {
|
| 117 |
$characters_to_convert[] = $ascii;
|
| 118 |
}
|
| 119 |
}
|
| 120 |
|
| 121 |
// Convert ligatures and arrows
|
| 122 |
if (count($characters_to_convert) > 0) {
|
| 123 |
$text = convert_characters($text, $characters_to_convert);
|
| 124 |
}
|
| 125 |
|
| 126 |
return $text;
|
| 127 |
}
|
| 128 |
|
| 129 |
|
| 130 |
/**
|
| 131 |
* Typogrify filter settings form.
|
| 132 |
*
|
| 133 |
* @param integer $format
|
| 134 |
* ID if the input format to generate a settings form for.
|
| 135 |
* @return array
|
| 136 |
* Form API array containing our settings form.
|
| 137 |
*/
|
| 138 |
function _typogrify_settings(&$form_state, $filter, $defaults, $format, $filters) {
|
| 139 |
module_load_include('class.php', 'typogrify');
|
| 140 |
module_load_include('php', 'typogrify', 'unicode-conversion');
|
| 141 |
module_load_include('php', 'typogrify', 'smartypants');
|
| 142 |
|
| 143 |
$form = array();
|
| 144 |
|
| 145 |
$form['help'] = array(
|
| 146 |
'#type' => 'markup',
|
| 147 |
'#value' => '<p>Enable the following typographic refinements:</p>',
|
| 148 |
);
|
| 149 |
|
| 150 |
// Smartypants settings.
|
| 151 |
$form['smartypants_enabled'] = array(
|
| 152 |
'#type' => 'checkbox',
|
| 153 |
'#title' => t('Use typographers quotation marks and dashes (!smartylink)', array('!smartylink' => l('SmartyPants', 'http://daringfireball.net/projects/smartypants/'))),
|
| 154 |
'#default_value' => $defaults->settings['smartypants_enabled'],
|
| 155 |
);
|
| 156 |
|
| 157 |
// Smartypants hyphenation settings.
|
| 158 |
// Uses the same values as the parse attributes in the SmartyPants
|
| 159 |
// function (@see SmartyPants in smartypants.php)
|
| 160 |
$form['smartypants_hyphens'] = array(
|
| 161 |
'#type' => 'select',
|
| 162 |
'#title' => t('Hyphenation settings for SmartyPants'),
|
| 163 |
'#default_value' => $defaults->settings['smartypants_hyphens'],
|
| 164 |
'#options' => array(
|
| 165 |
1 => t('“--” for em-dashes; no en-dash support'),
|
| 166 |
3 => t('“--” for em-dashes; “---” for en-dashes'),
|
| 167 |
2 => t('“---” for em-dashes; “--” for en-dashes'),
|
| 168 |
),
|
| 169 |
);
|
| 170 |
|
| 171 |
// Wrap ampersand settings.
|
| 172 |
$form['wrap_ampersand'] = array(
|
| 173 |
'#type' => 'checkbox',
|
| 174 |
'#title' => t('Wrap ampersands'),
|
| 175 |
'#default_value' => $defaults->settings['wrap_ampersand'],
|
| 176 |
);
|
| 177 |
|
| 178 |
// Remove widows settings.
|
| 179 |
$form['widont_enabled'] = array(
|
| 180 |
'#type' => 'checkbox',
|
| 181 |
'#title' => t('Remove widows'),
|
| 182 |
'#default_value' => $defaults->settings['widont_enabled'],
|
| 183 |
);
|
| 184 |
|
| 185 |
// Wrap caps settings.
|
| 186 |
$form['wrap_caps'] = array(
|
| 187 |
'#type' => 'checkbox',
|
| 188 |
'#title' => t('Wrap caps'),
|
| 189 |
'#default_value' => $defaults->settings['wrap_caps'],
|
| 190 |
);
|
| 191 |
|
| 192 |
// Wrap initial quotes settings.
|
| 193 |
$form['wrap_initial_quotes'] = array(
|
| 194 |
'#type' => 'checkbox',
|
| 195 |
'#title' => t('Wrap quotation marks'),
|
| 196 |
'#default_value' => $defaults->settings['wrap_initial_quotes'],
|
| 197 |
);
|
| 198 |
|
| 199 |
// Ligature conversion settings.
|
| 200 |
$ligature_options = array();
|
| 201 |
foreach (unicode_conversion_map('ligature') as $ascii => $unicode) {
|
| 202 |
$ligature_options[$ascii] = t('Convert !ascii to !unicode', array('!ascii' => $ascii, '!unicode' => $unicode));
|
| 203 |
}
|
| 204 |
|
| 205 |
$form['ligatures'] = array(
|
| 206 |
'#type' => 'checkboxes',
|
| 207 |
'#title' => t('Ligatures'),
|
| 208 |
'#options' => $ligature_options,
|
| 209 |
'#default_value' => $defaults->settings['ligatures'],
|
| 210 |
);
|
| 211 |
|
| 212 |
// Arrow conversion settings.
|
| 213 |
$arrow_options = array();
|
| 214 |
foreach (unicode_conversion_map('arrow') as $ascii => $unicode) {
|
| 215 |
$arrow_options[$ascii] = t('Convert @ascii to !unicode', array('@ascii' => $ascii, '!unicode' => $unicode));
|
| 216 |
}
|
| 217 |
|
| 218 |
$form['arrows'] = array(
|
| 219 |
'#type' => 'checkboxes',
|
| 220 |
'#title' => t('Arrows'),
|
| 221 |
'#options' => $arrow_options,
|
| 222 |
'#default_value' => $defaults->settings['arrows'],
|
| 223 |
);
|
| 224 |
|
| 225 |
// Version Information Settings
|
| 226 |
$version_strings = array();
|
| 227 |
$version_strings[] = t('SmartyPants PHP version: !version', array('!version' => l(SMARTYPANTS_PHP_VERSION, 'http://www.michelf.com/projects/php-smartypants/')));
|
| 228 |
$version_strings[] = t('PHP Typogrify Version: !version', array('!version' => l('1.0', 'http://blog.hamstu.com/')));
|
| 229 |
|
| 230 |
$form['info']['typogrify_status'] = array(
|
| 231 |
'#type' => 'markup',
|
| 232 |
'#value' => theme('item_list', $version_strings, t('Version Information'))
|
| 233 |
);
|
| 234 |
|
| 235 |
return $form;
|
| 236 |
}
|
| 237 |
|