Text_Wiki
[ class tree: Text_Wiki ] [ index: Text_Wiki ] [ all elements ]

Source for file Rule.php

Documentation is available at Rule.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at                              |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Paul M. Jones <pmjones@ciaweb.net>                          |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Rule.php,v 1.7 2004/03/27 14:46:41 pmjones Exp $
  20.  
  21. /**
  22. * Baseline rule class for extension into a "real" wiki rule.
  23. * Text_Wiki_Rule classes do not stand on their own; they are called by a
  24. * Text_Wiki object, typcially in the transform()method. Each rule class
  25. * performs three main activities: parse, process, and render.
  26. * The parse() method takes a regex and applies it to the whole block of
  27. * source text at one time. Each match is sent as $matches to the
  28. * process() method.
  29. * The process() method acts on the matched text from the source, and
  30. * then processes the source text is some way.  This may mean the
  31. * creation of a delimited token using addToken().  In every case, the
  32. * process() method returns the text that should replace the matched text
  33. * from parse().
  34. * Finally, the render*() methods take any token created by the rule and
  35. * creates output text matching a specific format.  Individual rules may
  36. * or may not have options for specific formats.
  37. *
  38. * Typically, the extended rule only needs to define the process() and
  39. * render() methods; the parse() method is generally the same from rule
  40. * to rule, and has been included here.  However, there is no reason that
  41. * rules cannot override these methods, so long as the new methods operate
  42. * in substantially the same manner; similarly, the rule need not generate
  43. * any tokens at all, and may use a specialized parse() method to filter
  44. * the source text.  See the default rule classes for many examples.
  45. @author Paul M. Jones <pmjones@ciaweb.net>
  46. @package Text_Wiki
  47. */
  48.  
  49. class Text_Wiki_Rule {
  50.     
  51.     
  52.     /**
  53.     * 
  54.     * The regular expression used to parse the source text and find
  55.     * matches conforming to this rule.  Used by the parse() method.
  56.     * 
  57.     * @access public
  58.     * 
  59.     * @var string 
  60.     * 
  61.     * @see parse()
  62.     * 
  63.     */
  64.     
  65.     var $regex = null;
  66.     
  67.     
  68.     /**
  69.     * 
  70.     * A reference to the calling Text_Wiki object.  This is needed so
  71.     * that each rule has access to the same source text, token set,
  72.     * URLs, interwiki maps, page names, etc.
  73.     * 
  74.     * @access public
  75.     * 
  76.     * @var object 
  77.     */
  78.     
  79.     var $_wiki = null;
  80.     
  81.     
  82.     /**
  83.     * 
  84.     * The name of this rule; used when inserting new token array
  85.     * elements.
  86.     * 
  87.     * @access private
  88.     * 
  89.     * @var string 
  90.     * 
  91.     */
  92.     
  93.     var $_rule = null;
  94.     
  95.     
  96.     /**
  97.     * 
  98.     * Configuration options for this rule.
  99.     * 
  100.     * @access private
  101.     * 
  102.     * @var string 
  103.     * 
  104.     */
  105.     
  106.     var $_conf = null;
  107.     
  108.     
  109.     /**
  110.     * 
  111.     * Constructor for the rule.
  112.     * 
  113.     * @access public
  114.     * 
  115.     * @param object &$obj The calling "parent" Text_Wiki object.
  116.     * 
  117.     * @param string $name The token name to use for this rule.
  118.     * 
  119.     */
  120.     
  121.     function Text_Wiki_Rule(&$obj$name)
  122.     {
  123.         // set the reference to the calling Text_Wiki object;
  124.         // this allows us access to the shared source text, token
  125.         // array, etc.
  126.         $this->_wiki =$obj;
  127.         
  128.         // set the name of this rule; generally used when adding
  129.         // to the tokens array.
  130.         $this->_rule $name;
  131.         
  132.         // config options reference
  133.         $this->_conf =$this->_wiki->rules[$this->_rule]['conf'];
  134.     }
  135.     
  136.     
  137.     /**
  138.     * 
  139.     * Add a token to the Text_Wiki tokens array, and return a delimited
  140.     * token number.
  141.     * 
  142.     * @access public
  143.     * 
  144.     * @param array $options An associative array of options for the new
  145.     *  token array element.  The keys and values are specific to the
  146.     *  rule, and may or may not be common to other rule options.  Typical
  147.     *  options keys are 'text' and 'type' but may include others.
  148.     * 
  149.     * @param boolean $id_only If true, return only the token number, not
  150.     *  a delimited token string.
  151.     * 
  152.     * @return string|intBy default, return the number of the
  153.     *  newly-created token array element with a delimiter prefix and
  154.     *  suffix; however, if $id_only is set to true, return only the token
  155.     *  number (no delimiters).
  156.     * 
  157.     */
  158.     
  159.     function addToken($options = array()$id_only = false)
  160.     {
  161.         // force the options to be an array
  162.         settype($options'array');
  163.         
  164.         // find the next token number
  165.         $id count($this->_wiki->_tokens);
  166.         
  167.         // add the token
  168.         $this->_wiki->_tokens[$id= array(
  169.             0 => $this->_rule,
  170.             1 => $options
  171.         );
  172.         
  173.         // return a value
  174.         if ($id_only{
  175.             // return the last token number
  176.             return $id;
  177.         else {
  178.             // return the token number with delimiters
  179.             return $this->_wiki->delim . $id $this->_wiki->delim;
  180.         }
  181.     }
  182.     
  183.     
  184.     /**
  185.     * 
  186.     * Set or re-set a token with specific information, overwriting any
  187.     * previous rule name and rule options.
  188.     * 
  189.     * @access public
  190.     * 
  191.     * @param int $id The token number to reset.
  192.     * 
  193.     * @param int $rule The rule name to use; by default, use the current
  194.     *  rule name, although you can specify any rule.
  195.     * 
  196.     * @param array $options An associative array of options for the
  197.     *  token array element.  The keys and values are specific to the
  198.     *  rule, and may or may not be common to other rule options.  Typical
  199.     *  options keys are 'text' and 'type' but may include others.
  200.     * 
  201.     * @return void 
  202.     * 
  203.     */
  204.     
  205.     function setToken($id$rule = null$options = array())
  206.     {
  207.         // get a rule name
  208.         if (is_null($rule)) {
  209.             $rule $this->_rule;
  210.         }
  211.         
  212.         // reset the token
  213.         $this->_wiki->_tokens[$id= array(
  214.             0 => $rule,
  215.             1 => $options
  216.         );
  217.     }
  218.     
  219.     
  220.     /**
  221.     * 
  222.     * Simple parsing method to apply the rule's regular expression to
  223.     * the source text, pass every match to the process() method, and
  224.     * replace the matched text with the results of the processing.
  225.     *
  226.     * @access public
  227.     * 
  228.     */
  229.     
  230.     function parse()
  231.     {
  232.         $this->_wiki->_source = preg_replace_callback(
  233.             $this->regex,
  234.             array(&$this'process'),
  235.             $this->_wiki->_source
  236.         );
  237.     }
  238.     
  239.     
  240.     /**
  241.     * 
  242.     * Simple processing mathod to take matched text and generate
  243.     * replacement text. This is one of the methods you will definitely
  244.     * want to override in your rule class extensions.
  245.     * 
  246.     * @access public
  247.     * 
  248.     * @param array $matches An array of matches from the parse() method
  249.     *  as generated by preg_replace_callback.  $matches[0] is the full
  250.     *  matched string, $matches[1] is the first matched pattern,
  251.     *  $matches[2] is the second matched pattern, and so on.
  252.     * 
  253.     * @return string The processed text replacement; defaults to the
  254.     *  full matched string (i.e., no changes to the text).
  255.     * 
  256.     */
  257.     
  258.     function process(&$matches)
  259.     {
  260.         return $matches[0];
  261.     }
  262.     
  263.     
  264.     /**
  265.     * 
  266.     * Simple rendering method to take a set of token options and
  267.     * generate replacement text for it.  This is another method you will
  268.     * definitely want to override in your rule subclass extensions.
  269.     *
  270.     * @access public
  271.     * 
  272.     * @param array $options The "options" portion of the token (second element).
  273.     * 
  274.     * @return string The text rendered from the token options; by default,
  275.     *  no text is returned.  You should change this in your subclass.  ;-)
  276.     * 
  277.     */
  278.     
  279.     function renderXhtml($options)
  280.     {
  281.         return '';
  282.     }
  283.     
  284.     
  285.     /**
  286.     * 
  287.     * Simple method to extract 'option="value"' portions of wiki markup,
  288.     * typically used only in macros.
  289.     * 
  290.     * The syntax is pretty strict; there can be no spaces between the
  291.     * option name, the equals, and the first double-quote; the value
  292.     * must be surrounded by double-quotes.  You can escape characters in
  293.     * the value with a backslash, and the backslash will be stripped for
  294.     * you.
  295.     * 
  296.     * @access public
  297.     * 
  298.     * @param string $text The "macro options" portion of macro markup.
  299.     * 
  300.     * @return array An associative array of key-value pairs where the
  301.     *  key is the option name and the value is the option value.
  302.     * 
  303.     */
  304.     
  305.     function getMacroArgs($text)
  306.     {
  307.         // find the =" sections;
  308.         $tmp explode('="'trim($text));
  309.         
  310.         // basic setup
  311.         $k count($tmp- 1;
  312.         $arg = array();
  313.         $key = null;
  314.         
  315.         // loop through the sections
  316.         foreach ($tmp as $i => $val{
  317.             
  318.             // first element is always the first key
  319.             if ($i == 0{
  320.                 $key trim($val);
  321.                 continue;
  322.             }
  323.             
  324.             // find the last double-quote in the value.
  325.             // the part to the left is the value for the last key,
  326.             // the part to the right is the next key name
  327.             $pos strrpos($val'"');
  328.             $arg[$keystripslashes(substr($val0$pos));
  329.             $key trim(substr($val$pos+1));
  330.             
  331.         }
  332.         
  333.         return $arg;
  334.         
  335.     }
  336. }
  337. ?>

Documentation generated on Mon, 11 Mar 2019 10:14:15 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.