Q:

MediaWiki to Markdown conversion with PHP preg_replace

Since I discovered BoostNote (Open source note-taking app made for programmers), I decided to migrate from a local MediaWiki (on a NAS) to this new app.
But that also means migrating from MediaWiki syntax to Markdown!!
I tried several online conversion tools, but they didn't work very well. The most interesting one is Pandoc, but it also has a few problems (ex. I couldn't force it to use fenced blocks instead of indentation to delimit code blocks).  
SOLUTION:
I ended up writing a tiny PHP script which represents a quick working solution (altough it may not be very elegant and polished ;) ). It is based on php preg_replace() function.
(The script will be available in the accepted answer below... if you have any suggestion, please post a comment)

A:

Usage:

save MediaWiki markup in a txt file ('mediawiki.txt') in the same directory of the php script
run the script
a new txt file ('markdown.txt') will be created usgin markdown syntax

Enjoy!!
<?php
//Before running this script, fix manually all the mixed list (ordered + unordered). It's the only missing feature.

$string = file_get_contents("mediawiki.txt");

$pattern = array(
    "/\*\*\*(?![^<]*>|[^<>]*<\/)/", //List: replace '***' with '    * '
    "/\*\*(?![^<]*>|[^<>]*<\/)/", //List: replace '**' with '  * '
    "/\*(?!\s+|[^<]*>|[^<>]*<\/)/", //List: replace '*' with '* '
    "/\#\#\#(?![^<]*>|[^<>]*<\/)/", //List: replace '###' with '      1. '
    "/\#\#(?![^<]*>|[^<>]*<\/)/", //List: replace '##' with '   1. '
    "/\#(?![^<]*>|[^<>]*<\/)/", //List: replace '#' with '1. '
    "/\'\'\'\'\'(.*?)\'\'\'\'\'(?![^<]*>|[^<>]*<\/)/", //Bold & Italic: replace "'''''(text)'''''" with "**_(text)_**"
    "/\'\'\'([\s\S]*?)\'\'\'(?![^<]*>|[^<>]*<\/)/", //Bold: replace "'''(text)'''" with "**(text)**"
    "/\'\'([\s\S]*?)\'\'(?![^<]*>|[^<>]*<\/)/", //Italic: replace "''(text)''" with "_(text)_"
    "/\=\=\=\=([\s\S]*?)\=\=\=\=(?![^<]*>|[^<>]*<\/)/", //Headings: replace "====(text)====" with "#### (text)"
    "/\=\=\=([\s\S]*?)\=\=\=(?![^<]*>|[^<>]*<\/)/", //Headings: replace "===(text)===" with "### (text)"
    "/\=\=([\s\S]*?)\=\=(?![^<]*>|[^<>]*<\/)/", //Headings: replace "==(text)==" with "## (text)"
    "/\=([\s\S]*?)\=(?![^<]*>|[^<>]*<\/)/", //Headings: replace "=(text)=" with "# (text)"
    "/\{\{[\s\S]*?\|([\s\S]*?)\}\}(?![^<]*>|[^<>]*<\/)/", //Notes: replace "{{...|(text)}}" with "(text)"
    "/\<pre[\s\S]*?\>([\s\S]*?)\<\/pre\>/", //Code block: replace "<pre ...>(code)</pre>" with "```java```"  !!CHANGE THIS (php, javascript, etc)!!
    "/\<code\>([\s\S]*?)\<\/code\>/", //Inline code: replace "<code>(code)</code>" with "`(code)`"
);

$replacement = array(
    "    * ",
    "  * ",
    "* ",
    "      1. ",
    "   1. ",
    "1. ",
    "**_$1_**",
    "**$1**",
    "_$1_",
    "#### $1",
    "### $1",
    "## $1",
    "# $1",
    "$1",
    "```java
$1
```",
    "`$1`",
);

$parsedString = preg_replace($pattern, $replacement, $string);

file_put_contents("markdown.txt", $parsedString);
?>

