| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * |
| 5 | * Parses for wiki freelink text. |
| 6 | * |
| 7 | * @category Text |
| 8 | * |
| 9 | * @package Text_Wiki |
| 10 | * |
| 11 | * @author Paul M. Jones <pmjones@php.net> |
| 12 | * |
| 13 | * @license LGPL |
| 14 | * |
| 15 | * @version $Id: Freelink.php,v 1.3 2005/02/23 17:38:29 pmjones Exp $ |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | /** |
| 20 | * |
| 21 | * Parses for freelinked page links. |
| 22 | * |
| 23 | * This class implements a Text_Wiki_Parse to find source text marked as a |
| 24 | * wiki freelink, and automatically create a link to that page. |
| 25 | * |
| 26 | * A freelink is any page name not conforming to the standard |
| 27 | * StudlyCapsStyle for a wiki page name. For example, a page normally |
| 28 | * named MyHomePage can be renamed and referred to as ((My Home Page)) -- |
| 29 | * note the spaces in the page name. You can also make a "nice-looking" |
| 30 | * link without renaming the target page; e.g., ((MyHomePage|My Home |
| 31 | * Page)). Finally, you can use named anchors on the target page: |
| 32 | * ((MyHomePage|My Home Page#Section1)). |
| 33 | * |
| 34 | * @category Text |
| 35 | * |
| 36 | * @package Text_Wiki |
| 37 | * |
| 38 | * @author Paul M. Jones <pmjones@php.net> |
| 39 | * |
| 40 | */ |
| 41 | |
| 42 | class Text_Wiki_Parse_Freelink extends Text_Wiki_Parse { |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * |
| 47 | * Constructor. We override the Text_Wiki_Parse constructor so we can |
| 48 | * explicitly comment each part of the $regex property. |
| 49 | * |
| 50 | * @access public |
| 51 | * |
| 52 | * @param object &$obj The calling "parent" Text_Wiki object. |
| 53 | * |
| 54 | */ |
| 55 | |
| 56 | function Text_Wiki_Parse_Freelink(&$obj) |
| 57 | { |
| 58 | parent::Text_Wiki_Parse($obj); |
| 59 | |
| 60 | $this->regex = |
| 61 | '/' . // START regex |
| 62 | "\\(\\(" . // double open-parens |
| 63 | "(" . // START freelink page patter |
| 64 | "[-A-Za-z0-9 _+\\/.,;:!?'\"\\[\\]\\{\\}&\xc0-\xff]+" . // 1 or more of just about any character |
| 65 | ")" . // END freelink page pattern |
| 66 | "(" . // START display-name |
| 67 | "\|" . // a pipe to start the display name |
| 68 | "[-A-Za-z0-9 _+\\/.,;:!?'\"\\[\\]\\{\\}&\xc0-\xff]+" . // 1 or more of just about any character |
| 69 | ")?" . // END display-name pattern 0 or 1 |
| 70 | "(" . // START pattern for named anchors |
| 71 | "\#" . // a hash mark |
| 72 | "[A-Za-z]" . // 1 alpha |
| 73 | "[-A-Za-z0-9_:.]*" . // 0 or more alpha, digit, underscore |
| 74 | ")?" . // END named anchors pattern 0 or 1 |
| 75 | "()\\)\\)" . // double close-parens |
| 76 | '/'; // END regex |
| 77 | } |
| 78 | |
| 79 | |
| 80 | /** |
| 81 | * |
| 82 | * Generates a replacement for the matched text. Token options are: |
| 83 | * |
| 84 | * 'page' => the wiki page name (e.g., HomePage). |
| 85 | * |
| 86 | * 'text' => alternative text to be displayed in place of the wiki |
| 87 | * page name. |
| 88 | * |
| 89 | * 'anchor' => a named anchor on the target wiki page |
| 90 | * |
| 91 | * @access public |
| 92 | * |
| 93 | * @param array &$matches The array of matches from parse(). |
| 94 | * |
| 95 | * @return A delimited token to be used as a placeholder in |
| 96 | * the source text, plus any text priot to the match. |
| 97 | * |
| 98 | */ |
| 99 | |
| 100 | function process(&$matches) |
| 101 | { |
| 102 | // use nice variable names |
| 103 | $page = $matches[1]; |
| 104 | $text = $matches[2]; |
| 105 | |
| 106 | // get rid of the leading # from the anchor, if any |
| 107 | $anchor = substr($matches[3], 1); |
| 108 | |
| 109 | // is the page given a new text appearance? |
| 110 | if (trim($text) == '') { |
| 111 | // no |
| 112 | $text = $page; |
| 113 | } else { |
| 114 | // yes, strip the leading | character |
| 115 | $text = substr($text, 1); |
| 116 | } |
| 117 | |
| 118 | // set the options |
| 119 | $options = array( |
| 120 | 'page' => $page, |
| 121 | 'text' => $text, |
| 122 | 'anchor' => $anchor |
| 123 | ); |
| 124 | |
| 125 | // return a token placeholder |
| 126 | return $this->wiki->addToken($this->rule, $options); |
| 127 | } |
| 128 | } |
| 129 | ?> |