Very primitive filter lets your users use some of TeX/LaTeX commands in their postings.
"; break; } return t($output); } /** * Implementation of filter_filter(). */ function latex_filter($op, $delta = 0, $format = -1, $text = '') { switch ($op) { case 'list': return array(t('TeX/LaTeX')); case 'description': return t("Allows to use some elements of TeX/LaTeX."); case 'settings': $form['conf'] = array( '#type' => 'fieldset', '#title' => t('LaTeX filter'), '#collapsible' => TRUE, '#collapsed' => TRUE, ); $form['conf']['latex_filter_link'] = array( '#type' => 'select', '#title' => t("Recognize TeX/LaTeX commands"), '#default_value' => variable_get("latex_filter_link", 0), '#options' => array(t("Show unknown commands"), t("Strip unknown commands")), '#description' => t('Enable usage of TeX/LaTeX commands in user-distributed content. \link{(.*)}{(.*)} <a href="$1">$2</a> - separate pattern part from replace parts with one space character. For inserting space in pattern part use numeric code "\040". '),
);
return $form;
case 'process':
return _latex_filter_process($text);
default:
return $text;
}
}
function _latex_filter_process($text) {
// Save verbatims from conversions:
$text = str_replace("\begin{verbatim}", "", $text);
$text = str_replace("\end{verbatim}", "", $text);
$text = preg_replace_callback("|(.*)|smU", "_latex_hash_verbatim", $text); // Convert some special characters: $text = str_replace("\TeX", "TEX", $text); $text = str_replace("\LaTeX", "LATEX", $text); $text = str_replace("\copyright", "©", $text); $text = str_replace("\dots", " ", $text); $text = str_replace("\ldots", " ", $text); $text = str_replace("\cdots", " ", $text); $text = str_replace("\pm", "±", $text); $text = str_replace("\div", "÷", $text); $text = str_replace("\le", "≤", $text); $text = str_replace("\ge", "≥", $text); $text = str_replace("\lt", "<", $text); $text = str_replace("\gt", ">", $text); $text = str_replace("``", '', $text); $text = str_replace("''", '', $text); $text = str_replace("\;", " ", $text); // BUG: next two lines never pass to filter if Drupal HTML filtering ON and strip unknown tags $text = str_replace("<<", '«', $text); $text = str_replace(">>", '»', $text); $text = str_replace("~---", " ", $text); // I not sure for TeX standards for dash conversion, but Latex on my system eat such things and produce appropriate longdashes $text = str_replace(" ---", " ", $text); $text = str_replace(" --", " ", $text); $text = str_replace("-{}-", "--", $text); $text = str_replace("\ ", " ", $text); // Custom commands (from `latex_custom_commands` site variable): if ($lines = explode("\n", variable_get("latex_custom_commands", ""))) { foreach ($lines as $line) { if (! trim($line)) { continue; } list($pattern, $command) = split(' ', $line, 2); $text = preg_replace($pattern, $command, $text); } } // TeX comments -> HTML comments $text = preg_replace("/[^\\\]?%(.*)/iS", "", $text); // Text blocks: $text = str_replace("\begin{itemize}", "
$1", $text);
$text = preg_replace("/\\\\texttt{(.*)}/U", "$1", $text);
$text = preg_replace("/{\\\bf (.*)}/U", "$1", $text);
$text = preg_replace("/\\\\textbf{(.*)}/U", "$1", $text);
$text = preg_replace("/{\\\textsc (.*)}/U", "$1", $text);
// Footnotes:
preg_match_all("/\\\footnote{((.)(.*))}/uUms", $text, $footnotes);
if ($footnotes) {
$text = preg_replace_callback("/\\\footnote{((.)(.*))}/uUms", "_latex_hash_name", $text);
}
// Unknown commands visibility:
if (variable_get("latex_filter_link", 0) == 1) {
// ignore unknown commands:
$text = preg_replace("/\\\[a-z0-9\[\]]+{(.*)}/Ui", "", $text);
}
else {
// show unknown commands
$text = preg_replace("/\\\([a-z0-9\[\]]+){(.*)}/Ui", "\$1\{$2}", $text);
}
// Finally some miscellaneous conversions:
// $text = preg_replace("/(". $value. "", $text); } if($footnotes) { $footnote = "