We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21560
    • 145 Posts
    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;
    }
      [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com
      • 7231
      • 4,205 Posts
      This is interesting. Thanks grin
        [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

        Something is happening here, but you don&#39;t know what it is.
        Do you, Mr. Jones? - [bob dylan]
        • 21560
        • 145 Posts
        My pleasure, if you find any glaring errors, please say so. I’m trying to make this useful. ^_^
          [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com
          • 3749
          • 24,544 Posts
          Interesting idea. Thanks for sharing.

          I should be pretty easy to have it ignore chunks and snippets and TVs with strstr() or a reglular expression looking for ’[[’, ’[!’, ’{{’, ’{!’, etc. and just passing those those lines through unchanged.

          This might be more appropriate in the "in development" forum.

            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 28042 ☆ A M B ☆
            • 24,524 Posts
            Why not use the OnBeforeDocFormSave event, and just do this to the content? It could also be applied to OnBeforeChunkFormSave.
              Studying MODX in the desert - http://sottwell.com
              Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
              Join the Slack Community - http://modx.org
              • 21560
              • 145 Posts
              Well it’s not a completely ’done’ script. Also, this is meant for people who want to do more complex things with it. I wouldn’t reccomend it for ’simple users’ as they’ll love their text-editor anyway.

              And yes, it could certainly be used on the ’save’ event, but wouldn’t that mean it would re-do it if you edit it?
                [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com
                • 28042 ☆ A M B ☆
                • 24,524 Posts
                Hm. I see what you mean. That would take a lot more coding.
                  Studying MODX in the desert - http://sottwell.com
                  Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                  Join the Slack Community - http://modx.org
                  • 21560
                  • 145 Posts
                  The problem with automated formatting is that you don’t want users to see it in the document. So either you’d have to have an extra row in the table ’formatted content’ or do it, like I do, at request time.

                  Also, the overhead is quite minimal. The script only runs over it once before any other parsing is done. So, unless you’re going to do really weird things in the script, this should suffice for most.

                  And, as with most of my scripts. It’s mostly there to learn from. ^_^
                    [font=Times]Comics, stories, music, graphics, games and more! http://Nimja.com
                    • 34017
                    • 898 Posts
                    Have you looked at the Textile plugin? I’ve been using that on my sites just wrap it around in your template <textile>[*content*]</textile>

                    And it gives you some good formatting options. One of these days one of us will get around to creating a jquery markitup editor. http://markitup.jaysalvat.com/home/
                      Chuck the Trukk
                      ProWebscape.com :: Nashville-WebDesign.com
                      - - - - - - - -
                      What are TV's? Here's some info below.
                      http://modxcms.com/forums/index.php/topic,21081.msg159009.html#msg1590091
                      http://modxcms.com/forums/index.php/topic,14957.msg97008.html#msg97008
                      • 25663 MODX Staff
                      • 12,272 Posts
                      Markitup ... how cool ... if only it was WYSIWYG.
                        Ryan Thrash, MODX Co-Founder
                        Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me