-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparse.php
152 lines (124 loc) · 4.25 KB
/
parse.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
$pageTemplate = "";
loadHtmlTemplate();
$files = array(
"rfc4918-manual.html",
"rfc2616-sec10.html",
"rfc-additions.html"
);
parseRfcHtmlDocuments($files);
// -------------------------------------------------------------------------
function parseRfcHtmlDocuments($files)
{
$statuses = array();
foreach($files as $filename)
{
$dom = new DOMDocument;
$dom->loadHTMLFile($filename);
// Just look for the <h3> elements, and then their peers
$items = $dom->getElementsByTagName('h3');
for ($i = 0; $i < $items->length; $i++)
{
$explanation = "";
list($major, $minor, $rev, $status, $description) = sscanf($items->item($i)->nodeValue, "%d.%d.%d %d %[^$]s");
// If we fail to parse, it's probably a section thing
if($status == 0)
{
// TODO: could actually make a section feature for this
// 10.1 Informational 1xx
// 10.2 Successful 2xx
// 10.3 Redirection 3xx
// 10.4 Client Error 4xx
// 10.5 Server Error 5xx
//print("" . $items->item($i)->nodeValue . "\n");
continue;
}
$p = $items->item($i)->nextSibling;
while($p != null)
{
if(strlen($p->nodeValue) > 1)
{
// TODO: consider capturing the document as-is ("<p>...</p>", instead of text value)
$explanation .= $p->nodeValue;
}
$p = $p->nextSibling;
// HACK: if the next node is the same as the original sibling (h3), bail
if($p->nodeName == $items->item($i)->nodeName)
$p = null;
}
// tidy things up - removed for now, causes formatting issues on 200
// $explanation = str_replace(" ", "", $explanation);
$result = array("status" => $status, "description" => trim($description), "explanation" => $explanation);
$statuses[] = $result;
}
}
foreach($statuses as $status)
{
// Write the full page, including template/header
generateHtmlPage($status);
// Write the cURL version page
generateCurlPage($status);
}
generateLimonadeRoutes($statuses);
}
// -------------------------------------------------------------------------
function generateHtmlPage($status)
{
global $pageTemplate;
$statusCode = $status["status"];
$filename = sprintf("./web/pages/%d.html.php", $status["status"]);
$fp = fopen($filename, "w");
$contents = $pageTemplate;
$externalSponsorFile = "../sponsor.php";
if(file_exists($externalSponsorFile))
require_once($externalSponsorFile);
else
require_once("sponsor.php");
$sponsor = sponsorDetails();
$contents = str_replace(__PAGE_TITLE_TEXT__, $status["status"] . ": " . $status["description"], $contents); // 200: OK
$contents = str_replace(__PAGE_HEADING_TEXT__, $status["status"] . ": " . $status["description"], $contents); // 200: OK
$contents = str_replace(__PAGE_PRE_TEXT__, $status["explanation"], $contents);
$contents = str_replace(__PAGE_STATUS_CODE__, $status["status"], $contents);
$contents = str_replace(__PAGE_LEAD_TEXT__, $sponsor[$statusCode], $contents);
$contents = str_replace(__PAGE_SPONSOR_TITLE__, $sponsor["title"], $contents);
$contents = str_replace(__PAGE_SPONSOR_URL__, $sponsor["url"], $contents);
fwrite($fp, $contents);
fclose($fp);
}
// -------------------------------------------------------------------------
function generateCurlPage($status)
{
$filename = sprintf("./web/pages/%d.curl.php", $status["status"]);
$fp = fopen($filename, "w");
fwrite($fp, sprintf("%d: %s\n%s", $status["status"], $status["description"], $status["explanation"]));
fclose($fp);
}
// -------------------------------------------------------------------------
function generateLimonadeRoutes($statuses)
{
// Create limonade_routes.php
$filename = sprintf("./web/pages/limonade_routes.php", $status["status"]);
$fp = fopen($filename, "w");
fwrite($fp, "<?php\n\n/* THIS FILE IS GENERATED BY PARSE.PHP - DO NOT MODIFY! */\n\n");
fwrite($fp, "$" . "pages = array(\n");
foreach($statuses as $status)
{
fwrite($fp, sprintf(" %d,\n", $status["status"]));
/*
dispatch('/NNN', 'page_NNN');
function page_NNN()
{
return 'Hello world!';
}
*/
//$dispatch = sprintf("dispatch('/%d', 'page_%d');\n", $status["status"], $status["status"]);
//fwrite($fp, $dispatch);
}
fwrite($fp, ");\n\n");
fclose($fp);
}
function loadHtmlTemplate()
{
global $pageTemplate;
$pageTemplate = file_get_contents("./template_html.txt");
}