PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/includes/wikiengine/Tables.php

https://code.google.com/p/enanocms/
PHP | 198 lines | 136 code | 19 blank | 43 comment | 58 complexity | 85b8c3888b9b0e8b8a9c9bcafd0a8e8b MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*
  3. * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
  4. * Copyright (C) 2006-2009 Dan Fuhry
  5. *
  6. * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  10. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
  11. *
  12. * This script contains code originally found in MediaWiki (http://www.mediawiki.org). MediaWiki is also licensed under
  13. * the GPLv2 or later; see the file GPL included with this package for details.
  14. *
  15. * We're using the MW parser because the Text_Wiki version simply refused to work under PHP 5.2.0. Porting this was
  16. * _not_ easy. <leaves to get cup of coffee>
  17. */
  18. global $mStripState, $wgRandomKey;
  19. $mStripState = Array();
  20. /**
  21. * emulate mediawiki parser, including stripping, etc.
  22. *
  23. * @param string $text the text to parse
  24. * @return string
  25. * @access public
  26. */
  27. function process_tables( $text )
  28. {
  29. // include some globals, do some parser stuff that would normally be done in the parent parser function
  30. global $mStripState;
  31. $x =& $mStripState;
  32. // parse the text
  33. $text = doTableStuff($text);
  34. return $text;
  35. }
  36. /**
  37. * parse the wiki syntax used to render tables
  38. *
  39. * @param string $t the text to parse
  40. * @return string
  41. * @access private
  42. */
  43. function doTableStuff( $t ) {
  44. $t = explode ( "\n" , $t ) ;
  45. $td = array () ; # Is currently a td tag open?
  46. $ltd = array () ; # Was it TD or TH?
  47. $tr = array () ; # Is currently a tr tag open?
  48. $ltr = array () ; # tr attributes
  49. $has_opened_tr = array(); # Did this table open a <tr> element?
  50. $indent_level = 0; # indent level of the table
  51. $row_count = 0;
  52. foreach ( $t AS $k => $line )
  53. {
  54. $line = trim ( $line ) ;
  55. $first_char = substr ( $line , 0 , 1 ) ;
  56. if ( preg_match( '/^(:*)\{\|(.*)$/', $line, $matches ) ) {
  57. $indent_level = strlen( $matches[1] );
  58. $attributes = unstripForHTML( $matches[2] );
  59. $styled_table = false;
  60. if ( trim($attributes) == "styled" )
  61. {
  62. $attributes='cellspacing="1" cellpadding="4"';
  63. $styled_table = true;
  64. }
  65. $t[$k] = str_repeat( '<dl><dd>', $indent_level ) .
  66. ( $styled_table ? '<div class="tblholder">' : '' ) .
  67. '<table' . fixTagAttributes( $attributes, 'table' ) . '>' ;
  68. array_push ( $td , false ) ;
  69. array_push ( $ltd , '' ) ;
  70. array_push ( $tr , false ) ;
  71. array_push ( $ltr , '' ) ;
  72. array_push ( $has_opened_tr, false );
  73. }
  74. else if ( count ( $td ) == 0 ) { } # Don't do any of the following
  75. else if ( '|}' == substr ( $line , 0 , 2 ) ) {
  76. $z = "</table>" . ( $styled_table ? '</div>' : '' ) . substr ( $line , 2);
  77. $l = array_pop ( $ltd ) ;
  78. if ( !array_pop ( $has_opened_tr ) ) $z = "<tr><td></td></tr>" . $z ;
  79. if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
  80. if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
  81. array_pop ( $ltr ) ;
  82. $t[$k] = $z . str_repeat( '</dd></dl>', $indent_level );
  83. }
  84. else if ( '|-' == substr ( $line , 0 , 2 ) ) { # Allows for |---------------
  85. $row_count++;
  86. $line = substr ( $line , 1 ) ;
  87. while ( $line != '' && substr ( $line , 0 , 1 ) == '-' ) $line = substr ( $line , 1 ) ;
  88. $z = '' ;
  89. $l = array_pop ( $ltd ) ;
  90. array_pop ( $has_opened_tr );
  91. array_push ( $has_opened_tr , true ) ;
  92. if ( array_pop ( $tr ) ) $z = '</tr>' . $z ;
  93. if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
  94. array_pop ( $ltr ) ;
  95. $t[$k] = $z ;
  96. array_push ( $tr , false ) ;
  97. array_push ( $td , false ) ;
  98. array_push ( $ltd , '' ) ;
  99. $attributes = unstripForHTML( $line );
  100. array_push ( $ltr , fixTagAttributes( $attributes, 'tr' ) ) ;
  101. }
  102. else if ( '|' == $first_char || '!' == $first_char || '|+' == substr ( $line , 0 , 2 ) ) { # Caption
  103. # $line is a table row
  104. if ( '|+' == substr ( $line , 0 , 2 ) ) {
  105. $first_char = '+' ;
  106. $line = substr ( $line , 1 ) ;
  107. }
  108. $after = substr ( $line , 1 ) ;
  109. if ( $first_char == '!' ) $after = str_replace ( '!!' , '||' , $after ) ;
  110. // Split up multiple cells on the same line.
  111. // FIXME: This can result in improper nesting of tags processed
  112. // by earlier parser steps, but should avoid splitting up eg
  113. // attribute values containing literal "||".
  114. $after = wfExplodeMarkup( '||', $after );
  115. $t[$k] = '' ;
  116. # Loop through each table cell
  117. foreach ( $after AS $theline )
  118. {
  119. $z = '' ;
  120. if ( $first_char != '+' )
  121. {
  122. $tra = array_pop ( $ltr ) ;
  123. if ( !array_pop ( $tr ) ) $z = '<tr'.$tra.">\n" ;
  124. array_push ( $tr , true ) ;
  125. array_push ( $ltr , '' ) ;
  126. array_pop ( $has_opened_tr );
  127. array_push ( $has_opened_tr , true ) ;
  128. }
  129. $l = array_pop ( $ltd ) ;
  130. if ( array_pop ( $td ) ) $z = '</'.$l.'>' . $z ;
  131. if ( $first_char == '|' ) $l = 'td' ;
  132. else if ( $first_char == '!' ) $l = 'th' ;
  133. else if ( $first_char == '+' ) $l = 'caption' ;
  134. else $l = '' ;
  135. array_push ( $ltd , $l ) ;
  136. # Cell parameters
  137. $y = explode ( '|' , $theline , 2 ) ;
  138. # Note that a '|' inside an invalid link should not
  139. # be mistaken as delimiting cell parameters
  140. if ( strpos( $y[0], '[[' ) !== false ) {
  141. $y = array ($theline);
  142. }
  143. $attr_append = '';
  144. if ( $styled_table && $l == 'td' )
  145. {
  146. $rowclass = 1 + ($row_count % 2);
  147. $attr_append .= ' class="row' . $rowclass . '"';
  148. }
  149. if ( count ( $y ) == 1 )
  150. {
  151. $y = "{$z}<{$l}{$attr_append}>{$y[0]}" ;
  152. }
  153. else
  154. {
  155. $attributes = unstripForHTML( $y[0] );
  156. if ( !strstr($attributes, "class=") )
  157. $attributes .= $attr_append;
  158. $y = "{$z}<{$l}".fixTagAttributes($attributes, $l).">{$y[1]}" ;
  159. }
  160. $t[$k] .= $y ;
  161. array_push ( $td , true ) ;
  162. }
  163. }
  164. }
  165. # Closing open td, tr && table
  166. while ( count ( $td ) > 0 )
  167. {
  168. $l = array_pop ( $ltd ) ;
  169. if ( array_pop ( $td ) ) $t[] = '</td>' ;
  170. if ( array_pop ( $tr ) ) $t[] = '</tr>' ;
  171. if ( !array_pop ( $has_opened_tr ) ) $t[] = "<tr><td></td></tr>" ;
  172. $t[] = '</table></_paragraph_bypass>' ;
  173. }
  174. $t = implode ( "\n" , $t ) ;
  175. # special case: don't return empty table
  176. if($t == "<table>\n<tr><td></td></tr>\n</table>")
  177. $t = '';
  178. return $t ;
  179. }