Wikilink.php 5.11 KB
Newer Older
zYne's avatar
zYne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php

/**
*
* Parse for links to wiki pages.
*
* @category Text
*
* @package Text_Wiki
*
* @author Paul M. Jones <pmjones@php.net>
*
* @license LGPL
*
15
* @version $Id: Wikilink.php,v 1.9 2006/12/08 08:23:51 justinpatrin Exp $
zYne's avatar
zYne committed
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
*
*/

/**
*
* Parse for links to wiki pages.
*
* 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]
*
* The token options for this rule are:
*
* 'page' => the wiki page name.
*
* 'text' => the displayed link text.
*
* 'anchor' => a named anchor on the target wiki page.
*
* @category Text
*
* @package Text_Wiki
*
* @author Paul M. Jones <pmjones@php.net>
*
*/

class Text_Wiki_Parse_Wikilink extends Text_Wiki_Parse {

    var $conf = array (
48 49
                       'ext_chars' => false,
                       'utf-8' => false
zYne's avatar
zYne committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
    );

    /**
    *
    * Constructor.
    *
    * We override the Text_Wiki_Parse constructor so we can
    * explicitly comment each part of the $regex property.
    *
    * @access public
    *
    * @param object &$obj The calling "parent" Text_Wiki object.
    *
    */

    function Text_Wiki_Parse_Wikilink(&$obj)
    {
        parent::Text_Wiki_Parse($obj);

69 70 71 72 73
        if ($this->getConf('utf-8')) {
			$upper = 'A-Z\p{Lu}';
			$lower = 'a-z0-9\p{Ll}';
			$either = 'A-Za-z0-9\p{L}';
        } else if ($this->getConf('ext_chars')) {
zYne's avatar
zYne committed
74 75 76
        	// use an extended character set; this should
        	// allow for umlauts and so on.  taken from the
        	// Tavi project defaults.php file.
77 78 79
			$upper = 'A-Z\xc0-\xde';
			$lower = 'a-z0-9\xdf-\xfe';
			$either = 'A-Za-z0-9\xc0-\xfe';
zYne's avatar
zYne committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
		} else {
			// the default character set, should be fine
			// for most purposes.
			$upper = "A-Z";
			$lower = "a-z0-9";
			$either = "A-Za-z0-9";
		}

        // build the regular expression for finding WikiPage names.
        $this->regex =
            "(!?" .            // START WikiPage pattern (1)
            "[$upper]" .       // 1 upper
            "[$either]*" .     // 0+ alpha or digit
            "[$lower]+" .      // 1+ lower or digit
            "[$upper]" .       // 1 upper
            "[$either]*" .     // 0+ or more alpha or digit
            ")" .              // END WikiPage pattern (/1)
            "((\#" .           // START Anchor pattern (2)(3)
            "[$either]" .      // 1 alpha
            "(" .              // start sub pattern (4)
            "[-_$either:.]*" . // 0+ dash, alpha, digit, underscore, colon, dot
            "[-_$either]" .    // 1 dash, alpha, digit, or underscore
            ")?)?)";           // end subpatterns (/4)(/3)(/2)
    }


    /**
    *
    * First parses for described links, then for standalone links.
    *
    * @access public
    *
    * @return void
    *
    */

    function parse()
    {
        // described wiki links
119
        $tmp_regex = '/\[' . $this->regex . ' (.+?)\]/'.($this->getConf('utf-8') ? 'u' : '');
zYne's avatar
zYne committed
120 121 122 123 124 125 126
        $this->wiki->source = preg_replace_callback(
            $tmp_regex,
            array(&$this, 'processDescr'),
            $this->wiki->source
        );

        // standalone wiki links
127 128 129
        if ($this->getConf('utf-8')) {
			$either = 'A-Za-z0-9\p{L}';
        } else if ($this->getConf('ext_chars')) {
zYne's avatar
zYne committed
130 131 132 133 134
			$either = "A-Za-z0-9\xc0-\xfe";
		} else {
			$either = "A-Za-z0-9";
		}

135
        $tmp_regex = "/(^|[^{$either}\-_]){$this->regex}/".($this->getConf('utf-8') ? 'u' : '');
zYne's avatar
zYne committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
        $this->wiki->source = preg_replace_callback(
            $tmp_regex,
            array(&$this, 'process'),
            $this->wiki->source
        );
    }


    /**
    *
    * Generate a replacement for described links.
    *
    * @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 priot to the match.
    *
    */

    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->wiki->addToken($this->rule, $options); // . $matches[7];
    }


    /**
    *
    * Generate a replacement for standalone links.
    *
    *
    * @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 ($matches[2]{0} == '!') {
            return $matches[1] . substr($matches[2], 1) . $matches[3];
        }

        // set the options
        $options = array(
            'page' => $matches[2],
            'text' => $matches[2] . $matches[3],
            'anchor' => $matches[3]
        );

        // create and return the replacement token and preceding text
        return $matches[1] . $this->wiki->addToken($this->rule, $options);
    }
}
?>