public function run($path = '/') { if ($this->kirby->option('patterns.lock') && !$this->kirby->site()->user()) { go($this->kirby->option('error')); } // error handling $whoops = new \Whoops\Run(); $whoops->pushHandler(function ($e) { throw $e; return \Whoops\Handler\Handler::QUIT; }); $whoops->register(); tpl::$data = ['site' => $this->kirby->site(), 'pages' => $this->kirby->site()->children(), 'page' => $this->kirby->site()->find($this->kirby->option('home')), 'lab' => $this]; $router = new Router(); $router->register([['pattern' => '/', 'action' => function () { $readme = $this->root() . DS . 'readme.md'; if (!is_dir($this->root())) { $modal = $this->view('modals/folder'); } else { $modal = null; } if (is_file($readme)) { $markdown = kirbytext(f::read($readme)); } else { $markdown = null; } return $this->view('layouts/main', ['title' => $this->title(), 'menu' => $this->menu(), 'content' => $this->view('views/dashboard', ['markdown' => $markdown]), 'modal' => $modal]); }], ['pattern' => 'assets/(:any)', 'action' => function ($file) { switch ($file) { case 'index.js': case 'index.min.js': $mime = 'text/javascript'; break; case 'index.css': case 'index.min.css': $mime = 'text/css'; break; default: return new Response('Not found', 'text/html', 404); break; } // build the root for the file $file = dirname(__DIR__) . DS . 'assets/dist/' . $file; return new Response(f::read($file), $mime); }], ['pattern' => '(:all)/preview', 'action' => function ($path) { lab::$mode = 'preview'; $pattern = new Pattern($path); $config = $pattern->config(); try { $html = $pattern->render(); } catch (Exception $e) { $html = ''; } return $this->view('views/preview', ['pattern' => $pattern, 'html' => $html, 'background' => a::get($config, 'background', $this->kirby->option('patterns.preview.background')), 'css' => $this->kirby->option('patterns.preview.css', 'assets/css/index.css'), 'js' => $this->kirby->option('patterns.preview.js', 'assets/js/index.js')]); }], ['pattern' => '(:all)', 'action' => function ($path) { $pattern = new Pattern($path); $file = null; if (!$pattern->exists()) { $filename = basename($path); $path = dirname($path); if ($path == '.') { $preview = $this->view('previews/error', ['error' => 'The pattern could not be found']); } else { $pattern = new Pattern($path); $file = $pattern->files()->get($filename); if ($file) { $preview = $this->preview($pattern, $file); } else { $preview = $this->view('previews/error', ['error' => 'The file could not be found']); } } } else { if ($file = $pattern->files()->get($pattern->name() . '.html.php')) { go($pattern->url() . '/' . $file->filename()); } else { if ($file = $pattern->files()->first()) { go($pattern->url() . '/' . $file->filename()); } else { $preview = $this->view('previews/empty'); } } } if ($pattern->isHidden()) { go($this->url()); } return $this->view('layouts/main', ['title' => $this->title() . ' / ' . $pattern->title(), 'menu' => $this->menu(null, $path), 'content' => $this->view('views/pattern', ['preview' => $preview, 'info' => $this->view('snippets/info', ['pattern' => $pattern, 'file' => $file])])]); }]]); if ($route = $router->run($path ? $path : '/')) { return new Response(call($route->action(), $route->arguments())); } else { go('error'); } }
public function preview($pattern, $file) { $data = ['pattern' => $pattern, 'file' => $file]; if (get('raw') == 'true') { $this->raw($pattern, $file); } if ($file->filename() == $pattern->name() . '.html.php') { $views = ['preview', 'html', 'php']; $snippet = 'html'; // pass the mode to the template $data['view'] = in_array(get('view'), $views) ? get('view') : $this->kirby->option('patterns.preview.mode', 'preview'); switch ($data['view']) { case 'preview': try { lab::$mode = 'preview'; $pattern->render(); $data['content'] = '<iframe src="' . $pattern->url() . '/preview"></iframe>'; } catch (Exception $e) { $data['content'] = $this->error($e); } break; case 'php': $data['content'] = $this->codeblock($file); break; case 'html': $data['content'] = $this->codeblock($pattern); break; } } else { if (in_array(strtolower($file->extension()), ['gif', 'jpg', 'jpeg', 'svg', 'png'])) { $snippet = 'image'; } else { if (in_array(strtolower($file->extension()), ['md', 'mdown'])) { $snippet = 'markdown'; $data['content'] = kirbytext($file->read()); } else { $ext = $file->extension(); $code = ['php', 'html', 'js', 'css', 'scss', 'less', 'json', 'txt']; if (in_array($ext, $code)) { $snippet = 'code'; $data['content'] = $this->codeblock($file); } else { $snippet = 'empty'; } } } } return $this->view('previews/' . $snippet, $data); }