>
* Author: HendrikScheider
*/
class WikiPlugin_HtmlConverter extends WikiPlugin
{
function getName()
{
return "HtmlConverter";
}
function getDescription()
{
return _("Convert HTML markup into wiki markup.");
}
function getDefaultArguments()
{
return array();
}
function run($dbi, $argstr, &$request, $basepage)
{
/* plugin not yet has arguments - save for later (copied from UpLoad)
$args = $this->getArgs($argstr, $request);
extract($args);
*/
$form = HTML::form(array('action' => $request->getPostURL(),
'enctype' => 'multipart/form-data',
'method' => 'post'));
$contents = HTML::div(array('class' => 'wikiaction'));
$contents->pushContent(HTML::input(array('type' => 'hidden',
'name' => 'MAX_FILE_SIZE',
'value' => MAX_UPLOAD_SIZE)));
$contents->pushContent(HTML::input(array('name' => 'userfile',
'type' => 'file',
'size' => '50')));
$contents->pushContent(HTML::raw(" "));
$contents->pushContent(HTML::input(array('value' => _("Convert"),
'type' => 'submit')));
$form->pushContent($contents);
$message = HTML();
$userfile = $request->getUploadedFile('userfile');
if ($userfile) {
$userfile_name = $userfile->getName();
$userfile_name = basename($userfile_name);
$userfile_tmpname = $userfile->getTmpName();
if (!preg_match("/(\.html|\.htm)$/i", $userfile_name)) {
$message->pushContent(_("Only files with extension HTML are allowed"), HTML::br(), HTML::br());
} else {
$message->pushContent(_("Processed $userfile_name"), HTML::br(), HTML::br());
$message->pushContent(_("Copy the output below and paste it into your Wiki page."), HTML::br());
$message->pushContent($this->_process($userfile_tmpname));
}
} else {
$message->pushContent(HTML::br(), HTML::br());
}
$result = HTML();
$result->pushContent($form);
$result->pushContent($message);
return $result;
}
function _processA(&$file)
{
$file = eregi_replace(
"]*)>", "{{\\4}}", $file);
$file = eregi_replace("{{([-/a-zA-Z0-9._~#@%$?&=:\200-\377\(\)[:space:]]+)}}([^<]+)", "[ \\2 | \\1 ]", $file);
}
function _processIMG(&$file)
{
$img_regexp = "_]*>_";
$file = preg_replace($img_regexp, "\n\n[Upload:\\1]", $file);
}
function _processUL(&$file)
{
// put any -Tag in a new line to indent correctly and strip trailing white space (including new-lines)
$file = str_replace("\s*/", "", $file);
$enclosing_regexp = "_(.*)(.*)_is";
$indent_tag = " is inserted instead of the erased tags to have a paragraph generated at the end of the script
$file = $embedded_fragment_array[1] . "" . $indented . $embedded_fragment_array[3];
$found = preg_match($enclosing_regexp, $file, $embedded_fragment_array);
}
}
function _process($file_name)
{
$result = HTML();
$file = file_get_contents($file_name);
$file = html_entity_decode($file);
$ascii = '[\x00-\x7F]';
$euc = '[\xA1-\xFE][\xA1-\xFE]';
$character = "$ascii|$euc";
$this->_processA($file);
$this->_processIMG($file);
$this->_processUL($file);
$file = str_replace("\r\n", "\n", $file);
$file = eregi_replace("
]*>", "\n\n!!!!", $file);
$file = eregi_replace("]*>", "\n\n!!!", $file);
$file = eregi_replace("]*>", "\n\n!!", $file);
$file = eregi_replace("]*>", "\n\n!", $file);
$file = eregi_replace("]*>", "\n\n__", $file);
$file = eregi_replace("
", "\n\n", $file);
$file = eregi_replace("", "\n\n", $file);
$file = eregi_replace("", "\n\n", $file);
$file = eregi_replace("", "\n\n", $file);
$file = eregi_replace("", "__\n\n", $file);
$file = eregi_replace("
]*>", "\n----\n", $file);
$file = eregi_replace("- ]*>", "* ", $file);
// strip all tags, except for
, which is supported by wiki
// and 's which will be converted after compression.
$file = strip_tags($file, "
");
// strip
end tags with trailing white space
$file = preg_replace("_
\s*_i", "", $file);
// get rid of all blank lines
$file = preg_replace("/\n\s*\n/", "\n", $file);
// finally only add paragraphs where defined by inserting double new-lines
// be sure to only catch or
and not
!
// Actually tags with all white space and one new-line before
// and after around are replaced
$file = preg_replace("_\n?[^\S\n]*
]*|)>[^\S\n]*\n?_i", "\n\n", $file);
// strip attributes from
-Tags and add a new-line before
$file = preg_replace("_]*|)>_iU", "\n", $file);
$outputArea = HTML::textarea(array('rows' => '30', 'cols' => '80'));
$outputArea->pushContent(_($file));
$result->pushContent($outputArea);
return $result;
}
}