-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_doc
executable file
·104 lines (76 loc) · 2.31 KB
/
update_doc
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env php
<?php
$readmeFilePath = __DIR__ . '/README.md';
$readmeContent = file_get_contents($readmeFilePath);
/**
* PARSE ABBREVIATIONS
*/
$commandGroups = [];
$currentGroupIndex = 'Other';
foreach (file(__DIR__ . '/abbreviations.fish') as $line) {
$line = trim($line);
if (empty($line)) {
continue;
}
if (0 === strpos($line, '#')) {
$currentGroupIndex = trim(substr($line, 1));
} elseif (0 === strpos($line, 'abbr ')) {
list($void, $abbr, $command) = explode(' ', $line, 3);
$commandGroups[$currentGroupIndex][$abbr] = str_replace('|', '\\|', $command);
}
}
$lines = [];
foreach ($commandGroups as $groupLabel => $commands) {
$line = [
' <thead><tr><th colspan="2" align="left">' . $groupLabel . '</th></tr></thead>',
' <tbody>'
];
foreach ($commands as $abbr => $command) {
$line[] = ' <tr><th align="right">' . $abbr . '</th><td>' . trim($command, '\'') . '</td></tr>';
}
$line[] = ' </tbody>';
$lines[] = implode("\n", $line);
}
$startTag = '[//]: # (abbreviations)';
$endTag = '[//]: # (/abbreviations)';
$output = $startTag . "\n\n";
$output .= '<table>' . "\n";
$output .= implode("\n <tr><td colspan=\"2\"></td></tr>\n", $lines);
$output .= "\n" . '</table>';
$output .= "\n\n" . $endTag;
$readmeContent = preg_replace(
sprintf('`^%s.*%s$`sm', preg_quote($startTag), preg_quote($endTag)),
$output,
$readmeContent
);
/**
* PARSE COMMANDS
*/
$lines = [];
foreach (scandir(__DIR__ . '/functions') as $fileName) {
$filePath = __DIR__ . '/functions/' . $fileName;
if (!is_file($filePath)) {
continue;
}
$fileContent = file_get_contents($filePath);
if (!preg_match('`^#doc(.*)#enddoc$`ms', $fileContent, $match)) {
continue;
}
$lines[] = '';
$lines[] = '#### ' . basename($fileName, '.fish');
$lines[] = '';
$lines[] = preg_replace('`^# ?`m', '', trim($match[1]));
}
$startTag = '[//]: # (commands)';
$endTag = '[//]: # (/commands)';
array_unshift($lines, $startTag);
array_push($lines, '', $endTag);
$readmeContent = preg_replace(
sprintf('`^%s.*%s$`sm', preg_quote($startTag), preg_quote($endTag)),
implode("\n", $lines),
$readmeContent
);
/**
* WRITE NEW README
*/
file_put_contents($readmeFilePath, $readmeContent);