101c9e77690e803f30828f0702f58cbfe23caafa
[mdref/mdref] / bin / ref2html
1 #!/usr/bin/env php
2 <?php
3
4 namespace mdref;
5
6 use http\Env\Request;
7 use http\Env\Response;
8 use http\Message\Body;
9 use function file_put_contents;
10
11 require_once __DIR__."/../vendor/autoload.php";
12 define("ROOT", dirname(__DIR__));
13 define("REF2HTML", true);
14
15 if ($argc < 3) {
16 fprintf(STDERR, "Usage: %s <basedir> <ref>[ <ref> ...]\n", $argv[0]);
17 fprintf(STDERR, " Note: the basedir will also be used as <base href>\n");
18 exit(1);
19 }
20 function say($fmt, ...$args) {
21 return fprintf(STDERR, $fmt, ...$args);
22 };
23 $out = $argv[1];
24 if (!is_dir($out) && !mkdir($out, 0775, true)) {
25 fprintf(STDERR, "Could not create output directory %s\n", $out);
26 exit(1);
27 }
28 $nul = fopen("/dev/null", "w");
29 $url = new BaseUrl("/" . $out . "/");
30 $url->scheme = null;
31 $url->host = null;
32 $ref = new Reference(array_slice($argv, 2));
33 $fmt = function(string $src, string $dst) use($ref, $out, $nul, $url) {
34 $req = new Request;
35 $req->setRequestMethod("GET");
36 $req->setRequestUrl($url . "./" . $src);
37 $res = new Response;
38 $res->setBody(new Body(fopen($dst, "w+")));
39 $act = new Action($ref, $req, $res, $url, $nul);
40 $act->handle();
41 };
42 $xfm = [
43 "php/PropertyProxy" => "propro/php/PropertyProxy",
44 "pq/Gateway" => "pq-gateway/pq/Gateway",
45 "pq/Query" => "pq-gateway/pq/Query",
46 ];
47 $red = function($from, $dest, $name) use($out, $url) {
48 $from = $out . "/" . str_replace($dest, $from, $name);
49 if (!is_dir(dirname($from))) {
50 mkdir(dirname($from), 0775, true);
51 }
52 file_put_contents($from . ".html", <<<EOF
53 <html>
54 <meta http-equiv='refresh' content='0; $url$name'>
55 </html>
56 EOF
57 );
58 };
59 $gen = function(Entry $entry) use($fmt, $out, $xfm, $red, &$gen) {
60 $src = $entry->getName();
61 $dir = $out . "/" . $src;
62 $dst = $dir . ".html";
63 foreach ($xfm as $from => $dest) {
64 if (strpos($src, $dest) !== false) {
65 say("Redirecting from %s to %s\n", $from, $dest);
66 $red($from, $dest, $src);
67 break;
68 }
69 }
70 if ($entry->hasIterator()) {
71 if (!is_dir($dir)) {
72 mkdir($dir, 0755, true);
73 }
74 foreach ($entry as $subentry) {
75 $gen($subentry);
76 }
77 }
78 say("Generating %s from %s\n", $dst, $src);
79 $fmt($src, $dst);
80 };
81 /** @var $repo Repo */
82 foreach ($ref as $repo) {
83 say("Entering ref %s\n", $repo->getName());
84 if (is_file($stub = $repo->getPath($repo->getName().".stub.php"))) {
85 copy($stub, $out . "/" . basename($stub));
86 }
87 foreach ($repo as $root) {
88 $gen($root);
89 }
90 }
91 $fmt("", $out . "/" . "index.html");
92
93 $presets = [
94 "AUTHORS",
95 "LICENSE",
96 "VERSION",
97 "public/index.css" => "index.css",
98 "public/index.js" => "index.js",
99 "public/favicon.ico" => "favicon.ico",
100 ];
101 foreach ($presets as $src => $dst) {
102 if (!is_string($src)) {
103 $src = $dst;
104 }
105 copy(ROOT . "/" . $src, $out . "/" . $dst);
106 }
107 // no jekyll
108 touch($out . "/.nojekyll");
109 // htacess for apache
110 file_put_contents($out . "/.htaccess", <<<EOF
111 Options -Indexes +MultiViews +FollowSymLinks
112 DirectorySlash Off
113
114 RewriteEngine On
115 RewriteCond %{REQUEST_FILENAME} -d
116 RewriteRule ^(.+)$ $1.html [L]
117
118 <Files *.php>
119 ForceType text/x-php
120 SetHandler default-handler
121 </Files>
122
123 EOF
124 );