We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6890
    • 21 Posts
    New source : see http://modxcms.com/forums/index.php/topic,47198.msg334261.html#msg334261

    I’ve implemented 4 different sites with ModX and simply love the ability and unrestrictive use. However, I am getting sick of viewing unformatted source code.

    In an attempt to remedy this issue, I’ve attempted to use a function I use at work, but, it’s not quite working too well...

    Can anyone see a problem?

    <?php
    //Function to seperate multiple tags one line
    function fix_newlines_for_clean_html($fixthistext)
    {
    	$fixthistext_array = explode("\n", $fixthistext);
    	foreach ($fixthistext_array as $unfixedtextkey => $unfixedtextvalue)
    	{
    		//Makes sure empty lines are ignores
    		if (!preg_match("/^(\s)*$/", $unfixedtextvalue))
    		{
    			$fixedtextvalue = preg_replace("/>(\s|\t)*</U", ">\n<", $unfixedtextvalue);
    			$fixedtext_array[$unfixedtextkey] = $fixedtextvalue;
    		}
    	}
    	return implode("\n", $fixedtext_array);
    }
    
    function clean_html_code($uncleanhtml)
    {
    	//Set wanted indentation
    	$indent = "    ";
    
    
    	//Uses previous function to seperate tags
    	$fixed_uncleanhtml = fix_newlines_for_clean_html($uncleanhtml);
    	$uncleanhtml_array = explode("\n", $fixed_uncleanhtml);
    	//Sets no indentation
    	$indentlevel = 0;
    	foreach ($uncleanhtml_array as $uncleanhtml_key => $currentuncleanhtml)
    	{
    		//Removes all indentation
    		$currentuncleanhtml = preg_replace("/\t+/", "", $currentuncleanhtml);
    		$currentuncleanhtml = preg_replace("/^\s+/", "", $currentuncleanhtml);
    		
    		$replaceindent = "";
    		
    		//Sets the indentation from current indentlevel
    		for ($o = 0; $o < $indentlevel; $o++)
    		{
    			$replaceindent .= $indent;
    		}
    		
    		//If self-closing tag, simply apply indent
    		if (preg_match("/<(.+)\/>/", $currentuncleanhtml))
    		{ 
    			$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
    		}
    		//If doctype declaration, simply apply indent
    		else if (preg_match("/<!(.*)>/", $currentuncleanhtml))
    		{ 
    			$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
    		}
    		//If opening AND closing tag on same line, simply apply indent
    		else if (preg_match("/<[^\/](.*)>/", $currentuncleanhtml) && preg_match("/<\/(.*)>/", $currentuncleanhtml))
    		{ 
    			$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
    		}
    		//If closing HTML tag or closing JavaScript clams, decrease indentation and then apply the new level
    		else if (preg_match("/<\/(.*)>/", $currentuncleanhtml) || preg_match("/^(\s|\t)*\}{1}(\s|\t)*$/", $currentuncleanhtml))
    		{
    			$indentlevel--;
    			$replaceindent = "";
    			for ($o = 0; $o < $indentlevel; $o++)
    			{
    				$replaceindent .= $indent;
    			}
    			
    			$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
    		}
    		//If opening HTML tag AND not a stand-alone tag, or opening JavaScript clams, increase indentation and then apply new level
    		else if ((preg_match("/<[^\/](.*)>/", $currentuncleanhtml) && !preg_match("/<(link|meta|base|br|img|hr)(.*)>/", $currentuncleanhtml)) || preg_match("/^(\s|\t)*\{{1}(\s|\t)*$/", $currentuncleanhtml))
    		{
    			$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
    			
    			$indentlevel++;
    			$replaceindent = "";
    			for ($o = 0; $o < $indentlevel; $o++)
    			{
    				$replaceindent .= $indent;
    			}
    		}
    		else
    		//Else, only apply indentation
    		{$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;}
    	}
    	//Return single string seperated by newline
    	return implode("\n", $cleanhtml_array);	
    }
    
    //Get and clean the prerendered modx page
    $modx->documentOutput = clean_html_code($modx->documentOutput);
    ?>
    
      • 20413
      • 2,877 Posts
      Nice code! cool
      MODx Plugin created!!

      Name: Tidy
      System Events: OnWebPagePrerender
      <?php /* REMOVE! Only for syntax highlightning */
      function fix_newlines_for_clean_html($fixthistext)
      {
          $fixthistext_array = explode("\n", $fixthistext);
          foreach ($fixthistext_array as $unfixedtextkey => $unfixedtextvalue)
          {
              //Makes sure empty lines are ignores
              if (!preg_match("/^(\s)*$/", $unfixedtextvalue))
              {
                  $fixedtextvalue = preg_replace("/>(\s|\t)*</U", ">\n<", $unfixedtextvalue);
                  $fixedtext_array[$unfixedtextkey] = $fixedtextvalue;
              }
          }
          return implode("\n", $fixedtext_array);
      }
      
      function clean_html_code($uncleanhtml)
      {
          //Set wanted indentation
          $indent = "    ";
      
      
          //Uses previous function to seperate tags
          $fixed_uncleanhtml = fix_newlines_for_clean_html($uncleanhtml);
          $uncleanhtml_array = explode("\n", $fixed_uncleanhtml);
          //Sets no indentation
          $indentlevel = 0;
          foreach ($uncleanhtml_array as $uncleanhtml_key => $currentuncleanhtml)
          {
              //Removes all indentation
              $currentuncleanhtml = preg_replace("/\t+/", "", $currentuncleanhtml);
              $currentuncleanhtml = preg_replace("/^\s+/", "", $currentuncleanhtml);
              
              $replaceindent = "";
              
              //Sets the indentation from current indentlevel
              for ($o = 0; $o < $indentlevel; $o++)
              {
                  $replaceindent .= $indent;
              }
              
              //If self-closing tag, simply apply indent
              if (preg_match("/<(.+)\/>/", $currentuncleanhtml))
              { 
                  $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
              }
              //If doctype declaration, simply apply indent
              else if (preg_match("/<!(.*)>/", $currentuncleanhtml))
              { 
                  $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
              }
              //If opening AND closing tag on same line, simply apply indent
              else if (preg_match("/<[^\/](.*)>/", $currentuncleanhtml) && preg_match("/<\/(.*)>/", $currentuncleanhtml))
              { 
                  $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
              }
              //If closing HTML tag or closing JavaScript clams, decrease indentation and then apply the new level
              else if (preg_match("/<\/(.*)>/", $currentuncleanhtml) || preg_match("/^(\s|\t)*\}{1}(\s|\t)*$/", $currentuncleanhtml))
              {
                  $indentlevel--;
                  $replaceindent = "";
                  for ($o = 0; $o < $indentlevel; $o++)
                  {
                      $replaceindent .= $indent;
                  }
                  
                  $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
              }
              //If opening HTML tag AND not a stand-alone tag, or opening JavaScript clams, increase indentation and then apply new level
              else if ((preg_match("/<[^\/](.*)>/", $currentuncleanhtml) && !preg_match("/<(link|meta|base|br|img|hr)(.*)>/", $currentuncleanhtml)) || preg_match("/^(\s|\t)*\{{1}(\s|\t)*$/", $currentuncleanhtml))
              {
                  $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
                  
                  $indentlevel++;
                  $replaceindent = "";
                  for ($o = 0; $o < $indentlevel; $o++)
                  {
                      $replaceindent .= $indent;
                  }
              }
              else
              //Else, only apply indentation
              {$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;}
          }
          //Return single string seperated by newline
          return implode("\n", $cleanhtml_array); 
      }
      
      $e = &$modx->Event;
      switch ($e->name) {
      case "OnWebPagePrerender":
      
      $modx->documentOutput = clean_html_code($modx->documentOutput);
      
      break;
      default :
      return; // stop here.
       break;
      }
      

        @hawproductions | http://mrhaw.com/

        Infograph: MODX Advanced Install in 7 steps:
        http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

        Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
        http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
        • 20413
        • 2,877 Posts
        Version 2 that is... grin
          @hawproductions | http://mrhaw.com/

          Infograph: MODX Advanced Install in 7 steps:
          http://forums.modx.com/thread/96954/infograph-modx-advanced-install-in-7-steps

          Recap: Portland, OR (PDX) MODX CMS Meetup, Oct 6, 2015. US Bancorp Tower
          http://mrhaw.com/modx_portland_oregon_pdx_modx_cms_meetup_oct_2015_us_bancorp_tower
          • 32046
          • 75 Posts
          Okay, this looks cool. For someone whos only had a couple of days using MODx, how do I use this please? Install it as a plugin and it works as a event or something prior to render???
          Cheers
            • 6890
            • 21 Posts
            Installation is simple.

            • Create a new plugin
            • Paste code from below
            • Under the settings tab, enable "OnPagePrerender"

            Then refresh you page, it should be indented correctly now. YAY.

            function fix_newlines_for_clean_html($fixthistext)
            {
                $fixthistext_array = explode("\n", $fixthistext);
                foreach ($fixthistext_array as $unfixedtextkey => $unfixedtextvalue)
                {
                    //Makes sure empty lines are ignores
                    if (!preg_match("/^(\s)*$/", $unfixedtextvalue))
                    {
                        $fixedtextvalue = preg_replace("/>(\s|\t)*</U", ">\n<", $unfixedtextvalue);
                        $fixedtext_array[$unfixedtextkey] = $fixedtextvalue;
                    }
                }
                return implode("\n", $fixedtext_array);
            }
            
            function clean_html_code($uncleanhtml)
            {
                //Set wanted indentation
                $indent = "    ";
            
            
                //Uses previous function to seperate tags
                $fixed_uncleanhtml = fix_newlines_for_clean_html($uncleanhtml);
                $uncleanhtml_array = explode("\n", $fixed_uncleanhtml);
                //Sets no indentation
                $indentlevel = 0;
                foreach ($uncleanhtml_array as $uncleanhtml_key => $currentuncleanhtml)
                {
                    //Removes all indentation
                    $currentuncleanhtml = preg_replace("/\t+/", "", $currentuncleanhtml);
                    $currentuncleanhtml = preg_replace("/^\s+/", "", $currentuncleanhtml);
                    
                    $replaceindent = "";
                    
                    //Sets the indentation from current indentlevel
                    for ($o = 0; $o < $indentlevel; $o++)
                    {
                        $replaceindent .= $indent;
                    }
                    
                    //If self-closing tag, simply apply indent
                    if (preg_match("/<(.+)\/>/", $currentuncleanhtml))
                    { 
                        $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
                    }
                    //If doctype declaration, simply apply indent
                    else if (preg_match("/<!(.*)>/", $currentuncleanhtml))
                    { 
                        $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
                    }
                    //If opening AND closing tag on same line, simply apply indent
                    else if (preg_match("/<[^\/](.*)>/", $currentuncleanhtml) && preg_match("/<\/(.*)>/", $currentuncleanhtml))
                    { 
                        $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
                    }
                    //If closing HTML tag or closing JavaScript clams, decrease indentation and then apply the new level
                    else if (preg_match("/<\/(.*)>/", $currentuncleanhtml) || preg_match("/^(\s|\t)*\}{1}(\s|\t)*$/", $currentuncleanhtml))
                    {
                        $indentlevel--;
                        $replaceindent = "";
                        for ($o = 0; $o < $indentlevel; $o++)
                        {
                            $replaceindent .= $indent;
                        }
                        
                        $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
                    }
                    //If opening HTML tag AND not a stand-alone tag, or opening JavaScript clams, increase indentation and then apply new level
                    else if ((preg_match("/<[^\/](.*)>/", $currentuncleanhtml) && !preg_match("/<(link|meta|base|br|img|hr)(.*)>/", $currentuncleanhtml)) || preg_match("/^(\s|\t)*\{{1}(\s|\t)*$/", $currentuncleanhtml))
                    {
                        $cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;
                        
                        $indentlevel++;
                        $replaceindent = "";
                        for ($o = 0; $o < $indentlevel; $o++)
                        {
                            $replaceindent .= $indent;
                        }
                    }
                    else
                    //Else, only apply indentation
                    {$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;}
                }
                //Return single string seperated by newline
                return implode("\n", $cleanhtml_array); 
            }
            
            $e = &$modx->Event;
            switch ($e->name) {
            case "OnWebPagePrerender":
            
            $modx->documentOutput = clean_html_code($modx->documentOutput);
            
            break;
            default :
            return; // stop here.
             break;
            }


              • 25260
              • 156 Posts
              Install EditArea smiley
                • 11055 ☆ A M B ☆
                • 3,112 Posts
                wow...
                this is a ’simple cool must have’ plugin.
                very helpful for debugging.

                thx.
                  Rico
                  Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
                  MODx is great, but knowing how to use it well makes it perfect!

                  www.virtudraft.com

                  Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

                  Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

                  Maintainter/contributor of Babel

                  Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
                  • 11055 ☆ A M B ☆
                  • 3,112 Posts
                  but i believe, this does not work well for nested divs?
                  and restarted its process if pass by a ’}’ bracket.
                    Rico
                    Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
                    MODx is great, but knowing how to use it well makes it perfect!

                    www.virtudraft.com

                    Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

                    Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

                    Maintainter/contributor of Babel

                    Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
                    • 3749
                    • 24,544 Posts
                    Very nice.

                    But since you’re only responding to one event, you can replace this:

                    $e = &$modx->Event;
                    switch ($e->name) {
                    case "OnWebPagePrerender":
                    
                    $modx->documentOutput = clean_html_code($modx->documentOutput);
                    
                    break;
                    default :
                    return; // stop here.
                     break;
                    }
                    

                    with this:

                    $modx->documentOutput = clean_html_code($modx->documentOutput);
                    return;


                      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
                      • 6890
                      • 21 Posts
                      Quote from: goldsky at May 11, 2010, 07:49 PM

                      but i believe, this does not work well for nested divs?
                      and restarted its process if pass by a ’}’ bracket.
                      I’m not to sure what you mean goldsky. I’ve got this running on several sites with various containers nested with each other and it still formats correctly. Could you give me an example link?

                      Quote from: BobRay at May 11, 2010, 10:28 PM

                      Very nice.

                      But since you’re only responding to one event, you can replace this:

                      $e = &$modx->Event;
                      switch ($e->name) {
                      case "OnWebPagePrerender":
                      
                      $modx->documentOutput = clean_html_code($modx->documentOutput);
                      
                      break;
                      default :
                      return; // stop here.
                       break;
                      }
                      

                      with this:
                      $modx->documentOutput = clean_html_code($modx->documentOutput);
                      return;


                      Thanks BobRay, that simplifies things further.