PageRenderTime 105ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/tasks/lib/doc/lib/JsDoc/Parse/Docwiki/List.php

https://github.com/Jeff2Ma/GMU
PHP | 194 lines | 104 code | 36 blank | 54 comment | 13 complexity | 4c90a1fababad0d25bc4a301df5e485f MD5 | raw file
  1. <?php
  2. class Text_Wiki_Parse_List extends Text_Wiki_Parse {
  3. var $regex = '/^((\*|n|-|\+)\s.*\n)(?!\s+\S|\2\s|(?:\s+((?:\*|n|-|\+) |\n)))/Usm';
  4. function parse(){
  5. //统一数字列表符号
  6. $this->wiki->source = preg_replace('/^(\s*)\d+\./sm', '\1n', $this->wiki->source);
  7. parent::parse();
  8. }
  9. function process(&$matches)
  10. {
  11. // the replacement text we will return
  12. $return = '';
  13. // the list of post-processing matches
  14. $list = array();
  15. // a stack of list-start and list-end types; we keep this
  16. // so that we know what kind of list we're working with
  17. // (bullet or number) and what indent level we're at.
  18. $stack = array();
  19. // the item count is the number of list items for any
  20. // given list-type on the stack
  21. $itemcount = array();
  22. // have we processed the very first list item?
  23. $pastFirst = false;
  24. // populate $list with this set of matches. $matches[1] is the
  25. // text matched as a list set by parse().
  26. preg_match_all(
  27. '=^(\s*)(\*|\+|-|n)\s(.*)\n(?!^\s+[^*+\-n\s].*\n)=Usm',
  28. //'=^(\s*)(\*|#)\s(.*)$=Ums',
  29. $matches[1],
  30. $list,
  31. PREG_SET_ORDER
  32. );
  33. $numSpaces = 0;
  34. // loop through each list-item element.
  35. foreach ($list as $key => $val) {
  36. // $val[0] is the full matched list-item line
  37. // $val[1] is the number of initial spaces (indent level)
  38. // $val[2] is the list item type (* or #)
  39. // $val[3] is the list item text
  40. // how many levels are we indented? (1 means the "root"
  41. // list level, no indenting.)
  42. $val[1] = str_replace(" ", "\t", $val[1]);
  43. $level = strlen($val[1]) + 1;
  44. // get the list item type
  45. if (in_array($val[2], array('*', '+', '-'))) {
  46. $type = 'bullet';
  47. } elseif ($val[2] == 'n') {
  48. $type = 'number';
  49. } else {
  50. $type = 'unknown';
  51. }
  52. // get the text of the list item
  53. $text = $val[3];
  54. // add a level to the list?
  55. if ($level > count($stack)) {
  56. //watch for the same # of spaces and reset level
  57. if ($level == $numSpaces) {
  58. $level = count($stack);
  59. } else {
  60. $numSpaces = $level;
  61. // reset level as sometimes people use too many spaces
  62. $level = count($stack) + 1;
  63. // the current indent level is greater than the
  64. // number of stack elements, so we must be starting
  65. // a new list. push the new list type onto the
  66. // stack...
  67. array_push($stack, $type);
  68. // ...and add a list-start token to the return.
  69. $return .= $this->wiki->addToken(
  70. $this->rule,
  71. array(
  72. 'type' => $type . '_list_start',
  73. 'level' => $level - 1
  74. )
  75. );
  76. }
  77. }
  78. // remove a level from the list?
  79. while (count($stack) > $level) {
  80. // so we don't keep counting the stack, we set up a temp
  81. // var for the count. -1 becuase we're going to pop the
  82. // stack in the next command. $tmp will then equal the
  83. // current level of indent.
  84. $tmp = count($stack) - 1;
  85. // as long as the stack count is greater than the
  86. // current indent level, we need to end list types.
  87. // continue adding end-list tokens until the stack count
  88. // and the indent level are the same.
  89. $return .= $this->wiki->addToken(
  90. $this->rule,
  91. array (
  92. 'type' => array_pop($stack) . '_list_end',
  93. 'level' => $tmp
  94. )
  95. );
  96. // reset to the current (previous) list type so that
  97. // the new list item matches the proper list type.
  98. $type = $stack[$tmp - 1];
  99. // reset the item count for the popped indent level
  100. unset($itemcount[$tmp + 1]);
  101. $numSpaces = $level;
  102. }
  103. // add to the item count for this list (taking into account
  104. // which level we are at).
  105. if (! isset($itemcount[$level])) {
  106. // first count
  107. $itemcount[$level] = 0;
  108. } else {
  109. // increment count
  110. $itemcount[$level]++;
  111. }
  112. // is this the very first item in the list?
  113. if (! $pastFirst) {
  114. $first = true;
  115. $pastFirst = true;
  116. } else {
  117. $first = false;
  118. }
  119. // create a list-item starting token.
  120. $start = $this->wiki->addToken(
  121. $this->rule,
  122. array(
  123. 'type' => $type . '_item_start',
  124. 'level' => $level,
  125. 'count' => $itemcount[$level],
  126. 'first' => $first
  127. )
  128. );
  129. // create a list-item ending token.
  130. $end = $this->wiki->addToken(
  131. $this->rule,
  132. array(
  133. 'type' => $type . '_item_end',
  134. 'level' => $level,
  135. 'count' => $itemcount[$level]
  136. )
  137. );
  138. // add the starting token, list-item text, and ending token
  139. // to the return.
  140. $return .= $start . $text . $end;
  141. }
  142. // the last list-item may have been indented. go through the
  143. // list-type stack and create end-list tokens until the stack
  144. // is empty.
  145. while (count($stack) > 0) {
  146. $return .= $this->wiki->addToken(
  147. $this->rule,
  148. array (
  149. 'type' => array_pop($stack) . '_list_end',
  150. 'level' => count($stack)
  151. )
  152. );
  153. }
  154. // we're done! send back the replacement text.
  155. return "\n\n" . $return . "\n\n";
  156. }
  157. }
  158. ?>