From ecd5efe758dba823411641b89656c5207c561408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ste=CC=81phane=20Goetz?= Date: Wed, 22 Apr 2015 18:24:10 +0200 Subject: [PATCH] Extract tree builder Add related files directly in the documentation --- README.md | 2 +- docs/00_Getting_Started.md | 2 +- docs/_index.md | 4 +- {img => docs}/app-thumbs.png | Bin {img => docs}/app.png | Bin docs/config.json | 6 +- libs/Daux.php | 83 ++++++++----------- libs/DauxHelper.php | 66 +-------------- libs/Entry.php | 127 ----------------------------- libs/Generator/Generator.php | 8 +- libs/Generator/Helper.php | 4 +- libs/MarkdownPage.php | 21 +++-- libs/Server/ErrorPage.php | 12 +-- libs/Server/Helper.php | 24 ------ libs/Server/Server.php | 43 ++++++++-- libs/Tree/Builder.php | 62 ++++++++++++++ libs/Tree/Content.php | 19 +++++ libs/Tree/Directory.php | 43 ++++++++++ libs/Tree/Entry.php | 86 +++++++++++++++++++ libs/Tree/Raw.php | 9 ++ {img => resources/img}/favicon.png | Bin templates/default/default.tpl | 5 +- templates/default/error.tpl | 5 +- 23 files changed, 327 insertions(+), 304 deletions(-) rename {img => docs}/app-thumbs.png (100%) rename {img => docs}/app.png (100%) delete mode 100644 libs/Entry.php create mode 100644 libs/Tree/Builder.php create mode 100644 libs/Tree/Content.php create mode 100644 libs/Tree/Directory.php create mode 100644 libs/Tree/Entry.php create mode 100644 libs/Tree/Raw.php rename {img => resources/img}/favicon.png (100%) diff --git a/README.md b/README.md index 94b69c0..f29e248 100755 --- a/README.md +++ b/README.md @@ -74,7 +74,7 @@ If you want to create a beautiful landing page for your project, simply create a { "title": "Daux.io", "tagline": "The Easiest Way To Document Your Project", - "image": "img/app.png" + "image": "app.png" } ``` diff --git a/docs/00_Getting_Started.md b/docs/00_Getting_Started.md index 629e91a..fef7fad 100644 --- a/docs/00_Getting_Started.md +++ b/docs/00_Getting_Started.md @@ -75,7 +75,7 @@ If you want to create a beautiful landing page for your project, simply create a { "title": "Daux.io", "tagline": "The Easiest Way To Document Your Project", - "image": "img/app.png" + "image": "app.png" } ``` diff --git a/docs/_index.md b/docs/_index.md index 0993c2b..bb12a60 100644 --- a/docs/_index.md +++ b/docs/_index.md @@ -6,7 +6,7 @@

Features


