-
-
Save Danack/f37e4092b3e0aea5c0d248f24b3dfc0c to your computer and use it in GitHub Desktop.
docuwiki to markdown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function convert_file_to_markdown(string $file) | |
{ | |
$changes = [ | |
// Titles | |
'#======(.*)======#iu' => '#\1', | |
'#=====(.*)=====#iu' => '##\1', | |
'#====(.*)====#iu' => '###\1', | |
'#===(.*)===#iu' => '####\1', | |
"#\[\[(.+)\|(.+)\]\]#iu" => '[\2](\1)', | |
"#''#iu" => '`', | |
'#<code (.+)>#iu' => '``` \1', | |
'#</code>#iu' => '```', | |
'#%%(.*)%%#iu' => '``` \1', | |
'#<nowiki>(.*)</nowiki>#iu' => '\1' | |
]; | |
$filename = $file . '.docuwiki'; | |
$contents = file_get_contents($filename); | |
if ($contents === false) { | |
echo "Failed to read file $filename"; | |
exit(-1); | |
} | |
$new_contents = preg_replace( | |
array_keys($changes), | |
array_values($changes), | |
$contents | |
); | |
$new_filename = $filename = $file . '_test.md'; | |
$file_written = file_put_contents( | |
$new_filename, | |
$new_contents | |
); | |
if ($file_written !== false) { | |
echo "yay, written\n"; | |
} | |
else { | |
echo "Boo, not written, written\n"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment