<?php
/**
 * Beautify class.
 *
 * This class compound GeSHI and tidy functions. GeSHI can highlight man language codes and tidy can auto indent html file.
 *
 * PHPGEN is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PHPGEN is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PHPGEN; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * @copyright   EasySoft R&D Team (C) 2006
 * @author      Chunsheng Wang <wwccss@263.net>
 * @link        http://www.phpgen.com
 * @package     Beautify
 * @version     $Id: Beautify.class.php,v 1.5 2006/08/15 14:40:15 wangcs Exp $
 */
/**
 * The class defination file of GeSHI class.
 */
require_once("GeSHI/geshi.php");
class Beautify extends geshi
{
   /**
    * Structor.
    *
    * @param string  $Style       the url path of style file.
    * @param string  $Soruce      the code to parse.
    * @param string  $Language    the language type of the code.
    */
    function Beautify($Style = "", $Source = "", $Language = "")
    {
        /* Set source, language and style params. */
        $this->setStyle($Style);
        $this->setSource($Source);
        $this->setLanguage($Language);

        /* Extends GeSHI class. */
        $this->geshi($this->Source, $this->Language);

        /* Set highlighting params. */
        $this->set_header_type(GESHI_HEADER_PRE);                // Use pre to quote the code.
        $this->enable_classes();                                 // Use style.
        $this->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 5); // Show line number, strong every 5 lines.
        $this->set_header_content("POWERED BY www.phpgen.com");  // Header content.
        $this->set_footer_content("PARSED IN <TIME> SECONDS");   // Footer content.
    }

   /**
    * Set source code of the parse.
    *
    * @param string  $Soruce      the code to parse, can be a string or a file name.
    */
    function setSource($Source)
    {
        is_file($Source) ? $this->load_from_file($Source) : $this->set_source($Source);
    }

   /**
    * Set code language type of the parse.
    *
    * @param string  $Language
    */
    function setLanguage($Language)
    {
        $Language ==  "HTML" ? $this->set_language("html4strict") : $this->set_language($Language);
    }

   /**
    * Echo the style link.
    *
    * @param string  $Style
    */
    function setStyle($Style)
    {
        echo "<link href='$Style' rel='stylesheet' type='text/css' />";
    }

   /**
    * Repaire html code. This function can't be used to smarty file. 
    */
    function repair()
    {
        if(function_exists("tidy_parse_string"))
        {
            tidy_set_encoding('raw');
            tidy_parse_string($this->source);
            tidy_setopt('output-xhtml', true);
            tidy_setopt('indent', true);
            tidy_setopt('indent-spaces', 2);
            tidy_setopt('wrap', 400);
            tidy_clean_repair();
            $this->source = tidy_get_output();
            return $this->source;
        }
    }

   /**
    * Parse code.
    */
    function parse()
    {
        //eregi("html", $this->language) ? $this->repair() : "";
        return $this->parse_code($this->Source, $this->Language);
    }
}
?>