We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21560
    • 145 Posts
    I was going more for a way to do standard text formatting without using <p>, because NOTHING IS MORE HORRIBLE THAN THE <p> TAG for normal text. And because I tend to share the weird stuff I write for my site here, this one felt rather obvious to share as well.

    markItUp is quite a bit beyond my desires for this little plugin.
      [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com
      • 27708 MODX Staff
      • 2,502 Posts
      Wordpress uses a hybrid system between this and TinyMCE whereby all text is considered to be a paragraph unless made something else by the user such as a heading or a list.

      This is a great idea for simple sites as I hate to have clients marking up documents a tag for any element be it HTML or MediaWiki or Textile its markup. Limiting choice and simplifying is the way to go.
        Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup magician. Blog ✦ Twitter ✦ LinkedIn ✦ GitHub
        • 21560
        • 145 Posts
        Yeah, you could take this principle and even expand on it in several clever ways.

        Like parsing
        * List item
        * List item

        To a neat HTML list, using LI. And doing the same with
        1) bla bla.
        2) etc...

        The fun thing is that my code parses BEFORE any snippets do, so the result of snippets (unless ’commented out’) is neatly encapsulated as well with no effort, something that was mostly desirable. I hope it will be used by someone with a lot more time than me. ^_^
          [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com
          • 21560
          • 145 Posts
          I fixed a little but that caused odd <BR />’s to appear after ’commented’ lines.

          It was a really, really stupid error.

          It’s all fine now smiley

          Change: (added after $result .= substr($cur, $ilen).$ln;) -> $cur = $prev;

          Original post edited.
            [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com
            • 21560
            • 145 Posts
            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;
            }
              [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com