/drupal/sites/default/modules/pearwiki_filter/Text/Wiki/Parse/Tiki/List.php

https://github.com/psoetens/orocos-www · PHP · 250 lines · 91 code · 33 blank · 126 comment · 11 complexity · ff8eaef6bfcd3baa7c73d4a4f26b4319 MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * Parses for bulleted and numbered lists.
  5. *
  6. * @category Text
  7. *
  8. * @package Text_Wiki
  9. *
  10. * @author Justin Patrin <papercrane@reversefold.com>
  11. * @author Paul M. Jones <pmjones@php.net>
  12. *
  13. * @license LGPL
  14. *
  15. * @version $Id: List.php,v 1.1 2005/07/21 20:56:14 justinpatrin Exp $
  16. *
  17. */
  18. /**
  19. *
  20. * Parses for bulleted and numbered lists.
  21. *
  22. * This class implements a Text_Wiki_Parse to find source text marked as
  23. * a bulleted or numbered list. In short, if a line starts with '* ' then
  24. * it is a bullet list item; if a line starts with '# ' then it is a
  25. * number list item. Spaces in front of the * or # indicate an indented
  26. * sub-list. The list items must be on sequential lines, and may be
  27. * separated by blank lines to improve readability. Using a non-* non-#
  28. * non-whitespace character at the beginning of a line ends the list.
  29. *
  30. * @category Text
  31. *
  32. * @package Text_Wiki
  33. *
  34. * @author Justin Patrin <papercrane@reversefold.com>
  35. * @author Paul M. Jones <pmjones@php.net>
  36. *
  37. */
  38. class Text_Wiki_Parse_List extends Text_Wiki_Parse {
  39. /**
  40. *
  41. * The regular expression used to parse the source text and find
  42. * matches conforming to this rule. Used by the parse() method.
  43. *
  44. * @access public
  45. *
  46. * @var string
  47. *
  48. * @see parse()
  49. *
  50. */
  51. //TODO: add text continuations (any number of + signs) and expandable areas (- after *s ot #s)
  52. var $regex = '/\n((?:\*|#)+.*?\n(?!(?:\*|#)+))/s';
  53. /**
  54. *
  55. * Generates a replacement for the matched text. Token options are:
  56. *
  57. * 'type' =>
  58. * 'bullet_start' : the start of a bullet list
  59. * 'bullet_end' : the end of a bullet list
  60. * 'number_start' : the start of a number list
  61. * 'number_end' : the end of a number list
  62. * 'item_start' : the start of item text (bullet or number)
  63. * 'item_end' : the end of item text (bullet or number)
  64. * 'unknown' : unknown type of list or item
  65. *
  66. * 'level' => the indent level (0 for the first level, 1 for the
  67. * second, etc)
  68. *
  69. * 'count' => the list item number at this level. not needed for
  70. * xhtml, but very useful for PDF and RTF.
  71. *
  72. * @access public
  73. *
  74. * @param array &$matches The array of matches from parse().
  75. *
  76. * @return A series of text and delimited tokens marking the different
  77. * list text and list elements.
  78. *
  79. */
  80. function process(&$matches)
  81. {
  82. // the replacement text we will return
  83. $return = '';
  84. // the list of post-processing matches
  85. $list = array();
  86. // a stack of list-start and list-end types; we keep this
  87. // so that we know what kind of list we're working with
  88. // (bullet or number) and what indent level we're at.
  89. $stack = array();
  90. // the item count is the number of list items for any
  91. // given list-type on the stack
  92. $itemcount = array();
  93. // have we processed the very first list item?
  94. $pastFirst = false;
  95. // populate $list with this set of matches. $matches[1] is the
  96. // text matched as a list set by parse().
  97. preg_match_all(
  98. '/^((\*|#)+)(.*?)$/ms',
  99. $matches[1],
  100. $list,
  101. PREG_SET_ORDER
  102. );
  103. // loop through each list-item element.
  104. foreach ($list as $key => $val) {
  105. // $val[0] is the full matched list-item line
  106. // $val[1] is the type (* or #)
  107. // $val[2] is the level (number)
  108. // $val[3] is the list item text
  109. // how many levels are we indented? (1 means the "root"
  110. // list level, no indenting.)
  111. $level = strlen($val[1]);
  112. // get the list item type
  113. if ($val[2] == '*') {
  114. $type = 'bullet';
  115. } elseif ($val[2] == '#') {
  116. $type = 'number';
  117. } else {
  118. $type = 'unknown';
  119. }
  120. // get the text of the list item
  121. $text = $val[3];
  122. // add a level to the list?
  123. if ($level > count($stack)) {
  124. // the current indent level is greater than the
  125. // number of stack elements, so we must be starting
  126. // a new list. push the new list type onto the
  127. // stack...
  128. array_push($stack, $type);
  129. // ...and add a list-start token to the return.
  130. $return .= $this->wiki->addToken(
  131. $this->rule,
  132. array(
  133. 'type' => $type . '_list_start',
  134. 'level' => $level - 1
  135. )
  136. );
  137. }
  138. // remove a level from the list?
  139. while (count($stack) > $level) {
  140. // so we don't keep counting the stack, we set up a temp
  141. // var for the count. -1 becuase we're going to pop the
  142. // stack in the next command. $tmp will then equal the
  143. // current level of indent.
  144. $tmp = count($stack) - 1;
  145. // as long as the stack count is greater than the
  146. // current indent level, we need to end list types.
  147. // continue adding end-list tokens until the stack count
  148. // and the indent level are the same.
  149. $return .= $this->wiki->addToken(
  150. $this->rule,
  151. array (
  152. 'type' => array_pop($stack) . '_list_end',
  153. 'level' => $tmp
  154. )
  155. );
  156. // reset to the current (previous) list type so that
  157. // the new list item matches the proper list type.
  158. $type = $stack[$tmp - 1];
  159. // reset the item count for the popped indent level
  160. unset($itemcount[$tmp + 1]);
  161. }
  162. // add to the item count for this list (taking into account
  163. // which level we are at).
  164. if (! isset($itemcount[$level])) {
  165. // first count
  166. $itemcount[$level] = 0;
  167. } else {
  168. // increment count
  169. $itemcount[$level]++;
  170. }
  171. // is this the very first item in the list?
  172. if (! $pastFirst) {
  173. $first = true;
  174. $pastFirst = true;
  175. } else {
  176. $first = false;
  177. }
  178. // create a list-item starting token.
  179. $start = $this->wiki->addToken(
  180. $this->rule,
  181. array(
  182. 'type' => $type . '_item_start',
  183. 'level' => $level,
  184. 'count' => $itemcount[$level],
  185. 'first' => $first
  186. )
  187. );
  188. // create a list-item ending token.
  189. $end = $this->wiki->addToken(
  190. $this->rule,
  191. array(
  192. 'type' => $type . '_item_end',
  193. 'level' => $level,
  194. 'count' => $itemcount[$level]
  195. )
  196. );
  197. // add the starting token, list-item text, and ending token
  198. // to the return.
  199. $return .= $start . $text . $end;
  200. }
  201. // the last list-item may have been indented. go through the
  202. // list-type stack and create end-list tokens until the stack
  203. // is empty.
  204. while (count($stack) > 0) {
  205. $return .= $this->wiki->addToken(
  206. $this->rule,
  207. array (
  208. 'type' => array_pop($stack) . '_list_end',
  209. 'level' => count($stack)
  210. )
  211. );
  212. }
  213. // we're done! send back the replacement text.
  214. return "\n" . $return . "\n\n";
  215. }
  216. }
  217. ?>