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

https://github.com/ggunlugu/ornekler · PHP · 176 lines · 52 code · 10 blank · 114 comment · 0 complexity · 931b90c3046a7b77e23a63c0f832be96 MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * Block plugin to convert Markdown headers into XHTML headers.
  5. *
  6. * For Setext-style headers, this code ...
  7. *
  8. * Header 1
  9. * ========
  10. *
  11. * Header 2
  12. * --------
  13. *
  14. * ... would become ...
  15. *
  16. * <h1>Header 1</h1>
  17. *
  18. * <h2>Header 2</h2>
  19. *
  20. *
  21. * For ATX-style headers, this code ...
  22. *
  23. * # Header 1
  24. *
  25. * ## Header 2
  26. *
  27. * ##### Header 5
  28. *
  29. * ... would become ...
  30. *
  31. * <h1>Header 1</h1>
  32. *
  33. * <h2>Header 2</h2>
  34. *
  35. * <h5>Header 5</h5>
  36. *
  37. * @category Solar
  38. *
  39. * @package Solar_Markdown
  40. *
  41. * @author John Gruber <http://daringfireball.net/projects/markdown/>
  42. *
  43. * @author Michel Fortin <http://www.michelf.com/projects/php-markdown/>
  44. *
  45. * @author Paul M. Jones <pmjones@solarphp.com>
  46. *
  47. * @license http://opensource.org/licenses/bsd-license.php BSD
  48. *
  49. * @version $Id: Header.php 3153 2008-05-05 23:14:16Z pmjones $
  50. *
  51. */
  52. class Solar_Markdown_Plugin_Header extends Solar_Markdown_Plugin
  53. {
  54. /**
  55. *
  56. * This is a block plugin.
  57. *
  58. * @var bool
  59. *
  60. */
  61. protected $_is_block = true;
  62. /**
  63. *
  64. * These should be encoded as special Markdown characters.
  65. *
  66. * @var string
  67. *
  68. */
  69. protected $_chars = '-=#';
  70. /**
  71. *
  72. * Turns ATX- and setext-style headers into XHTML header tags.
  73. *
  74. * @param string $text Portion of the Markdown source text.
  75. *
  76. * @return string The transformed XHTML.
  77. *
  78. */
  79. public function parse($text)
  80. {
  81. // setext top-level
  82. $text = preg_replace_callback(
  83. '{ ^(.+)[ \t]*\n=+[ \t]*\n+ }mx',
  84. array($this, '_parseTop'),
  85. $text
  86. );
  87. // setext sub-level
  88. $text = preg_replace_callback(
  89. '{ ^(.+)[ \t]*\n-+[ \t]*\n+ }mx',
  90. array($this, '_parseSub'),
  91. $text
  92. );
  93. // atx
  94. $text = preg_replace_callback(
  95. "{
  96. ^(\\#{1,6}) # $1 = string of #'s
  97. [ \\t]*
  98. (.+?) # $2 = Header text
  99. [ \\t]*
  100. \\#* # optional closing #'s (not counted)
  101. \\n+
  102. }xm",
  103. array($this, '_parseAtx'),
  104. $text
  105. );
  106. return $text;
  107. }
  108. /**
  109. *
  110. * Support callback for top-level setext headers ("h1").
  111. *
  112. * @param array $matches Matches from preg_replace_callback().
  113. *
  114. * @return string The replacement text.
  115. *
  116. */
  117. protected function _parseTop($matches)
  118. {
  119. return $this->_header('h1', $matches[1]);
  120. }
  121. /**
  122. *
  123. * Support callback for sub-level setext headers ("h2").
  124. *
  125. * @param array $matches Matches from preg_replace_callback().
  126. *
  127. * @return string The replacement text.
  128. *
  129. */
  130. protected function _parseSub($matches)
  131. {
  132. return $this->_header('h2', $matches[1]);
  133. }
  134. /**
  135. *
  136. * Support callback for ATX headers.
  137. *
  138. * @param array $matches Matches from preg_replace_callback().
  139. *
  140. * @return string The replacement text.
  141. *
  142. */
  143. protected function _parseAtx($matches)
  144. {
  145. $tag = 'h' . strlen($matches[1]); // h1, h2, h5, etc
  146. return $this->_header($tag, $matches[2]);
  147. }
  148. /**
  149. *
  150. * Support callback for all headers.
  151. *
  152. * @param string $tag The header tag ('h1', 'h5', etc).
  153. *
  154. * @param string $text The header text.
  155. *
  156. * @return string The replacement header HTML token.
  157. *
  158. */
  159. protected function _header($tag, $text)
  160. {
  161. $html = "<$tag>"
  162. . $this->_processSpans($text)
  163. . "</$tag>";
  164. return $this->_toHtmlToken($html) . "\n\n";
  165. }
  166. }