/frameworks/solar/1.1.1/source/solar/Solar/Markdown/Wiki/Header.php

https://github.com/ggunlugu/ornekler · PHP · 247 lines · 84 code · 18 blank · 145 comment · 4 complexity · 57ccf7f45afadbc9468bfaaa6d18f386 MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * Block plugin to convert wiki-fied headers into XHTML headers.
  5. *
  6. * This code ...
  7. *
  8. * =======
  9. * Title
  10. * =======
  11. *
  12. * ---------------
  13. * Super-Section
  14. * ---------------
  15. *
  16. * Section
  17. * =======
  18. *
  19. * Sub Section
  20. * -----------
  21. *
  22. * ... would become ...
  23. *
  24. * <h1>Title</h1>
  25. *
  26. * <h2>Super-Section</h2>
  27. *
  28. * <h3>Section</h3>
  29. *
  30. * <h4>Sub-Section</h4>
  31. *
  32. * You can also suffix the header text with `{#id}` and that will become the
  33. * the header ID attribute. For example, this code ...
  34. *
  35. * Section {#foo}
  36. * ==============
  37. *
  38. * ... would become ...
  39. *
  40. * <h3 id="foo">Section</h3>
  41. *
  42. * @category Solar
  43. *
  44. * @package Solar_Markdown_Wiki
  45. *
  46. * @author John Gruber <http://daringfireball.net/projects/markdown/>
  47. *
  48. * @author Michel Fortin <http://www.michelf.com/projects/php-markdown/>
  49. *
  50. * @author Paul M. Jones <pmjones@solarphp.com>
  51. *
  52. * @license http://opensource.org/licenses/bsd-license.php BSD
  53. *
  54. * @version $Id: Header.php 3153 2008-05-05 23:14:16Z pmjones $
  55. *
  56. */
  57. class Solar_Markdown_Wiki_Header extends Solar_Markdown_Plugin
  58. {
  59. /**
  60. *
  61. * This is a block plugin.
  62. *
  63. * @var bool
  64. *
  65. */
  66. protected $_is_block = true;
  67. /**
  68. *
  69. * These should be encoded as special Markdown characters.
  70. *
  71. * @var string
  72. *
  73. */
  74. protected $_chars = '-=';
  75. /**
  76. *
  77. * Turns setext-style headers into XHTML header tags.
  78. *
  79. * @param string $text Portion of the Markdown source text.
  80. *
  81. * @return string The transformed XHTML.
  82. *
  83. */
  84. public function parse($text)
  85. {
  86. // h2
  87. $text = preg_replace_callback(
  88. // '{ ^=+[ \t]*\n(.+)[ \t]*\n=+[ \t]*\n+ }mx',
  89. '{ ^=+[ \t]*\n(.+?) (?:[ ]+\{\#([-_:a-zA-Z0-9]+)\})? [ \t]*\n=+[ \t]*\n+ }mx',
  90. array($this, '_parseTitle'),
  91. $text
  92. );
  93. // h3
  94. $text = preg_replace_callback(
  95. // '{ ^-+[ \t]*\n(.+)[ \t]*\n-+[ \t]*\n+ }mx',
  96. '{ ^-+[ \t]*\n(.+?) (?:[ ]+\{\#([-_:a-zA-Z0-9]+)\})? [ \t]*\n-+[ \t]*\n+ }mx',
  97. array($this, '_parseSuperSection'),
  98. $text
  99. );
  100. // h4
  101. $text = preg_replace_callback(
  102. '{ (^.+?) (?:[ ]+\{\#([-_:a-zA-Z0-9]+)\})? [ \t]*\n=+[ \t]*\n+ }mx',
  103. array($this, '_parseSection'),
  104. $text
  105. );
  106. // h5
  107. $text = preg_replace_callback(
  108. '{ (^.+?) (?:[ ]+\{\#([-_:a-zA-Z0-9]+)\})? [ \t]*\n-+[ \t]*\n+ }mx',
  109. array($this, '_parseSubSection'),
  110. $text
  111. );
  112. // atx 1 through 4
  113. $text = preg_replace_callback(
  114. "{
  115. ^(\\#{1,4}) # $1 = string of #'s
  116. [ \\t]*
  117. (.+?) # $2 = header text
  118. (?:[ ]+\{\#([-_:a-zA-Z0-9]+)\})? # $3 = ID
  119. [ \\t]*
  120. \\#* # optional closing #'s
  121. \\n+
  122. }xm",
  123. array($this, '_parseAtx'),
  124. $text
  125. );
  126. // done
  127. return $text;
  128. }
  129. /**
  130. *
  131. * Support callback for ATX headers.
  132. *
  133. * Only supports 1-4 leading hash marks.
  134. *
  135. * @param array $matches Matches from preg_replace_callback().
  136. *
  137. * @return string The replacement text.
  138. *
  139. */
  140. protected function _parseAtx($matches)
  141. {
  142. $tag = 'h' . strlen($matches[1]);
  143. if (! empty($matches[3])) {
  144. $id = ' id="' . $this->_escape($matches[3]) . '"';
  145. } else {
  146. $id = '';
  147. }
  148. $html = "<$tag$id>"
  149. . $this->_processSpans($matches[2])
  150. . "</$tag>";
  151. return $this->_toHtmlToken($html) . "\n\n";
  152. }
  153. /**
  154. *
  155. * Support callback for H1 headers.
  156. *
  157. * @param array $matches Matches from preg_replace_callback().
  158. *
  159. * @return string The replacement text.
  160. *
  161. */
  162. protected function _parseTitle($matches)
  163. {
  164. return $this->_header('h1', $matches);
  165. }
  166. /**
  167. *
  168. * Support callback for H2 headers.
  169. *
  170. * @param array $matches Matches from preg_replace_callback().
  171. *
  172. * @return string The replacement text.
  173. *
  174. */
  175. protected function _parseSuperSection($matches)
  176. {
  177. return $this->_header('h2', $matches);
  178. }
  179. /**
  180. *
  181. * Support callback for H3 headers.
  182. *
  183. * @param array $matches Matches from preg_replace_callback().
  184. *
  185. * @return string The replacement text.
  186. *
  187. */
  188. protected function _parseSection($matches)
  189. {
  190. return $this->_header('h3', $matches);
  191. }
  192. /**
  193. *
  194. * Support callback for H4 headers.
  195. *
  196. * @param array $matches Matches from preg_replace_callback().
  197. *
  198. * @return string The replacement text.
  199. *
  200. */
  201. protected function _parseSubSection($matches)
  202. {
  203. return $this->_header('h4', $matches);
  204. }
  205. /**
  206. *
  207. * Support callback for all headers.
  208. *
  209. * @param string $tag The header tag ('h1', 'h5', etc).
  210. *
  211. * @param string $matches The matched values, element 1 is the text,
  212. * optional element 2 is the ID (if any).
  213. *
  214. * @return string The replacement header HTML token.
  215. *
  216. */
  217. protected function _header($tag, $matches)
  218. {
  219. if (! empty($matches[2])) {
  220. $id = ' id="' . $this->_escape($matches[2]) . '"';
  221. } else {
  222. $id = '';
  223. }
  224. $html = "<$tag$id>"
  225. . $this->_processSpans($matches[1])
  226. . "</$tag>";
  227. return $this->_toHtmlToken($html) . "\n\n";
  228. }
  229. }