From 31c2b666caffa28ea1576ed9c2f4c2280f2a2d45 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Mon, 20 Jul 2015 08:16:41 +0200 Subject: [PATCH] static generation does not work with overlapping namespaces --- bin/gen-static | 14 ---- mdref/Action.php | 2 +- mdref/Generator.php | 180 -------------------------------------------- public/.htaccess | 2 - views/layout.phtml | 8 -- 5 files changed, 1 insertion(+), 205 deletions(-) delete mode 100755 bin/gen-static delete mode 100644 mdref/Generator.php diff --git a/bin/gen-static b/bin/gen-static deleted file mode 100755 index 15e8609..0000000 --- a/bin/gen-static +++ /dev/null @@ -1,14 +0,0 @@ -#!/usr/bin/env php -add("mdref", ROOT); - -$g = new mdref\Generator(REFS, @$argv[1]); -$g->run(); diff --git a/mdref/Action.php b/mdref/Action.php index 3e318be..37b3d51 100644 --- a/mdref/Action.php +++ b/mdref/Action.php @@ -59,7 +59,7 @@ class Action extends Observer { */ private function serveCanonical($ctl, $cnn) { $ctl->detachAll(Observer\View::class); - $ctl->getResponse()->setHeader("Location", $this->baseUrl->mod($cnn)); + $ctl->getResponse()->setHeader("Location", $this->baseUrl->mod(["path" => $cnn])); $ctl->getResponse()->setResponseCode(301); } diff --git a/mdref/Generator.php b/mdref/Generator.php deleted file mode 100644 index 7a58387..0000000 --- a/mdref/Generator.php +++ /dev/null @@ -1,180 +0,0 @@ -reference = new Reference(explode(PATH_SEPARATOR, $refs)); - $this->renderer = new Generator\Renderer($dir ?: "public/static"); - } - - /** - * Run the generator - */ - public function run() { - $this->generateRoot(); - foreach ($this->reference as $repo) { - $iter = new \RecursiveIteratorIterator($repo, - \RecursiveIteratorIterator::SELF_FIRST); - foreach ($iter as $ref) { - $this->generateEntry($ref); - } - } - } - - /** - * Generate index.html and LICENSE.html - */ - private function generateRoot() { - printf("Generating index ...\n"); - $data = $this->createPayload(null); - $data->ref = "index"; - $this->renderer->persist($data); - - printf("Generating LICENSE ...\n"); - $data->text = file_get_contents(__DIR__."/../LICENSE"); - $data->ref = "LICENSE"; - $this->renderer->persist($data); - } - - /** - * Generate HTML for an entry - * @param \mdref\Entry $ref - */ - private function generateEntry(Entry $ref) { - printf("Generating %s ...\n", $ref->getName()); - $data = $this->createPayload($ref); - $this->renderer->persist($data); - } - - /** - * Create the view payload - * @param \mdref\Entry $ref - * @param \mdref\Generator\Renderer $view - * @return \stdClass - */ - private function createPayload(Entry $ref = null) { - $pld = new \stdClass; - - $pld->quick = [$this->reference, "formatString"]; - $pld->file = [$this->reference, "formatFile"]; - $pld->refs = $this->reference; - $pld->view = $this->renderer; - if ($ref) { - $pld->entry = $ref; - $pld->ref = $ref->getName(); - } - - return $pld; - } -} - -namespace mdref\Generator; - -class Renderer -{ - /** - * @var string - */ - private $dir; - - /** - * @param string $dir output directory - */ - public function __construct($dir = "public/static") { - $this->dir = $dir; - } - - /** - * HTML entity encode special characters - * @param string $string - * @return string - */ - public function esc($string) { - return htmlspecialchars($string); - } - - /** - * Render mdref page - * @param \stdClass $pld - * @return string - */ - public function render(\stdClass $pld) { - $content = ""; - ob_start(function($data) use(&$content) { - $content .= $data; - return true; - }); - static::renderFile("views/layout.phtml", (array) $pld); - ob_end_flush(); - return $content; - } - - /** - * Persist mdref page to output directory - * @param \stdClass $data - */ - public function persist(\stdClass $data) { - $html = $this->render($data); - $file = sprintf("%s/%s.html", $this->dir, $data->ref); - $this->saveFile($file, $html); - $this->linkIndex(dirname($file)); - } - - /** - * Save data to file (write to $file.tmp and rename to $file) - * @param string $file - * @param string $data - * @throws \Exception - */ - private function saveFile($file, $data) { - $dir = dirname($file); - if (!is_dir($dir) && !mkdir($dir, 0755, true)) { - throw new \Exception("Failed to create directory '$dir'"); - } - if (!file_put_contents("$file.tmp", $data)) { - throw new \Exception("Failed to save file '$file.tmp'"); - } - if (!rename("$file.tmp", $file)) { - throw new \Exception("Failed to rename to '$file'"); - } - } - - private function linkIndex($dir) { - $index = "$dir.html"; - $link = "$dir/index.html"; - if (is_file($index) && !is_file($link)) { - printf("Generating index for '%s'\n", substr($dir, strlen($this->dir))); - link($index, $link); - } - } - - /** - * Render file - */ - static private function renderFile() { - if (func_num_args() > 1) { - extract(func_get_arg(1)); - } - include func_get_arg(0); - } -} diff --git a/public/.htaccess b/public/.htaccess index c236b73..87e2468 100644 --- a/public/.htaccess +++ b/public/.htaccess @@ -1,7 +1,5 @@ DirectorySlash Off RewriteEngine On -RewriteRule ^static.*\.html - [L] -RewriteRule ^(static/.+) $1.html [L] RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d [OR] RewriteCond %{REQUEST_FILENAME} -l diff --git a/views/layout.phtml b/views/layout.phtml index 05367b9..6cc571b 100644 --- a/views/layout.phtml +++ b/views/layout.phtml @@ -14,10 +14,6 @@ - - @@ -50,10 +46,6 @@ - - -- 2.30.2