MediaWiki 到 Markdown 的转换 PHP preg_replace
MediaWiki to Markdown conversion with PHP preg_replace
自从我发现 BoostNote(开源 为程序员制作的笔记应用程序),我决定从本地 MediaWiki(在 NAS 上)迁移到这个新应用。
但这也意味着从 MediaWiki 语法迁移到 Markdown!!
我尝试了几种在线转换工具,但效果不佳。最有趣的是 Pandoc,但它也有一些问题(例如,我不能强制它使用围栏块而不是缩进来分隔代码块)。
解决方案:
我最终写了一个小的 PHP 脚本,它代表了一个快速工作的解决方案(尽管它可能不是很优雅和完美 ;))。它基于 php preg_replace()
函数。
(该脚本将在下面的已接受答案中提供...如果您有任何建议,请 post 发表评论)
用法:
- 将 MediaWiki 标记保存在 php 脚本同一目录下的 txt 文件 ('mediawiki.txt') 中
- 运行 脚本
- 将使用 markdown 语法创建一个新的 txt 文件 ('markdown.txt')
尽情享受!!
<?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. ",
"**__**",
"****",
"__",
"#### ",
"### ",
"## ",
"# ",
"",
"```java
```",
"``",
);
$parsedString = preg_replace($pattern, $replacement, $string);
file_put_contents("markdown.txt", $parsedString);
?>
自从我发现 BoostNote(开源 为程序员制作的笔记应用程序),我决定从本地 MediaWiki(在 NAS 上)迁移到这个新应用。
但这也意味着从 MediaWiki 语法迁移到 Markdown!!
我尝试了几种在线转换工具,但效果不佳。最有趣的是 Pandoc,但它也有一些问题(例如,我不能强制它使用围栏块而不是缩进来分隔代码块)。
解决方案:
我最终写了一个小的 PHP 脚本,它代表了一个快速工作的解决方案(尽管它可能不是很优雅和完美 ;))。它基于 php preg_replace()
函数。
(该脚本将在下面的已接受答案中提供...如果您有任何建议,请 post 发表评论)
用法:
- 将 MediaWiki 标记保存在 php 脚本同一目录下的 txt 文件 ('mediawiki.txt') 中
- 运行 脚本
- 将使用 markdown 语法创建一个新的 txt 文件 ('markdown.txt')
尽情享受!!
<?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. ",
"**__**",
"****",
"__",
"#### ",
"### ",
"## ",
"# ",
"",
"```java
```",
"``",
);
$parsedString = preg_replace($pattern, $replacement, $string);
file_put_contents("markdown.txt", $parsedString);
?>