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