[php-blog-cvs] serendipity/bundled-libs/Text/Wiki/Rule break.php,NONE,1.1 center.php,NONE,1.1 colort
A reliable, secure & extensible PHP blog | Not mainstream since 2002
Brought to you by:
garvinhicking,
jhermanns
Update of /cvsroot/php-blog/serendipity/bundled-libs/Text/Wiki/Rule In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27598/bundled-libs/Text/Wiki/Rule Modified Files: blockquote.php bold.php code.php deflist.php delimiter.php emphasis.php entities.php freelink.php heading.php horiz.php html.php interwiki.php italic.php list.php newline.php paragraph.php phpcode.php phplookup.php prefilter.php raw.php revise.php strong.php superscript.php table.php tighten.php toc.php tt.php url.php wikilink.php Added Files: break.php center.php colortext.php embed.php image.php include.php Log Message: PEAR::Text_Wiki Upgade from Tobias Schlitt Index: list.php =================================================================== RCS file: /cvsroot/php-blog/serendipity/bundled-libs/Text/Wiki/Rule/list.php,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- list.php 26 Feb 2004 11:27:54 -0000 1.1 +++ list.php 25 May 2004 09:57:54 -0000 1.2 @@ -223,7 +223,7 @@ } // we're done! send back the replacement text. - return $return; + return "\n" . $return . "\n"; } @@ -250,26 +250,36 @@ $pad = str_pad('', $level, "\t"); $pad = str_replace("\t", ' ', $pad); + // attempt XHTML compliance so that sub-lists are part + // of a list item, not between list items + if ($level > 0) { + $pre = '<li style="list-style: none;">'; + $post = '</li>'; + } else { + $pre = ''; + $post = ''; + } + switch ($type) { case 'bullet_start': - return $pad . "<ul>\n"; + return "$pad$pre<ul>\n"; break; case 'bullet_end': - return $pad . "</ul>\n"; + return "$pad</ul>$post\n"; break; case 'number_start': - return $pad . "<ol>\n"; + return "$pad$pre<ol>\n"; break; case 'number_end': - return $pad . "</ol>\n"; + return "$pad</ol>$post\n"; break; case 'item_start': - return $pad . "<li>"; + return "$pad<li>"; break; case 'item_end': Index: interwiki.php =================================================================== RCS file: /cvsroot/php-blog/serendipity/bundled-libs/Text/Wiki/Rule/interwiki.php,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- interwiki.php 26 Feb 2004 11:27:54 -0000 1.1 +++ interwiki.php 25 May 2004 09:57:54 -0000 1.2 @@ -34,34 +34,90 @@ class Text_Wiki_Rule_interwiki extends Text_Wiki_Rule { + var $regex = '([A-Za-z0-9_]+):([\/=&~#A-Za-z0-9_]+)'; + + /** * - * Constructor. We override the Text_Wiki_Rule constructor so we can - * explicitly comment each part of the $regex property. + * Parser. We override the standard parser so we can + * find both described interwiki links and standalone links. * * @access public * - * @param object &$obj The calling "parent" Text_Wiki object. - * - * @param string $name The token name to use for this rule. + * @return void * */ - function Text_Wiki_Rule_interwiki(&$obj, $name) + function parse() { - parent::Text_Wiki_Rule($obj, $name); - $this->regex = '/([A-Za-z0-9]+):([\/=&~#A-Za-z0-9]+)/'; + // described interwiki links + $tmp_regex = '/\[' . $this->regex . ' (.+?)\]/'; + $this->_wiki->_source = preg_replace_callback( + $tmp_regex, + array(&$this, 'processDescr'), + $this->_wiki->_source + ); + + // standalone interwiki links + $tmp_regex = '/' . $this->regex . '/'; + $this->_wiki->_source = preg_replace_callback( + $tmp_regex, + array(&$this, 'process'), + $this->_wiki->_source + ); + } /** * - * Generates a replacement for the matched text. Token options are: + * Generates a replacement for the matched standalone interwiki text. + * Token options are: + * + * 'site' => The key name for the Text_Wiki interwiki array map, + * usually the name of the interwiki site. + * + * 'page' => The page on the target interwiki to link to. + * + * 'text' => The text to display as the link. * - * 'site' => The key name for the Text_Wiki interwiki array map, usually - * the name of the interwiki site. + * @access public * - * 'page' => The page on the target interwiki to link to. + * @param array &$matches The array of matches from parse(). + * + * @return A delimited token to be used as a placeholder in + * the source text, plus any text priot to the match. + * + */ + + function process(&$matches) + { + $options = array( + 'site' => $matches[1], + 'page' => $matches[2], + 'text' => $matches[0] + ); + + // if not in the interwiki map, don't make it an interwiki link + if (isset($this->_conf['sites'][$options['site']])) { + return $this->addToken($options); + } else { + return $matches[0]; + } + } + + + /** + * + * Generates a replacement for described interwiki links. Token + * options are: + * + * 'site' => The key name for the Text_Wiki interwiki array map, + * usually the name of the interwiki site. + * + * 'page' => The page on the target interwiki to link to. + * + * 'text' => The text to display as the link. * * @access public * @@ -72,15 +128,16 @@ * */ - function process(&$matches) + function processDescr(&$matches) { $options = array( 'site' => $matches[1], - 'page' => $matches[2] + 'page' => $matches[2], + 'text' => $matches[3] ); // if not in the interwiki map, don't make it an interwiki link - if (isset($this->_wiki->interwiki[$options['site']])) { + if (isset($this->_conf['sites'][$options['site']])) { return $this->addToken($options); } else { return $matches[0]; @@ -105,18 +162,34 @@ { $site = $options['site']; $page = $options['page']; + $text = $options['text']; - if (isset($this->_wiki->interwiki[$site])) { - $href = $this->_wiki->interwiki[$site]; + if (isset($this->_conf['sites'][$site])) { + $href = $this->_conf['sites'][$site]; } else { - $href = ''; + return $text; } - if ($href != '') { - return "<a href=\"$href$page\">$site:$page</a>"; - } else { - return "$site:$page"; - } + // old form where page is at end, + // or new form with %s placeholder for sprintf()? + if (strpos($href, '%s') === false) { + // use the old form + $href = $href . $page; + } else { + // use the new form + $href = sprintf($href, $page); + } + + // allow for alternative targets + if (isset($this->_conf['target']) && + trim($this->_conf['target']) != '') { + $target = 'target="' . $this->_conf['target'] . '"'; + } else { + $target = ''; + } + + + return "<a $target href=\"$href\">$text</a>"; } } ?> \ No newline at end of file Index: tighten.php =================================================================== RCS file: /cvsroot/php-blog/serendipity/bundled-libs/Text/Wiki/Rule/tighten.php,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- tighten.php 26 Feb 2004 11:27:54 -0000 1.1 +++ tighten.php 25 May 2004 09:57:54 -0000 1.2 @@ -21,7 +21,7 @@ /** * -* This rule removes all newlines from the source text. +* The rule removes all remaining newlines. * * @author Paul M. Jones <pm...@ci...> * @@ -43,8 +43,8 @@ function parse() { - $this->_wiki->_source = str_replace("\n", "", - $this->_wiki->_source); + $this->_wiki->_source = str_replace("\n", '', + $this->_wiki->_source); } } ?> \ No newline at end of file --- NEW FILE: include.php --- <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2003 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | li...@ph... so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Paul M. Jones <pm...@ci...> | // +----------------------------------------------------------------------+ // // $Id: include.php,v 1.1 2004/05/25 09:57:54 garvinhicking Exp $ /** * * This class implements a Text_Wiki_Rule to include the results of a * script directly into the source at parse-time; thus, the output of the * script will be parsed by Text_Wiki. This differs from the 'embed' * rule, which incorporates the results at render-time, meaning that the * 'embed' content is not parsed by Text_Wiki. * * This rule is inherently not secure; it allows cross-site scripting to * occur if the embedded output has <script> or other similar tags. Be * careful. * * @author Paul M. Jones <pm...@ci...> * * @package Text_Wiki * */ class Text_Wiki_Rule_include extends Text_Wiki_Rule { /** * * The regular expression used to find source text matching this * rule. * * @access public * * @var string * */ var $regex = '/(\[\[include )(.+?)(\]\])/i'; /** * * Includes the results of the script directly into the source; the output * will subsequently be parsed by the remaining Text_Wiki rules. * * @access public * * @param array &$matches The array of matches from parse(). * * @return The results of the included script. * */ function process(&$matches) { $file = $this->_conf['base'] . $matches[2]; ob_start(); include($file); $output = ob_get_contents(); ob_end_clean(); return $output; } } ?> Index: phpcode.php =================================================================== RCS file: /cvsroot/php-blog/serendipity/bundled-libs/Text/Wiki/Rule/phpcode.php,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- phpcode.php 26 Feb 2004 11:27:54 -0000 1.1 +++ phpcode.php 25 May 2004 09:57:54 -0000 1.2 @@ -102,17 +102,31 @@ ob_end_clean(); // replace <br /> tags with simple newlines - $text = str_replace("<br />", "\n", $text); + //$text = str_replace("<br />", "\n", $text); // replace non-breaking space with simple spaces - $text = str_replace(" ", " ", $text); + //$text = str_replace(" ", " ", $text); + // replace <br /> tags with simple newlines + // replace non-breaking space with simple spaces + // translate old HTML to new XHTML + // courtesy of research by A. Kalin :-) + $map = array( + '<br />' => "\n", + ' ' => ' ', + '<font' => '<span', + '</font>' => '</span>', + 'color="' => 'style="color:' + ); + $text = strtr($text, $map); + // get rid of the last newline inside the code block // (becuase higlight_string puts one there) if (substr($text, -8) == "\n</code>") { $text = substr($text, 0, -8) . "</code>"; } + // done return "\n<pre>$text</pre>\n"; } } Index: heading.php =================================================================== RCS file: /cvsroot/php-blog/serendipity/bundled-libs/Text/Wiki/Rule/heading.php,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- heading.php 26 Feb 2004 11:27:54 -0000 1.1 +++ heading.php 25 May 2004 09:57:54 -0000 1.2 @@ -85,7 +85,7 @@ ) ); - return $start . $matches[2] . $end; + return $start . $matches[2] . $end . "\n"; } Index: paragraph.php =================================================================== RCS file: /cvsroot/php-blog/serendipity/bundled-libs/Text/Wiki/Rule/paragraph.php,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- paragraph.php 26 Feb 2004 11:27:54 -0000 1.1 +++ paragraph.php 25 May 2004 09:57:54 -0000 1.2 @@ -33,7 +33,6 @@ class Text_Wiki_Rule_paragraph extends Text_Wiki_Rule { - /** * * The regular expression used to find source text matching this @@ -45,17 +44,16 @@ * */ - function Text_Wiki_Rule_paragraph(&$obj, $name) - { - parent::Text_Wiki_Rule($obj, $name); - $this->regex = "/^(?!{$this->_wiki->delim}).*?\n\n/m"; - } + var $regex = "/^.*?\n\n/m"; + /** * * Generates a token entry for the matched text. Token options are: * - * 'text' => The full matched text. + * 'start' => The starting point of the paragraph. + * + * 'end' => The endinging point of the paragraph. * * @access public * @@ -68,8 +66,44 @@ function process(&$matches) { - if (trim($matches[0]) == '') { - return $matches[0]; + $delim = $this->_wiki->delim; + + // was anything there? + if (trim($matches[0]) == '') { + return ''; + } + + // does the match start with a delimiter? + if (substr($matches[0], 0, 1) != $delim) { + // no. + $start = $this->addToken(array('type' => 'start')); + $end = $this->addToken(array('type' => 'end')); + return $start . trim($matches[0]) . $end; + } + + // the line starts with a delimiter. read in the delimited + // token number, check the token, and see if we should + // skip it. + + // loop starting at the second character (we already know + // the first is a delimiter) until we find another + // delimiter; the text between them is a token key number. + $key = ''; + $len = strlen($matches[0]); + for ($i = 1; $i < $len; $i++) { + $char = $matches[0]{$i}; + if ($char == $delim) { + break; + } else { + $key .= $char; + } + } + + // look at the token and see if it's skippable (if we skip, + // it will not be marked as a paragraph) + $token_type = $this->_wiki->_tokens[$key][0]; + if (in_array($token_type, $this->_conf['skip'])) { + return $matches[0]; } else { $start = $this->addToken(array('type' => 'start')); $end = $this->addToken(array('type' => 'end')); --- NEW FILE: break.php --- <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2003 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | li...@ph... so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Paul M. Jones <pm...@ci...> | // +----------------------------------------------------------------------+ // // $Id: break.php,v 1.1 2004/05/25 09:57:53 garvinhicking Exp $ /** * * This class implements a Text_Wiki_Rule to mark forced line breaks in the * source text. * * @author Paul M. Jones <pm...@ci...> * * @package Text_Wiki * */ class Text_Wiki_Rule_break extends Text_Wiki_Rule { /** * * The regular expression used to parse the source text and find * matches conforming to this rule. Used by the parse() method. * * @access public * * @var string * * @see parse() * */ var $regex = '/ _\n/'; /** * * Generates a replacement token for the matched text. * * @access public * * @param array &$matches The array of matches from parse(). * * @return string A delimited token to be used as a placeholder in * the source text. * */ function process(&$matches) { return $this->addToken(); } /** * * Renders a token into text matching the requested format. * * @access public * * @param array $options The "options" portion of the token (second * element). * * @return string The text rendered from the token options. * */ function renderXhtml($options) { return "<br />\n"; } } ?> --- NEW FILE: center.php --- <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2003 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | li...@ph... so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Paul M. Jones <pm...@ci...> | // +----------------------------------------------------------------------+ // // $Id: center.php,v 1.1 2004/05/25 09:57:54 garvinhicking Exp $ /** * * This class implements a Text_Wiki_Rule to find lines marked for centering. * The line must start with "= " (i.e., an equal-sign followed by a space). * * @author Paul M. Jones <pm...@ci...> * * @package Text_Wiki * */ class Text_Wiki_Rule_center extends Text_Wiki_Rule { /** * * The regular expression used to find source text matching this * rule. * * @access public * * @var string * */ //var $regex = '/\n(\<center\>)\n(.+)\n(\<\/center\>)\n/Umsi'; var $regex = '/\n\= (.*?)\n/'; /** * * Generates a token entry for the matched text. * * @access public * * @param array &$matches The array of matches from parse(). * * @return A delimited token number to be used as a placeholder in * the source text. * */ function process(&$matches) { $start = $this->addToken(array('type' => 'start')); $end = $this->addToken(array('type' => 'end')); //return "\n" . $start . "\n" . $matches[2] . "\n\n" . $end . "\n"; return "\n" . $start . $matches[1] . $end . "\n"; } /** * * Renders a token into text matching the requested format. * * @access public * * @param array $options The "options" portion of the token (second * element). * * @return string The text rendered from the token options. * */ function renderXhtml($options) { if ($options['type'] == 'start') { //return "\n<center>\n"; return '<div style="text-align: center;">'; } if ($options['type'] == 'end') { //return "</center>\n"; return '</div>'; } } } ?> Index: newline.php =================================================================== RCS file: /cvsroot/php-blog/serendipity/bundled-libs/Text/Wiki/Rule/newline.php,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- newline.php 26 Feb 2004 11:27:54 -0000 1.1 +++ newline.php 25 May 2004 09:57:54 -0000 1.2 @@ -21,8 +21,9 @@ /** * -* This class implements a Text_Wiki_Rule to mark "hard" newlines in the -* source text. +* This class implements a Text_Wiki_Rule to mark implied line breaks in the +* source text, usually a single carriage return in the middle of a paragraph +* or block-quoted text. * * @author Paul M. Jones <pm...@ci...> * @@ -46,7 +47,7 @@ * */ - var $regex = '/\n/m'; + var $regex = '/([^\n])\n([^\n])/m'; /** @@ -64,7 +65,7 @@ function process(&$matches) { - return $this->addToken(); + return $matches[1] . $this->addToken() . $matches[2]; } @@ -83,7 +84,7 @@ function renderXhtml($options) { - return "\n<br />\n"; + return "<br />\n"; } } Index: toc.php =================================================================== RCS file: /cvsroot/php-blog/serendipity/bundled-libs/Text/Wiki/Rule/toc.php,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- toc.php 26 Feb 2004 11:27:54 -0000 1.1 +++ toc.php 25 May 2004 09:57:54 -0000 1.2 @@ -1,19 +1,19 @@ <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ -// | PHP version 4 | +// | PHP version 4 | // +----------------------------------------------------------------------+ -// | Copyright (c) 1997-2003 The PHP Group | +// | Copyright (c) 1997-2003 The PHP Group | // +----------------------------------------------------------------------+ -// | This source file is subject to version 2.0 of the PHP license, | -// | that is bundled with this package in the file LICENSE, and is | -// | available through the world-wide-web at | -// | http://www.php.net/license/2_02.txt. | +// | This source file is subject to version 2.0 of the PHP license, | +// | that is bundled with this package in the file LICENSE, and is | +// | available through the world-wide-web at | +// | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | -// | obtain it through the world-wide-web, please send a note to | -// | li...@ph... so we can mail you a copy immediately. | +// | obtain it through the world-wide-web, please send a note to | +// | li...@ph... so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ -// | Authors: Paul M. Jones <pm...@ci...> | +// | Authors: Paul M. Jones <pm...@ci...> | // +----------------------------------------------------------------------+ // // $Id$ @@ -22,7 +22,7 @@ /** * * This class implements a Text_Wiki_Rule to find all heading tokens and -* build a table of contents. The %%TOC%% tag gets replaced with a list +* build a table of contents. The [[toc]] tag gets replaced with a list * of all the level-2 through level-6 headings. * * @author Paul M. Jones <pm...@ci...> @@ -33,119 +33,119 @@ class Text_Wiki_Rule_toc extends Text_Wiki_Rule { - - - /** - * - * The regular expression used to parse the source text and find - * matches conforming to this rule. Used by the parse() method. - * - * @access public - * - * @var string - * - * @see parse() - * - */ - - var $regex = "/%%TOC%%/m"; - - - /** - * - * The collection of headings (text and levels). - * - * @access public - * - * @var array - * - * @see _getEntries() - * - */ - - var $entry = array(); - - - /** - * - * Custom parsing (have to process heading entries first). - * - * @access public - * - * @see Text_Wiki::parse() - * - */ - - function parse() - { - $this->_getEntries(); - parent::parse(); - } - - - /** - * - * Generates a replacement for the matched text. Token options are: - * - * 'type' => ['list_start'|'list_end'|'item_end'|'item_end'|'target'] - * - * 'level' => The heading level (1-6). - * - * 'count' => Which heading this is in the list. - * - * @access public - * - * @param array &$matches The array of matches from parse(). - * - * @return string A token indicating the TOC collection point. - * - */ - - function process(&$matches) - { - $output = $this->addToken(array('type' => 'list_start')); - - foreach ($this->entry as $key => $val) { - - $options = array( - 'type' => 'item_start', - 'count' => $val['count'], - 'level' => $val['level'] - ); - - $output .= $this->addToken($options); - - $output .= $val['text']; - - $output .= $this->addToken(array('type' => 'item_end')); - } - - $output .= $this->addToken(array('type' => 'list_end')); - return $output; - } - - - /** - * - * Finds all headings in the text and saves them in $this->entry. - * - * @access private - * - * @return void - * - */ - - function _getEntries() - { - // the wiki delimiter - $delim = $this->_wiki->delim; - - // list of all TOC entries (h2, h3, etc) - $this->entry = array(); - - // the new source text with TOC entry tokens - $newsrc = ''; - + + + /** + * + * The regular expression used to parse the source text and find + * matches conforming to this rule. Used by the parse() method. + * + * @access public + * + * @var string + * + * @see parse() + * + */ + + var $regex = "/\[\[toc\]\]/m"; + + + /** + * + * The collection of headings (text and levels). + * + * @access public + * + * @var array + * + * @see _getEntries() + * + */ + + var $entry = array(); + + + /** + * + * Custom parsing (have to process heading entries first). + * + * @access public + * + * @see Text_Wiki::parse() + * + */ + + function parse() + { + $this->_getEntries(); + parent::parse(); + } + + + /** + * + * Generates a replacement for the matched text. Token options are: + * + * 'type' => ['list_start'|'list_end'|'item_end'|'item_end'|'target'] + * + * 'level' => The heading level (1-6). + * + * 'count' => Which heading this is in the list. + * + * @access public + * + * @param array &$matches The array of matches from parse(). + * + * @return string A token indicating the TOC collection point. + * + */ + + function process(&$matches) + { + $output = $this->addToken(array('type' => 'list_start')); + + foreach ($this->entry as $key => $val) { + + $options = array( + 'type' => 'item_start', + 'count' => $val['count'], + 'level' => $val['level'] + ); + + $output .= $this->addToken($options); + + $output .= $val['text']; + + $output .= $this->addToken(array('type' => 'item_end')); + } + + $output .= $this->addToken(array('type' => 'list_end')); + return $output; + } + + + /** + * + * Finds all headings in the text and saves them in $this->entry. + * + * @access private + * + * @return void + * + */ + + function _getEntries() + { + // the wiki delimiter + $delim = $this->_wiki->delim; + + // list of all TOC entries (h2, h3, etc) + $this->entry = array(); + + // the new source text with TOC entry tokens + $newsrc = ''; + // when passing through the parsed source text, keep track of when // we are in a delimited section $in_delim = false; @@ -179,38 +179,38 @@ // is the key a start heading token // of level 2 or deeper? if ($rule == 'heading' && - $opts['type'] == 'start' && - $opts['level'] > 1) { - - // yes, add a TOC target link to the - // tokens array... - $token = $this->addToken( - array( - 'type' => 'target', - 'count' => $count, - 'level' => $opts['level'] - ) - ); - - // ... and to the new source, before the - // heading-start token. - $newsrc .= $token . $delim . $key . $delim; - - // retain the toc item - $this->entry[] = array ( - 'count' => $count, - 'level' => $opts['level'], - 'text' => $opts['text'] - ); - - // increase the count for the next entry - $count++; - + $opts['type'] == 'start' && + $opts['level'] > 1) { + + // yes, add a TOC target link to the + // tokens array... + $token = $this->addToken( + array( + 'type' => 'target', + 'count' => $count, + 'level' => $opts['level'] + ) + ); + + // ... and to the new source, before the + // heading-start token. + $newsrc .= $token . $delim . $key . $delim; + + // retain the toc item + $this->entry[] = array ( + 'count' => $count, + 'level' => $opts['level'], + 'text' => $opts['text'] + ); + + // increase the count for the next entry + $count++; + } else { - // not a heading-start of 2 or deeper. - // re-add the delimited token number - // as it was in the original source. - $newsrc .= $delim . $key . $delim; + // not a heading-start of 2 or deeper. + // re-add the delimited token number + // as it was in the original source. + $newsrc .= $delim . $key . $delim; } } else { @@ -221,16 +221,16 @@ } } else { - - // not currently in a delimited section. - // are we starting into a delimited section? + + // not currently in a delimited section. + // are we starting into a delimited section? if ($char == $delim) { - // yes, reset the previous key and - // set the flag. + // yes, reset the previous key and + // set the flag. $key = ''; $in_delim = true; } else { - // no, add to the output as-is + // no, add to the output as-is $newsrc .= $char; } } @@ -239,114 +239,114 @@ // replace with changed source text $this->_wiki->_source = $newsrc; - - /* - // PRIOR VERSION - // has problems mistaking marked-up numbers for delimited tokens - - // creates target tokens, retrieves heading level and text - $this->entry = array(); - $count = 0; - - // loop through all tokens and get headings - foreach ($this->_wiki->_tokens as $key => $val) { - - // only get heading starts of level 2 or deeper - if ($val[0] == 'heading' && - $val[1]['type'] == 'start' && - $val[1]['level'] > 1) { - - // the level of this header - $level = $val[1]['level']; - - // the text of this header - $text = $val[1]['text']; - - // add a toc-target link to the tokens array - $token = $this->addToken( - array( - 'type' => 'target', - 'count' => $count, - 'level' => $level - ) - ); - - // put the toc target token in front of the - // heading-start token - $start = $delim . $key . $delim; - $this->_wiki->_source = str_replace($start, $token.$start, - $this->_wiki->_source); - - // retain the toc item - $this->entry[] = array ( - 'count' => $count, - 'level' => $level, - 'text' => $text - ); - - // increase the count for the next item - $count++; - } - } - */ - } + + /* + // PRIOR VERSION + // has problems mistaking marked-up numbers for delimited tokens + + // creates target tokens, retrieves heading level and text + $this->entry = array(); + $count = 0; + + // loop through all tokens and get headings + foreach ($this->_wiki->_tokens as $key => $val) { + + // only get heading starts of level 2 or deeper + if ($val[0] == 'heading' && + $val[1]['type'] == 'start' && + $val[1]['level'] > 1) { + + // the level of this header + $level = $val[1]['level']; + + // the text of this header + $text = $val[1]['text']; + + // add a toc-target link to the tokens array + $token = $this->addToken( + array( + 'type' => 'target', + 'count' => $count, + 'level' => $level + ) + ); + + // put the toc target token in front of the + // heading-start token + $start = $delim . $key . $delim; + $this->_wiki->_source = str_replace($start, $token.$start, + $this->_wiki->_source); + + // retain the toc item + $this->entry[] = array ( + 'count' => $count, + 'level' => $level, + 'text' => $text + ); + + // increase the count for the next item + $count++; + } + } + */ + } - - /** - * - * Renders a token into text matching the requested format. - * - * @access public - * - * @param array $options The "options" portion of the token (second - * element). - * - * @return string The text rendered from the token options. - * - */ - - function renderXhtml($options) - { - // type, count, level - extract($options); - - // the prefix used for anchor names - $prefix = 'toc'; - - if ($type == 'target') { - // ... generate an anchor. - return "<a name=\"$prefix$count\"></a>"; - } - - if ($type == 'list_start') { - return "<p>\n"; - } - - if ($type == 'list_end') { - return "</p>\n"; - } - - if ($type == 'item_start') { - - // build some indenting spaces for the text - $indent = ($level - 2) * 4; - $pad = str_pad('', $indent); - $pad = str_replace(' ', ' ', $pad); - - // add an extra linebreak in front of heading-2 - // entries (makes for nice section separations) - if ($level <= 2 && $count > 0) { - $pad = "<br />\n$pad"; - } - - // create the entry line as a link to the toc anchor - return "$pad<a href=\"#$prefix$count\">"; - } - - if ($type == 'item_end') { - return "</a><br />\n"; - } - } + + /** + * + * Renders a token into text matching the requested format. + * + * @access public + * + * @param array $options The "options" portion of the token (second + * element). + * + * @return string The text rendered from the token options. + * + */ + + function renderXhtml($options) + { + // type, count, level + extract($options); + + // the prefix used for anchor names + $prefix = 'toc'; + + if ($type == 'target') { + // ... generate an anchor. + return "<a id=\"$prefix$count\"></a>"; + } + + if ($type == 'list_start') { + return "<p>\n"; + } + + if ($type == 'list_end') { + return "</p>\n"; + } + + if ($type == 'item_start') { + + // build some indenting spaces for the text + $indent = ($level - 2) * 4; + $pad = str_pad('', $indent); + $pad = str_replace(' ', ' ', $pad); + + // add an extra linebreak in front of heading-2 + // entries (makes for nice section separations) + if ($level <= 2 && $count > 0) { + $pad = "<br />\n$pad"; + } + + // create the entry line as a link to the toc anchor + return "$pad<a href=\"#$prefix$count\">"; + } + + if ($type == 'item_end') { + return "</a><br />\n"; + } + } } ?> \ No newline at end of file --- NEW FILE: embed.php --- <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2003 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | li...@ph... so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Paul M. Jones <pm...@ci...> | // +----------------------------------------------------------------------+ // // $Id: embed.php,v 1.1 2004/05/25 09:57:54 garvinhicking Exp $ /** * * This class implements a Text_Wiki_Rule to embed the contents of a URL * inside the page at render-time. Typically used to get script output. * This differs from the 'include' rule, which incorporates results at * parse-time; 'embed' output does not get parsed by Text_Wiki, while * 'include' ouput does. * * This rule is inherently not secure; it allows cross-site scripting to * occur if the embedded output has <script> or other similar tags. Be * careful. * * @author Paul M. Jones <pm...@ci...> * * @package Text_Wiki * */ class Text_Wiki_Rule_embed extends Text_Wiki_Rule { /** * * The regular expression used to find source text matching this * rule. * * @access public * * @var string * */ var $regex = '/(\[\[embed )(.+?)(\]\])/i'; /** * * Generates a token entry for the matched text. Token options are: * * 'text' => The full matched text, not including the <code></code> tags. * * @access public * * @param array &$matches The array of matches from parse(). * * @return A delimited token number to be used as a placeholder in * the source text. * */ function process(&$matches) { $options = array('path' => $matches[2]); return $this->addToken($options); } /** * * Renders a token into text matching the requested format. * * @access public * * @param array $options The "options" portion of the token (second * element). * * @return string The text rendered from the token options. * */ function renderXhtml($options) { $file = $this->_conf['base'] . $options['path']; ob_start(); include($file); $output = ob_get_contents(); ob_end_clean(); return $output; } } ?> Index: prefilter.php =================================================================== RCS file: /cvsroot/php-blog/serendipity/bundled-libs/Text/Wiki/Rule/prefilter.php,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- prefilter.php 26 Feb 2004 11:27:54 -0000 1.1 +++ prefilter.php 25 May 2004 09:57:54 -0000 1.2 @@ -60,6 +60,17 @@ // convert tabs to spaces $this->_wiki->_source = str_replace("\t", " ", $this->_wiki->_source); + + // add extra newlines at the top and end; this + // seems to help many rules. + $this->_wiki->_source = "\n" . $this->_wiki->_source . "\n\n"; + + // finally, compress all instances of 3 or more newlines + // down to two newlines. + $find = "/\n{3,}/m"; + $replace = "\n\n"; + $this->_wiki->_source = preg_replace($find, $replace, + $this->_wiki->_source); } } Index: phplookup.php =================================================================== RCS file: /cvsroot/php-blog/serendipity/bundled-libs/Text/Wiki/Rule/phplookup.php,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- phplookup.php 26 Feb 2004 11:27:54 -0000 1.1 +++ phplookup.php 25 May 2004 09:57:54 -0000 1.2 @@ -46,7 +46,7 @@ * */ - var $regex = "/\<php\:({*?.*}*?)\>/U"; + var $regex = "/\[\[php (.+?)\]\]/"; /** Index: blockquote.php =================================================================== RCS file: /cvsroot/php-blog/serendipity/bundled-libs/Text/Wiki/Rule/blockquote.php,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- blockquote.php 26 Feb 2004 11:27:54 -0000 1.1 +++ blockquote.php 25 May 2004 09:57:53 -0000 1.2 @@ -115,6 +115,8 @@ // dummy value (boolean true)... array_push($stack, true); + $return .= "\n"; + // ...and add a start token to the return. $return .= $this->addToken( array( @@ -123,7 +125,7 @@ ) ); - $return .= "\n"; + $return .= "\n\n"; } // remove a level? @@ -135,6 +137,8 @@ // and the indent level are the same. array_pop($stack); + $return .= "\n\n"; + $return .= $this->addToken( array ( 'type' => 'end', @@ -146,11 +150,13 @@ } // add the line text. - $return .= $text . "\n"; + $return .= $text; } // the last line may have been indented. go through the stack // and create end-tokens until the stack is empty. + $return .= "\n"; + while (count($stack) > 0) { array_pop($stack); $return .= $this->addToken( @@ -162,7 +168,7 @@ } // we're done! send back the replacement text. - return "\n$return"; + return "$return\n"; } Index: deflist.php =================================================================== RCS file: /cvsroot/php-blog/serendipity/bundled-libs/Text/Wiki/Rule/deflist.php,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- deflist.php 26 Feb 2004 11:27:54 -0000 1.1 +++ deflist.php 25 May 2004 09:57:54 -0000 1.2 @@ -115,7 +115,7 @@ $return .= $this->addToken($options); // done! - return $return; + return "\n" . $return . "\n"; } Index: url.php =================================================================== RCS file: /cvsroot/php-blog/serendipity/bundled-libs/Text/Wiki/Rule/url.php,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- url.php 26 Feb 2004 11:27:54 -0000 1.1 +++ url.php 25 May 2004 09:57:54 -0000 1.2 @@ -70,6 +70,21 @@ var $img_ext = array('.jpg', '.png', '.gif'); + function Text_Wiki_Rule_url(&$obj, $name) + { + parent::Text_Wiki_Rule($obj, $name); + + $this->regex = + "(http:\/\/|https:\/\/|ftp:\/\/|gopher:\/\/|news:\/\/|mailto:)" . // protocols + "(" . + "[^ \\/\"\'{$this->_wiki->delim}]*\\/" . // no spaces, \, /, ", or single quotes; + ")*" . + "[^ \\t\\n\\/\"\'{$this->_wiki->delim}]*" . + "[A-Za-z0-9\\/?=&~_]"; + + } + + /** * * A somewhat complex parsing method to find three different kinds @@ -81,8 +96,6 @@ function parse() { - $this->regex = "(http:\/\/|https:\/\/|ftp:\/\/|gopher:\/\/|news:\/\/|mailto:)([^ \\/\"\']*\\/)*[^ \\t\\n\\/\"\'{$this->_wiki->delim}]*[A-Za-z0-9\\/?=&~_]"; - // ------------------------------------------------------------- // // Described-reference (named) URLs. @@ -123,9 +136,9 @@ // // the regular expression for this kind of URL - $tmp_regex = '/(^|[^A-Za-z])(' . $this->regex . - ')(\$|[^\\/?=&~A-Za-z0-9])/'; - + + $tmp_regex = '/(^|[^A-Za-z])(' . $this->regex . ')(.*?)/'; + // use the standard callback for inline URLs $this->_wiki->_source = preg_replace_callback( $tmp_regex, @@ -279,9 +292,17 @@ $output = "<img src=\"$href\" alt=\"$text\" />"; } else { - + + // allow for alternative targets + if (isset($this->_conf['target']) && + trim($this->_conf['target']) != '') { + $target = 'target="' . $this->_conf['target'] . '"'; + } else { + $target = ''; + } + // generate a regular link (not an image) - $output = "<a href=\"$href\">$text</a>"; + $output = "<a $target href=\"$href\">$text</a>"; // make numbered references look like footnotes if ($type == 'footnote') { Index: table.php =================================================================== RCS file: /cvsroot/php-blog/serendipity/bundled-libs/Text/Wiki/Rule/table.php,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- table.php 26 Feb 2004 11:27:54 -0000 1.1 +++ table.php 25 May 2004 09:57:54 -0000 1.2 @@ -118,10 +118,29 @@ } else { // this cell has content. + + // find the alignment, if any. + if (substr($cell[$i], 0, 2) == '> ') { + // set to right-align and strip the tag + $align = 'right'; + $cell[$i] = substr($cell[$i], 2); + } elseif (substr($cell[$i], 0, 2) == '= ') { + // set to center-align and strip the tag + $align = 'center'; + $cell[$i] = substr($cell[$i], 2); + } elseif (substr($cell[$i], 0, 2) == '< ') { + // set to left-align and strip the tag + $align = 'left'; + $cell[$i] = substr($cell[$i], 2); + } else { + $align = null; + } + // start a new cell... $return .= $this->addToken( array ( 'type' => 'cell_start', + 'align' => $align, 'colspan' => $span ) ); @@ -133,6 +152,7 @@ $return .= $this->addToken( array ( 'type' => 'cell_end', + 'align' => $align, 'colspan' => $span ) ); @@ -152,7 +172,7 @@ $return .= $this->addToken(array('type' => 'table_end')); // we're done! - return "\n$return"; + return "\n$return\n"; } @@ -171,15 +191,26 @@ function renderXhtml($options) { - // make nice variable names (type, colspan) + // make nice variable names (type, align, colspan) extract($options); $pad = ' '; + $border = (isset($this->_conf['border'])) + ? $this->_conf['border'] : '1'; + + $spacing = (isset($this->_conf['spacing'])) + ? $this->_conf['spacing'] : '0'; + + $padding = (isset($this->_conf['padding'])) + ? $this->_conf['padding'] : '4'; + switch ($type) { case 'table_start': - return "<table border=\"1\" cellspacing=\"0\" cellpadding=\"4\">\n"; + return "<table border=\"$border\" " . + "cellspacing=\"$spacing\" " . + "cellpadding=\"$padding\">\n"; break; case 'table_end': @@ -195,11 +226,14 @@ break; case 'cell_start': + $tmp = $pad . $pad . '<td'; if ($colspan > 1) { - return "$pad$pad<td colspan=\"$colspan\">"; - } else { - return "$pad$pad<td>"; + $tmp .= " colspan=\"$colspan\""; } + if ($align) { + $tmp .= " align=\"$align\""; + } + return $tmp . '>'; break; case 'cell_end': --- NEW FILE: image.php --- <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2003 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available through the world-wide-web at | // | http://www.php.net/license/2_02.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | li...@ph... so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Paul M. Jones <pm...@ci...> | // +----------------------------------------------------------------------+ // // $Id: image.php,v 1.1 2004/05/25 09:57:54 garvinhicking Exp $ /** * * This class implements a Text_Wiki_Rule to embed the contents of a URL * inside the page. Typically used to get script output. * * This rule is inherently not secure; it allows cross-site scripting to * occur if the embedded output has <script> or other similar tags. Be * careful. * * In the future, we'll add a rule config options to set the base embed * path so that it is limited to one directory. * * @author Paul M. Jones <pm...@ci...> * * @package Text_Wiki * */ class Text_Wiki_Rule_image extends Text_Wiki_Rule { /** * * The regular expression used to find source text matching this * rule. * * @access public * * @var string * */ var $regex = '/(\[\[image )(.+?)(\]\])/i'; /** * * Generates a token entry for the matched text. Token options are: * * 'src' => The image source, typically a relative path name. * * 'opts' => Any macro options following the source. * * @access public * * @param array &$matches The array of matches from parse(). * * @return A delimited token number to be used as a placeholder in * the source text. * */ function process(&$matches) { $pos = strpos($matches[2], ' '); if ($pos === false) { $options = array( 'src' => $matches[2], 'args' => array()); } else { // everything after the space is macro options $options = array( 'src' => substr($matches[2], 0, $pos), 'args' => $this->getMacroArgs(substr($matches[2], $pos+1)) ); } return $this->addToken($options); } /** * * Renders a token into text matching the requested format. * * @access public * * @param array $options The "options" portion of the token (second * element). * * @return string The text rendered from the token options. * */ function renderXhtml($options) { $src = '"' . $this->_conf['base'] . $options['src'] . '"'; if (isset($options['args']['link'])) { // this image has a wikilink $href = $this->_wiki->getRuleConf('wikilink', 'view_url') . $options['args']['link']; } else { // image is not linked $href = null; } // unset these so they don't show up as attributes unset($options['args']['src']); unset($options['args']['link']); $attr = ''; foreach ($options['args'] as $key => $val) { $attr .= "$key=\"$val\" "; } if ($href) { return "<a href=\"$href\"><img src=$src $attr/></a>"; } else { return "<img src=$src $attr/>"; } } } ?> Index: wikilink.php =================================================================== RCS file: /cvsroot/php-blog/serendipity/bundled-libs/Text/Wiki/Rule/wikilink.php,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- wikilink.php 26 Feb 2004 11:27:54 -0000 1.1 +++ wikilink.php 25 May 2004 09:57:54 -0000 1.2 @@ -27,6 +27,9 @@ * Wiki page names are typically in StudlyCapsStyle made of * WordsSmashedTogether. * +* You can also create described links to pages in this style: +* [WikiPageName nice text link to use for display] +* * @author Paul M. Jones <pm...@ci...> * * @package Text_Wiki @@ -54,38 +57,61 @@ parent::Text_Wiki_Rule($obj, $name); $this->regex = - "/" . // START regex - "(^|[^A-Za-z])" . // - "(!?" . // START WikiPage pattern - "[A-Z\xc0-\xde]" . // 1 upper - "[A-Za-z\xc0-\xfe]*" . // 0 or more alpha - "[a-z\xdf-\xfe]+" . // 1 or more lower - "[A-Z\xc0-\xde]" . // 1 upper - "[A-Za-z\xc0-\xfe]*" . // 0 or more alpha - '(\\/' . // start subpattern - "[A-Z\xc0-\xde]" . // 1 upper - "[A-Za-z\xc0-\xfe]*" . // 0 or more alpha - ')?' . // end subpattern 0 or 1 - ")" . // END WikiPage pattern - "((\#" . // START Anchor pattern - "[A-Za-z]" . // 1 alpha - "(" . // start sub pattern - "[-A-Za-z0-9_:.]*" . // 0+ dash, alpha, digit, underscore - "[-A-Za-z0-9_]" . // 1 dash, alpha, digit, or underscore - ")?" . // end subpattern 0 or 1 - ")?)" . // END Anchor pattern - "(\"\")?" . // - "/"; // END regex + "(!?" . // START WikiPage pattern (1) + "[A-Z]" . // 1 upper + "[A-Za-z]*" . // 0+ alpha + "[a-z]+" . // 1+ lower + "[A-Z]" . // 1 upper + "[A-Za-z]*" . // 0+ or more alpha + ")" . // END WikiPage pattern (/1) + "((\#" . // START Anchor pattern (2)(3) + "[A-Za-z]" . // 1 alpha + "(" . // start sub pattern (4) + "[-A-Za-z0-9_:.]*" . // 0+ dash, alpha, digit, underscore, colon, dot + "[-A-Za-z0-9_]" . // 1 dash, alpha, digit, or underscore + ")?)?)"; // end subpatterns (/4)(/3)(/2) } /** * - * Generates a replacement for the matched text. Token options are: + * First parses for described links, then for standalone links. * - * 'page' => the wiki page name (e.g., HomePage). + * @access public * - * 'anchor' => a named anchor on the target wiki page + * @return void + * + */ + + function parse() + { + // described wiki links + $tmp_regex = '/\[' . $this->regex . ' (.+?)\]/'; + $this->_wiki->_source = preg_replace_callback( + $tmp_regex, + array(&$this, 'processDescr'), + $this->_wiki->_source + ); + + // standalone wiki links + $tmp_regex = '/(^|[^A-Za-z0-9\-_])' . $this->regex . '/'; + $this->_wiki->_source = preg_replace_callback( + $tmp_regex, + array(&$this, 'process'), + $this->_wiki->_source + ); + } + + + /** + * + * Generates a replacement for described links. Token options are: + * + * 'page' => the wiki page name. + * + * 'text' => the displayed link text. + * + * 'anchor' => a named anchor on the target wiki page. * * @access public * @@ -96,19 +122,52 @@ * */ + function processDescr(&$matches) + { + // set the options + $options = array( + 'page' => $matches[1], + 'text' => $matches[5], + 'anchor' => $matches[3] + ); + + // create and return the replacement token and preceding text + return $this->addToken($options); // . $matches[7]; + } + + + /** + * + * Generates a replacement for standalone links. Token options are: + * + * 'page' => the wiki page name. + * + * 'text' => the displayed link text. + * + * 'anchor' => a named anchor on the target wiki page. + * + * @access public + * + * @param array &$matches The array of matches from parse(). + * + * @return A delimited token to be used as a placeholder in + * the source text, plus any text prior to the match. + * + */ + function process(&$matches) { // when prefixed with !, it's explicitly not a wiki link. // return everything as it was. if ($mat... [truncated message content] |