Subversion Repositories Applications.papyrus

Compare Revisions

Ignore whitespace Rev 177 → Rev 178

/trunk/api/text/wiki_papyrus/Papyrus.class.php
New file
0,0 → 1,284
<?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.1 2004-11-26 12:11:49 jpm 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.1 $ $Date: 2004-11-26 12:11:49 $
// +------------------------------------------------------------------------------------------------------+
*/
 
// +------------------------------------------------------------------------------------------------------+
// | 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(
'Inclure'// Action Inclure
// Le texte récupéré doit être du XHTML
);
/**
*
* The list of rules to not-apply to the source text.
*
* @access public
*
* @var array
*
*/
var $disable = array(
'Include',
'Embed'
);
/**
*
* The delimiter for token numbers of parsed elements in source text.
*
* @access public
*
* @var string
*
*/
var $delim = 12;
function Text_Papyrus()
{
Text_Wiki::Text_Wiki();
$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 (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 = $this->tokens[$key][0];
$opts = $this->tokens[$key][1];
$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 (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.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 ----------------------------------------------------------------------------------------+
*/
?>