Répertoire de codes source

Coloration synthaxique grace ? Geshi
déposé par Grummfy
le 04/08/2007
nombre de visites : 9596

Grâce ? la libraire Geshi (http://qbnz.com/highlighter/) et ? l'ingéniosité de ZF, colorer tout vos code, facilement, via ce nouveau filtre.

Pour l'utiliser,
Télécharger la libraire geshi et décompresser l? (dans l'exemple dans ./../site/lib/geshi/, donc le fichier geshi.php se trouve dans se répertoire et ./../site/lib/geshi/geshi/ contient les fichiers de langues)

<?php
Zend_Loader::loadClass('Zend_Filter_Geshi');
$zfg = new Zend_Filter_Geshi('php', '../site/lib/geshi/');
echo $zfg->filter(<<<ABC
<?php
Zend_Loader::loadClass('Zend_Filter_Geshi');
\$zfg = new Zend_Filter_Geshi('php', '../site/lib/geshi/');
ABC
);? Grummfy
http://www.grummfy.be/blog/index.php?post/2007/08/04/Zend-Framework-ZF-%3A-Geshi-comme-filtre-dans-ZF
<?php
 
/**
 * This program 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 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category   Zend
 * @package    Zend_Filter
 * @copyright  Copyright (c) 2007 Grummfy (http://www.grummfy.com)
 * @license    http://www.gnu.org/licenses/lgpl.html LGPL v2
 * @version    v0.1 03/07/2007 Grummfy
 */
 
/**
 * @see Zend_Filter_Interface
 */
require_once 'Zend/Filter/Interface.php';
 
 
/**
 * @category   Zend
 * @package    Zend_Filter
 * @copyright  Copyright (c) 2007 Grummfy (http://www.grummfy.com)
 * @license    http://www.gnu.org/licenses/lgpl.html LGPL v2
 */
class Zend_Filter_Geshi implements Zend_Filter_Interface
{
	/**
	 * Current higlight language
	 *
	 * @var string
	 */
	private $_language;
 
	/**
	 * Current directory containing the language files
	 *
	 * @var string
	 */
	private $_languageDirectory;
 
	/**
	 * Geshi object
	 *
	 * @var Geshi
	 */
	protected $_geshi = null;
 
	/**
	 * Sets filter options
	 *
	 * @param string $language
	 * @param string $dirG
	 */
	public function __construct($language = null, $dirG = null)
	{
		$this->setLanguageDirectory( is_null($dirG)?dirname(__FILE__) . '/geshi/geshi/':$dirG );
		$this->setLanguage( is_null($language)?'php':$language );
		Zend_Loader::loadFile('geshi.php', $this->getLanguageDirectory() . '../', true);
	}
 
	/**
	 * Defines the current language to higlight
	 *
	 * @param string $language
	 */
	public function setLanguage($language)
	{
		if (!Zend_Loader::isReadable($this->getLanguageDirectory() . $language . '.php'))
		{
			require_once 'Zend/Filter/Exception.php';
			throw new Zend_Filter_Exception('Geshi filter error : This file (' . $language . ') don\'t exist or is unreadble');
		}
		$this->_language = $language;
	}
 
	/**
	 * Define the directory containing the language files
	 *
	 * @param string $dir
	 */
	public function setLanguageDirectory($dir)
	{
		if (!Zend_Loader::isReadable($dir))
		{
			require_once 'Zend/Filter/Exception.php';
			throw new Zend_Filter_Exception('Geshi filter error : This directory (' . $dir . ') don\'t exist or is unreadble');
		}
		$this->_languageDirectory = $dir;
	}
 
	/**
	 * Returns the current higlight language
	 *
	 * @return string
	 */
	public function getLanguage()
	{
		return $this->_language;
	}
 
	/**
	 * Returns the directory containing the language files
	 *
	 * @return string
	 */
	public function getLanguageDirectory()
	{
		return $this->_languageDirectory;
	}
 
    /**
     * Defined by Zend_Filter_Interface
     * Returns the code ($source) highlighted with html tags
     * 
     * @param  string $source
     * @return string
     */
	public function filter($source)
	{
		if (is_null($this->_geshi))
			$this->_geshi = new GeSHi($source, $this->getLanguage());
		else
		{
			$this->_geshi	->set_language($this->getLanguage())
							->set_source($source);
 
		}
		$this->_geshi	->set_language_path($this->getLanguageDirectory());
		$this->_geshi	->set_tab_width(4);
 
		$this->_Error();
 
		$sourceGeshiser = $this->_geshi->parse_code();
 
		$this->_Error();
 
		return $sourceGeshiser;
	}
 
    protected function _Error()
    {
    	if ($this->_geshi->error() !== false)
		{
			require_once 'Zend/Filter/Exception.php';
			throw new Zend_Filter_Exception("Geshi filter error : " . $this->_geshi->error());
		}
    }
}
Graphisme réalisé par l'agence Rodolphe Eveilleau
Développement par Kitpages