-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.php
More file actions
94 lines (82 loc) · 1.84 KB
/
filter.php
File metadata and controls
94 lines (82 loc) · 1.84 KB
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
<?php
$file = $argv[1];
/* first processing
*/
if (file_exists($file)) {
$lines = file($file);
}
else {
exit("No such file: $file\n");
}
$remove_before = 'ユーザガイドを検索';
$remove_after = '前のトピック|ページの先頭';
$new_doc = '';
$state = 'head';
$in_list = FALSE;
foreach ($lines as $line) {
if ($state === 'head') {
if (preg_match('/' . $remove_before . '/u', $line, $matches)) {
//var_dump($matches);
$state = 'after_head';
}
}
else if ($state === 'after_head') {
if ($line != "\n") {
$state = 'content';
$new_doc .= $line;
}
}
else { // content
if (preg_match('/' . $remove_after . '/u', $line, $matches)) {
break;
}
else {
if (preg_match('/^\s*Note:\s(.*)/u', $line, $m)) {
//var_dump($m);
$new_doc .= '.. note:: ' . $m[1];
}
else if (preg_match('/^- (.+)/u', $line, $m)) {
//var_dump($m);
$new_doc .= '- ' . $m[1] . "\n";
$in_list = TRUE;
}
else if (preg_match('/^ - (.+)/u', $line, $m)) {
//var_dump($m);
$new_doc .= ' - ' . $m[1] . "\n";
$in_list = TRUE;
}
else if ($line == "\n") {
$in_list = FALSE;
$new_doc .= $line;
}
else if ($in_list) {
$new_doc .= ' ' . $line;
}
else {
$new_doc .= $line;
}
}
}
}
file_put_contents($file, $new_doc);
/* second processing
*/
if (file_exists($file)) {
$lines = file($file);
}
else {
exit("No such file: $file\n");
}
$new_doc = '';
$header_not_processed = TRUE;
foreach ($lines as $line) {
if ($header_not_processed && preg_match('/^###/u', $line, $matches)) {
//var_dump($matches);
$new_doc = $line . $new_doc . $line;
$header_not_processed = FALSE;
}
else {
$new_doc .= $line;
}
}
file_put_contents($file, $new_doc);