| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- <?php
- /* vim: set expandtab tabstop=4 shiftwidth=4: */
- // +----------------------------------------------------------------------+
- // | PHP version 4 |
- // +----------------------------------------------------------------------+
- // | Copyright (c) 1997-2003 The PHP Group |
- // +----------------------------------------------------------------------+
- // | This source file is subject to version 2.0 of the PHP license, |
- // | that is bundled with this package in the file LICENSE, and is |
- // | available through the world-wide-web at |
- // | http://www.php.net/license/2_02.txt. |
- // | If you did not receive a copy of the PHP license and are unable to |
- // | obtain it through the world-wide-web, please send a note to |
- // | license@php.net so we can mail you a copy immediately. |
- // +----------------------------------------------------------------------+
- // | Authors: Paul M. Jones <pmjones@ciaweb.net> |
- // +----------------------------------------------------------------------+
- //
- // $Id: url.php,v 1.2 2004/01/31 15:52:08 pmjones Exp $
- /**
- *
- * This class implements a Text_Wiki_Rule to find source text marked as a
- * URL. Various URL markings are supported: inline (the URL by itself),
- * numbered or footnote reference (where the URL is enclosed in square brackets), and
- * named reference (where the URL is enclosed in square brackets and has a
- * name included inside the brackets). E.g.:
- *
- * inline -- http://example.com
- * numbered -- [http://example.com]
- * described -- [http://example.com Example Description]
- *
- * When rendering a URL token, this will convert URLs pointing to a .gif,
- * .jpg, or .png image into an inline <img /> tag (for the 'xhtml'
- * format).
- *
- * @author Paul M. Jones <pmjones@ciaweb.net>
- *
- * @package Text_Wiki
- *
- */
- class Text_Wiki_Rule_url extends Text_Wiki_Rule {
-
-
- /**
- *
- * When doing numbered references (footnote-style references), we
- * need to keep a running count of how many references there are.
- *
- * @access public
- *
- * @var int
- *
- */
-
- var $footnoteCount = 0;
-
-
- /**
- *
- * An array of filename extensions that indicate a file is an image.
- *
- * @access public
- *
- * @var array
- *
- */
-
- var $img_ext = array('.jpg', '.png', '.gif');
-
-
- /**
- *
- * A somewhat complex parsing method to find three different kinds
- * of URLs in the source text.
- *
- * @access public
- *
- */
-
- function parse()
- {
- $this->regex = "(http:|mailto:|https:|ftp:|gopher:|news:)([^ \\/\"\']*\\/)*[^ \\t\\n\\/\"\'{$this->_wiki->delim}]*[A-Za-z0-9\\/?=&~_]";
- // -------------------------------------------------------------
- //
- // Described-reference (named) URLs.
- //
-
- // the regular expression for this kind of URL
- $tmp_regex = '/\[(' . $this->regex . ') ([^\]]+)\]/';
-
- // use a custom callback processing method to generate
- // the replacement text for matches.
- $this->_wiki->_source = preg_replace_callback(
- $tmp_regex,
- array(&$this, 'processDescr'),
- $this->_wiki->_source
- );
-
-
- // -------------------------------------------------------------
- //
- // Numbered-reference (footnote-style) URLs.
- //
-
- // the regular expression for this kind of URL
- $tmp_regex = '/\[(' . $this->regex . ')\]/U';
-
- // use a custom callback processing method to generate
- // the replacement text for matches.
- $this->_wiki->_source = preg_replace_callback(
- $tmp_regex,
- array(&$this, 'processFootnote'),
- $this->_wiki->_source
- );
-
-
- // -------------------------------------------------------------
- //
- // Normal inline URLs.
- //
-
- // the regular expression for this kind of URL
- $tmp_regex = '/(^|[^A-Za-z])(' . $this->regex .
- ')(\$|[^\\/?=&~A-Za-z0-9])/';
- // use the standard callback for inline URLs
- $this->_wiki->_source = preg_replace_callback(
- $tmp_regex,
- array(&$this, 'process'),
- $this->_wiki->_source
- );
- }
-
-
- /**
- *
- * Process inline URLs and return replacement text with a delimited
- * token.
- *
- * Token options are:
- * 'type' => ['inline'|'footnote'|'descr'] the type of URL
- * 'href' => the URL link href portion
- * 'text' => the displayed text of the URL link
- *
- * @param array &$matches
- *
- * @param array $matches An array of matches from the parse() method
- * as generated by preg_replace_callback. $matches[0] is the full
- * matched string, $matches[1] is the first matched pattern,
- * $matches[2] is the second matched pattern, and so on.
- *
- * @return string The processed text replacement.
- *
- */
-
- function process(&$matches)
- {
- // set options
- $options = array(
- 'type' => 'inline',
- 'href' => $matches[2],
- 'text' => $matches[2]
- );
-
- // tokenize
- return $matches[1] . $this->addToken($options) . $matches[5];
- }
-
-
- /**
- *
- * Process numbered (footnote) URLs and return replacement text with
- * a delimited token.
- *
- * Token options are:
- * 'type' => ['inline'|'footnote'|'descr'] the type of URL
- * 'href' => the URL link href portion
- * 'text' => the displayed text of the URL link
- *
- * @param array &$matches
- *
- * @param array $matches An array of matches from the parse() method
- * as generated by preg_replace_callback. $matches[0] is the full
- * matched string, $matches[1] is the first matched pattern,
- * $matches[2] is the second matched pattern, and so on.
- *
- * @return string The processed text replacement.
- *
- */
-
- function processFootnote(&$matches)
- {
- // keep a running count for footnotes
- $this->footnoteCount++;
-
- // set options
- $options = array(
- 'type' => 'footnote',
- 'href' => $matches[1],
- 'text' => $this->footnoteCount
- );
-
- // tokenize
- return $this->addToken($options);
- }
-
-
- /**
- *
- * Process described-reference (named-reference) URLs and return
- * replacement text with a delimited token.
- *
- * Token options are:
- * 'type' => ['inline'|'footnote'|'descr'] the type of URL
- * 'href' => the URL link href portion
- * 'text' => the displayed text of the URL link
- *
- * @param array &$matches
- *
- * @param array $matches An array of matches from the parse() method
- * as generated by preg_replace_callback. $matches[0] is the full
- * matched string, $matches[1] is the first matched pattern,
- * $matches[2] is the second matched pattern, and so on.
- *
- * @return string The processed text replacement.
- *
- */
-
- function processDescr(&$matches)
- {
- // set options
- $options = array(
- 'type' => 'descr',
- 'href' => $matches[1],
- 'text' => $matches[4]
- );
-
- // tokenize
- return $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)
- {
- // create local variables from the options array (text,
- // href, type)
- extract($options);
-
- // find the rightmost dot and determine the filename
- // extension.
- $pos = strrpos($href, '.');
- $ext = strtolower(substr($href, $pos));
-
- // does the filename extension indicate an image file?
- if (in_array($ext, $this->img_ext)) {
-
- // create alt text for the image
- if (! isset($text) || $text == '') {
- $text = basename($href);
- }
-
- // generate an image tag
- $output = "<img src=\"$href\" alt=\"$text\" />";
-
- } else {
-
- // generate a regular link (not an image)
- $output = "<a href=\"$href\">$text</a>";
-
- // make numbered references look like footnotes
- if ($type == 'footnote') {
- $output = '<sup>' . $output . '</sup>';
- }
- }
-
- return $output;
- }
- }
- ?>
|