-
Notifications
You must be signed in to change notification settings - Fork 10
/
generate-phpunit.php
113 lines (92 loc) · 4.36 KB
/
generate-phpunit.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
// set your language (en/ja/zh_cn)
$lang = 'en';
$ver = '4.2';
exec("rm -rf PHPUnit.docset/Contents/Resources/");
exec("mkdir -p PHPUnit.docset/Contents/Resources/");
exec("wget -rkl1 http://phpunit.de/manual/current/{$lang}/index.html");
exec("mv " . __DIR__ . "/phpunit.de/manual/current/{$lang} " . __DIR__ . "/PHPUnit.docset/Contents/Resources/Documents/");
exec("rm -r " . __DIR__ . "/phpunit.de/");
file_put_contents(__DIR__ . "/PHPUnit.docset/Contents/Info.plist", <<<ENDE
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleIdentifier</key>
<string>phpunit-{$lang}</string>
<key>CFBundleName</key>
<string>PHPUnit {$ver}-{$lang}</string>
<key>DocSetPlatformFamily</key>
<string>phpunit</string>
<key>isDashDocset</key>
<true/>
<key>dashIndexFilePath</key>
<string>index.html</string>
</dict>
</plist>
ENDE
);
copy(__DIR__ . "/icon.png", __DIR__ . "/PHPUnit.docset/icon.png");
$dom = new DomDocument;
@$dom->loadHTMLFile(__DIR__ . "/PHPUnit.docset/Contents/Resources/Documents/index.html");
$db = new sqlite3(__DIR__ . "/PHPUnit.docset/Contents/Resources/docSet.dsidx");
$db->query("CREATE TABLE searchIndex(id INTEGER PRIMARY KEY, name TEXT, type TEXT, path TEXT)");
$db->query("CREATE UNIQUE INDEX anchor ON searchIndex (name, type, path)");
$html = file_get_contents(__DIR__ . "/PHPUnit.docset/Contents/Resources/Documents/index.html");
$p = strpos($html, '<nav');
if ($p !== false) {
$q = strpos($html, '</nav');
$html = substr($html, 0, $p) . substr($html, $q + 6);
file_put_contents(__DIR__ . "/PHPUnit.docset/Contents/Resources/Documents/index.html", $html);
}
// add links from the table of contents
$links = $edited = array();
foreach ($dom->getElementsByTagName("a") as $a) {
$href = $a->getAttribute("href");
$str = substr($href, 0, 6);
if ($str[0] == '.') continue;
if ($str == 'https:' || !strncmp($str, 'http:', 5)) continue;
$file = preg_replace("/#.*$/", "", $href);
if (!isset($edited[$file]) && $file != "index.html") {
$html = file_get_contents(__DIR__ . "/PHPUnit.docset/Contents/Resources/Documents/" . $file);
$p = strpos($html, '<div class="col-md-4 col-lg-3">');
if ($p !== false) {
$q = strpos($html, '<div class="col-md-8 col-lg-9">');
$html = substr($html, 0, $p) . "<div style='padding: 1.5em'>" . substr($html, $q + 31);
$p = strpos($html, '<nav');
$q = strpos($html, '</nav');
$html = substr($html, 0, $p) . substr($html, $q + 6);
file_put_contents(__DIR__ . "/PHPUnit.docset/Contents/Resources/Documents/" . $file, $html);
}
$edited[$file] = true;
}
$name = trim(preg_replace("#\s+#", " ", preg_replace("#^[A-Z0-9–]+\.#", "", $a->nodeValue)));
if (empty($name)) continue;
$class = "Guide";
if (substr($href, 0, 30) == "writing-tests-for-phpunit.html" && strpos($name, "(") !== false) $class = "Function";
$links[$name] = true;
$db->query("INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES (\"$name\",\"$class\",\"$href\")");
}
// now go through some of the files to add pointers to inline documentation
foreach (array("appendixes.assertions", "appendixes.annotations", "incomplete-and-skipped-tests", "test-doubles", "writing-tests-for-phpunit") as $file) {
$search = $replace = array();
@$dom->loadHTMLFile(__DIR__ . "/PHPUnit.docset/Contents/Resources/Documents/$file.html");
foreach ($dom->getElementsByTagName("td") as $td) {
if (!$td->firstChild) continue;
if (strtolower($td->firstChild->nodeName) != "code") continue;
$name = $td->firstChild->nodeValue;
if (!preg_match("#^([a-z_]+ )?([a-z0-9_]+\()#i", $name, $m)) continue;
$name = isset($m[2]) ? $m[2] : $m[1];
$anchor = preg_replace("#[^a-z]#i", "", $name);
$href = $file .".html#" . $anchor;
$search[] = '<td align="left"><code class="literal">' . $m[0];
$replace[] = '<td align="left"><code class="literal" style="white-space: normal" id="' . $anchor . '">' . $m[0];
$name .= ")";
// echo $name, " -> ", $href, "\n";
if (isset($links[$name])) continue;
$db->query("INSERT OR IGNORE INTO searchIndex(name, type, path) VALUES (\"$name\",\"Function\",\"$href\")");
}
$html = file_get_contents(__DIR__ . "/PHPUnit.docset/Contents/Resources/Documents/$file.html");
$html = str_replace($search, $replace, $html);
file_put_contents(__DIR__ . "/PHPUnit.docset/Contents/Resources/Documents/$file.html", $html);
}