phpcode.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Paul M. Jones <pmjones@ciaweb.net> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: phpcode.php,v 1.1 2004/01/04 01:35:23 pmjones Exp $
  20. /**
  21. *
  22. * This class implements a Text_Wiki_Rule to find sections marked as code
  23. * examples. Blocks are marked as the string <code> on a line by itself,
  24. * followed by the inline code example, and terminated with the string
  25. * </code> on a line by itself. The code example is run through the
  26. * native PHP highlight_string() function to colorize it, then surrounded
  27. * with <pre>...</pre> tags when rendered as XHTML.
  28. *
  29. * @author Paul M. Jones <pmjones@ciaweb.net>
  30. *
  31. * @package Text_Wiki
  32. *
  33. */
  34. class Text_Wiki_Rule_phpcode extends Text_Wiki_Rule {
  35. /**
  36. *
  37. * The regular expression used to find source text matching this
  38. * rule.
  39. *
  40. * @access public
  41. *
  42. * @var string
  43. *
  44. */
  45. var $regex = '/^(\<php\>)\n(.+)\n(\<\/php\>)(\s|$)/Umsi';
  46. /**
  47. *
  48. * Generates a token entry for the matched text. Token options are:
  49. *
  50. * 'text' => The full matched text, not including the <code></code> tags.
  51. *
  52. * @access public
  53. *
  54. * @param array &$matches The array of matches from parse().
  55. *
  56. * @return A delimited token number to be used as a placeholder in
  57. * the source text.
  58. *
  59. */
  60. function process(&$matches)
  61. {
  62. $options = array('text' => $matches[2]);
  63. return $this->addToken($options) . $matches[4];
  64. }
  65. /**
  66. *
  67. * Renders a token into text matching the requested format.
  68. *
  69. * @access public
  70. *
  71. * @param array $options The "options" portion of the token (second
  72. * element).
  73. *
  74. * @return string The text rendered from the token options.
  75. *
  76. */
  77. function renderXhtml($options)
  78. {
  79. // add the PHP tags
  80. $text = "<?php\n" . $options['text'] . "\n?>"; // <?php
  81. // convert tabs to four spaces
  82. $text = str_replace("\t", " ", $text);
  83. // colorize the code block (also converts HTML entities and adds
  84. // <code>...</code> tags)
  85. ob_start();
  86. highlight_string($text);
  87. $text = ob_get_contents();
  88. ob_end_clean();
  89. // replace <br /> tags with simple newlines
  90. $text = str_replace("<br />", "\n", $text);
  91. // replace non-breaking space with simple spaces
  92. $text = str_replace("&nbsp;", " ", $text);
  93. // get rid of the last newline inside the code block
  94. // (becuase higlight_string puts one there)
  95. if (substr($text, -8) == "\n</code>") {
  96. $text = substr($text, 0, -8) . "</code>";
  97. }
  98. return "\n<pre>$text</pre>\n";
  99. }
  100. }
  101. ?>