Skip to content

Instantly share code, notes, and snippets.

@Danack
Created October 19, 2020 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Danack/f37e4092b3e0aea5c0d248f24b3dfc0c to your computer and use it in GitHub Desktop.
Save Danack/f37e4092b3e0aea5c0d248f24b3dfc0c to your computer and use it in GitHub Desktop.
docuwiki to markdown.
<?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