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