/parsers/wikirenderer/WikiRenderer.class.php

https://gitlab.com/JKANetwork/ZeusWiki · PHP · 221 lines · 127 code · 24 blank · 70 comment · 25 complexity · 5249a01801e58559d8efec6c48f865a6 MD5 · raw file

  1. <?php
  2. /**
  3. * Wikirenderer is a wiki text parser. It can transform a wiki text into xhtml or other formats
  4. * @package WikiRenderer
  5. * @author Laurent Jouanneau
  6. * @copyright 2003-2008 Laurent Jouanneau
  7. * @link http://wikirenderer.jelix.org
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public 2.1
  11. * License as published by the Free Software Foundation.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. *
  22. */
  23. /**
  24. * Main class of WikiRenderenr. You should instantiate like this:
  25. * $ctr = new WikiRenderer();
  26. * $monTexteXHTML = $ctr->render($montexte);
  27. */
  28. class WikiRenderer {
  29. /**
  30. * @var string contains the final content
  31. */
  32. protected $_newtext;
  33. /**
  34. * @var WikiRendererBloc the current opened bloc element
  35. */
  36. protected $_currentBloc=null;
  37. /**
  38. * @var WikiRendererBloc the previous opened bloc element
  39. */
  40. protected $_previousBloc=null;
  41. /**
  42. * @var array list of all possible blocs
  43. */
  44. protected $_blocList= array();
  45. /**
  46. * @var WikiRendererBloc the default bloc used for unrecognized line
  47. */
  48. protected $_defaultBlock = null;
  49. /**
  50. * @var WikiInlineParser the parser for inline content
  51. */
  52. public $inlineParser=null;
  53. /**
  54. * list of lines which contain an error
  55. */
  56. public $errors=array();
  57. protected $config=null;
  58. /**
  59. * prepare the engine
  60. * @param WikiRendererConfig $config a config object. if it is not present, it uses wr3_to_xhtml rules.
  61. */
  62. function __construct( $config=null){
  63. if(is_string($config)){
  64. $f = WIKIRENDERER_PATH.'rules/'.basename($config).'.php';
  65. if(file_exists($f)){
  66. require_once($f);
  67. $this->config= new $config();
  68. }else
  69. throw new Exception('Wikirenderer : bad config name');
  70. }elseif(is_object($config)){
  71. $this->config=$config;
  72. }else{
  73. require_once(WIKIRENDERER_PATH . 'rules/wr3_to_xhtml.php');
  74. $this->config= new wr3_to_xhtml();
  75. }
  76. $this->inlineParser = new WikiInlineParser($this->config);
  77. foreach($this->config->bloctags as $name){
  78. $this->_blocList[]= new $name($this);
  79. }
  80. if ($this->config->defaultBlock) {
  81. $name = $this->config->defaultBlock;
  82. $this->_defaultBlock = new $name($this);
  83. }
  84. }
  85. /**
  86. * Main method to call to convert a wiki text into an other format, according to the
  87. * rules given to the constructor.
  88. * @param string $text the wiki text to convert
  89. * @return string the converted text.
  90. */
  91. public function render($text){
  92. $text = $this->config->onStart($text);
  93. $lignes=preg_split("/\015\012|\015|\012/",$text); // we split the text at all line feeds
  94. $this->_newtext=array();
  95. $this->errors=array();
  96. $this->_currentBloc = null;
  97. $this->_previousBloc = null;
  98. // we loop over all lines
  99. foreach($lignes as $num=>$ligne){
  100. if($this->_currentBloc){
  101. // a bloc is already open
  102. if($this->_currentBloc->detect($ligne)){
  103. $s =$this->_currentBloc->getRenderedLine();
  104. if($s !== false)
  105. $this->_newtext[]=$s;
  106. }else{
  107. $this->_newtext[count($this->_newtext)-1].=$this->_currentBloc->close();
  108. $found=false;
  109. foreach($this->_blocList as $bloc){
  110. if ($bloc->detect($ligne)) {
  111. $found=true;
  112. // we open the new bloc
  113. if($bloc->closeNow()){
  114. // if we have to close now the bloc, we close.
  115. $this->_newtext[]=$bloc->open().$bloc->getRenderedLine().$bloc->close();
  116. $this->_previousBloc = $bloc;
  117. $this->_currentBloc = null;
  118. }else{
  119. $this->_previousBloc = $this->_currentBloc;
  120. $this->_currentBloc = clone $bloc; // careful ! it MUST be a copy here !
  121. $this->_newtext[]=$this->_currentBloc->open().$this->_currentBloc->getRenderedLine();
  122. }
  123. break;
  124. }
  125. }
  126. if (!$found) {
  127. if (trim($ligne) == '') {
  128. $this->_newtext[] = '';
  129. }
  130. else if ($this->_defaultBlock) {
  131. $this->_defaultBlock->detect($ligne);
  132. $this->_newtext[] = $this->_defaultBlock->open().$this->_defaultBlock->getRenderedLine().$this->_defaultBlock->close();
  133. }
  134. else {
  135. $this->_newtext[] = $this->inlineParser->parse($ligne);
  136. }
  137. $this->_previousBloc = $this->_currentBloc;
  138. $this->_currentBloc = null;;
  139. }
  140. }
  141. }
  142. else {
  143. $found=false;
  144. // no opened bloc, we saw if the line correspond to a bloc
  145. foreach($this->_blocList as $bloc){
  146. if($bloc->detect($ligne)){
  147. $found=true;
  148. if($bloc->closeNow()){
  149. $this->_newtext[]=$bloc->open().$bloc->getRenderedLine().$bloc->close();
  150. $this->_previousBloc = $bloc;
  151. }else{
  152. $this->_currentBloc = clone $bloc; // careful ! it MUST be a copy here !
  153. $this->_newtext[]=$this->_currentBloc->open().$this->_currentBloc->getRenderedLine();
  154. }
  155. break;
  156. }
  157. }
  158. if(!$found){
  159. if (trim($ligne) == '') {
  160. $this->_newtext[] = '';
  161. }
  162. else if ($this->_defaultBlock) {
  163. $this->_defaultBlock->detect($ligne);
  164. $this->_newtext[] = $this->_defaultBlock->open().$this->_defaultBlock->getRenderedLine().$this->_defaultBlock->close();
  165. }
  166. else {
  167. $this->_newtext[]=$this->inlineParser->parse($ligne);
  168. }
  169. }
  170. }
  171. if($this->inlineParser->error){
  172. $this->errors[$num+1]=$ligne;
  173. }
  174. }
  175. if($this->_currentBloc){
  176. $this->_newtext[count($this->_newtext)-1].=$this->_currentBloc->close();
  177. }
  178. return $this->config->onParse(implode("\n",$this->_newtext));
  179. }
  180. /**
  181. * return the version of WikiRenderer
  182. * @access public
  183. * @return string version
  184. */
  185. public function getVersion(){
  186. return WIKIRENDERER_VERSION;
  187. }
  188. /**
  189. * @return WikiRendererConfig
  190. */
  191. public function getConfig(){
  192. return $this->config;
  193. }
  194. public function getPreviousBloc() {
  195. return $this->_previousBloc;
  196. }
  197. }