Salve come da titolo vorrei una mano con una modifica al core di un plugin per Wordpress che implementa un forum in Wordpress.
Il parsing dei bbcode è assegnato a due file: bbtohtml.php e htmltobb.php (di chiara comprensione).
Questo bbtohtml da me modificato:
Codice PHP:
<?php
/*
Simple:Press
bbCode to xhtml parser
$LastChangedDate: 2011-01-23 10:56:55 -0700 (Sun, 23 Jan 2011) $
$Rev: 5346 $
*/
if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) {
die('Access Denied');
}
function sf_BBCode2Html($text, $dobr=true)
{
$text = trim($text);
# BBCode [code]
if (!function_exists('bbtohtml_escape')) {
function bbtohtml_escape($s) {
global $text;
$text = strip_tags($text);
return '<code>'.htmlspecialchars($s[1]).'</code>';
}
}
$text = preg_replace_callback('/\[code\](.*?)\[\/code\]/ms', "bbtohtml_escape", $text);
# BBCode to find...
$in = array( '/\[b\](.*?)\[\/b\]/ms',
'/\[i\](.*?)\[\/i\]/ms',
'/\[u\](.*?)\[\/u\]/ms',
'/\[left\](.*?)\[\/left\]/ms',
'/\[right\](.*?)\[\/right\]/ms',
'/\[center\](.*?)\[\/center\]/ms',
'/\[img\](.*?)\[\/img\]/ms',
'/\[url\="?(.*?)"?\](.*?)\[\/url\]/is',
'/\[url\](.*?)\[\/url\]/is',
'/\[quote\](.*?)\[\/quote\]/ms',
'/\[list\=(.*?)\](.*?)\[\/list\]/ms',
'/\[list\](.*?)\[\/list\]/ms',
'/\[B\](.*?)\[\/B\]/ms',
'/\[I\](.*?)\[\/I\]/ms',
'/\[U\](.*?)\[\/U\]/ms',
'/\[LEFT\](.*?)\[\/LEFT\]/ms',
'/\[RIGHT\](.*?)\[\/RIGHT\]/ms',
'/\[CENTER\](.*?)\[\/CENTER\]/ms',
'/\[IMG\](.*?)\[\/IMG\]/ms',
'/\[URL\="?(.*?)"?\](.*?)\[\/URL\]/is',
'/\[QUOTE\](.*?)\[\/QUOTE\]/ms',
'/\[LIST\=(.*?)\](.*?)\[\/LIST\]/ms',
'/\[LIST\](.*?)\[\/LIST\]/ms',
'/\[\*\]\s?(.*?)\n/ms',
'/\[tex\](.*?)\[\/tex\]/ms', #Added by Tid: Latex tag
'/\[TEX\](.*?)\[\/TEX\]/ms'
);
# And replace them by...
$out = array( '[b]\1[/b]',
'[i]\1[/i]',
'<u>\1</u>',
'<div style="text-align:left">\1</div>',
'<div style="text-align:right">\1</div>',
'<div style="text-align:center">\1</div>',
'[img]\1[/img]',
'[url="\1"]\2[/url]',
'\1',
'<blockquote>\1</blockquote>',
'[list=1]\2[/list]',
'<ul>\1[/list]',
'[b]\1[/b]',
'[i]\1[/i]',
'<u>\1</u>',
'<div style="text-align:left">\1</div>',
'<div style="text-align:right">\1</div>',
'<div style="text-align:center">\1</div>',
'[img]\1[/img]',
'[url="\1"]\2[/url]',
'<blockquote>\1</blockquote>',
'[list=1]\2[/list]',
'<ul>\1[/list]',
'[*]\1',
'$$\1$$',
'$$\1$$'
);
$text = preg_replace($in, $out, $text);
# special case for nested quotes
$text = str_replace('[quote]', '<blockquote>', $text);
$text = str_replace('[/quote]', '</blockquote>', $text);
# paragraphs
if($dobr)
{
$text = str_replace("\r", "", $text);
# clean some tags to remain strict
if (!function_exists('bbtohtml_removeBr')) {
function bbtohtml_removeBr($s) {
return str_replace("
", "", $s[0]);
}
}
$text = preg_replace_callback('/<pre>(.*?)<\/pre>/ms', "bbtohtml_removeBr", $text);
$text = preg_replace('/
<pre>(.*?)<\/pre><\/p>/ms', "<pre>\\1</pre>", $text);
$text = preg_replace_callback('/<ul>(.*?)<\/ul>/ms', "bbtohtml_removeBr", $text);
$text = preg_replace('/
<ul>(.*?)<\/ul><\/p>/ms', "<ul>\\1[/list]", $text);
}
return $text;
}
?>
Che funziona perfettamente con la mia modifica, mettengo i tag [tex][/tex] viene visualizzato correttamente il testo tra i due segni di dollaro. (Poi uno script chiamato Mathjax mi sostituisce la stringa tra $$$$ nella giusta formulazione Latex)
La mia aggiunta:
Codice PHP:
// Nell'array dei bbcode
'/\[tex\](.*?)\[\/tex\]/ms', #Added by Tid: Latex tag
'/\[TEX\](.*?)\[\/TEX\]/ms'
//Nell'array dei BBcode
'$$\1$$'
Il problema ora sorge quando un utente deve modificare il suo messaggio. Mathjax sostituisce l'espressione $$(codice Latex)$$ con una serie di span:
Codice PHP:
<span class="MathJax_Preview" style="display: none;"></span> <span class="MathJax" role="textbox" aria-readonly="true" style=""><serie di span annidati> <script id="MathJax-Element-1" type="math/tex">(codice Latex)</script>
Io avrei bisogno di recuperare solo il codice latex all'interno degli span per ristamparlo all'atto dell'edit del messaggio tra i tag [tex](codice Latex)[/tex]
Il codice che si occupa di riparsare l'html in bbcode è questo:
Codice PHP:
<?php
/*
Simple:Press
xhtml to bbCode parser
$LastChangedDate: 2010-03-26 16:38:27 -0700 (Fri, 26 Mar 2010) $
$Rev: 3818 $
*/
if(preg_match('#' . basename(__FILE__) . '#', $_SERVER['PHP_SELF'])) {
die('Access Denied');
}
function sf_Html2BBCode($text)
{
$text = trim($text);
$text = str_replace("\n\n", "\n", $text);
$text = str_replace ('<div class="sfcode">', "<code>", $text);
$text = str_replace ('</div>', "</code>", $text);
# BBCode [code]
if (!function_exists('htmltobb_escape')) {
function htmltobb_escape($s) {
global $text;
return '[code]'.htmlspecialchars_decode($s[1]).'[/code]';
}
}
$text = preg_replace_callback('/\<code\>(.*?)\<\/code\>/ms', "htmltobb_escape", $text);
# Tags to Find
$htmltags = array(
'/\<b\>(.*?)\<\/b\>/is',
'/\<em\>(.*?)\<\/em\>/is',
'/\<u\>(.*?)\<\/u\>/is',
'/\<ul\>(.*?)\<\/ul\>/is',
'/\<li\>(.*?)\<\/li\>/is',
'/\<img(.*?) src=\"(.*?)\" (.*?)\>/is',
'/\<blockquote\>(.*?)\<\/blockquote\>/is',
'/\<strong\>(.*?)\<\/strong\>/is',
'/\<a href=\"(.*?)\"(.*?)\>(.*?)\<\/a\>/is',
'/\<span class=\'MathJax_Preview\'\>(.*?)\<\/span\>\<script type=\'math\/tex\'\>(.*?)\<\/script\>/is'
);
# Replace with
$bbtags = array(
'[b]$1[/b]',
'[i]$1[/i]',
'[u]$1[/u]',
'[list]$1[/list]',
'[*]$1',
'[img]$2[/img]',
'[quote]$1[/quote]',
'[b]$1[/b]',
'[url=$1]$3[/url]',
'[tex]$2[/tex]'
);
# Replace $htmltags in $text with $bbtags
$text = preg_replace ($htmltags, $bbtags, $text);
return $text;
}
?>
Con le mie modifiche già inserite. Io ho provato la regex fuori dal parse del forum e sembra funzionare. Mi sapete dire cosa sbagliO?