heading.php 3.6 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: heading.php,v 1.1 2004/01/04 01:35:23 pmjones Exp $
  20. /**
  21. *
  22. * This class implements a Text_Wiki_Rule to find source text marked to
  23. * be a heading element, as defined by text on a line by itself prefixed
  24. * with a number of plus signs (+). The heading text itself is left in
  25. * the source, but is prefixed and suffixed with delimited tokens marking
  26. * the start and end of the heading.
  27. *
  28. * @author Paul M. Jones <pmjones@ciaweb.net>
  29. *
  30. * @package Text_Wiki
  31. *
  32. */
  33. class Text_Wiki_Rule_heading extends Text_Wiki_Rule {
  34. /**
  35. *
  36. * The regular expression used to parse the source text and find
  37. * matches conforming to this rule. Used by the parse() method.
  38. *
  39. * @access public
  40. *
  41. * @var string
  42. *
  43. * @see parse()
  44. *
  45. */
  46. var $regex = '/^(\+{1,6}) (.*)/m';
  47. /**
  48. *
  49. * Generates a replacement for the matched text. Token options are:
  50. *
  51. * 'type' => ['start'|'end'] The starting or ending point of the
  52. * heading text. The text itself is left in the source.
  53. *
  54. * @access public
  55. *
  56. * @param array &$matches The array of matches from parse().
  57. *
  58. * @return string A pair of delimited tokens to be used as a
  59. * placeholder in the source text surrounding the heading text.
  60. *
  61. */
  62. function process(&$matches)
  63. {
  64. $start = $this->addToken(
  65. array(
  66. 'type' => 'start',
  67. 'level' => strlen($matches[1]),
  68. 'text' => $matches[2]
  69. )
  70. );
  71. $end = $this->addToken(
  72. array(
  73. 'type' => 'end',
  74. 'level' => strlen($matches[1])
  75. )
  76. );
  77. return $start . $matches[2] . $end;
  78. }
  79. /**
  80. *
  81. * Renders a token into text matching the requested format.
  82. *
  83. * @access public
  84. *
  85. * @param array $options The "options" portion of the token (second
  86. * element).
  87. *
  88. * @return string The text rendered from the token options.
  89. *
  90. */
  91. function renderXhtml($options)
  92. {
  93. // get nice variable names (type, level)
  94. extract($options);
  95. if ($type == 'start') {
  96. return "<h$level>";
  97. }
  98. if ($type == 'end') {
  99. return "</h$level>\n";
  100. }
  101. }
  102. }
  103. ?>