If people are interested, I made a variant that mimicks Wiki formatting for a large part.
Please note: This is a
Work in Progress
Code here:
//Simple (yet hopefully elegant) formatting function.
function formatColumns($string) {
$patterns = array(
"left" => "#\<\<(.*?)\<\<#is",
"right" => "#\>\>(.*?)\>\>#is"
);
$replacements = array(
"left" => "<div class=\"LeftC\">$1\n</div>",
"right" => "<div class=\"RightC\">$1\n</div>"
);
return preg_replace($patterns, $replacements, $string);
}
function formatLine($string) {
$patterns = array(
"li" => "#^[\*\#\>](.*)$#s",
"h4" => "#^\=\=\=\=(.*?)\=\=\=\=#is",
"h3" => "#^\=\=\=(.*?)\=\=\=#is",
"h2" => "#^\=\=(.*?)\=\=#is",
"bold" => "#\'\'\'(.*?)\'\'\'#is",
"italics" => "#\'\'(.*?)\'\'#is",
"underline" => "#\_\_(.*?)\_\_#is",
"link" => "#\-\-(.*?)\|(.*?)\-\-#i",
"img" => "#img\=(.*?)\|center\=#i",
"img" => "#img\=(.*?)\|(.*?)\=#i",
);
$replacements = array(
"li" => "<li>$1</li>",
"h4" => "<h4>$1</h4>",
"h3" => "<h3>$1</h3>",
"h2" => "<h2>$1</h2>",
"bold" => "<b>$1</b>",
"italics" => "<i>$1</i>",
"underline" => "<u>$1</u>",
"link" => "<a href=\"$1\">$2</a>",
"img" => "<div style=\"text-align: center;\"><img src=\"assets/images/$1\" ></div>",
"img" => "<img src=\"assets/images/$1\" style=\"float: $2;\" >",
);
return preg_replace($patterns, $replacements, $string);
}
//Main formatting function, added to the main for use in other functions.
function formatContent($content) {
//Remove windows linebreaks.
$content = str_replace("\r", '', $content);
//Add column divs.
$content = formatColumns($content);
//Split the content into natural lines.
$lines = split("\n", $content);
//To make sure the source looks nice as well. (and not all on one line)
$ln = "\n";
$result = '';
$cur = '';
$open = false;
$ignore = '!'; //Ignore character.
$ul = false;
$sl = false;
$ol = false;
$td = false;
$th = false;
$table = false;
$special = '^(\!|\img\=|\=\=|\*|\#|\<|\>)';
//This little loop creates <p> tags around paragraphs and adds <br /> tags where lines do not have an empty line in between. Very simple.
foreach ($lines as $line) {
$prev = $cur;
$cur = trim($line);
//Table stuff.
$table = false;
if (substr($cur, 0, 1) == '|') {
$table = true;
$cur = substr($cur, 1);
if ($td) {
$result .= '</td>';
$td = false;
}
if ($th) {
$result .= '</th>';
$th = false;
}
switch (substr($cur, 0, 1)) {
case '{':
$cur = substr($cur, 1);
$class = (empty($cur)) ? 'Normal' : $cur;
$result .= '<table class="'.$class.'"><tr>';
$cur = '';
break;
case '}':
$result .= '</tr></table>';
$cur = '';
break;
case '-':
$result .= '</tr><tr>';
$cur = '';
break;
case '|':
$result .= '<th>';
$th = true;
$cur = substr($cur, 1);
break;
case '(':
$result .= '<td class="Dot">';
$td = true;
$cur = substr($cur, 1);
break;
default: //Standard table data.
$result .= '<td>';
$td = true;
break;
}
}
//If the sentence starts with the ignore marker.
if (ereg($special,$cur)) {
switch (substr($cur, 0, 1)) {
case '#':
if (!$ol) {
$ol = true;
$result .= '<ol>'.$ln;
}
$result .= formatLine($cur);
break;
case '*':
if (!$ul) {
$ul = true;
$result .= '<ul>'.$ln;
}
$result .= formatLine($cur);
break;
case '>':
if (!$sl) {
$sl = true;
$result .= '<ul class="Special">'.$ln;
}
$result .= formatLine($cur);
break;
case '=':
$result .= formatLine($cur);
$prev = ''; //Force start of a new paragraph.
break;
default: //Ignored.
$cur = (substr($cur, 0, 1) == '!') ? substr($cur, 1) : $cur;
$result .= formatLine($cur);
break;
}
$result .= $ln;
$cur = $prev;
} else {
if ($ul) {
$result .= '</ul>'.$ln;
$ul = false;
}
if ($sl) {
$result .= '</ul>'.$ln;
$sl = false;
}
if ($ol) {
$result .= '</ol>'.$ln;
$ol = false;
}
if (!empty($cur)) $result .= formatLine($cur);
if (!$table) $result .= '<br />'.$ln;
}
}
if ($ul) $result .= '</ul>'.$ln;
if ($sl) $result .= '</ul>'.$ln;
if ($ol) $result .= '</ol>'.$ln;
if ($open) $result .= '</p>'.$ln;
return $result;
}
/* FORMAT plugin
* Formats text properly with <p> and <br /> tags.
* Set to use the Sytem Event -> OnLoadWebDocument
*/
$template = $modx->documentObject['template'];
//Filter out blank templates, as they have different content and shouldn't be formatted.
if ($template > -1) {
//Get the document content from MODX
$content = $modx->documentObject['content'];
$result = formatContent($content);
//Put the modified content back to MODX.
$modx->documentObject['content'] = $result;
}