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

https://github.com/ggunlugu/ornekler · PHP · 220 lines · 104 code · 16 blank · 100 comment · 2 complexity · 11a06a3bad25b766a7229e8cfc9d52c3 MD5 · raw file

  1. <?php
  2. /**
  3. *
  4. * Block class to form tables from Markdown syntax.
  5. *
  6. * Syntax is ...
  7. *
  8. * | Header 1 | Header 2 | Header N
  9. * | ---------- | ---------- | ----------
  10. * | data cell | data cell | data cell
  11. * | data cell | data cell | data cell
  12. * | data cell | data cell | data cell
  13. * | data cell | data cell | data cell
  14. *
  15. * You can force columns alignment by putting a colon in the header-
  16. * underline row.
  17. *
  18. * | Left-Aligned | No Align | Right-Aligned
  19. * | :----------- | --------- | -------------:
  20. * | data cell | data cell | data cell
  21. * | data cell | data cell | data cell
  22. * | data cell | data cell | data cell
  23. * | data cell | data cell | data cell
  24. *
  25. * @category Solar
  26. *
  27. * @package Solar_Markdown_Extra
  28. *
  29. * @author Michel Fortin <http://www.michelf.com/projects/php-markdown/>
  30. *
  31. * @author Paul M. Jones <pmjones@solarphp.com>
  32. *
  33. * @license http://opensource.org/licenses/bsd-license.php BSD
  34. *
  35. * @version $Id: Table.php 3153 2008-05-05 23:14:16Z pmjones $
  36. *
  37. */
  38. class Solar_Markdown_Extra_Table extends Solar_Markdown_Plugin
  39. {
  40. /**
  41. *
  42. * This is a block plugin.
  43. *
  44. * @var bool
  45. *
  46. */
  47. protected $_is_block = true;
  48. /**
  49. *
  50. * Uses these chars for parsing.
  51. *
  52. * @var string
  53. *
  54. */
  55. protected $_chars = '|';
  56. /**
  57. *
  58. * Transforms Markdown syntax to XHTML tables.
  59. *
  60. * @param string $text The source text.
  61. *
  62. * @return string The transformed XHTML.
  63. *
  64. */
  65. public function parse($text)
  66. {
  67. $less_than_tab = $this->_getTabWidth() - 1;
  68. // Find tables with leading pipe.
  69. //
  70. // | Header 1 | Header 2
  71. // | -------- | --------
  72. // | Cell 1 | Cell 2
  73. // | Cell 3 | Cell 4
  74. //
  75. $text = preg_replace_callback('
  76. {
  77. ^ # Start of a line
  78. [ ]{0,'.$less_than_tab.'} # Allowed whitespace.
  79. [|] # Optional leading pipe (present)
  80. (.+) \n # $1: Header row (at least one pipe)
  81. [ ]{0,'.$less_than_tab.'} # Allowed whitespace.
  82. [|] ([ ]*[-:]+[-| :]*) \n # $2: Header underline
  83. ( # $3: Cells
  84. (?:
  85. [ ]* # Allowed whitespace.
  86. [|] .* \n # Row content.
  87. )*
  88. )
  89. (?=\n|\Z) # Stop at final double newline.
  90. }xm',
  91. array($this, '_parsePipe'),
  92. $text
  93. );
  94. //
  95. // Find tables without leading pipe.
  96. //
  97. // Header 1 | Header 2
  98. // -------- | --------
  99. // Cell 1 | Cell 2
  100. // Cell 3 | Cell 4
  101. //
  102. $text = preg_replace_callback('
  103. {
  104. ^ # Start of a line
  105. [ ]{0,'.$less_than_tab.'} # Allowed whitespace.
  106. (\S.*[|].*) \n # $1: Header row (at least one pipe)
  107. [ ]{0,'.$less_than_tab.'} # Allowed whitespace.
  108. ([-:]+[ ]*[|][-| :]*) \n # $2: Header underline
  109. ( # $3: Cells
  110. (?:
  111. .* [|] .* \n # Row content
  112. )*
  113. )
  114. (?=\n|\Z) # Stop at final double newline.
  115. }xm',
  116. array($this, '_parsePlain'),
  117. $text
  118. );
  119. return $text;
  120. }
  121. /**
  122. *
  123. * Support callback for leading-pipe syntax.
  124. *
  125. * @param array $matches Matches from preg_replace_callback().
  126. *
  127. * @return string The replacement text.
  128. *
  129. */
  130. protected function _parsePipe($matches)
  131. {
  132. // Remove leading pipe for each row.
  133. $matches[3] = preg_replace('/^ *[|]/m', '', $matches[3]);
  134. return $this->_parsePlain($matches);
  135. }
  136. /**
  137. *
  138. * Support callback for table conversion.
  139. *
  140. * @param array $matches Matches from preg_replace_callback().
  141. *
  142. * @return string The replacement text.
  143. *
  144. */
  145. protected function _parsePlain($matches)
  146. {
  147. $head = $matches[1];
  148. $underline = $matches[2];
  149. $content = $matches[3];
  150. // Remove any tailing pipes for each line.
  151. $head = preg_replace('/[|] *$/m', '', $head);
  152. $underline = preg_replace('/[|] *$/m', '', $underline);
  153. $content = preg_replace('/[|] *$/m', '', $content);
  154. // Reading alignment from header underline.
  155. $separators = preg_split('/ *[|] */', $underline);
  156. $attr = array();
  157. foreach ($separators as $n => $s) {
  158. if (preg_match('/^ *-+: *$/', $s)) {
  159. $attr[$n] = ' align="right"';
  160. } elseif (preg_match('/^ *:-+: *$/', $s)) {
  161. $attr[$n] = ' align="center"';
  162. } elseif (preg_match('/^ *:-+ *$/', $s)) {
  163. $attr[$n] = ' align="left"';
  164. } else {
  165. $attr[$n] = '';
  166. }
  167. }
  168. // handle all spans at once, not just code spans
  169. $head = $this->_processSpans($head);
  170. $headers = preg_split('/ *[|] */', $head);
  171. $col_count = count($headers);
  172. // Write column headers.
  173. $text = "\n<table>\n";
  174. $text .= " <thead>\n";
  175. $text .= " <tr>\n";
  176. foreach ($headers as $n => $header) {
  177. $text .= " <th$attr[$n]>". trim($header) ."</th>\n";
  178. }
  179. $text .= " </tr>\n";
  180. $text .= " </thead>\n";
  181. // Split content by row.
  182. $rows = explode("\n", trim($content, "\n"));
  183. $text .= " <tbody>\n";
  184. foreach ($rows as $row) {
  185. // handle all spans at once, not just code spans
  186. $row = $this->_processSpans($row);
  187. // Split row by cell.
  188. $row_cells = preg_split('/ *[|] */', $row, $col_count);
  189. $row_cells = array_pad($row_cells, $col_count, '');
  190. $text .= " <tr>\n";
  191. foreach ($row_cells as $n => $cell) {
  192. $text .= " <td$attr[$n]>". trim($cell) ."</td>\n";
  193. }
  194. $text .= " </tr>\n";
  195. }
  196. $text .= " </tbody>\n";
  197. $text .= "</table>\n";
  198. return $this->_toHtmlToken($text) . "\n";
  199. }
  200. }