Subversion Repositories Applications.papyrus

Compare Revisions

Ignore whitespace Rev 1167 → Rev 1166

/trunk/api/text/wiki_papyrus/Papyrus.class.php
New file
0,0 → 1,330
<?php
/*vim: set expandtab tabstop=4 shiftwidth=4: */
// +------------------------------------------------------------------------------------------------------+
// | PHP version 4.3 |
// +------------------------------------------------------------------------------------------------------+
// | Copyright (C) 2004 Tela Botanica (accueil@tela-botanica.org) |
// +------------------------------------------------------------------------------------------------------+
// | This file is part of Papyrus. |
// | |
// | Foobar 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. |
// | |
// | Foobar 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 Foobar; if not, write to the Free Software |
// | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
// +------------------------------------------------------------------------------------------------------+
// CVS : $Id: Papyrus.class.php,v 1.15 2006-12-13 17:14:00 jp_milcent Exp $
/**
* Classe configurant le formatage pour Papyrus.
*
* Ce fichier contient une classe configurant les règles de formatage de Papyrus.
* Nécessite que l'application appelant ce fichier est précédement inclu le fichier de Pear:
* 'Text/Wiki.php';
*
*@package Text_Wiki
*@subpackage Papyrus
//Auteur original :
*@author Jean-Pascal MILCENT <jpm@tela-botanica.org>
//Autres auteurs :
*@author Aucun
*@copyright Tela-Botanica 2000-2004
*@version $Revision: 1.15 $ $Date: 2006-12-13 17:14:00 $
// +------------------------------------------------------------------------------------------------------+
*/
 
// +------------------------------------------------------------------------------------------------------+
// | ENTETE du PROGRAMME |
// +------------------------------------------------------------------------------------------------------+
 
// +------------------------------------------------------------------------------------------------------+
// | CORPS du PROGRAMME |
// +------------------------------------------------------------------------------------------------------+
/**
*
* Parse structured wiki text and render into arbitrary formats such as XHTML.
*
* Cette classe fille permet de configurer les régles de formatage pour Papyrus.
* Généralement nous avons à faire à des actions.
*
* @author Paul M. Jones <pmjones@ciaweb.net>
* @package Text_Wiki
* @version 0.23.1
* @license LGPL
*/
class Text_Papyrus extends Text_Wiki {
/**
*
* Liste de règles par défaut du format Papyrs dans leur ordre d'application au texte
* à transformer.
*
* @access public
*
* @var array
*
*/
var $rules = array();
/**
*
* The list of rules to not-apply to the source text.
*
* @access public
*
* @var array
*
*/
var $disable = array();
/**
*
* The delimiter for token numbers of parsed elements in source text.
*
* @access public
*
* @var string
*
*/
var $delim = 12;
function Text_Papyrus($rules = null)
{
//Text_Wiki::Text_Wiki();
if (is_array($rules)) {
$this->rules = $rules;
}
// Nous devons sortir les fichiers de Text_Wiki du dépot Pear car la fonction file_exists de PHP utilisée dans
// la méthode findfile de Text_Wiki renvoie false.
$this->addPath('parse', $this->fixPath(dirname(__FILE__)) .'../../pear/Text/Wiki/Parse/');
$this->addPath('render', $this->fixPath(dirname(__FILE__)) .'../../pear/Text/Wiki/Render/');
// Pour les règles spécifiques à Papyrus:
$this->addPath('parse', $this->fixPath(dirname(__FILE__)) . 'Parse/');
$this->addPath('render', $this->fixPath(dirname(__FILE__)) . 'Render/');
}
/**
*
* Renders tokens back into the source text, based on the requested format.
*
* @access public
*
* @param string $format The target output format, typically 'xhtml'.
* If a rule does not support a given format, the output from that
* rule is rule-specific.
*
* @return string The transformed wiki text.
*
*/
function render($format = 'Xhtml')
{
// the rendering method we're going to use from each rule
$format = ucwords(strtolower($format));
// the eventual output text
$output = '';
// when passing through the parsed source text, keep track of when
// we are in a delimited section
$in_delim = false;
// when in a delimited section, capture the token key number
$key = '';
// load the format object
$this->loadFormatObj($format);
// pre-rendering activity
if (isset($this->formatObj[$format]) && is_object($this->formatObj[$format])) {
$output .= $this->formatObj[$format]->pre();
}
// load the render objects
foreach (array_keys($this->parseObj) as $rule) {
$this->loadRenderObj($format, $rule);
}
// pass through the parsed source text character by character
$k = strlen($this->source);
for ($i = 0; $i < $k; $i++) {
// the current character
$char = $this->source{$i};
// are alredy in a delimited section?
if ($in_delim) {
// yes; are we ending the section?
if ($char == chr($this->delim)) {
// yes, get the replacement text for the delimited
// token number and unset the flag.
$key = (int)$key;
$rule = null;
if (isset($this->tokens[$key][0])) {
$rule = $this->tokens[$key][0];
}
$opts = null;
if (isset($this->tokens[$key][1])) {
$opts = $this->tokens[$key][1];
}
if (isset($this->renderObj[$rule]) && is_object($this->renderObj[$rule])) {
$output .= $this->renderObj[$rule]->token($opts);
}
$in_delim = false;
} else {
// no, add to the dlimited token key number
$key .= $char;
}
} else {
// not currently in a delimited section.
// are we starting into a delimited section?
if ($char == chr($this->delim)) {
// yes, reset the previous key and
// set the flag.
$key = '';
$in_delim = true;
} else {
// no, add to the output as-is
$output .= $char;
}
}
}
// post-rendering activity
if (isset($this->formatObj[$format]) && is_object($this->formatObj[$format])) {
$output .= $this->formatObj[$format]->post();
}
// return the rendered source text.
return $output;
}
/**
*
* Add a token to the Text_Wiki tokens array, and return a delimited
* token number.
*
* @access public
*
* @param array $options An associative array of options for the new
* token array element. The keys and values are specific to the
* rule, and may or may not be common to other rule options. Typical
* options keys are 'text' and 'type' but may include others.
*
* @param boolean $id_only If true, return only the token number, not
* a delimited token string.
*
* @return string|int By default, return the number of the
* newly-created token array element with a delimiter prefix and
* suffix; however, if $id_only is set to true, return only the token
* number (no delimiters).
*
*/
function addToken($rule, $options = array(), $id_only = false)
{
// increment the token ID number. note that if you parse
// multiple times with the same Text_Wiki object, the ID number
// will not reset to zero.
static $id;
if (! isset($id)) {
$id = 0;
} else {
$id ++;
}
// force the options to be an array
settype($options, 'array');
// add the token
$this->tokens[$id] = array(
0 => $rule,
1 => $options
);
// return a value
if ($id_only) {
// return the last token number
return $id;
} else {
// return the token number with delimiters
return chr($this->delim) . $id . chr($this->delim);
}
}
}
 
