1
0
mirror of synced 2024-12-16 16:16:04 +03:00
doctrine2/vendor/Text/Wiki/Render/Xhtml/Code.php

149 lines
4.9 KiB
PHP
Raw Normal View History

2007-02-23 00:41:25 +03:00
<?php
// vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4:
/**
* Code rule end renderer for Xhtml
*
* PHP versions 4 and 5
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones <pmjones@php.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version CVS: $Id: Code.php,v 1.13 2006/02/10 23:07:03 toggg Exp $
* @link http://pear.php.net/package/Text_Wiki
*/
/**
* This class renders code blocks in XHTML.
*
* @category Text
* @package Text_Wiki
* @author Paul M. Jones <pmjones@php.net>
* @license http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
* @version Release: @package_version@
* @link http://pear.php.net/package/Text_Wiki
*/
class Text_Wiki_Render_Xhtml_Code extends Text_Wiki_Render {
var $conf = array(
'css' => null, // class for <pre>
'css_code' => null, // class for generic <code>
'css_php' => null, // class for PHP <code>
'css_html' => null, // class for HTML <code>
'css_filename' => null // class for optional filename <div>
);
/**
*
* 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 token($options)
{
$text = $options['text'];
$attr = $options['attr'];
$type = strtolower($attr['type']);
$css = $this->formatConf(' class="%s"', 'css');
$css_code = $this->formatConf(' class="%s"', 'css_code');
$css_php = $this->formatConf(' class="%s"', 'css_php');
$css_html = $this->formatConf(' class="%s"', 'css_html');
$css_filename = $this->formatConf(' class="%s"', 'css_filename');
2007-03-05 15:14:42 +03:00
if ($type == 'php') {
2007-02-23 00:41:25 +03:00
if (substr($options['text'], 0, 5) != '<?php') {
// PHP code example:
// add the PHP tags
2007-02-23 22:45:22 +03:00
$text = "<?php
" . $options['text'] . "\n?>"; // <?php
2007-02-23 00:41:25 +03:00
}
// convert tabs to four spaces
2007-02-23 22:45:22 +03:00
$text = str_replace("\t", "&nbsp;&nbsp;%nbsp;&nbsp;", $text);
2007-02-23 00:41:25 +03:00
// colorize the code block (also converts HTML entities and adds
// <code>...</code> tags)
2007-02-23 22:45:22 +03:00
$h = new PHP_Highlight;
$h->loadString($text);
$text = $h->toHtml(true);
2007-02-23 00:41:25 +03:00
// replace <br /> tags with simple newlines.
// replace non-breaking space with simple spaces.
// translate HTML <font> and color to XHTML <span> and style.
// courtesy of research by A. Kalin :-).
2007-02-23 22:45:22 +03:00
/**
2007-02-23 00:41:25 +03:00
$map = array(
'<br />' => "\n",
'&nbsp;' => ' ',
2007-02-23 22:45:22 +03:00
"\n" => "<br \>",
2007-02-23 00:41:25 +03:00
'<font' => '<span',
'</font>' => '</span>',
'color="' => 'style="color:'
);
2007-02-23 22:45:22 +03:00
2007-02-23 00:41:25 +03:00
$text = strtr($text, $map);
2007-02-23 22:45:22 +03:00
*/
2007-02-23 00:41:25 +03:00
// get rid of the last newline inside the code block
// (becuase higlight_string puts one there)
if (substr($text, -8) == "\n</code>") {
$text = substr($text, 0, -8) . "</code>";
}
// replace all <code> tags with classed tags
if ($css_php) {
$text = str_replace('<code>', "<code$css_php>", $text);
}
// done
2007-02-23 22:45:22 +03:00
$text = "<table border=1 class='dashed' cellpadding=0 cellspacing=0>
<tr><td><b>$text
</b></td></tr>
</table>";
2007-02-23 00:41:25 +03:00
} elseif ($type == 'html' || $type == 'xhtml') {
// HTML code example:
// add <html> opening and closing tags,
// convert tabs to four spaces,
// convert entities.
$text = str_replace("\t", " ", $text);
$text = "<html>\n$text\n</html>";
$text = $this->textEncode($text);
$text = "<pre$css><code$css_html>$text</code></pre>";
2007-03-05 15:14:42 +03:00
} elseif ($type == 'sql') {
// HTML code example:
// add <html> opening and closing tags,
// convert tabs to four spaces,
// convert entities.
$text = str_replace("\t", " ", $text);
$text = $this->textEncode($text);
$text = "<div class='sql'>$text</div>";
2007-02-23 00:41:25 +03:00
} else {
// generic code example:
// convert tabs to four spaces,
// convert entities.
$text = str_replace("\t", " ", $text);
$text = $this->textEncode($text);
$text = "<pre$css><code$css_code>$text</code></pre>";
}
if ($css_filename && isset($attr['filename'])) {
$text = "<div$css_filename>" .
$attr['filename'] . '</div>' . $text;
}
return "\n$text\n\n";
}
}
?>