Dotclear

source: plugins/wikiTables/lib/lib.wikiTables.php @ 2747

Revision 2747, 3.4 KB checked in by Moe, 13 years ago (diff)

Wiki Tables 0.1.1:

  • fixed bug with icon (closes #598)
  • compatibility with PHP 5.3 ?
  • added french locales
Line 
1<?php
2# ***** BEGIN LICENSE BLOCK *****
3#
4# This file is part of Wiki Tables, a plugin for Dotclear 2
5# Copyright (C) 2009,2010 Moe (http://gniark.net/)
6#
7# Wiki Tables is free software; you can redistribute it and/or
8# modify it under the terms of the GNU General Public License v2.0
9# as published by the Free Software Foundation.
10#
11# Wiki Tables is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program; if not, write to the Free Software Foundation,
18# Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19#
20# Icon (icon.png) is from Silk Icons :
21# http://www.famfamfam.com/lab/icons/silk/
22#
23# ***** END LICENSE BLOCK *****
24
25if (!defined('DC_RC_PATH')) {return;}
26
27class wikiTables
28{
29     public static function transform($text,$args)
30     {
31          $table_extra_html = '';
32          $caption = '';
33          $content = '';
34         
35          $row_extra_html = '';
36         
37          $text = trim($text);
38         
39          if (!preg_match('/\{\|(.+?)\n(.+?)\n\|\}/msu',
40               $text,$match))
41          {
42               return(__('invalid table'));
43          }
44         
45          if (!empty($match[1]))
46          {
47               $table_extra_html = ''.$match[1];
48          }
49         
50          $lines = explode("\n",$match[2]);
51         
52          $row = '';
53         
54          foreach ($lines as $line)
55          {
56               $line = trim($line);
57               if (empty($line)) {continue;}
58               
59               $l2 = substr($line,0,2);
60               $l1 = substr($l2,0,1);
61               
62               if ($l2 == '|-')
63               {
64                    $end = substr($line,2);
65                   
66                    if (!empty($row))
67                    {
68                         $content .= '<tr '.$row_extra_html.'>'.$row.'</tr>'."\n";
69                    }
70                   
71                    $row = '';
72                    $row_extra_html = $end;
73               }
74               elseif ($l2 == '|+')
75               {
76                    $end = substr($line,2);
77                   
78                    if (strpos($end,'|') !== false)
79                    {
80                         $explode = explode('|',$str,2);
81                         $caption = '<caption '.$explode[0].'>'.$explode[1].
82                              '</caption>';
83                    }
84                    else
85                    {
86                         $caption = '<caption>'.$end.'</caption>';
87                    }
88               }
89               elseif ($l1 == '!')
90               {
91                    $end = substr($line,1);
92                   
93                    if (strpos($end,'!!') !== false)
94                    {
95                         $cells = explode('!!',$end);
96                         foreach ($cells as $cell)
97                         {
98                              $row .= self::th($cell);
99                         }
100                    }
101                    elseif (strpos($end,'||') !== false)
102                    {
103                         $cells = explode('||',$end);
104                         foreach ($cells as $cell)
105                         {
106                              $row .= self::th($cell);
107                         }
108                    }
109                    else
110                    {
111                         $row .= self::th($end);
112                    }
113               }
114               elseif ($l1 == '|')
115               {
116                    $end = substr($line,1);
117                   
118                    if (strpos($end,'||') !== false)
119                    {
120                         $cells = explode('||',$end);
121                         foreach ($cells as $cell)
122                         {
123                              $row .= self::td($cell);
124                         }
125                    }
126                    else
127                    {
128                         $row .= self::td($end);
129                    }
130               }
131               else
132               {
133                    $row .= $line;
134                    //throw new Exception(__('invalid line:').' '.$line);
135               }
136          }
137         
138          # if there is no '|-' at the end
139          $content .= '<tr '.$row_extra_html.'>'.$row.'</tr>'."\n";
140         
141          return('<table'.$table_extra_html.'>'.
142          $caption.
143          $content.
144          '</table>');
145     }
146     
147     public static function th($str)
148     {
149          if (strpos($str,'!') !== false)
150          {
151               $explode = explode('!',$str,2);
152               return('<th '.$explode[0].'>'.$explode[1].'</th>');
153          }
154         
155          return('<th>'.$str.'</th>');
156     }
157     
158     public static function td($str)
159     {
160          if (strpos($str,'|') !== false)
161          {
162               $explode = explode('|',$str,2);
163               return('<td '.$explode[0].'>'.$explode[1].'</td>');
164          }
165         
166          return('<td>'.$str.'</td>');
167     }
168}
169
170?>
Note: See TracBrowser for help on using the repository browser.

Sites map