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