-alt text +alt text * 100% Mobile Responsive * Supports GitHub Flavored Markdown @@ -38,4 +38,4 @@ var google_remarketing_only = true;
- \ No newline at end of file + diff --git a/img/app-thumbs.png b/docs/app-thumbs.png similarity index 100% rename from img/app-thumbs.png rename to docs/app-thumbs.png diff --git a/img/app.png b/docs/app.png similarity index 100% rename from img/app.png rename to docs/app.png diff --git a/docs/config.json b/docs/config.json index 47bbdbf..e84ad5b 100644 --- a/docs/config.json +++ b/docs/config.json @@ -1,8 +1,8 @@ { "title": "DAUX.IO", "tagline": "The Easiest Way To Document Your Project", - "author": "Justin Walsh", - "image": "img/app.png", + "author": "Justin Walsh", + "image": "app.png", "theme": "daux-blue", "template": "default", "clean_urls": true, @@ -11,7 +11,7 @@ "breadcrumb_separator": "Chevrons", "date_modified": true, "float": true, - "file_editor": false, + "file_editor": false, "repo": "justinwalsh/daux.io", "ignore": { "files": ["Work_In_Progress.md"], diff --git a/libs/Daux.php b/libs/Daux.php index ff8abe0..5e270e4 100644 --- a/libs/Daux.php +++ b/libs/Daux.php @@ -2,6 +2,7 @@ use Todaymade\Daux\Server\Helper as ServerHelper; use Todaymade\Daux\Generator\Helper as GeneratorHelper; + use Todaymade\Daux\Tree\Builder; class Daux { @@ -80,7 +81,7 @@ } private function generate_directory_tree() { - $this->tree = DauxHelper::build_directory_tree($this->docs_path, $this->options['ignore'], $this->mode); + $this->tree = Builder::build($this->docs_path, $this->options['ignore'], $this->mode); if (!empty($this->options['languages'])) { foreach ($this->options['languages'] as $key => $node) { $this->tree->value[$key]->title = $node; @@ -89,9 +90,8 @@ } public function get_base_params() { - return array( - 'local_base' => $this->local_base, - + $params = array( + //Informations 'tagline' => $this->options['tagline'], 'title' => $this->options['title'], 'author' => $this->options['author'], @@ -99,44 +99,46 @@ 'repo' => $this->options['repo'], 'links' => $this->options['links'], 'twitter' => $this->options['twitter'], + + //Features 'google_analytics' => ($g = $this->options['google_analytics']) ? DauxHelper::google_analytics($g, $this->host) : '', 'piwik_analytics' => ($p = $this->options['piwik_analytics']) ? DauxHelper::piwik_analytics($p, $this->options['piwik_analytics_id']) : '', + 'toggle_code' => $this->options['toggle_code'], + 'float' => $this->options['float'], + 'date_modified' => $this->options['date_modified'], + 'file_editor' => false, + 'breadcrumbs' => $this->options['breadcrumbs'], + 'breadcrumb_separator' => $this->options['breadcrumb_separator'], + 'multilanguage' => !empty($this->options['languages']), + 'languages' => $this->options['languages'], + + //Paths and tree + 'mode' => $this->mode, + 'local_base' => $this->local_base, 'docs_path' => $this->docs_path, 'tree' => $this->tree, 'index' => ($this->tree->index_page !== false) ? $this->tree->index_page : $this->tree->first_page, 'template' => $this->options['template'], ); - } - //TODO :: move to generator - public function get_page_params($mode = '') { - - if ($mode === '') $mode = $this->mode; - - $params = $this->get_base_params(); - $params['mode'] = $mode; - - $params['index_key'] = 'index.html'; - $params['base_url'] = ''; - $params['base_page'] = $params['base_url']; - - if ($params['breadcrumbs'] = $this->options['breadcrumbs']) - $params['breadcrumb_separator'] = $this->options['breadcrumb_separator']; - $params['multilanguage'] = !empty($this->options['languages']); - $params['languages'] = $this->options['languages']; - if (empty($this->options['languages'])) { - $params['entry_page'] = $this->tree->first_page; - } else { + if (!$params['multilanguage']) { foreach ($this->options['languages'] as $key => $name) { $params['entry_page'][$key] = $this->tree->value[$key]->first_page; } + } else { + $params['entry_page'] = $this->tree->first_page; } - $params['toggle_code'] = $this->options['toggle_code']; - $params['float'] = $this->options['float']; - $params['date_modified'] = $this->options['date_modified']; - $params['file_editor'] = false; + return $params; + } + + //TODO :: move to generator + public function get_page_params() { + $params = $this->get_base_params(); + + $params['index_key'] = 'index.html'; + $params['base_page'] = $params['base_url'] = ''; $params['theme'] = DauxHelper::get_theme( $this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['theme'], @@ -152,34 +154,13 @@ public function get_live_page_params() { $params = $this->get_base_params(); - $params['mode'] = Daux::LIVE_MODE; - $params['index_key'] = 'index'; - $protocol = '//'; - $params['base_url'] = $protocol . $this->base_url; - $params['base_page'] = $params['base_url']; $params['host'] = $this->host; - $params['clean_urls'] = $this->options['clean_urls']; + $params['base_page'] = $params['base_url'] = '//' . $this->base_url; + if (!$this->options['clean_urls']) $params['base_page'] .= 'index.php/'; if ($params['image'] !== '') $params['image'] = str_replace('', $params['base_url'], $params['image']); - if ($params['breadcrumbs'] = $this->options['breadcrumbs']) - $params['breadcrumb_separator'] = $this->options['breadcrumb_separator']; - $params['multilanguage'] = !empty($this->options['languages']); - $params['languages'] = $this->options['languages']; - if (empty($this->options['languages'])) { - $params['entry_page'] = $this->tree->first_page; - } else { - foreach ($this->options['languages'] as $key => $name) { - $params['entry_page'][$key] = $this->tree->value[$key]->first_page; - } - } - - $params['toggle_code'] = $this->options['toggle_code']; - $params['float'] = $this->options['float']; - $params['date_modified'] = $this->options['date_modified']; - $params['file_editor'] = $this->options['file_editor']; - $params['theme'] = DauxHelper::get_theme( $this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['theme'], $params['base_url'], diff --git a/libs/DauxHelper.php b/libs/DauxHelper.php index 1fb429f..82753c4 100644 --- a/libs/DauxHelper.php +++ b/libs/DauxHelper.php @@ -22,38 +22,6 @@ return $request; } - public static function get_title_from_file($file) { - $file = static::pathinfo($file); - return static::get_title_from_filename($file['filename']); - } - - public static function get_title_from_filename($filename) { - $filename = explode('_', $filename); - if ($filename[0] == '' || is_numeric($filename[0])) unset($filename[0]); - else { - $t = $filename[0]; - if ($t[0] == '-') $filename[0] = substr($t, 1); - } - $filename = implode(' ', $filename); - return $filename; - } - - public static function get_url_from_file($file) { - $file = static::pathinfo($file); - return static::get_url_from_filename($file['filename']); - } - - public static function get_url_from_filename($filename) { - $filename = explode('_', $filename); - if ($filename[0] == '' || is_numeric($filename[0])) unset($filename[0]); - else { - $t = $filename[0]; - if ($t[0] == '-') $filename[0] = substr($t, 1); - } - $filename = implode('_', $filename); - return $filename; - } - public static function get_theme($theme_folder, $base_url, $local_base, $theme_url) { $name = static::pathinfo($theme_folder); @@ -71,7 +39,7 @@ 'fonts' => [], 'require-jquery' => false, 'bootstrap-js' => false, - 'favicon' => 'img/favicon.png', + 'favicon' => 'resources/img/favicon.png', 'template' => $local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default/default.tpl', 'error-template' => $local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default/error.tpl', ]; @@ -132,38 +100,6 @@ EOT; return $pa; } - public static function build_directory_tree($dir, $ignore, $mode = Daux::LIVE_MODE, $parents = null) { - if ($dh = opendir($dir)) { - $node = new Entry($dir, $parents); - $new_parents = $parents; - if (is_null($new_parents)) $new_parents = array(); - else $new_parents[] = $node; - while (($entry = readdir($dh)) !== false) { - if ($entry == '.' || $entry == '..') continue; - $path = $dir . DIRECTORY_SEPARATOR . $entry; - if (is_dir($path) && in_array($entry, $ignore['folders'])) continue; - if (!is_dir($path) && in_array($entry, $ignore['files'])) continue; - - $file_details = static::pathinfo($path); - if (is_dir($path)) $entry = static::build_directory_tree($path, $ignore, $mode, $new_parents); - else if (in_array($file_details['extension'], Daux::$VALID_MARKDOWN_EXTENSIONS)) - { - $entry = new Entry($path, $new_parents); - if ($mode === Daux::STATIC_MODE) $entry->uri .= '.html'; - } - if ($entry instanceof Entry) $node->value[$entry->uri] = $entry; - } - $node->sort(); - $node->first_page = $node->get_first_page(); - $index_key = ($mode === Daux::LIVE_MODE) ? 'index' : 'index.html'; - if (isset($node->value[$index_key])) { - $node->value[$index_key]->first_page = $node->first_page; - $node->index_page = $node->value[$index_key]; - } else $node->index_page = false; - return $node; - } - } - public static function pathinfo($path) { preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im', $path, $m); if (isset($m[1])) $ret['dir']=$m[1]; diff --git a/libs/Entry.php b/libs/Entry.php deleted file mode 100644 index cffe0b9..0000000 --- a/libs/Entry.php +++ /dev/null @@ -1,127 +0,0 @@ -local_path = $path; - $this->parents = $parents; - $this->last_modified = filemtime($path); - $this->name = DauxHelper::pathinfo($path); - $this->name = $this->name['filename']; - $this->title = DauxHelper::get_title_from_file($this->name); - $this->uri = DauxHelper::get_url_from_filename($this->name); - $this->index_page = false; - if (is_dir($path)) { - $this->type = Entry::DIRECTORY_TYPE; - $this->value = array(); - } else { - $this->type = Entry::FILE_TYPE; - $this->value = $this->uri; - } - } - - public function sort() { - if ($this->type == static::DIRECTORY_TYPE) uasort($this->value, array($this, 'compare_directory_entries')); - } - - public function retrieve_file($request, $get_first_file = false) { - $tree = $this; - $request = explode('/', $request); - foreach ($request as $node) { - if ($tree->type === static::DIRECTORY_TYPE) { - if (isset($tree->value[$node])) $tree = $tree->value[$node]; - else { - if ($node === 'index' || $node === 'index.html') { - if ($get_first_file) { - return ($tree->index_page) ? $tree->index_page : $tree->first_page; - } else { - return $tree->index_page; - } - } else return false; - } - } else return false; - } - if ($tree->type === static::DIRECTORY_TYPE) { - if ($tree->index_page) return $tree->index_page; - else return ($get_first_file) ? $tree->first_page : false; - } else { - return $tree; - } - } - - public function get_url() { - $url = ''; - foreach ($this->parents as $node) { - $url .= $node->uri . '/'; - } - $url .= $this->uri; - return $url; - } - - public function get_first_page() { - foreach ($this->value as $node) { - if ($node->type === static::FILE_TYPE && $node->title != 'index') - return $node; - } - foreach ($this->value as $node) { - if ($node->type === static::DIRECTORY_TYPE) { - $page = $node->get_first_page(); - if ($page) return $page; - } - } - return false; - } - - public function write($content) { - if (is_writable($this->local_path)) file_put_contents($this->local_path, $content); - else return false; - return true; - } - - private function compare_directory_entries($a, $b) { - $name_a = explode('_', $a->name); - $name_b = explode('_', $b->name); - if (is_numeric($name_a[0])) { - $a = intval($name_a[0]); - if (is_numeric($name_b[0])) { - $b = intval($name_b[0]); - if (($a >= 0) == ($b >= 0)) { - $a = abs($a); - $b = abs($b); - if ($a == $b) return (strcasecmp($name_a[1], $name_b[1])); - return ($a > $b) ? 1 : -1; - } - return ($a >= 0) ? -1 : 1; - } - $t = $name_b[0]; - if ($t && $t[0] === '-') return -1; - return ($a < 0) ? 1 : -1; - } else { - if (is_numeric($name_b[0])) { - $b = intval($name_b[0]); - if ($b >= 0) return 1; - $t = $name_a[0]; - if ($t && $t[0] === '-') return 1; - return ($b >= 0) ? 1 : -1; - } - $p = $name_a[0]; - $q = $name_b[0]; - if (($p && $p[0] === '-') == ($q && $q[0] === '-')) return strcasecmp($p, $q); - else return ($p[0] === '-') ? 1 : -1; - } - } - } diff --git a/libs/Generator/Generator.php b/libs/Generator/Generator.php index 7bd33d2..977340f 100644 --- a/libs/Generator/Generator.php +++ b/libs/Generator/Generator.php @@ -3,6 +3,8 @@ use Todaymade\Daux\Daux; use Todaymade\Daux\Entry; use Todaymade\Daux\MarkdownPage; +use Todaymade\Daux\Tree\Directory; +use Todaymade\Daux\Tree\Content; class Generator { public function generate($global_config, $destination) { @@ -26,16 +28,18 @@ class Generator { $params['image'] = str_replace('', $base_url, $params['image']); if ($base_url !== '') $params['entry_page'] = $tree->first_page; foreach ($tree->value as $key => $node) { - if ($node->type === Entry::DIRECTORY_TYPE) { + if ($node instanceof Directory) { $new_output_dir = $output_dir . DIRECTORY_SEPARATOR . $key; @mkdir($new_output_dir); $this->recursive_generate_static($node, $new_output_dir, $new_params, '../' . $base_url); - } else { + } else if ($node instanceof Content) { $params['request'] = $node->get_url(); $params['file_uri'] = $node->name; $page = MarkdownPage::fromFile($node, $params); file_put_contents($output_dir . DIRECTORY_SEPARATOR . $key, $page->get_page_content()); + } else { + copy($node->local_path, $output_dir . DIRECTORY_SEPARATOR . $key); } } } diff --git a/libs/Generator/Helper.php b/libs/Generator/Helper.php index c985550..ef1ae33 100644 --- a/libs/Generator/Helper.php +++ b/libs/Generator/Helper.php @@ -8,8 +8,8 @@ class Helper { @mkdir($path); static::clean_directory($path); - @mkdir($path . DIRECTORY_SEPARATOR . 'img'); - static::copy_recursive($local_base . DIRECTORY_SEPARATOR . 'img', $path . DIRECTORY_SEPARATOR . 'img'); + @mkdir($path . DIRECTORY_SEPARATOR . 'resources'); + static::copy_recursive($local_base . DIRECTORY_SEPARATOR . 'resources', $path . DIRECTORY_SEPARATOR . 'resources'); @mkdir($path . DIRECTORY_SEPARATOR . 'js'); static::copy_recursive($local_base . DIRECTORY_SEPARATOR . 'js', $path . DIRECTORY_SEPARATOR . 'js'); //added and changed these in order to fetch the theme files and put them in the right place diff --git a/libs/MarkdownPage.php b/libs/MarkdownPage.php index bf3395d..a1fe68e 100644 --- a/libs/MarkdownPage.php +++ b/libs/MarkdownPage.php @@ -75,15 +75,18 @@ class MarkdownPage extends SimplePage private function generate_page() { $params = $this->params; $Parsedown = new \Parsedown(); - if ($params['request'] === $params['index_key']) { - if ($params['multilanguage']) { - foreach ($params['languages'] as $key => $name) { - $entry_page[utf8_encode($name)] = utf8_encode($params['base_page'] . $params['entry_page'][$key]->get_url()); - } - } else $entry_page['View Documentation'] = utf8_encode($params['base_page'] . $params['entry_page']->uri); - } else if ($params['file_uri'] === 'index') - $entry_page[utf8_encode($params['entry_page']->title)] = utf8_encode($params['base_page']. - $params['entry_page']->get_url()); + $entry_page = []; + //TODO :: debug entry pages + //if ($params['request'] === $params['index_key']) { + // if ($params['multilanguage']) { + // foreach ($params['languages'] as $key => $name) { + // $entry_page[utf8_encode($name)] = utf8_encode($params['base_page'] . $params['entry_page'][$key]->get_url()); + // } + // } else $entry_page['View Documentation'] = utf8_encode($params['base_page'] . $params['entry_page']->uri); + //} else if ($params['file_uri'] === 'index') { + // $entry_page[utf8_encode($params['entry_page']->title)] = utf8_encode($params['base_page'] . $params['entry_page']->get_url()); + //} + $page['entry_page'] = (isset($entry_page)) ? $entry_page : null; $page['homepage'] = $this->homepage; diff --git a/libs/Server/ErrorPage.php b/libs/Server/ErrorPage.php index e112c73..3918f81 100644 --- a/libs/Server/ErrorPage.php +++ b/libs/Server/ErrorPage.php @@ -1,6 +1,7 @@ params = $params; - $this->type = $params['error_type']; } public function display() { - http_response_code($this->type === static::MISSING_PAGE_ERROR_TYPE ? 404 : 500); + http_response_code(404); parent::display(); } public function get_page_content() { - if ($this->type !== static::FATAL_ERROR_TYPE && is_null(static::$template)) { - include_once($this->params['theme']['error-template']); - static::$template = new Template(); - } + include_once($this->params['theme']['error-template']); + static::$template = new Template(); if (is_null($this->html)) { $this->html = $this->generate_page(); @@ -37,7 +34,6 @@ class ErrorPage extends SimplePage } public function generate_page() { - if ($this->type === static::FATAL_ERROR_TYPE) return $this->content; $params = $this->params; $page['title'] = $this->title; $page['theme'] = $params['theme']; diff --git a/libs/Server/Helper.php b/libs/Server/Helper.php index 25b6d73..029ec6f 100644 --- a/libs/Server/Helper.php +++ b/libs/Server/Helper.php @@ -4,30 +4,6 @@ use Todaymade\Daux\Daux; use Todaymade\Daux\DauxHelper; class Helper { - public static function get_error_params(Daux $daux) - { - $params = $daux->get_base_params(); - $params['theme'] = DauxHelper::get_theme( - $daux->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $daux->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $daux->options['theme'], - $params['base_url'], - $daux->local_base, - $params['base_url'] . "templates/" . $params['template'] . "/themes/" . $daux->options['theme'] . '/' - ); - - $params['index_key'] = 'index'; - - $protocol = '//'; - $params['base_url'] = $protocol . $daux->base_url; - $params['base_page'] = $params['base_url']; - $params['host'] = $daux->host; - - $params['clean_urls'] = $daux->options['clean_urls']; - - if ($params['image'] !== '') $params['image'] = str_replace('', $params['base_url'], $params['image']); - - return $params; - } - public static function get_request() { if (isset($_SERVER['PATH_INFO'])) $uri = $_SERVER['PATH_INFO']; diff --git a/libs/Server/Server.php b/libs/Server/Server.php index 1445350..aa25ed1 100644 --- a/libs/Server/Server.php +++ b/libs/Server/Server.php @@ -22,7 +22,7 @@ class Server { } catch( NotFoundException $e ) { - $page = new ErrorPage("An error occured", $e->getMessage(), Helper::get_error_params($daux)); + $page = new ErrorPage("An error occured", $e->getMessage(), $daux->get_live_page_params()); } $page->display(); @@ -33,10 +33,8 @@ class Server { } public function handle($url, $query = []) { - $this->params = $this->daux->get_live_page_params(); - if (!$this->params['clean_urls']) $this->params['base_page'] .= 'index.php/'; $request = Helper::get_request(); $request = urldecode($request); $request_type = isset($query['method']) ? $query['method'] : ''; @@ -68,8 +66,43 @@ class Server { } private function get_file_from_request($request) { - $file = $this->daux->tree->retrieve_file($request); - return $file; + $tree = $this->daux->tree; + $request = explode('/', $request); + foreach ($request as $node) { + // If the element we're in currently is not a + // directory, we failed to find the requested file + if (!$tree instanceof Directory) { + return false; + } + + // if the node exists in the current request tree, + // change the $tree variable to reference the new + // node and proceed to the next url part + if (isset($tree->value[$node])) { + $tree = $tree->value[$node]; + continue; + } + + // At this stage, we're in a directory, but no + // sub-item matches, so the current node must + // be an index page or we failed + if ($node !== 'index' && $node !== 'index.html') { + return false; + } + + return $tree->index_page; + } + + // If the entry we found is not a directory, we're done + if (!$tree instanceof Directory) { + return $tree; + } + + if ($tree->index_page){ + return $tree->index_page; + } + + return ($get_first_file) ? $tree->first_page : false; } private function get_page($request) { diff --git a/libs/Tree/Builder.php b/libs/Tree/Builder.php new file mode 100644 index 0000000..88d69db --- /dev/null +++ b/libs/Tree/Builder.php @@ -0,0 +1,62 @@ +uri .= '.html'; + } + } else { + $entry = new Raw($path, $new_parents); + } + + if ($entry instanceof Entry) { + $node->value[$entry->uri] = $entry; + } + } + + $node->sort(); + $node->first_page = $node->get_first_page(); + $index_key = ($mode === Daux::LIVE_MODE) ? 'index' : 'index.html'; + if (isset($node->value[$index_key])) { + $node->value[$index_key]->first_page = $node->first_page; + $node->index_page = $node->value[$index_key]; + } else $node->index_page = false; + return $node; + + } +} diff --git a/libs/Tree/Content.php b/libs/Tree/Content.php new file mode 100644 index 0000000..36d8501 --- /dev/null +++ b/libs/Tree/Content.php @@ -0,0 +1,19 @@ +value = $this->uri; + } + + protected function getFilename($file) { + $file = DauxHelper::pathinfo($file); + return $file['filename']; + } +} diff --git a/libs/Tree/Directory.php b/libs/Tree/Directory.php new file mode 100644 index 0000000..95873b7 --- /dev/null +++ b/libs/Tree/Directory.php @@ -0,0 +1,43 @@ +value, array($this, 'compare_directory_entries')); + } + + private function compare_directory_entries($a, $b) { + $name_a = explode('_', $a->name); + $name_b = explode('_', $b->name); + if (is_numeric($name_a[0])) { + $a = intval($name_a[0]); + if (is_numeric($name_b[0])) { + $b = intval($name_b[0]); + if (($a >= 0) == ($b >= 0)) { + $a = abs($a); + $b = abs($b); + if ($a == $b) return (strcasecmp($name_a[1], $name_b[1])); + return ($a > $b) ? 1 : -1; + } + return ($a >= 0) ? -1 : 1; + } + $t = $name_b[0]; + if ($t && $t[0] === '-') return -1; + return ($a < 0) ? 1 : -1; + } else { + if (is_numeric($name_b[0])) { + $b = intval($name_b[0]); + if ($b >= 0) return 1; + $t = $name_a[0]; + if ($t && $t[0] === '-') return 1; + return ($b >= 0) ? 1 : -1; + } + $p = $name_a[0]; + $q = $name_b[0]; + if (($p && $p[0] === '-') == ($q && $q[0] === '-')) return strcasecmp($p, $q); + else return ($p[0] === '-') ? 1 : -1; + } + } +} diff --git a/libs/Tree/Entry.php b/libs/Tree/Entry.php new file mode 100644 index 0000000..e3208ff --- /dev/null +++ b/libs/Tree/Entry.php @@ -0,0 +1,86 @@ +local_path = $path; + $this->parents = $parents; + $this->last_modified = filemtime($path); + $this->name = DauxHelper::pathinfo($path)['filename']; + $this->title = $this->get_title_from_filename($this->name); + $this->uri = $this->get_url_from_filename($this->getFilename($path)); + $this->index_page = false; + } + + public function get_url() { + $url = ''; + foreach ($this->parents as $node) { + $url .= $node->uri . '/'; + } + $url .= $this->uri; + return $url; + } + + public function get_first_page() { + foreach ($this->value as $node) { + if ($node instanceof Content && $node->title != 'index') + return $node; + } + foreach ($this->value as $node) { + if ($node instanceof Directory) { + $page = $node->get_first_page(); + if ($page) return $page; + } + } + return false; + } + + public function write($content) { + if (!is_writable($this->local_path)) { + return false; + } + + file_put_contents($this->local_path, $content); + return true; + } + + protected function getFilename($file) { + $parts = explode('/', $file); + return end($parts); + } + + protected function get_title_from_filename($filename) { + $filename = explode('_', $filename); + if ($filename[0] == '' || is_numeric($filename[0])) unset($filename[0]); + else { + $t = $filename[0]; + if ($t[0] == '-') $filename[0] = substr($t, 1); + } + $filename = implode(' ', $filename); + return $filename; + } + + protected function get_url_from_filename($filename) { + $filename = explode('_', $filename); + if ($filename[0] == '' || is_numeric($filename[0])) unset($filename[0]); + else { + $t = $filename[0]; + if ($t[0] == '-') $filename[0] = substr($t, 1); + } + $filename = implode('_', $filename); + return $filename; + } + + } diff --git a/libs/Tree/Raw.php b/libs/Tree/Raw.php new file mode 100644 index 0000000..2a5e471 --- /dev/null +++ b/libs/Tree/Raw.php @@ -0,0 +1,9 @@ +value = $this->uri; + } +} diff --git a/img/favicon.png b/resources/img/favicon.png similarity index 100% rename from img/favicon.png rename to resources/img/favicon.png diff --git a/templates/default/default.tpl b/templates/default/default.tpl index f5c70f8..51aa28f 100644 --- a/templates/default/default.tpl +++ b/templates/default/default.tpl @@ -13,13 +13,14 @@ $nav = ''; foreach ($tree->value as $node) { $url = $node->uri; - if ($node->type === \TodayMade\Daux\Entry::FILE_TYPE) { + if ($node instanceof \Todaymade\Daux\Tree\Content) { if ($node->value === 'index') continue; $nav .= '' . $node->title . ''; - } else { + } + if ($node instanceof \Todaymade\Daux\Tree\Directory) { $nav .= 'value as $url => $node) { - if ($node->type === \TodayMade\Daux\Entry::FILE_TYPE) { + if ($node instanceof \Todaymade\Daux\Tree\Content) { if ($node->value === 'index') continue; $link = ($path === '') ? $url : $path . '/' . $url; $nav .= '
  • ' . $node->title . '
  • '; - } else { + } + if ($node instanceof \Todaymade\Daux\Tree\Directory) { $nav .= '
  • '; $link = ($path === '') ? $url : $path . '/' . $url; if ($node->index_page) $nav .= '' . $node->title . '';