// +------------------------------------------------------------------------------------------------------+
// | PIED du PROGRAMME |
// +------------------------------------------------------------------------------------------------------+
 
 
 
/* +--Fin du code ----------------------------------------------------------------------------------------+
*
* $Log: not supported by cvs2svn $
* Revision 1.14 2006/12/13 10:53:09 jp_milcent
* Suppression de l'action Redirection transformée en Applette.
*
* Revision 1.13 2006/12/13 09:43:13 jp_milcent
* Suppression de l'action Plan transformée en Applette.
*
* Revision 1.12 2006/12/12 17:16:52 jp_milcent
* Suppression de l'action Nouveaute transformée en Applette.
*
* Revision 1.11 2006/12/12 13:33:57 jp_milcent
* Suppression de l'action MotCles transformée en Applette.
*
* Revision 1.10 2006/12/08 20:16:27 jp_milcent
* Suppression de l'action Lien remplacée par une applette.
*
* Revision 1.9 2006/12/08 15:29:33 jp_milcent
* Suppression des actions transformées en applette.
*
* Revision 1.8 2006/05/10 16:02:49 ddelon
* Finition multilinguise et schizo flo
*
* Revision 1.7 2005/09/23 13:58:07 ddelon
* Php5, Projet et Redirection
*
* Revision 1.6 2005/04/18 16:41:53 jpm
* Ajout des actions Plan et Syndication.
*
* Revision 1.5 2005/04/14 16:35:42 jpm
* Ajout de nouvelles actions pour Papyrus XHTML.
*
* Revision 1.4 2005/01/20 19:39:39 jpm
* Correction bogue du à la fonction file_exists qui renvoie false pour les fichiers présent dans le dossier Pear /usr/local/lib/php/.
*
* Revision 1.3 2004/12/07 12:17:37 jpm
* Correction message d'erreur.
*
* Revision 1.2 2004/11/26 12:13:03 jpm
* Correction de résidu...
*
* Revision 1.1 2004/11/26 12:11:49 jpm
* Ajout des action Papyrus à Text_Wiki.
*
* Revision 1.2 2004/11/25 15:36:41 jpm
* Suppression régle Delimiter car problème avec les délimitations de fin de ligne.
*
* Revision 1.1 2004/11/23 17:25:38 jpm
* Début classe PEAR WIKI pour la syntaxe Wikini.
*
*
* +-- Fin du code ----------------------------------------------------------------------------------------+
*/
?>