Let me start with that I don’t like Rich Text editors. So I wrote my own little plugin to format text nicely for me in a much more structured way. This is a VERY basic plugin that probably needs to be extended for more complex uses. But it will do the trick for most normal content pages.
What does it do?
It splits the text into lines and paragraphs, putting <p> tags around paragraphs (with one!! empty line inbetween) and
tags after lines that are immediately followed by a next. This is basically an extremely simple WYSIWYG solution for plain text to HTML, with the added advantage that you can fuss around with the P settings and make the text look prettier.
Almost all pages on
Nimja.com use this system.
Update (jan 14th 2009)
In my last post I’ve posted a version of the plugin that does simple wiki formatting.
Note
It WILL put a <p> block around snippets if you put them on a seperate line, but it will do that BEFORE parsing, so this could, in fact, be considered desirable.
And it will NOT format the result of snippets/chunks etc. Only the direct content.
Features/Fixes
Adapted the code to allow ignore multiple linebreaks. (so no empty <p></p> will occur, neither will
’s on empty lines.)
Also allowed lines to be ignored for formatting by putting a ^ in front of it. This is set in $ignore and can be changed to any string, even one of multiple characters. Those lines are ’ignored’ by the formatting but will still appear, obviously. This is useful for excluding certain lines like <divs>
Install
Create a new plugin with the name
Format and copy the code below into it. Go to the System Event tab on the new plugin and tick the OnLoadWebDocument event, which will get this to run before any snippets are parsed.
Then, just use the
[*content*] tag like usual in your templates.
/* 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 > 0) {
//Get the document content from MODX
$content = $modx->documentObject['content'];
//Remove windows linebreaks.
$content = str_replace("\r", '', $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 = '^';
$ilen = strlen($ignore);
//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 $nr => $line) {
$prev = $cur;
$cur = trim($line);
//If the sentence starts with the ignore marker.
if (substr($cur, 0, $ilen) == $ignore) {
$result .= substr($cur, $ilen).$ln;
$cur = $prev;
} else {
//Start a new block when an empty line has passed
if ( empty($prev) && !empty($cur) ) {
$result .= '<p>'.$cur;
$open = true;
} else { //Or if in an existing block.
//If the current line is empty...
if ( empty($cur) ) {
if ($open) {
$result .= '</p>'.$ln;
$open = false;
}
} else {
$result .= '<br />'.$ln.$cur;
}
}
}
}
if ($open) $result .= '</p>'.$ln;
//Put the modified content back to MODX.
$modx->documentObject['content'] = $result;
}