]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/HtmlConverter.php
New FSF address
[SourceForge/phpwiki.git] / lib / plugin / HtmlConverter.php
1 <?php // -*-php-*-
2 // $Id$
3 /*
4  * Copyright 2005 Wincor Nixdorf International GmbH
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki 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
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 /**
24  * HtmlConverter:  Convert HTML tags as far to Wiki markup as possible
25  *          and eliminate all other HTML markup, so the output can be
26  *          copied and pasted into a wiki page.
27  *          Credit to an unknown programmer, who has provided the first
28  *          version 0.01 on http://www.gpgstudy.com/striphtml.phps
29  * Usage:   <<HtmlConverter >>
30  * Author:  HendrikScheider <hendrik.scheider@wincor-nixdorf.com>
31  */
32
33 class WikiPlugin_HtmlConverter extends WikiPlugin
34 {
35
36     function getName () {
37         return "HtmlConverter";
38     }
39
40     function getDescription () {
41         return _("Convert HTML markup into wiki markup.");
42     }
43
44     function getDefaultArguments() {
45         return array();
46     }
47
48     function run($dbi, $argstr, &$request, $basepage) {
49
50         /* plugin not yet has arguments - save for later (copied from UpLoad)
51         $args = $this->getArgs($argstr, $request);
52         extract($args);
53                 */
54
55         $form = HTML::form(array('action' => $request->getPostURL(),
56                                  'enctype' => 'multipart/form-data',
57                                  'method' => 'post'));
58         $contents = HTML::div(array('class' => 'wikiaction'));
59         $contents->pushContent(HTML::input(array('type' => 'hidden',
60                                                  'name' => 'MAX_FILE_SIZE',
61                                                  'value' => MAX_UPLOAD_SIZE)));
62         $contents->pushContent(HTML::input(array('name' => 'userfile',
63                                                  'type' => 'file',
64                                                  'size' => '50')));
65         $contents->pushContent(HTML::raw(" "));
66         $contents->pushContent(HTML::input(array('value' => _("Convert"),
67                                                  'type' => 'submit')));
68         $form->pushContent($contents);
69
70         $message = HTML();
71         $userfile = $request->getUploadedFile('userfile');
72         if ($userfile) {
73             $userfile_name = $userfile->getName();
74             $userfile_name = basename($userfile_name);
75             $userfile_tmpname = $userfile->getTmpName();
76
77             if ( !preg_match("/(\.html|\.htm)$/i", $userfile_name)) {
78                 $message->pushContent(_("Only files with extension HTML are allowed"),HTML::br(),HTML::br());
79             } else {
80                     $message->pushContent( _("Processed $userfile_name"), HTML::br(), HTML::br());
81                     $message->pushContent( _("Copy the output below and paste it into your Wiki page."), HTML::br());
82                     $message->pushContent( $this->_process( $userfile_tmpname));
83             }
84         } else {
85             $message->pushContent(HTML::br(),HTML::br());
86         }
87
88         $result = HTML();
89         $result->pushContent($form);
90         $result->pushContent($message);
91         return $result;
92     }
93
94         function _processA(&$file) {
95
96             $file = eregi_replace(
97             "<a([[:space:]]+)href([[:space:]]*)=([[:space:]]*)\"([-/.a-zA-Z0-9_~#@%$?&=:\200-\377\(\)[:space:]]+)\"([^>]*)>", "{{\\4}}", $file);
98
99                 $file = eregi_replace("{{([-/a-zA-Z0-9._~#@%$?&=:\200-\377\(\)[:space:]]+)}}([^<]+)</a>", "[ \\2 | \\1 ]", $file);
100         }
101
102         function _processIMG(&$file) {
103
104                 $img_regexp = "_<img\s+src\s*=\s*\"([-/.a-zA-Z0-9\_~#@%$?&=:\200-\377\(\)\s]+)\"[^>]*>_";
105
106             $file = preg_replace( $img_regexp, "\n\n[Upload:\\1]", $file);
107         }
108
109         function _processUL( &$file) {
110
111                 // put any <li>-Tag in a new line to indent correctly and strip trailing white space (including new-lines)
112                 $file = str_replace( "<li", "\n<li", $file);
113                 $file = preg_replace( "/<li>\s*/", "<li>", $file);
114
115                 $enclosing_regexp = "_(.*)<ul\s?[^>]*>((?U).*)</ul>(.*)_is";
116                 $indent_tag = "<li";
117                 $embedded_fragment_array = array();
118                 $found = preg_match( $enclosing_regexp, $file, $embedded_fragment_array);
119                 while ( $found) {
120                         $indented = str_replace( $indent_tag, "\t".$indent_tag, $embedded_fragment_array[2]);
121                         // string the file together again with the indented part in the middle.
122                         // a <p> is inserted instead of the erased <ul> tags to have a paragraph generated at the end of the script
123                         $file = $embedded_fragment_array[1] . "<p>" . $indented . $embedded_fragment_array[3];
124                         $found = preg_match( $enclosing_regexp, $file, $embedded_fragment_array);
125                 }
126         }
127
128         function _process( $file_name) {
129                 $result = HTML();
130             $file = file_get_contents( $file_name);
131                 $file = html_entity_decode( $file);
132
133                 $ascii  =  '[\x00-\x7F]';
134                 $euc  =  '[\xA1-\xFE][\xA1-\xFE]';
135                 $character  =  "$ascii|$euc";
136
137                 $this->_processA( $file);
138                 $this->_processIMG( $file);
139                 $this->_processUL( $file);
140
141                 $file = str_replace ("\r\n", "\n", $file);
142
143                 $file = eregi_replace ("<h1[[:space:]]?[^>]*>", "\n\n!!!!", $file);
144
145                 $file = eregi_replace ("<h2[[:space:]]?[^>]*>", "\n\n!!!", $file);
146
147                 $file = eregi_replace ("<h3[[:space:]]?[^>]*>", "\n\n!!", $file);
148
149                 $file = eregi_replace ("<h4[[:space:]]?[^>]*>", "\n\n!", $file);
150
151                 $file = eregi_replace ("<h5[[:space:]]?[^>]*>", "\n\n__", $file);
152
153                 $file = eregi_replace ("</h1>", "\n\n", $file);
154
155                 $file = eregi_replace ("</h2>", "\n\n", $file);
156
157                 $file = eregi_replace ("</h3>", "\n\n", $file);
158
159                 $file = eregi_replace ("</h4>", "\n\n", $file);
160
161                 $file = eregi_replace ("</h5>", "__\n\n", $file);
162
163                 $file = eregi_replace ("<hr[[:space:]]?[^>]*>", "\n----\n", $file);
164
165                 $file = eregi_replace ("<li[[:space:]]?[^>]*>", "* ", $file);
166
167                 // strip all tags, except for <pre>, which is supported by wiki
168                 // and <p>'s which will be converted after compression.
169                 $file = strip_tags($file, "<pre><p>");
170                 // strip </p> end tags with trailing white space
171                 $file = preg_replace ("_</p>\s*_i", "", $file);
172
173                 // get rid of all blank lines
174                 $file = preg_replace( "/\n\s*\n/", "\n", $file);
175
176                 // finally only add paragraphs where defined by inserting double new-lines
177                 // be sure to only catch <p> or <p[space]...> and not <pre>!
178                 // Actually <p> tags with all white space and one new-line before
179                 // and after around are replaced
180                 $file = preg_replace ("_\n?[^\S\n]*<p(\s[^>]*|)>[^\S\n]*\n?_i", "\n\n", $file);
181
182                 // strip attributes from <pre>-Tags and add a new-line before
183                 $file = preg_replace ("_<pre(\s[^>]*|)>_iU", "\n<pre>", $file);
184
185         $outputArea = HTML::textarea(array('rows' => '30', 'cols' => '80'));
186
187                 $outputArea->pushContent( _($file));
188                 $result->pushContent( $outputArea);
189                 return $result;
190         }
191 }
192 ?>