PageRenderTime 67ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/wikirenderer/rules/classicwr_to_text.php

https://github.com/foxmask/Booster
PHP | 295 lines | 181 code | 57 blank | 57 comment | 14 complexity | e5e44d2af6a2f9b5aebdc0791e51eb44 MD5 | raw file
  1. <?php
  2. /**
  3. * classic wikirenderer syntax to plain text
  4. *
  5. * @package WikiRenderer
  6. * @subpackage rules
  7. * @author Laurent Jouanneau
  8. * @copyright 2003-2006 Laurent Jouanneau
  9. * @link http://wikirenderer.berlios.de
  10. *
  11. * This library is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public 2.1
  13. * License as published by the Free Software Foundation.
  14. *
  15. * This library is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with this library; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. class classicwr_to_text extends WikiRendererConfig {
  26. public $defaultTextLineContainer = 'WikiTextLine';
  27. public $textLineContainers = array(
  28. 'WikiTextLine'=>array( 'cwrtext_strong','cwrtext_em','cwrtext_code','cwrtext_q',
  29. 'cwrtext_cite','cwrtext_acronym','cwrtext_link', 'cwrtext_image', 'cwrtext_anchor')
  30. );
  31. /**
  32. * liste des balises de type bloc reconnus par WikiRenderer.
  33. */
  34. public $bloctags = array('cwrtext_title', 'cwrtext_list', 'cwrtext_pre','cwrtext_hr',
  35. 'cwrtext_blockquote','cwrtext_definition','cwrtext_table', 'cwrtext_p');
  36. public $simpletags = array('%%%'=>"\n");
  37. }
  38. // ===================================== déclarations des tags inlines
  39. class cwrtext_strong extends WikiTag {
  40. public $beginTag='__';
  41. public $endTag='__';
  42. }
  43. class cwrtext_em extends WikiTag {
  44. public $beginTag='\'\'';
  45. public $endTag='\'\'';
  46. }
  47. class cwrtext_code extends WikiTag {
  48. public $beginTag='@@';
  49. public $endTag='@@';
  50. function getContent(){ return '['.$this->contents[0].']';}
  51. }
  52. class cwrtext_q extends WikiTag {
  53. public $beginTag='^^';
  54. public $endTag='^^';
  55. protected $attribute=array('$$','lang','cite');
  56. public $separators=array('|');
  57. public function getContent(){
  58. if($this->separatorCount > 1)
  59. return '"'.$this->contents[0].'" ('.$this->contents[2].')';
  60. else
  61. return '"'.$this->contents[0].'"';
  62. }
  63. }
  64. class cwrtext_cite extends WikiTag {
  65. public $beginTag='{{';
  66. public $endTag='}}';
  67. protected $attribute=array('$$','title');
  68. public $separators=array('|');
  69. public function getContent(){
  70. if($this->separatorCount > 1)
  71. return '"'.$this->contents[0].'" ('.$this->contents[2].')';
  72. else
  73. return '"'.$this->contents[0].'"';
  74. }
  75. }
  76. class cwrtext_acronym extends WikiTag {
  77. public $beginTag='??';
  78. public $endTag='??';
  79. protected $attribute=array('$$','title');
  80. public $separators=array('|');
  81. public function getContent(){
  82. if($this->separatorCount > 0)
  83. return $this->contents[0].' ('.$this->contents[2].')';
  84. else
  85. return $this->contents[0];
  86. }
  87. }
  88. class cwrtext_anchor extends WikiTag {
  89. public $beginTag='~~';
  90. public $endTag='~~';
  91. protected $attribute=array('name');
  92. public $separators=array('|');
  93. public function getContent(){ return ''; }
  94. }
  95. class cwrtext_link extends WikiTag {
  96. public $beginTag='[';
  97. public $endTag=']';
  98. protected $attribute=array('$$','href','hreflang','title');
  99. public $separators=array('|');
  100. public function getContent(){
  101. $cntattr=count($this->attribute);
  102. $cnt=($this->separatorCount + 1 > $cntattr?$cntattr:$this->separatorCount+1);
  103. if($cnt == 1 ){
  104. return $this->wikiContentArr[0];
  105. }else{
  106. return $this->wikiContentArr[0].' ('.$this->wikiContentArr[1].')';
  107. }
  108. }
  109. }
  110. class cwrtext_image extends WikiTag {
  111. public $beginTag='((';
  112. public $endTag='))';
  113. protected $attribute=array('src','alt','align','longdesc');
  114. public $separators=array('|');
  115. public function getContent(){ return ''; }
  116. }
  117. // ===================================== déclaration des différents bloc wiki
  118. /**
  119. * traite les signes de types liste
  120. */
  121. class cwrtext_list extends WikiRendererBloc {
  122. public $type='list';
  123. protected $regexp="/^([\*#-]+)(.*)/";
  124. public function getRenderedLine(){
  125. return $this->_detectMatch[1].$this->_renderInlineTag($this->_detectMatch[2]);
  126. }
  127. }
  128. /**
  129. * traite les signes de types table
  130. */
  131. class cwrtext_table extends WikiRendererBloc {
  132. public $type='table';
  133. protected $regexp="/^\| ?(.*)/";
  134. protected $_openTag="--------------------------------------------";
  135. protected $_closeTag="--------------------------------------------\n";
  136. protected $_colcount=0;
  137. public function open(){
  138. $this->_colcount=0;
  139. return $this->_openTag;
  140. }
  141. public function getRenderedLine(){
  142. $result=explode(' | ',trim($this->_detectMatch[1]));
  143. $str='';
  144. $t='';
  145. if((count($result) != $this->_colcount) && ($this->_colcount!=0))
  146. $t="--------------------------------------------\n";
  147. $this->_colcount=count($result);
  148. for($i=0; $i < $this->_colcount; $i++){
  149. $str.= $this->_renderInlineTag($result[$i])."\t| ";
  150. }
  151. $str=$t."| ".$str;
  152. return $str;
  153. }
  154. }
  155. /**
  156. * traite les signes de types hr
  157. */
  158. class cwrtext_hr extends WikiRendererBloc {
  159. public $type='hr';
  160. protected $regexp='/^={4,} *$/';
  161. protected $_closeNow=true;
  162. public function getRenderedLine(){
  163. return "=======================================================\n";
  164. }
  165. }
  166. /**
  167. * traite les signes de types titre
  168. */
  169. class cwrtext_title extends WikiRendererBloc {
  170. public $type='title';
  171. protected $regexp="/^(\!{1,3})(.*)/";
  172. protected $_closeNow=true;
  173. protected $_minlevel=3;
  174. /**
  175. * indique le sens dans lequel il faut interpreter le nombre de signe de titre
  176. * true -> ! = titre , !! = sous titre, !!! = sous-sous-titre
  177. * false-> !!! = titre , !! = sous titre, ! = sous-sous-titre
  178. */
  179. protected $_order=false;
  180. public function getRenderedLine(){
  181. if($this->_order){
  182. $repeat= 4- strlen($this->_detectMatch[1]);
  183. if($repeat <1) $repeat=1;
  184. }else
  185. $repeat= strlen($this->_detectMatch[1]);
  186. return str_repeat("\n",$repeat)."\t".$this->_renderInlineTag($this->_detectMatch[2])."\n";
  187. }
  188. }
  189. /**
  190. * traite les signes de type paragraphe
  191. */
  192. class cwrtext_p extends WikiRendererBloc {
  193. public $type='p';
  194. public function detect($string){
  195. if($string=='') return false;
  196. if(preg_match('/^={4,} *$/',$string)) return false;
  197. $c=$string[0];
  198. if(strpos("*#-!| \t>;" ,$c) === false){
  199. $this->_detectMatch=array($string,$string);
  200. return true;
  201. }else{
  202. return false;
  203. }
  204. }
  205. }
  206. /**
  207. * traite les signes de types pre (pour afficher du code..)
  208. */
  209. class cwrtext_pre extends WikiRendererBloc {
  210. public $type='pre';
  211. protected $regexp="/^(\s)(.*)/";
  212. public function getRenderedLine(){
  213. return $this->_detectMatch[1].$this->_renderInlineTag($this->_detectMatch[2]);
  214. }
  215. }
  216. /**
  217. * traite les signes de type blockquote
  218. */
  219. class cwrtext_blockquote extends WikiRendererBloc {
  220. public $type='bq';
  221. protected $regexp="/^(\>+)(.*)/";
  222. public function getRenderedLine(){
  223. return $this->_detectMatch[1].$this->_renderInlineTag($this->_detectMatch[2]);
  224. }
  225. }
  226. /**
  227. * traite les signes de type définitions
  228. */
  229. class cwrtext_definition extends WikiRendererBloc {
  230. public $type='dfn';
  231. protected $regexp="/^;(.*) : (.*)/i";
  232. public function getRenderedLine(){
  233. $dt=$this->_renderInlineTag($this->_detectMatch[1]);
  234. $dd=$this->_renderInlineTag($this->_detectMatch[2]);
  235. return "$dt :\n\t$dd";
  236. }
  237. }
  238. ?>