PageRenderTime 30ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/bundled-libs/Text/Wiki/Parse/Default/Blockquote.php

http://github.com/s9y/Serendipity
PHP | 163 lines | 58 code | 29 blank | 76 comment | 3 complexity | 011503667a676c127f4b74f1d7e029db MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-3.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, Apache-2.0
  1. <?php
  2. /**
  3. *
  4. * Parse for block-quoted text.
  5. *
  6. * Find source text marked as a blockquote, identified by any number of
  7. * greater-than signs '>' at the start of the line, followed by a space,
  8. * and then the quote text; each '>' indicates an additional level of
  9. * quoting.
  10. *
  11. * $Id: Blockquote.php,v 1.1 2005/01/31 15:46:52 pmjones Exp $
  12. *
  13. * @author Paul M. Jones <pmjones@ciaweb.net>
  14. *
  15. * @package Text_Wiki
  16. *
  17. */
  18. class Text_Wiki_Parse_Blockquote extends Text_Wiki_Parse {
  19. /**
  20. *
  21. * Regex for parsing the source text.
  22. *
  23. * @access public
  24. *
  25. * @var string
  26. *
  27. * @see parse()
  28. *
  29. */
  30. var $regex = '/\n((\>).*\n)(?!(\>))/Us';
  31. /**
  32. *
  33. * Generates a replacement for the matched text.
  34. *
  35. * Token options are:
  36. *
  37. * 'type' =>
  38. * 'start' : the start of a blockquote
  39. * 'end' : the end of a blockquote
  40. *
  41. * 'level' => the indent level (0 for the first level, 1 for the
  42. * second, etc)
  43. *
  44. * @access public
  45. *
  46. * @param array &$matches The array of matches from parse().
  47. *
  48. * @return A series of text and delimited tokens marking the different
  49. * list text and list elements.
  50. *
  51. */
  52. function process(&$matches)
  53. {
  54. // the replacement text we will return to parse()
  55. $return = '';
  56. // the list of post-processing matches
  57. $list = array();
  58. // $matches[1] is the text matched as a list set by parse();
  59. // create an array called $list that contains a new set of
  60. // matches for the various list-item elements.
  61. preg_match_all(
  62. '=^(\>+) (.*\n)=Ums',
  63. $matches[1],
  64. $list,
  65. PREG_SET_ORDER
  66. );
  67. // a stack of starts and ends; we keep this so that we know what
  68. // indent level we're at.
  69. $stack = array();
  70. // loop through each list-item element.
  71. foreach ($list as $key => $val) {
  72. // $val[0] is the full matched list-item line
  73. // $val[1] is the number of initial '>' chars (indent level)
  74. // $val[2] is the quote text
  75. // we number levels starting at 1, not zero
  76. $level = strlen($val[1]);
  77. // get the text of the line
  78. $text = $val[2];
  79. // add a level to the list?
  80. while ($level > count($stack)) {
  81. // the current indent level is greater than the number
  82. // of stack elements, so we must be starting a new
  83. // level. push the new level onto the stack with a
  84. // dummy value (boolean true)...
  85. array_push($stack, true);
  86. $return .= "\n";
  87. // ...and add a start token to the return.
  88. $return .= $this->wiki->addToken(
  89. $this->rule,
  90. array(
  91. 'type' => 'start',
  92. 'level' => $level - 1
  93. )
  94. );
  95. $return .= "\n\n";
  96. }
  97. // remove a level?
  98. while (count($stack) > $level) {
  99. // as long as the stack count is greater than the
  100. // current indent level, we need to end list types.
  101. // continue adding end-list tokens until the stack count
  102. // and the indent level are the same.
  103. array_pop($stack);
  104. $return .= "\n\n";
  105. $return .= $this->wiki->addToken(
  106. $this->rule,
  107. array (
  108. 'type' => 'end',
  109. 'level' => count($stack)
  110. )
  111. );
  112. $return .= "\n";
  113. }
  114. // add the line text.
  115. $return .= $text;
  116. }
  117. // the last line may have been indented. go through the stack
  118. // and create end-tokens until the stack is empty.
  119. $return .= "\n";
  120. while (count($stack) > 0) {
  121. array_pop($stack);
  122. $return .= $this->wiki->addToken(
  123. $this->rule,
  124. array (
  125. 'type' => 'end',
  126. 'level' => count($stack)
  127. )
  128. );
  129. }
  130. // we're done! send back the replacement text.
  131. return "\n$return\n\n";
  132. }
  133. }
  134. ?>