Utilisation:
- Enregistrer les balises MediaWiki dans un fichier txt ('mediawiki. txt) dans le même répertoire du script php
- exécutez le script
- un nouveau fichier txt ('markdown.txt') sera créé usgin minorée syntaxe
Appréciez!
<?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);
?>