PageRenderTime 78ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/wikiengine/render_xhtml.php

https://code.google.com/p/enanocms/
PHP | 183 lines | 150 code | 17 blank | 16 comment | 11 complexity | aced077cc6f2b1c1b382efb0bd090922 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. class Carpenter_Render_Xhtml
  13. {
  14. public $rules = array(
  15. 'lang' => '',
  16. 'templates' => '',
  17. 'bold' => '<strong>\\1</strong>',
  18. 'italic' => '<em>\\1</em>',
  19. 'underline' => '<span style="text-decoration: underline;">\\1</span>',
  20. 'externalwithtext' => '<a href="\\1" onclick="window.open(this.href); return false;">\\2</a>',
  21. 'externalnotext' => '<a href="\\1" onclick="window.open(this.href); return false;">\\1</a>',
  22. 'hr' => '<hr />'
  23. );
  24. public function heading($text, $pieces)
  25. {
  26. static $heading_names = array();
  27. foreach ( $pieces as $i => $piece )
  28. {
  29. $tocid = sanitize_page_id(trim($piece['text']));
  30. // (bad) workaround for links in headings
  31. $tocid = str_replace(array('[', ']'), '', $tocid);
  32. // conflict avoidance
  33. if ( isset($heading_names[$tocid]) )
  34. {
  35. $id = 2;
  36. while ( isset($heading_names["{$tocid}{$id}"]) )
  37. $id++;
  38. $tocid .= $id;
  39. }
  40. $heading_names[$tocid] = true;
  41. $tag = '<h' . $piece['level'] . ' id="head:' . $tocid . '">';
  42. $tag .= trim($piece['text']);
  43. $tag .= '</h' . $piece['level'] . '>';
  44. $text = str_replace(Carpenter::generate_token($i), $tag, $text);
  45. }
  46. return $text;
  47. }
  48. public function multilist($text, $pieces)
  49. {
  50. foreach ( $pieces as $i => $piece )
  51. {
  52. switch($piece['type'])
  53. {
  54. case 'unordered':
  55. default:
  56. $btag = 'ul';
  57. $itag = 'li';
  58. break;
  59. case 'ordered':
  60. $btag = 'ol';
  61. $itag = 'li';
  62. break;
  63. case 'indent':
  64. $btag = 'dl';
  65. $itag = 'dd';
  66. break;
  67. }
  68. $list = "<_paragraph_bypass><$btag>\n";
  69. $spacing = '';
  70. $depth = 1;
  71. foreach ( $piece['items'] as $j => $item )
  72. {
  73. // most of this just goes into pretty formatting.
  74. // everything else goes into meeting the PITA requirement that if you're going
  75. // another level deep, HTML requires the next level to be INSIDE of the <dd>/<li> tag.
  76. $itemdepth = $item['depth'];
  77. if ( $itemdepth > $depth )
  78. {
  79. while ( $depth < $itemdepth )
  80. {
  81. $spacing .= ' ';
  82. $list .= "{$spacing}<$btag>\n";
  83. $depth++;
  84. }
  85. }
  86. else if ( $itemdepth < $depth )
  87. {
  88. while ( $depth > $itemdepth )
  89. {
  90. $list .= "{$spacing}</$btag>\n";
  91. $spacing = substr($spacing, 4);
  92. $list .= "{$spacing}</$itag>\n";
  93. $spacing = substr($spacing, 4);
  94. $depth--;
  95. }
  96. }
  97. $list .= "{$spacing} <$itag>" . nl2br($item['text']);
  98. if ( ( isset($piece['items'][ ++$j ]) && $piece['items'][ $j ]['depth'] <= $itemdepth ) || !isset($piece['items'][ $j ]) )
  99. {
  100. $list .= "</$itag>\n";
  101. }
  102. else
  103. {
  104. $list .= "\n";
  105. $spacing .= " ";
  106. }
  107. }
  108. while ( $depth > 1 )
  109. {
  110. $list .= "{$spacing}</$btag>\n";
  111. $spacing = substr($spacing, 4);
  112. $list .= "{$spacing}</$itag>\n";
  113. $spacing = substr($spacing, 4);
  114. $depth--;
  115. }
  116. $list .= "</$btag></_paragraph_bypass>\n";
  117. $text = str_replace(Carpenter::generate_token($i), $list, $text);
  118. }
  119. return $text;
  120. }
  121. public function blockquote($text)
  122. {
  123. return $text;
  124. }
  125. public function blockquotepost($text, $rand_id)
  126. {
  127. $text = strtr($text, array(
  128. "<p>{blockquote:$rand_id}<br />" => '<blockquote>',
  129. "<br />\n{/blockquote:$rand_id}</p>" => '</blockquote>',
  130. "{blockquote:$rand_id}" => '<blockquote>',
  131. "{/blockquote:$rand_id}" => '</blockquote>'
  132. ));
  133. $text = strtr($text, array(
  134. "<blockquote><br />" => '<blockquote>',
  135. "</blockquote><br />" => '</blockquote>'
  136. ));
  137. return $text;
  138. }
  139. public function paragraph($text, $pieces)
  140. {
  141. foreach ( $pieces as $i => $piece )
  142. {
  143. $text = str_replace(Carpenter::generate_token($i), '<p>' . nl2br($piece) . '</p>', $text);
  144. }
  145. return $text;
  146. }
  147. public function mailtonotext($pieces)
  148. {
  149. $pieces[2] = $pieces[1];
  150. return $this->mailtowithtext($pieces);
  151. }
  152. public function mailtowithtext($pieces)
  153. {
  154. global $email;
  155. return $email->encryptEmail($pieces[1], '', '', $pieces[2]);
  156. }
  157. public function code($match)
  158. {
  159. return '<pre class="wikitext-code"><final>' . htmlspecialchars($match[1]) . '</final></pre>';
  160. }
  161. }
  162. // Alias internal link parsing to RenderMan's method
  163. function parser_mediawiki_xhtml_internallink($text)
  164. {
  165. return RenderMan::parse_internal_links($text);
  166. }