]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/HtmlConverter.php
No underscore for private function
[SourceForge/phpwiki.git] / lib / plugin / HtmlConverter.php
1 <?php
2
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     {
38         return "HtmlConverter";
39     }
40
41     function getDescription()
42     {
43         return _("Convert HTML markup into wiki markup.");
44     }
45
46     function getDefaultArguments()
47     {
48         return array();
49     }
50
51     function run($dbi, $argstr, &$request, $basepage)
52     {
53
54         /* plugin not yet has arguments - save for later (copied from UpLoad)
55         $args = $this->getArgs($argstr, $request);
56         extract($args);
57                 */
58
59         $form = HTML::form(array('action' => $request->getPostURL(),
60             'enctype' => 'multipart/form-data',
61             'method' => 'post'));
62         $contents = HTML::div(array('class' => 'wikiaction'));
63         $contents->pushContent(HTML::input(array('type' => 'hidden',
64             'name' => 'MAX_FILE_SIZE',
65             'value' => MAX_UPLOAD_SIZE)));
66         $contents->pushContent(HTML::input(array('name' => 'userfile',
67             'type' => 'file',
68             'size' => '50')));
69         $contents->pushContent(HTML::raw(" "));
70         $contents->pushContent(HTML::input(array('value' => _("Convert"),
71             'type' => 'submit')));
72         $form->pushContent($contents);
73
74         $message = HTML();
75         $userfile = $request->getUploadedFile('userfile');
76         if ($userfile) {
77             $userfile_name = $userfile->getName();
78             $userfile_name = basename($userfile_name);
79             $userfile_tmpname = $userfile->getTmpName();
80
81             if (!preg_match("/(\.html|\.htm)$/i", $userfile_name)) {
82                 $message->pushContent(_("Only files with extension HTML are allowed"), HTML::br(), HTML::br());
83             } else {
84                 $message->pushContent(_("Processed $userfile_name"), HTML::br(), HTML::br());
85                 $message->pushContent(_("Copy the output below and paste it into your Wiki page."), HTML::br());
86                 $message->pushContent($this->process($userfile_tmpname));
87             }
88         } else {
89             $message->pushContent(HTML::br(), HTML::br());
90         }
91
92         $result = HTML();
93         $result->pushContent($form);
94         $result->pushContent($message);
95         return $result;
96     }
97
98     private function processA(&$file)
99     {
100
101         $file = eregi_replace(
102             "<a([[:space:]]+)href([[:space:]]*)=([[:space:]]*)\"([-/.a-zA-Z0-9_~#@%$?&=:\200-\377\(\)[:space:]]+)\"([^>]*)>", "{{\\4}}", $file);
103
104         $file = eregi_replace("{{([-/a-zA-Z0-9._~#@%$?&=:\200-\377\(\)[:space:]]+)}}([^<]+)</a>", "[ \\2 | \\1 ]", $file);
105     }
106
107     private function processIMG(&$file)
108     {
109
110         $img_regexp = "_<img\s+src\s*=\s*\"([-/.a-zA-Z0-9\_~#@%$?&=:\200-\377\(\)\s]+)\"[^>]*>_";
111
112         $file = preg_replace($img_regexp, "\n\n[Upload:\\1]", $file);
113     }
114
115     private function processUL(&$file)
116     {
117
118         // put any <li>-Tag in a new line to indent correctly and strip trailing white space (including new-lines)
119         $file = str_replace("<li", "\n<li", $file);
120         $file = preg_replace("/<li>\s*/", "<li>", $file);
121
122         $enclosing_regexp = "_(.*)<ul\s?[^>]*>((?U).*)</ul>(.*)_is";
123         $indent_tag = "<li";
124         $embedded_fragment_array = array();
125         $found = preg_match($enclosing_regexp, $file, $embedded_fragment_array);
126         while ($found) {
127             $indented = str_replace($indent_tag, "\t" . $indent_tag, $embedded_fragment_array[2]);
128             // string the file together again with the indented part in the middle.
129             // a <p> is inserted instead of the erased <ul> tags to have a paragraph generated at the end of the script
130             $file = $embedded_fragment_array[1] . "<p>" . $indented . $embedded_fragment_array[3];
131             $found = preg_match($enclosing_regexp, $file, $embedded_fragment_array);
132         }
133     }
134
135     private function process($file_name)
136     {
137         $result = HTML();
138         $file = file_get_contents($file_name);
139         $file = html_entity_decode($file);
140
141         $this->processA($file);
142         $this->processIMG($file);
143         $this->processUL($file);
144
145         $file = str_replace("\r\n", "\n", $file);
146
147         $file = eregi_replace("<h1[[:space:]]?[^>]*>", "\n\n!!!!", $file);
148
149         $file = eregi_replace("<h2[[:space:]]?[^>]*>", "\n\n!!!", $file);
150
151         $file = eregi_replace("<h3[[:space:]]?[^>]*>", "\n\n!!", $file);
152
153         $file = eregi_replace("<h4[[:space:]]?[^>]*>", "\n\n!", $file);
154
155         $file = eregi_replace("<h5[[:space:]]?[^>]*>", "\n\n__", $file);
156
157         $file = eregi_replace("</h1>", "\n\n", $file);
158
159         $file = eregi_replace("</h2>", "\n\n", $file);
160
161         $file = eregi_replace("</h3>", "\n\n", $file);
162
163         $file = eregi_replace("</h4>", "\n\n", $file);
164
165         $file = eregi_replace("</h5>", "__\n\n", $file);
166
167         $file = eregi_replace("<hr[[:space:]]?[^>]*>", "\n----\n", $file);
168
169         $file = eregi_replace("<li[[:space:]]?[^>]*>", "* ", $file);
170
171         // strip all tags, except for <pre>, which is supported by wiki
172         // and <p>'s which will be converted after compression.
173         $file = strip_tags($file, "<pre><p>");
174         // strip </p> end tags with trailing white space
175         $file = preg_replace("_</p>\s*_i", "", $file);
176
177         // get rid of all blank lines
178         $file = preg_replace("/\n\s*\n/", "\n", $file);
179
180         // finally only add paragraphs where defined by inserting double new-lines
181         // be sure to only catch <p> or <p[space]...> and not <pre>!
182         // Actually <p> tags with all white space and one new-line before
183         // and after around are replaced
184         $file = preg_replace("_\n?[^\S\n]*<p(\s[^>]*|)>[^\S\n]*\n?_i", "\n\n", $file);
185
186         // strip attributes from <pre>-Tags and add a new-line before
187         $file = preg_replace("_<pre(\s[^>]*|)>_iU", "\n<pre>", $file);
188
189         $outputArea = HTML::textarea(array('rows' => '30', 'cols' => '80'));
190
191         $outputArea->pushContent(_($file));
192         $result->pushContent($outputArea);
193         return $result;
194     }
195 }