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

Source for file Parse.php

Documentation is available at Parse.php

  1. <?php
  2.  
  3. /**
  4. * Baseline rule class for extension into a "real" parser component.
  5. * Text_Wiki_Rule classes do not stand on their own; they are called by a
  6. * Text_Wiki object, typcially in the transform()method. Each rule class
  7. * performs three main activities: parse, process, and render.
  8. * The parse() method takes a regex and applies it to the whole block of
  9. * source text at one time. Each match is sent as $matches to the
  10. * process() method.
  11. * The process() method acts on the matched text from the source, and
  12. * then processes the source text is some way.  This may mean the
  13. * creation of a delimited token using addToken().  In every case, the
  14. * process() method returns the text that should replace the matched text
  15. * from parse().
  16. @author Paul M. Jones <pmjones@ciaweb.net>
  17. @package Text_Wiki
  18. *  $Id: Parse.php,v 1.2 2004/09/25 19:05:13 pmjones Exp $
  19. */
  20.  
  21. class Text_Wiki_Parse {
  22.     
  23.     
  24.     /**
  25.     * 
  26.     * Configuration options for this parser rule.
  27.     * 
  28.     * @access public
  29.     * 
  30.     * @var string 
  31.     * 
  32.     */
  33.     
  34.     var $conf = array();
  35.     
  36.     
  37.     /**
  38.     * 
  39.     * Regular expression to find matching text for this rule.
  40.     * 
  41.     * @access public
  42.     * 
  43.     * @var string 
  44.     * 
  45.     * @see parse()
  46.     * 
  47.     */
  48.     
  49.     var $regex = null;
  50.     
  51.     
  52.     /**
  53.     * 
  54.     * The name of this rule for new token array elements.
  55.     * 
  56.     * @access public
  57.     * 
  58.     * @var string 
  59.     * 
  60.     */
  61.     
  62.     var $rule = null;
  63.     
  64.     
  65.     /**
  66.     * 
  67.     * A reference to the calling Text_Wiki object.
  68.     * 
  69.     * This is needed so that each rule has access to the same source
  70.     * text, token set, URLs, interwiki maps, page names, etc.
  71.     * 
  72.     * @access public
  73.     * 
  74.     * @var object 
  75.     */
  76.     
  77.     var $wiki = null;
  78.     
  79.     
  80.     /**
  81.     * 
  82.     * Constructor for this parser rule.
  83.     * 
  84.     * @access public
  85.     * 
  86.     * @param object &$obj The calling "parent" Text_Wiki object.
  87.     * 
  88.     */
  89.     
  90.     function Text_Wiki_Parse(&$obj)
  91.     {
  92.         // set the reference to the calling Text_Wiki object;
  93.         // this allows us access to the shared source text, token
  94.         // array, etc.
  95.         $this->wiki =$obj;
  96.         
  97.         // set the name of this rule; generally used when adding
  98.         // to the tokens array. strip off the Text_Wiki_Parse_ portion.
  99.         // text_wiki_parse_
  100.         // 0123456789012345
  101.         $tmp substr(get_class($this)16);
  102.         $this->rule = ucwords(strtolower($tmp));
  103.         
  104.         // override config options for the rule if specified
  105.         if (isset($this->wiki->parseConf[$this->rule]&&
  106.             is_array($this->wiki->parseConf[$this->rule])) {
  107.             
  108.             $this->conf = array_merge(
  109.                 $this->conf,
  110.                 $this->wiki->parseConf[$this->rule]
  111.             );
  112.             
  113.         }
  114.     }
  115.     
  116.     
  117.     /**
  118.     * 
  119.     * Abstrct method to parse source text for matches.
  120.     *
  121.     * Applies the rule's regular expression to the source text, passes
  122.     * every match to the process() method, and replaces the matched text
  123.     * with the results of the processing.
  124.     *
  125.     * @access public
  126.     * 
  127.     * @see Text_Wiki_Parse::process()
  128.     * 
  129.     */
  130.     
  131.     function parse()
  132.     {
  133.         $this->wiki->source = preg_replace_callback(
  134.             $this->regex,
  135.             array(&$this'process'),
  136.             $this->wiki->source
  137.         );
  138.     }
  139.     
  140.     
  141.     /**
  142.     * 
  143.     * Abstract method to generate replacements for matched text.
  144.     * 
  145.     * @access public
  146.     * 
  147.     * @param array $matches An array of matches from the parse() method
  148.     *  as generated by preg_replace_callback.  $matches[0] is the full
  149.     *  matched string, $matches[1] is the first matched pattern,
  150.     *  $matches[2] is the second matched pattern, and so on.
  151.     * 
  152.     * @return string The processed text replacement; defaults to the
  153.     *  full matched string (i.e., no changes to the text).
  154.     * 
  155.     * @see Text_Wiki_Parse::parse()
  156.     * 
  157.     */
  158.     
  159.     function process(&$matches)
  160.     {
  161.         return $matches[0];
  162.     }
  163.     
  164.     
  165.     /**
  166.     * 
  167.     * Simple method to safely get configuration key values.
  168.     * 
  169.     * @access public
  170.     * 
  171.     * @param string $key The configuration key.
  172.     * 
  173.     * @param mixed $default If the key does not exist, return this value
  174.     *  instead.
  175.     * 
  176.     * @return mixed The configuration key value (if it exists) or the
  177.     *  default value (if not).
  178.     * 
  179.     */
  180.     
  181.     function getConf($key$default = null)
  182.     {
  183.         if (isset($this->conf[$key])) {
  184.             return $this->conf[$key];
  185.         else {
  186.             return $default;
  187.         }
  188.     }
  189.     
  190.     
  191.     /**
  192.     * 
  193.     * Extract 'attribute="value"' portions of wiki markup.
  194.     *
  195.     * This kind of markup is typically used only in macros, but is useful
  196.     * anywhere.
  197.     * 
  198.     * The syntax is pretty strict; there can be no spaces between the
  199.     * option name, the equals, and the first double-quote; the value
  200.     * must be surrounded by double-quotes.  You can escape characters in
  201.     * the value with a backslash, and the backslash will be stripped for
  202.     * you.
  203.     * 
  204.     * @access public
  205.     * 
  206.     * @param string $text The "attributes" portion of markup.
  207.     * 
  208.     * @return array An associative array of key-value pairs where the
  209.     *  key is the option name and the value is the option value.
  210.     * 
  211.     */
  212.     
  213.     function getAttrs($text)
  214.     {
  215.         // find the =" sections;
  216.         $tmp explode('="'trim($text));
  217.         
  218.         // basic setup
  219.         $k count($tmp- 1;
  220.         $attrs = array();
  221.         $key = null;
  222.         
  223.         // loop through the sections
  224.         foreach ($tmp as $i => $val{
  225.             
  226.             // first element is always the first key
  227.             if ($i == 0{
  228.                 $key trim($val);
  229.                 continue;
  230.             }
  231.             
  232.             // find the last double-quote in the value.
  233.             // the part to the left is the value for the last key,
  234.             // the part to the right is the next key name
  235.             $pos strrpos($val'"');
  236.             $attrs[$keystripslashes(substr($val0$pos));
  237.             $key trim(substr($val$pos+1));
  238.             
  239.         }
  240.         
  241.         return $attrs;
  242.         
  243.     }
  244. }
  245. ?>

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