TypographyHelper.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP 5.1.6 or newer
  6. *
  7. * @author ExpressionEngine Dev Team
  8. * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
  9. * @license http://codeigniter.com/user_guide/license.html CodeIgniter License
  10. * @link http://codeigniter.com
  11. * @since Version 1.0
  12. */
  13. App::uses('AppHelper', 'View/Helper');
  14. /**
  15. * Typography Class converted to Cake Helper
  16. *
  17. * In the database you usually got ", ', and - as uniform chars.
  18. * On output you might want to localize them according to your country's/languages' preferences.
  19. *
  20. * For Swiss, for example: "Some quote" might become «Some quote»
  21. * For German: "Some quote" might become „Some quote“
  22. *
  23. * @modified Mark Scherer
  24. * @php 5
  25. * @link http://www.dereuromark.de/2012/08/12/typographic-behavior-and-typography-helper/
  26. */
  27. class TypographyHelper extends AppHelper {
  28. // Block level elements that should not be wrapped inside <p> tags
  29. public $blockElements = 'address|blockquote|div|dl|fieldset|form|h\d|hr|noscript|object|ol|p|pre|script|table|ul';
  30. // Elements that should not have <p> and <br /> tags within them.
  31. public $skipElements = 'p|pre|ol|ul|dl|object|table|h\d';
  32. // Tags we want the parser to completely ignore when splitting the string.
  33. public $inlineElements =
  34. 'a|abbr|acronym|b|bdo|big|br|button|cite|code|del|dfn|em|i|img|ins|input|label|map|kbd|q|samp|select|small|span|strong|sub|sup|textarea|tt|var';
  35. // array of block level elements that require inner content to be within another block level element
  36. public $innerBlockRequired = ['blockquote'];
  37. // the last block element parsed
  38. public $lastBlockElement = '';
  39. // whether or not to protect quotes within { curly braces }
  40. public $protectBracedQuotes = false;
  41. public $matching = [
  42. 'deu' => 'low', // except for Switzerland
  43. 'eng' => 'default',
  44. 'fra' => 'angle',
  45. ];
  46. /**
  47. * Automatically uses the typography specified.
  48. * By default, uses Configure::read('App.language') to determine locale preference.
  49. * It will then try to match the language to the type of characters used.
  50. * You can hardwire this by using Configure::read('Typography.locale'); and directly set it
  51. * to 'low' or 'angle'. It will then disregard the language.
  52. *
  53. * This function converts text, making it typographically correct:
  54. * - Converts double spaces into paragraphs.
  55. * - Converts single line breaks into <br /> tags
  56. * - Converts single and double quotes into correctly facing curly quote entities.
  57. * - Converts three dots into ellipsis.
  58. * - Converts double dashes into em-dashes.
  59. * - Converts two spaces into entities
  60. *
  61. * @param string $str Text
  62. * @param bool $reduceLinebreaks Whether to reduce more then two consecutive newlines to two
  63. * @return string Text
  64. */
  65. public function autoTypography($str, $reduceLinebreaks = false) {
  66. if ($str === '') {
  67. return '';
  68. }
  69. // Standardize Newlines to make matching easier
  70. if (strpos($str, "\r") !== false) {
  71. $str = str_replace(["\r\n", "\r"], "\n", $str);
  72. }
  73. // Reduce line breaks. If there are more than two consecutive linebreaks
  74. // we'll compress them down to a maximum of two since there's no benefit to more.
  75. if ($reduceLinebreaks === true) {
  76. $str = preg_replace("/\n\n+/", "\n\n", $str);
  77. }
  78. // HTML comment tags don't conform to patterns of normal tags, so pull them out separately, only if needed
  79. $htmlComments = [];
  80. if (strpos($str, '<!--') !== false) {
  81. if (preg_match_all("#(<!\-\-.*?\-\->)#s", $str, $matches)) {
  82. for ($i = 0, $total = count($matches[0]); $i < $total; $i++) {
  83. $htmlComments[] = $matches[0][$i];
  84. $str = str_replace($matches[0][$i], '{@HC' . $i . '}', $str);
  85. }
  86. }
  87. }
  88. // match and yank <pre> tags if they exist. It's cheaper to do this separately since most content will
  89. // not contain <pre> tags, and it keeps the PCRE patterns below simpler and faster
  90. if (strpos($str, '<pre') !== false) {
  91. $str = preg_replace_callback("#<pre.*?>.*?</pre>#si", [$this, '_protectCharacters'], $str);
  92. }
  93. // Convert quotes within tags to temporary markers.
  94. $str = preg_replace_callback("#<.+?>#si", [$this, '_protectCharacters'], $str);
  95. // Do the same with braces if necessary
  96. if ($this->protectBracedQuotes === true) {
  97. $str = preg_replace_callback("#\{.+?\}#si", [$this, '_protectCharacters'], $str);
  98. }
  99. // Convert "ignore" tags to temporary marker. The parser splits out the string at every tag
  100. // it encounters. Certain inline tags, like image tags, links, span tags, etc. will be
  101. // adversely affected if they are split out so we'll convert the opening bracket < temporarily to: {@TAG}
  102. $str = preg_replace("#<(/*)(" . $this->inlineElements . ")([ >])#i", "{@TAG}\\1\\2\\3", $str);
  103. // Split the string at every tag. This expression creates an array with this prototype:
  104. //
  105. // [array]
  106. // {
  107. // [0] = <opening tag>
  108. // [1] = Content...
  109. // [2] = <closing tag>
  110. // Etc...
  111. // }
  112. $chunks = preg_split('/(<(?:[^<>]+(?:"[^"]*"|\'[^\']*\')?)+>)/', $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
  113. // Build our finalized string. We cycle through the array, skipping tags, and processing the contained text
  114. $str = '';
  115. $process = true;
  116. $currentChunk = 0;
  117. $totalChunks = count($chunks);
  118. foreach ($chunks as $chunk) {
  119. $currentChunk++;
  120. // Are we dealing with a tag? If so, we'll skip the processing for this cycle.
  121. // Well also set the "process" flag which allows us to skip <pre> tags and a few other things.
  122. if (preg_match("#<(/*)(" . $this->blockElements . ").*?>#", $chunk, $match)) {
  123. if (preg_match("#" . $this->skipElements . "#", $match[2])) {
  124. $process = ($match[1] === '/') ? true : false;
  125. }
  126. if ($match[1] === '') {
  127. $this->lastBlockElement = $match[2];
  128. }
  129. $str .= $chunk;
  130. continue;
  131. }
  132. if ($process == false) {
  133. $str .= $chunk;
  134. continue;
  135. }
  136. // Force a newline to make sure end tags get processed by _formatNewlines()
  137. if ($currentChunk == $totalChunks) {
  138. $chunk .= "\n";
  139. }
  140. // Convert Newlines into <p> and <br /> tags
  141. $str .= $this->_formatNewlines($chunk);
  142. }
  143. // No opening block level tag? Add it if needed.
  144. if (!preg_match("/^\s*<(?:" . $this->blockElements . ")/i", $str)) {
  145. $str = preg_replace("/^(.*?)<(" . $this->blockElements . ")/i", '<p>$1</p><$2', $str);
  146. }
  147. // Convert quotes, elipsis, em-dashes, non-breaking spaces, and ampersands
  148. $str = $this->formatCharacters($str);
  149. // restore HTML comments
  150. for ($i = 0, $total = count($htmlComments); $i < $total; $i++) {
  151. // remove surrounding paragraph tags, but only if there's an opening paragraph tag
  152. // otherwise HTML comments at the ends of paragraphs will have the closing tag removed
  153. // if '<p>{@HC1}' then replace <p>{@HC1}</p> with the comment, else replace only {@HC1} with the comment
  154. $str = preg_replace('#(?(?=<p>\{@HC' . $i . '\})<p>\{@HC' . $i . '\}(\s*</p>)|\{@HC' . $i . '\})#s', $htmlComments[$i], $str);
  155. }
  156. // Final clean up
  157. $table = [
  158. // If the user submitted their own paragraph tags within the text
  159. // we will retain them instead of using our tags.
  160. '/(<p[^>*?]>)<p>/' => '$1', // <?php BBEdit syntax coloring bug fix
  161. // Reduce multiple instances of opening/closing paragraph tags to a single one
  162. '#(</p>)+#' => '</p>',
  163. '/(<p>\W*<p>)+/' => '<p>',
  164. // Clean up stray paragraph tags that appear before block level elements
  165. '#<p></p><(' . $this->blockElements . ')#' => '<$1',
  166. // Clean up stray non-breaking spaces preceeding block elements
  167. '#(&nbsp;\s*)+<(' . $this->blockElements . ')#' => ' <$2',
  168. // Replace the temporary markers we added earlier
  169. '/\{@TAG\}/' => '<',
  170. '/\{@DQ\}/' => '"',
  171. '/\{@SQ\}/' => "'",
  172. '/\{@DD\}/' => '--',
  173. '/\{@NBS\}/' => ' ',
  174. // An unintended consequence of the _formatNewlines function is that
  175. // some of the newlines get truncated, resulting in <p> tags
  176. // starting immediately after <block> tags on the same line.
  177. // This forces a newline after such occurrences, which looks much nicer.
  178. "/><p>\n/" => ">\n<p>",
  179. // Similarly, there might be cases where a closing </block> will follow
  180. // a closing </p> tag, so we'll correct it by adding a newline in between
  181. "#</p></#" => "</p>\n</"
  182. ];
  183. // Do we need to reduce empty lines?
  184. if ($reduceLinebreaks === true) {
  185. $table['#<p>\n*</p>#'] = '';
  186. } else {
  187. // If we have empty paragraph tags we add a non-breaking space
  188. // otherwise most browsers won't treat them as true paragraphs
  189. $table['#<p></p>#'] = '<p>&nbsp;</p>';
  190. }
  191. return preg_replace(array_keys($table), $table, $str);
  192. }
  193. /**
  194. * Format Characters
  195. *
  196. * This function mainly converts double and single quotes
  197. * to curly entities, but it also converts em-dashes,
  198. * double spaces, and ampersands
  199. *
  200. * @param string
  201. * @return string
  202. */
  203. public function formatCharacters($str, $locale = null) {
  204. //static $table;
  205. if ($locale === null) {
  206. $locale = Configure::read('Typography.locale');
  207. }
  208. if (!$locale) {
  209. $locale = 'default';
  210. $language = Configure::read('App.language');
  211. if ($language && isset($this->matching[$language])) {
  212. $locale = $this->matching[$language];
  213. }
  214. }
  215. $locales = [
  216. 'default' => [
  217. 'leftSingle' => '&#8216;', // &lsquo; / ‘
  218. 'rightSingle' => '&#8217;', // &rsquo; / ’
  219. 'leftDouble' => '&#8220;', // &ldquo; / “
  220. 'rightDouble' => '&#8221;', // &rdquo; / ”
  221. ],
  222. 'low' => [
  223. 'leftSingle' => '&sbquo;', // &sbquo; / ‚
  224. 'rightSingle' => '&#8219;', // &rsquo; / ’
  225. 'leftDouble' => '&bdquo;', // &bdquo; / „
  226. 'rightDouble' => '&#8223;', // &rdquo; / ”
  227. ],
  228. 'angle' => [
  229. 'leftSingle' => '&lsaquo;', // ‹
  230. 'rightSingle' => '&rsaquo;', // ›
  231. 'leftDouble' => '&#171;', // &laquo; / «
  232. 'rightDouble' => '&#187;', // &raquo; / »
  233. ],
  234. ];
  235. if (!isset($table)) {
  236. $table = [
  237. // nested smart quotes, opening and closing
  238. // note that rules for grammar (English) allow only for two levels deep
  239. // and that single quotes are _supposed_ to always be on the outside
  240. // but we'll accommodate both
  241. // Note that in all cases, whitespace is the primary determining factor
  242. // on which direction to curl, with non-word characters like punctuation
  243. // being a secondary factor only after whitespace is addressed.
  244. '/\'"(\s|$)/' => '&#8217;&#8221;$1',
  245. '/(^|\s|<p>)\'"/' => '$1&#8216;&#8220;',
  246. '/\'"(\W)/' => '&#8217;&#8221;$1',
  247. '/(\W)\'"/' => '$1&#8216;&#8220;',
  248. '/"\'(\s|$)/' => '&#8221;&#8217;$1',
  249. '/(^|\s|<p>)"\'/' => '$1&#8220;&#8216;',
  250. '/"\'(\W)/' => '&#8221;&#8217;$1',
  251. '/(\W)"\'/' => '$1&#8220;&#8216;',
  252. // single quote smart quotes
  253. '/\'(\s|$)/' => '&#8217;$1',
  254. '/(^|\s|<p>)\'/' => '$1&#8216;',
  255. '/\'(\W)/' => '&#8217;$1',
  256. '/(\W)\'/' => '$1&#8216;',
  257. // double quote smart quotes
  258. '/"(\s|$)/' => '&#8221;$1',
  259. '/(^|\s|<p>)"/' => '$1&#8220;',
  260. '/"(\W)/' => '&#8221;$1',
  261. '/(\W)"/' => '$1&#8220;',
  262. // apostrophes
  263. "/(\w)'(\w)/" => '$1&rsquo;$2', // we dont use #8217; to avoid collision on replace
  264. // Em dash and ellipses dots
  265. '/\s?\-\-\s?/' => '&#8212;',
  266. '/(\w)\.{3}/' => '$1&#8230;',
  267. // double space after sentences
  268. '/(\W) /' => '$1&nbsp; ',
  269. // ampersands, if not a character entity
  270. '/&(?!#?[a-zA-Z0-9]{2,};)/' => '&amp;'
  271. ];
  272. if ($locale && !empty($locales[$locale])) {
  273. foreach ($table as $key => $val) {
  274. $table[$key] = str_replace($locales['default'], $locales[$locale], $val);
  275. }
  276. }
  277. }
  278. return preg_replace(array_keys($table), $table, $str);
  279. }
  280. /**
  281. * Format Newlines
  282. *
  283. * Converts newline characters into either <p> tags or <br />
  284. *
  285. * @param string
  286. * @return string
  287. */
  288. protected function _formatNewlines($str) {
  289. if ($str === '') {
  290. return $str;
  291. }
  292. if (strpos($str, "\n") === false && !in_array($this->lastBlockElement, $this->innerBlockRequired)) {
  293. return $str;
  294. }
  295. // Convert two consecutive newlines to paragraphs
  296. $str = str_replace("\n\n", "</p>\n\n<p>", $str);
  297. // Convert single spaces to <br /> tags
  298. $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1<br />\\2\\3", $str);
  299. // Wrap the whole enchilada in enclosing paragraphs
  300. if ($str !== "\n") {
  301. // We trim off the right-side new line so that the closing </p> tag
  302. // will be positioned immediately following the string, matching
  303. // the behavior of the opening <p> tag
  304. $str = '<p>' . rtrim($str) . '</p>';
  305. }
  306. // Remove empty paragraphs if they are on the first line, as this
  307. // is a potential unintended consequence of the previous code
  308. $str = preg_replace("/<p><\/p>(.*)/", "\\1", $str, 1);
  309. return $str;
  310. }
  311. /**
  312. * Protect Characters
  313. *
  314. * Protects special characters from being formatted later
  315. * We don't want quotes converted within tags so we'll temporarily convert them to {@DQ} and {@SQ}
  316. * and we don't want double dashes converted to emdash entities, so they are marked with {@DD}
  317. * likewise double spaces are converted to {@NBS} to prevent entity conversion
  318. *
  319. * @param array
  320. * @return string
  321. */
  322. protected function _protectCharacters($match) {
  323. return str_replace(["'", '"', '--', ' '], ['{@SQ}', '{@DQ}', '{@DD}', '{@NBS}'], $match[0]);
  324. }
  325. /**
  326. * Convert newlines to HTML line breaks except within PRE tags
  327. *
  328. * @param string
  329. * @return string
  330. */
  331. public function nl2brExceptPre($str) {
  332. $ex = explode("pre>", $str);
  333. $ct = count($ex);
  334. $newstr = '';
  335. for ($i = 0; $i < $ct; $i++) {
  336. if (($i % 2) == 0) {
  337. $newstr .= nl2br($ex[$i]);
  338. } else {
  339. $newstr .= $ex[$i];
  340. }
  341. if ($ct - 1 != $i) {
  342. $newstr .= "pre>";
  343. }
  344. }
  345. return $newstr;
  346. }
  347. }