forked from Girgias/php-rfcs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
md-to-dokuwiki.php
82 lines (66 loc) · 2.4 KB
/
md-to-dokuwiki.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
const OUTPUT_DIR = __DIR__ . DIRECTORY_SEPARATOR . 'dokuwiki' . DIRECTORY_SEPARATOR;
const INPUT_DIR = __DIR__ . DIRECTORY_SEPARATOR;
const REGEXES = [
// Titles
'/^#{6}(?: ?)(.+)$/m' => '= ${1} =',
'/^#{5}(?: ?)(.+)$/m' => '== ${1} ==',
'/^#{4}(?: ?)(.+)$/m' => '=== ${1} ===',
'/^#{3}(?: ?)(.+)$/m' => '==== ${1} ====',
'/^#{2}(?: ?)(.+)$/m' => '===== ${1} =====',
'/^#{1}(?: ?)(.+)$/m' => '====== ${1} ======',
// Italics
'/\*{1}(.+)\*{1}/U' => '//${1}//',
// Code
'/`{3}php(\X+)`{3}/U' => '<PHP>${1}</PHP>',
'/`{3}(\X+)`{3}/U' => '<code>${1}</code>',
'/`{2}(\X+)`{2}/U' => '<php>${1}</php>',
'/`{1}(\X+)`{1}/U' => '<php>${1}</php>',
// Lists
'/^(?: {0,3})- (.+)$/m' => ' * ${1}',
// URLS
// Internal URLS
'/\[(.+)\]\((?:https?):\/\/wiki\.php\.net\/rfc\/(.+)\)/' => '[[rfc:${2}|${1}]]',
'/\[(.+)\]\((.+)\)/' => '[[${2}|${1}]]',
];
$TITLE_START_OFFSET = strlen('# PHP RFC: ');
define('TITLE_START_OFFSET', $TITLE_START_OFFSET);
const VOTING_SNIPPET_CODE = <<<'VOTING'
As per the voting RFC a yes/no vote with a 2/3 majority is needed for this proposal to be accepted.
Voting started on 2024-XX-XX and will end on 2024-XX-XX.
<doodle title="Accept RFC_TITLE RFC?" auth="girgias" voteType="single" closed="true">
* Yes
* No
</doodle>
VOTING;
$REGEX_PATTERNS = array_keys(REGEXES);
$REGEX_REPLACEMENTS = array_values(REGEXES);
define('REGEX_PATTERNS', $REGEX_PATTERNS);
define('REGEX_REPLACEMENTS', $REGEX_REPLACEMENTS);
function convert_md_to_php_dokuwiki(string $input): string {
$output = preg_replace(REGEX_PATTERNS, REGEX_REPLACEMENTS, $input);
// Dokuwiki does not support code markup in a link text
$output = str_replace(
['|<php>', '</php>]]'],
['|', ']]'],
$output,
);
// Voting snippet
$offset_first_newline = strpos($input, "\n");
$title = substr($input, TITLE_START_OFFSET, $offset_first_newline-TITLE_START_OFFSET);
$voting_code = str_replace('RFC_TITLE', $title, VOTING_SNIPPET_CODE);
$output = str_replace(
'VOTING_SNIPPET',
$voting_code,
$output,
);
return $output;
}
@mkdir(OUTPUT_DIR);
$dir = glob("*.md");
foreach ($dir as $fileName) {
$mdContent = file_get_contents(INPUT_DIR . $fileName);
$dokuwiki = convert_md_to_php_dokuwiki($mdContent);
file_put_contents(OUTPUT_DIR . $fileName, $dokuwiki);
echo $fileName . "\n";
}