1<?php
2/**
3 * This parser parses "attributes," which carry meta-information about the
4 * page.  These attributes are in the form [[WikiWord: value]].
5 *
6 * @package Wicked
7 */
8class Text_Wiki_Parse_Attribute extends Text_Wiki_Parse
9{
10    /**
11     * The regular expression used to find source text matching this rule (this
12     * is set in the constructor).
13     *
14     * @var string
15     */
16    public $regex;
17
18    public function __construct(&$obj)
19    {
20        parent::__construct($obj);
21
22        $this->regex = '/((?:\[\[' . Wicked::REGEXP_WIKIWORD .
23                       ':\s+.*?\]\]\s*)+)/';
24    }
25
26    /**
27     * Generates a token entry for the matched text. Token options are:
28     *
29     * 'src'  => The image source, typically a relative path name.
30     * 'opts' => Any macro options following the source.
31     *
32     * @param array &$matches  The array of matches from parse().
33     *
34     * @return  A delimited token number to be used as a placeholder in
35     *          the source text.
36     */
37    public function process(&$matches)
38    {
39        $options = array('attributes' => array());
40
41        $text = $matches[1];
42        while (preg_match('/^\[\[(' . Wicked::REGEXP_WIKIWORD . '):\s+(.*?)\]\]\s*(.*)$/s',
43                          $text, $sub)) {
44
45            $options['attributes'][] = array('name' => $sub[1],
46                                             'value' => $sub[2]);
47            $text = $sub[3];
48        }
49
50        return $this->wiki->addToken($this->rule, $options);
51    }
52}
53