Skip to content

Instantly share code, notes, and snippets.

@dccampbell
Created March 12, 2016 03:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dccampbell/da49e243319b2f0374f3 to your computer and use it in GitHub Desktop.
Save dccampbell/da49e243319b2f0374f3 to your computer and use it in GitHub Desktop.
<?php
function getInput() {
return !empty($_POST['mediawiki']) ? $_POST['mediawiki'] : '';
}
function convertSyntax() {
$mediawiki = getInput();
if(empty($mediawiki)) return '';
$replacements = array(
'^[ ]*=([^=])'=>'<h1> ${1}', '([^=])=[ ]*$'=>'${1} </h1>',
'^[ ]*==([^=])'=>'<h2> ${1}', '([^=])==[ ]*$'=>'${1} </h2>',
'^[ ]*===([^=])'=>'<h3> ${1}', '([^=])===[ ]*$'=>'${1} </h3>',
'^[ ]*====([^=])'=>'<h4> ${1}', '([^=])====[ ]*$'=>'${1} </h4>',
'^[ ]*=====([^=])'=>'<h5> ${1}', '([^=])=====[ ]*$'=>'${1} </h5>',
'^[ ]*======([^=])'=>'<h6> ${1}', '([^=])======[ ]*$'=>'${1} </h6>',
'<\/?h1>'=>'======', '<\/?h2>'=>'=====', '<\/?h3>'=>'====', '<\/?h4>'=>'===', '<\/?h5>'=>'==', '<\/?h6>'=>'=',
'^[\*#]{4}\* ?'=>' * ', '^[\*#]{3}\* ?'=>' * ', '^[\*#]{2}\* ?'=>' * ', '^[\*#]{1}\* ?'=>' * ', '^\* ?'=>' * ',
'^[\*#]{4}# ?'=>' \- ', '^[\*\#]{3}\# ?'=>' \- ', '^[\*\#]{2}\# ?'=>' \- ', '^[\*\#]{1}\# ?'=>' \- ', '^\# ?'=>' - ',
'([^\[])\[([^\[])'=>'${1}[[${2}', '^\[([^\[])'=>'[[${1}', '([^\]])\]([^\]])'=>'${1}]]${2}', '([^\]])\]$'=>'${1}]]', '(\[\[[^| \]]*) ([^|\]]*\]\])'=>'${1}|${2}',
"'''"=>"**", "''"=>"//",
"^[ ]*:"=>">", ">:"=>">>", ">>:"=>">>>", ">>>:"=>">>>>", ">>>>:"=>">>>>>", ">>>>>:"=>">>>>>>", ">>>>>>:"=>">>>>>>>",
"<pre>"=>"<code>",
"<br[^>]*>"=>"\\\\\\\\",
"<\/pre>"=>"<\/code>"
);
$dokuwiki = split("\r\n",stripslashes($mediawiki));
if(!empty($dokuwiki)) {
foreach($replacements as $k=>$v){
$dokuwiki = preg_replace('/'.$k.'/',$v,$dokuwiki);
}
}
return join("\r\n",$dokuwiki);
}
?><!DOCTYPE html>
<html>
<head><title>Mediawiki to Dokuwiki Converter</title></head>
<body>
<form action="" method="post">
<div style="float:left; width: 49%;">
<h2>Mediawiki:</h2>
<textarea name="mediawiki" style="min-height:400px; width:98%;"><?= getInput() ?></textarea>
</div>
<div style="float:left; width: 49%;">
<h2>Dokuwiki:</h2>
<textarea name="dokuwiki" style="min-height:400px; width:98%; background-color:#F8F8F8" readonly><?= convertSyntax() ?></textarea>
</div>
<div style="clear:both;">
<input type="submit" value="convert">
</div>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment