initialize
[mdref/mdref] / index.php
1 <?php
2
3 error_reporting(E_ALL &~ E_DEPRECATED);
4
5 define("OUTPUT", fopen("php://memory", "w+"));
6
7 function cut(array $lines, array $specs) {
8 $delim = "[[:space:]]+";
9 $bytes = [];
10 $fields= [];
11
12 foreach ($specs as $spec => $value) {
13 switch ($spec) {
14 case "d":
15 $delim = $value;
16 break;
17 case "b":
18 $bytes = $value;
19 break;
20 case "f":
21 $fields = $value;
22 break;
23 }
24 }
25
26 $result = [];
27 if ($bytes) {
28 $func = "substr";
29 } else {
30 $func = function($a, $o = 0, $l = 0) {
31 return join(" ", array_slice($a, $o, $l ? $l+1 : count($a)-$o));
32 };
33 }
34 foreach ($lines as $line) {
35 if ($bytes) {
36 $spec = $bytes;
37 } else {
38 $line = split($delim, $line);
39 $spec = $fields;
40 }
41
42 if ($spec[0] == "-") {
43 $result[] = $func($line, 0, $spec[1]);
44 } elseif ($spec[1] == "-") {
45 if (empty($spec[2])) {
46 $result[] = $func($line, $spec[0]);
47 } else {
48 $result[] = $func($line, $spec[0], $spec[2]-$spec[0]);
49 }
50 } else {
51 $result[] = $line{$spec[0]};
52 }
53 }
54 return $result;
55 }
56
57 function head($file, $lines = 1) {
58 $ld = [];
59 if (($fd = fopen($file, "r"))) {
60 while ($lines--) {
61 $ld[] = fgets($fd);
62 }
63 }
64 return $ld;
65 }
66
67 function ns($file) {
68 return str_replace("/", "\\", str_replace("//", "/", trim($file, "/.")));
69 }
70
71 function urlpath($dir, $file) {
72 return (strlen($dir) ? $dir . "/" : "") . urlencode($file);
73 }
74
75 function ls($dir, $invert = false) {
76 fprintf(OUTPUT, "<ul>\n");
77 foreach (scandir($dir) as $file) {
78 $dir = trim($dir, "./");
79 $html = "";
80 if ($file === ".") {
81 continue;
82 } elseif ($file === "..") {
83 if ($dir === "" || $invert) {
84 continue;
85 }
86 $name = sprintf("namespace %s", ns(dirname($dir)));
87 } elseif (!$invert && is_dir("./$dir/$file")) {
88 $name = sprintf("namespace %s", ns("./$dir/$file"));
89 } elseif (!$invert && ctype_upper($file{0})) {
90 $name = join(" ", cut(head("./$dir/$file"), ["f"=>"1-2"]));
91 } elseif (!$invert || ctype_upper($file{0})) {
92 continue;
93 } else {
94 $name = ns($dir)."::".basename($file, ".md");
95 $html = "<p>".join(" ", cut(head("./$dir/$file"), ["f"=>"1-"]))."</p>";
96 }
97
98 fprintf(OUTPUT, "<li><a href=\"/%s\">%s</a>%s</li>\n",
99 urlpath($dir, $file),
100 htmlspecialchars($name),
101 $html);
102 }
103 fprintf(OUTPUT, "</ul>\n");
104 }
105
106 function ml($file) {
107 $pi = pathinfo($file);
108 if (ctype_upper($pi["filename"][0])) {
109 fprintf(OUTPUT, "<h2>Methods:</h2>\n");
110 $el = $pi["dirname"] . "/" . $pi["filename"];
111 ls($el, true);
112 }
113 }
114
115 function md($file) {
116 $r = fopen($file, "r");
117 $md = MarkdownDocument::createFromStream($r);
118 $md->compile();
119 $md->writeHtml(OUTPUT);
120 unset($md);
121 fclose($r);
122
123 // BS Markdown seeks around...
124 fseek(OUTPUT, 0, SEEK_END);
125
126 ml($file);
127 }
128
129 $r = new http\Env\Request;
130 $u = new http\Url($r->getRequestUrl());
131 $t = ["css"=>"text/css", "js"=>"application/javascript"];
132
133 switch($u->path) {
134 case "/index.js":
135 case "/index.css":
136 $s = new http\Env\Response;
137 $s->setHeader("Content-type", $t[pathinfo($u->path, PATHINFO_EXTENSION)]);
138 $s->setBody(new http\Message\Body(fopen(basename($u->path), "r")));
139 $s->send();
140 exit;
141 }
142
143 if (is_dir(".".$u->path)) {
144 ls(".".$u->path);
145 } else {
146 md(".".$u->path);
147 }
148
149 ?>
150 <!doctype html>
151 <html>
152 <head>
153 <meta charset="utf-8">
154 <title><?=$u->path?></title>
155 <link rel="stylesheet" href="/index.css">
156 </head>
157 <body>
158 <?php
159 rewind(OUTPUT);
160 fpassthru(OUTPUT);
161 fclose(OUTPUT);
162 ?>
163 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
164 <script src="/index.js"></script>
165 </body>
166 </html>