| // +----------------------------------------------------------------------+ // // $Id: wikilink.php,v 1.3 2004/01/31 15:52:08 pmjones Exp $ /** * * This class implements a Text_Wiki_Rule to find source text marked as a * wiki page name and automatically create a link to that page. * * Wiki page names are typically in StudlyCapsStyle made of * WordsSmashedTogether. * * @author Paul M. Jones * * @package Text_Wiki * */ class Text_Wiki_Rule_wikilink extends Text_Wiki_Rule { /** * * Constructor. We override the Text_Wiki_Rule constructor so we can * explicitly comment each part of the $regex property. * * @access public * * @param object &$obj The calling "parent" Text_Wiki object. * * @param string $name The token name to use for this rule. * */ function Text_Wiki_Rule_wikilink(&$obj, $name) { 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 } /** * * Generates a replacement for the matched text. Token options are: * * 'page' => the wiki page name (e.g., HomePage). * * '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 priot 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] . $matches[4]; } // set the options $options = array( 'page' => $matches[2], 'anchor' => substr($matches[4], 1) ); // create and return the replacement token and preceding text return $matches[1] . $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) { // make nice variable names (page, anchor) extract($options); // does the page exist? if (in_array($page, $this->_wiki->pages)) { // yes, link to the page view $href = $this->_wiki->view_url . $page . "#" . $anchor; return "$page"; } else { // no, link to a create-page url $href = $this->_wiki->new_url; return $page . "{$this->_wiki->new_text}"; } } } ?>