/frameworks/solar/1.1.1/source/solar/Solar/Markdown/Extra/DefList.php

https://github.com/ggunlugu/ornekler · PHP · 253 lines · 115 code · 20 blank · 118 comment · 3 complexity · a26afbc26a115b8f9382898367fec044 MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * Block class to form definition lists.
  5. *
  6. * Syntax is ...
  7. *
  8. * term
  9. * : definition
  10. *
  11. * term1
  12. * term2
  13. * : definition
  14. *
  15. * term
  16. * : definition 1
  17. * : definition 2
  18. *
  19. * @category Solar
  20. *
  21. * @package Solar_Markdown_Extra
  22. *
  23. * @author Michel Fortin <http://www.michelf.com/projects/php-markdown/>
  24. *
  25. * @author Paul M. Jones <pmjones@solarphp.com>
  26. *
  27. * @license http://opensource.org/licenses/bsd-license.php BSD
  28. *
  29. * @version $Id: DefList.php 4531 2010-04-12 17:23:56Z pmjones $
  30. *
  31. */
  32. class Solar_Markdown_Extra_DefList extends Solar_Markdown_Plugin
  33. {
  34. /**
  35. *
  36. * This is a block plugin.
  37. *
  38. * @var bool
  39. *
  40. */
  41. protected $_is_block = true;
  42. /**
  43. *
  44. * The tag to open a definition term.
  45. *
  46. * @var string
  47. *
  48. */
  49. protected $_dt_open = '<dt>';
  50. /**
  51. *
  52. * The tag to close a definition term.
  53. *
  54. * @var string
  55. *
  56. */
  57. protected $_dt_close = '</dt>';
  58. /**
  59. *
  60. * The tag to open a definition definition.
  61. *
  62. * @var string
  63. *
  64. */
  65. protected $_dd_open = '<dd>';
  66. /**
  67. *
  68. * The tag to close a definition definition.
  69. *
  70. * @var string
  71. *
  72. */
  73. protected $_dd_close = '</dd>';
  74. /**
  75. *
  76. * Processes source text to find definition lists.
  77. *
  78. * @param string $text The source text.
  79. *
  80. * @return string The transformed XHTML.
  81. *
  82. */
  83. public function parse($text)
  84. {
  85. $less_than_tab = $this->_getTabWidth() - 1;
  86. // Re-usable pattern to match any entire dl list:
  87. $whole_list = '
  88. ( # $1 = whole list
  89. ( # $2
  90. [ ]{0,'.$less_than_tab.'}
  91. ((?>.*\S.*\n)+) # $3 = defined term
  92. \n?
  93. [ ]{0,'.$less_than_tab.'}:[ ]+ # colon starting definition
  94. )
  95. (?s:.+?)
  96. ( # $4
  97. \z
  98. |
  99. \n{2,}
  100. (?=\S)
  101. (?! # Negative lookahead for another term
  102. [ ]{0,'.$less_than_tab.'}
  103. (?: \S.*\n )+? # defined term
  104. \n?
  105. [ ]{0,'.$less_than_tab.'}:[ ]+ # colon starting definition
  106. )
  107. (?! # Negative lookahead for another definition
  108. [ ]{0,'.$less_than_tab.'}:[ ]+ # colon starting definition
  109. )
  110. )
  111. )
  112. '; // mx
  113. $text = preg_replace_callback(
  114. '{
  115. (?:(?<=\n\n)|\A\n?)
  116. ' . $whole_list . '
  117. }mx',
  118. array($this, '_parse'),
  119. $text
  120. );
  121. return $text;
  122. }
  123. /**
  124. *
  125. * Support callback for block quotes.
  126. *
  127. * @param array $matches Matches from preg_replace_callback().
  128. *
  129. * @return string The replacement text.
  130. *
  131. */
  132. protected function _parse($matches)
  133. {
  134. // Re-usable patterns to match list item bullets and number markers:
  135. $list = $matches[1];
  136. // Turn double returns into triple returns, so that we can make a
  137. // paragraph for the last item in a list, if necessary:
  138. $result = trim($this->_processItems($list));
  139. $result = "<dl>\n" . $result . "\n</dl>";
  140. return $this->_toHtmlToken($result) . "\n\n";
  141. }
  142. /**
  143. *
  144. * Process the contents of a definition list, splitting it into
  145. * individual list items.
  146. *
  147. * @param string $list_str The source text of the list block.
  148. *
  149. * @return string The replacement text.
  150. *
  151. */
  152. protected function _processItems($list_str)
  153. {
  154. $less_than_tab = $this->_getTabWidth() - 1;
  155. // trim trailing blank lines:
  156. $list_str = preg_replace("/\n{2,}\\z/", "\n", $list_str);
  157. // Process definition terms.
  158. $list_str = preg_replace_callback(
  159. '{
  160. (?:\n\n+|\A\n?) # leading line
  161. ( # definition terms = $1
  162. [ ]{0,'.$less_than_tab.'} # leading whitespace
  163. (?![:][ ]|[ ]) # negative lookahead for a definition
  164. # mark (colon) or more whitespace.
  165. (?: \S.* \n)+? # actual term (not whitespace).
  166. )
  167. (?=\n?[ ]{0,3}:[ ]) # lookahead for following line feed
  168. # with a definition mark.
  169. }xm',
  170. array($this, '_processDt'),
  171. $list_str
  172. );
  173. // Process actual definitions.
  174. $list_str = preg_replace_callback(
  175. '{
  176. \n(\n+)? # leading line = $1
  177. [ ]{0,'.$less_than_tab.'} # whitespace before colon
  178. [:][ ]+ # definition mark (colon)
  179. ((?s:.+?)) # definition text = $2
  180. (?= \n+ # stop at next definition mark,
  181. (?: # next term or end of text
  182. [ ]{0,'.$less_than_tab.'} [:][ ] |
  183. '.$this->_dt_open.' | \z
  184. )
  185. )
  186. }xm',
  187. array($this, '_processDd'),
  188. $list_str
  189. );
  190. return $list_str;
  191. }
  192. /**
  193. *
  194. * Support callback for processing item terms.
  195. *
  196. * @param array $matches Matches from preg_replace_callback().
  197. *
  198. * @return string The replacement text.
  199. *
  200. */
  201. protected function _processDt($matches)
  202. {
  203. $terms = explode("\n", trim($matches[1]));
  204. $text = '';
  205. foreach ($terms as $term) {
  206. $term = $this->_processSpans(trim($term));
  207. $text .= "\n{$this->_dt_open}" . $term . $this->_dt_close;
  208. }
  209. return $text . "\n";
  210. }
  211. /**
  212. *
  213. * Support callback for processing item definitions.
  214. *
  215. * @param array $matches Matches from preg_replace_callback().
  216. *
  217. * @return string The replacement text.
  218. *
  219. */
  220. protected function _processDd($matches)
  221. {
  222. $leading_line = $matches[1];
  223. $def = $matches[2];
  224. if ($leading_line || preg_match('/\n{2,}/', $def)) {
  225. $def = $this->_processBlocks($this->_outdent($def . "\n\n"));
  226. $def = "\n". $def ."\n";
  227. } else {
  228. $def = rtrim($def);
  229. $def = $this->_processSpans($this->_outdent($def));
  230. }
  231. return "\n{$this->_dd_open}" . $def . "{$this->_dd_close}\n";
  232. }
  233. }