From 4c938ae25016ef7ebfb8681bdccb61d9e5abcccb Mon Sep 17 00:00:00 2001
From: Rick Ellis and tag right after the block element
+ * ends. Later on we'll do some further clean up.
+ *
+ */
+ $str = preg_replace("#(<)(".$this->block_elements.")(.*?>)#", " ", $str);
+
+ /*
+ * Convert "ignore" tags to temporary marker
+ *
+ * The parser splits out the string at every tag
+ * it encounters. Certain inline tags, like image
+ * tags, links, span tags, etc. will be adversely
+ * affected if they are split out so we'll convert
+ * the opening < temporarily to: {@TAG}
+ *
+ */
+ $str = preg_replace("#<(/*)(".$this->ignore_elements.")#i", "{@TAG}\\1\\2", $str);
+
+ /*
+ * Split the string at every tag
+ *
+ * This creates an array with this prototype:
+ *
+ * [array]
+ * {
+ * [0] = and ({@TAG}.*?)(".$this->block_elements.")(.*?>)#", "\\1\\2\\3", $str);
+ $str = preg_replace("#({@TAG}/.*?)(".$this->block_elements.")(.*?>) \n* #', "\\1", $str);
+
+ // Final clean up
+ $str = str_replace(
+ array(
+ '
tags within them.
+ var $skip_elements = 'pre|ol|ul';
+
+ // Tags we want the parser to completely ignore when splitting the string.
+ var $ignore_elements = 'a|b|i|em|strong|span|img|li';
+
+
+ /**
+ * Main Processing Function
+ *
+ */
+ function convert($str)
+ {
+ if ($str == '')
+ {
+ return '';
+ }
+
+ $str = ' '.$str.' ';
+
+ // Standardize Newlines to make matching easier
+ if (strpos($str, "\r") !== FALSE)
+ {
+ $str = str_replace(array("\r\n", "\r"), "\n", $str);
+ }
+
+ /*
+ * Reduce line breaks
+ *
+ * If there are more than two consecutive line
+ * breaks we'll compress them down to a maximum
+ * of two since there's no benefit to more.
+ *
+ */
+ $str = preg_replace("/\n\n+/", "\n\n", $str);
+
+ /*
+ * Convert quotes within tags to temporary marker
+ *
+ * We don't want quotes converted within
+ * tags so we'll temporarily convert them to
+ * {@DQ} and {@SQ}
+ *
+ */
+ if (preg_match_all("#\<.+?>#si", $str, $matches))
+ {
+ for ($i = 0; $i < count($matches['0']); $i++)
+ {
+ $str = str_replace($matches['0'][$i],
+ str_replace(array("'",'"'), array('{@SQ}', '{@DQ}'), $matches['0'][$i]),
+ $str);
+ }
+ }
+
+
+ /*
+ * Add closing/opening paragraph tags before/after "block" elements
+ *
+ * Since block elements (like , etc.) do not get
+ * wrapped in paragraph tags we will add a closing
tags and a few other things.
+ *
+ */
+ if (preg_match("#<(/*)(".$this->block_elements.").*?\>#", $chunk, $match))
+ {
+ if (preg_match("#".$this->skip_elements."#", $match['2']))
+ {
+ $process = ($match['1'] == '/') ? TRUE : FALSE;
+ }
+
+ $str .= $chunk;
+ continue;
+ }
+
+ if ($process == FALSE)
+ {
+ $str .= $chunk;
+ continue;
+ }
+
+ // Convert Newlines into
tags
+ $str .= $this->format_newlines($chunk);
+ }
+
+ // FINAL CLEAN UP
+ // IMPORTANT: DO NOT ALTER THE ORDER OF THE ITEMS BELOW!
+
+ /*
+ * Clean up paragraph tags before/after "block" elements
+ *
+ * Earlier we added
', + '
', + '
', + '{@TAG}', + '{@DQ}', + '{@SQ}', + '' + ), + array( + '', + '', + '
', + '
', + '<', + '"', + "'", + '' + ), + $str + ); + + return $str; + } + + // -------------------------------------------------------------------- + + /** + * Format Characters + * + * This function mainly converts double and single quotes + * to entities, but since these are directional, it does + * it based on some rules. It also converts em-dashes + * and a couple other things. + */ + function format_characters($str) + { + $table = array( + ' "' => " “", + '" ' => "” ", + " '" => " ‘", + "' " => "’ ", + + '>"' => ">“", + '"<' => "”<", + ">'" => ">‘", + "'<" => "’<", + + "\"." => "”.", + "\"," => "”,", + "\";" => "”;", + "\":" => "”:", + "\"!" => "”!", + "\"?" => "”?", + + ". " => ". ", + "? " => "? ", + "! " => "! ", + ": " => ": ", + ); + + // These deal with quotes within quotes, like: "'hi here'" + $start = 0; + $space = array("\n", "\t", " "); + + while(TRUE) + { + $current = strpos(substr($str, $start), "\"'"); + + if ($current === FALSE) break; + + $one_before = substr($str, $start+$current-1, 1); + $one_after = substr($str, $start+$current+2, 1); + + if ( ! in_array($one_after, $space, TRUE) && $one_after != "<") + { + $str = str_replace( $one_before."\"'".$one_after, + $one_before."“‘".$one_after, + $str); + } + elseif ( ! in_array($one_before, $space, TRUE) && (in_array($one_after, $space, TRUE) OR $one_after == '<')) + { + $str = str_replace( $one_before."\"'".$one_after, + $one_before."”’".$one_after, + $str); + } + + $start = $start+$current+2; + } + + $start = 0; + + while(TRUE) + { + $current = strpos(substr($str, $start), "'\""); + + if ($current === FALSE) break; + + $one_before = substr($str, $start+$current-1, 1); + $one_after = substr($str, $start+$current+2, 1); + + if ( in_array($one_before, $space, TRUE) && ! in_array($one_after, $space, TRUE) && $one_after != "<") + { + $str = str_replace( $one_before."'\"".$one_after, + $one_before."‘“".$one_after, + $str); + } + elseif ( ! in_array($one_before, $space, TRUE) && $one_before != ">") + { + $str = str_replace( $one_before."'\"".$one_after, + $one_before."’”".$one_after, + $str); + } + + $start = $start+$current+2; + } + + // Are there quotes within a word, as in: ("something") + if (preg_match_all("/(.)\"(\S+?)\"(.)/", $str, $matches)) + { + for ($i=0, $s=sizeof($matches['0']); $i < $s; ++$i) + { + if ( ! in_array($matches['1'][$i], $space, TRUE) && ! in_array($matches['3'][$i], $space, TRUE)) + { + $str = str_replace( $matches['0'][$i], + $matches['1'][$i]."“".$matches['2'][$i]."”".$matches['3'][$i], + $str); + } + } + } + + if (preg_match_all("/(.)\'(\S+?)\'(.)/", $str, $matches)) + { + for ($i=0, $s=sizeof($matches['0']); $i < $s; ++$i) + { + if ( ! in_array($matches['1'][$i], $space, TRUE) && ! in_array($matches['3'][$i], $space, TRUE)) + { + $str = str_replace( $matches['0'][$i], + $matches['1'][$i]."‘".$matches['2'][$i]."’".$matches['3'][$i], + $str); + } + } + } + + // How about one apostrophe, as in Rick's + $start = 0; + + while(TRUE) + { + $current = strpos(substr($str, $start), "'"); + + if ($current === FALSE) break; + + $one_before = substr($str, $start+$current-1, 1); + $one_after = substr($str, $start+$current+1, 1); + + if ( ! in_array($one_before, $space, TRUE) && ! in_array($one_after, $space, TRUE)) + { + $str = str_replace( $one_before."'".$one_after, + $one_before."’".$one_after, + $str); + } + + $start = $start+$current+2; + } + + // Em-dashes + $start = 0; + while(TRUE) + { + $current = strpos(substr($str, $start), "--"); + + if ($current === FALSE) break; + + $one_before = substr($str, $start+$current-1, 1); + $one_after = substr($str, $start+$current+2, 1); + $two_before = substr($str, $start+$current-2, 1); + $two_after = substr($str, $start+$current+3, 1); + + if (( ! in_array($one_before, $space, TRUE) && ! in_array($one_after, $space, TRUE)) + OR + ( ! in_array($two_before, $space, TRUE) && ! in_array($two_after, $space, TRUE) && $one_before == ' ' && $one_after == ' ') + ) + { + $str = str_replace( $two_before.$one_before."--".$one_after.$two_after, + $two_before.trim($one_before)."—".trim($one_after).$two_after, + $str); + } + + $start = $start+$current+2; + } + + // Ellipsis + $str = preg_replace("#(\w)\.\.\.(\s| tags or
+ *
+ */
+ function format_newlines($str)
+ {
+ if ($str == '')
+ {
+ return $str;
+ }
+
+ if (strpos($str, "\n") === FALSE)
+ {
+ return '
'.$str.'
'; + } + + $str = str_replace("\n\n", "\n\n", $str);
+ $str = preg_replace("/([^\n])(\n)([^\n])/", "\\1
\\2\\3", $str);
+
+ return '
'.$str.'
'; + } +} +// END Typography Class + +/* End of file Typography.php */ +/* Location: ./system/libraries/Typography.php */ \ No newline at end of file -- cgit v1.2.3-24-g4f1b