Extract tree builder

Add related files directly in the documentation
This commit is contained in:
Stéphane Goetz 2015-04-22 18:24:10 +02:00 gecommit door Stéphane Goetz
bovenliggende 2c228273fb
commit ecd5efe758
23 gewijzigde bestanden met toevoegingen van 327 en 304 verwijderingen

Bestand weergeven

@ -74,7 +74,7 @@ If you want to create a beautiful landing page for your project, simply create a
{ {
"title": "Daux.io", "title": "Daux.io",
"tagline": "The Easiest Way To Document Your Project", "tagline": "The Easiest Way To Document Your Project",
"image": "<base_url>img/app.png" "image": "app.png"
} }
``` ```

Bestand weergeven

@ -75,7 +75,7 @@ If you want to create a beautiful landing page for your project, simply create a
{ {
"title": "Daux.io", "title": "Daux.io",
"tagline": "The Easiest Way To Document Your Project", "tagline": "The Easiest Way To Document Your Project",
"image": "<base_url>img/app.png" "image": "app.png"
} }
``` ```

Bestand weergeven

@ -6,7 +6,7 @@
<h3>Features</h3> <h3>Features</h3>
<hr/> <hr/>
<img src="img/app-thumbs.png" alt="alt text" class="img-responsive pull-right" style="margin-bottom:20px;"> <img src="app-thumbs.png" alt="alt text" class="img-responsive pull-right" style="margin-bottom:20px;">
* 100% Mobile Responsive * 100% Mobile Responsive
* Supports GitHub Flavored Markdown * Supports GitHub Flavored Markdown
@ -38,4 +38,4 @@ var google_remarketing_only = true;
<div style="display:inline;"> <div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//googleads.g.doubleclick.net/pagead/viewthroughconversion/983836026/?value=0&amp;guid=ON&amp;script=0"/> <img height="1" width="1" style="border-style:none;" alt="" src="//googleads.g.doubleclick.net/pagead/viewthroughconversion/983836026/?value=0&amp;guid=ON&amp;script=0"/>
</div> </div>
</noscript> </noscript>

Bestand weergeven

Voor

Breedte:  |  Hoogte:  |  Grootte: 91 KiB

Na

Breedte:  |  Hoogte:  |  Grootte: 91 KiB

Bestand weergeven

Voor

Breedte:  |  Hoogte:  |  Grootte: 235 KiB

Na

Breedte:  |  Hoogte:  |  Grootte: 235 KiB

Bestand weergeven

@ -1,8 +1,8 @@
{ {
"title": "DAUX.IO", "title": "DAUX.IO",
"tagline": "The Easiest Way To Document Your Project", "tagline": "The Easiest Way To Document Your Project",
"author": "Justin Walsh", "author": "Justin Walsh",
"image": "<base_url>img/app.png", "image": "app.png",
"theme": "daux-blue", "theme": "daux-blue",
"template": "default", "template": "default",
"clean_urls": true, "clean_urls": true,
@ -11,7 +11,7 @@
"breadcrumb_separator": "Chevrons", "breadcrumb_separator": "Chevrons",
"date_modified": true, "date_modified": true,
"float": true, "float": true,
"file_editor": false, "file_editor": false,
"repo": "justinwalsh/daux.io", "repo": "justinwalsh/daux.io",
"ignore": { "ignore": {
"files": ["Work_In_Progress.md"], "files": ["Work_In_Progress.md"],

Bestand weergeven

@ -2,6 +2,7 @@
use Todaymade\Daux\Server\Helper as ServerHelper; use Todaymade\Daux\Server\Helper as ServerHelper;
use Todaymade\Daux\Generator\Helper as GeneratorHelper; use Todaymade\Daux\Generator\Helper as GeneratorHelper;
use Todaymade\Daux\Tree\Builder;
class Daux class Daux
{ {
@ -80,7 +81,7 @@
} }
private function generate_directory_tree() { 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'])) { if (!empty($this->options['languages'])) {
foreach ($this->options['languages'] as $key => $node) { foreach ($this->options['languages'] as $key => $node) {
$this->tree->value[$key]->title = $node; $this->tree->value[$key]->title = $node;
@ -89,9 +90,8 @@
} }
public function get_base_params() { public function get_base_params() {
return array( $params = array(
'local_base' => $this->local_base, //Informations
'tagline' => $this->options['tagline'], 'tagline' => $this->options['tagline'],
'title' => $this->options['title'], 'title' => $this->options['title'],
'author' => $this->options['author'], 'author' => $this->options['author'],
@ -99,44 +99,46 @@
'repo' => $this->options['repo'], 'repo' => $this->options['repo'],
'links' => $this->options['links'], 'links' => $this->options['links'],
'twitter' => $this->options['twitter'], 'twitter' => $this->options['twitter'],
//Features
'google_analytics' => ($g = $this->options['google_analytics']) ? DauxHelper::google_analytics($g, $this->host) : '', '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']) : '', '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, 'docs_path' => $this->docs_path,
'tree' => $this->tree, 'tree' => $this->tree,
'index' => ($this->tree->index_page !== false) ? $this->tree->index_page : $this->tree->first_page, 'index' => ($this->tree->index_page !== false) ? $this->tree->index_page : $this->tree->first_page,
'template' => $this->options['template'], 'template' => $this->options['template'],
); );
}
//TODO :: move to generator if (!$params['multilanguage']) {
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 {
foreach ($this->options['languages'] as $key => $name) { foreach ($this->options['languages'] as $key => $name) {
$params['entry_page'][$key] = $this->tree->value[$key]->first_page; $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']; return $params;
$params['float'] = $this->options['float']; }
$params['date_modified'] = $this->options['date_modified'];
$params['file_editor'] = false; //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( $params['theme'] = DauxHelper::get_theme(
$this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['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() { public function get_live_page_params() {
$params = $this->get_base_params(); $params = $this->get_base_params();
$params['mode'] = Daux::LIVE_MODE;
$params['index_key'] = 'index'; $params['index_key'] = 'index';
$protocol = '//';
$params['base_url'] = $protocol . $this->base_url;
$params['base_page'] = $params['base_url'];
$params['host'] = $this->host; $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('<base_url>', $params['base_url'], $params['image']); if ($params['image'] !== '') $params['image'] = str_replace('<base_url>', $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( $params['theme'] = DauxHelper::get_theme(
$this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['theme'], $this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['theme'],
$params['base_url'], $params['base_url'],

Bestand weergeven

@ -22,38 +22,6 @@
return $request; 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) { public static function get_theme($theme_folder, $base_url, $local_base, $theme_url) {
$name = static::pathinfo($theme_folder); $name = static::pathinfo($theme_folder);
@ -71,7 +39,7 @@
'fonts' => [], 'fonts' => [],
'require-jquery' => false, 'require-jquery' => false,
'bootstrap-js' => false, 'bootstrap-js' => false,
'favicon' => '<base_url>img/favicon.png', 'favicon' => '<base_url>resources/img/favicon.png',
'template' => $local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default/default.tpl', 'template' => $local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default/default.tpl',
'error-template' => $local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default/error.tpl', 'error-template' => $local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default/error.tpl',
]; ];
@ -132,38 +100,6 @@ EOT;
return $pa; 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) { public static function pathinfo($path) {
preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im', $path, $m); preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im', $path, $m);
if (isset($m[1])) $ret['dir']=$m[1]; if (isset($m[1])) $ret['dir']=$m[1];

Bestand weergeven

@ -1,127 +0,0 @@
<?php namespace Todaymade\Daux;
class Entry
{
const FILE_TYPE = 'FILE_TYPE';
const DIRECTORY_TYPE = 'DIRECTORY_TYPE';
public $name;
public $title;
public $type;
public $index_page;
public $first_page;
public $value;
public $uri;
public $local_path;
public $last_modified;
public $parents;
function __construct($path = '', $parents = array()) {
if (!isset($path) || $path == '' || !file_exists($path)) return;
$this->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;
}
}
}

Bestand weergeven

@ -3,6 +3,8 @@
use Todaymade\Daux\Daux; use Todaymade\Daux\Daux;
use Todaymade\Daux\Entry; use Todaymade\Daux\Entry;
use Todaymade\Daux\MarkdownPage; use Todaymade\Daux\MarkdownPage;
use Todaymade\Daux\Tree\Directory;
use Todaymade\Daux\Tree\Content;
class Generator { class Generator {
public function generate($global_config, $destination) { public function generate($global_config, $destination) {
@ -26,16 +28,18 @@ class Generator {
$params['image'] = str_replace('<base_url>', $base_url, $params['image']); $params['image'] = str_replace('<base_url>', $base_url, $params['image']);
if ($base_url !== '') $params['entry_page'] = $tree->first_page; if ($base_url !== '') $params['entry_page'] = $tree->first_page;
foreach ($tree->value as $key => $node) { foreach ($tree->value as $key => $node) {
if ($node->type === Entry::DIRECTORY_TYPE) { if ($node instanceof Directory) {
$new_output_dir = $output_dir . DIRECTORY_SEPARATOR . $key; $new_output_dir = $output_dir . DIRECTORY_SEPARATOR . $key;
@mkdir($new_output_dir); @mkdir($new_output_dir);
$this->recursive_generate_static($node, $new_output_dir, $new_params, '../' . $base_url); $this->recursive_generate_static($node, $new_output_dir, $new_params, '../' . $base_url);
} else { } else if ($node instanceof Content) {
$params['request'] = $node->get_url(); $params['request'] = $node->get_url();
$params['file_uri'] = $node->name; $params['file_uri'] = $node->name;
$page = MarkdownPage::fromFile($node, $params); $page = MarkdownPage::fromFile($node, $params);
file_put_contents($output_dir . DIRECTORY_SEPARATOR . $key, $page->get_page_content()); file_put_contents($output_dir . DIRECTORY_SEPARATOR . $key, $page->get_page_content());
} else {
copy($node->local_path, $output_dir . DIRECTORY_SEPARATOR . $key);
} }
} }
} }

Bestand weergeven

@ -8,8 +8,8 @@ class Helper {
@mkdir($path); @mkdir($path);
static::clean_directory($path); static::clean_directory($path);
@mkdir($path . DIRECTORY_SEPARATOR . 'img'); @mkdir($path . DIRECTORY_SEPARATOR . 'resources');
static::copy_recursive($local_base . DIRECTORY_SEPARATOR . 'img', $path . DIRECTORY_SEPARATOR . 'img'); static::copy_recursive($local_base . DIRECTORY_SEPARATOR . 'resources', $path . DIRECTORY_SEPARATOR . 'resources');
@mkdir($path . DIRECTORY_SEPARATOR . 'js'); @mkdir($path . DIRECTORY_SEPARATOR . 'js');
static::copy_recursive($local_base . DIRECTORY_SEPARATOR . 'js', $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 //added and changed these in order to fetch the theme files and put them in the right place

Bestand weergeven

@ -75,15 +75,18 @@ class MarkdownPage extends SimplePage
private function generate_page() { private function generate_page() {
$params = $this->params; $params = $this->params;
$Parsedown = new \Parsedown(); $Parsedown = new \Parsedown();
if ($params['request'] === $params['index_key']) { $entry_page = [];
if ($params['multilanguage']) { //TODO :: debug entry pages
foreach ($params['languages'] as $key => $name) { //if ($params['request'] === $params['index_key']) {
$entry_page[utf8_encode($name)] = utf8_encode($params['base_page'] . $params['entry_page'][$key]->get_url()); // if ($params['multilanguage']) {
} // foreach ($params['languages'] as $key => $name) {
} else $entry_page['View Documentation'] = utf8_encode($params['base_page'] . $params['entry_page']->uri); // $entry_page[utf8_encode($name)] = utf8_encode($params['base_page'] . $params['entry_page'][$key]->get_url());
} else if ($params['file_uri'] === 'index') // }
$entry_page[utf8_encode($params['entry_page']->title)] = utf8_encode($params['base_page']. // } else $entry_page['View Documentation'] = utf8_encode($params['base_page'] . $params['entry_page']->uri);
$params['entry_page']->get_url()); //} 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['entry_page'] = (isset($entry_page)) ? $entry_page : null;
$page['homepage'] = $this->homepage; $page['homepage'] = $this->homepage;

Bestand weergeven

@ -1,6 +1,7 @@
<?php namespace Todaymade\Daux\Server; <?php namespace Todaymade\Daux\Server;
use Todaymade\Daux\SimplePage; use Todaymade\Daux\SimplePage;
use Todaymade\Daux\Template;
class ErrorPage extends SimplePage class ErrorPage extends SimplePage
{ {
@ -9,25 +10,21 @@ class ErrorPage extends SimplePage
const FATAL_ERROR_TYPE = 'FATAL_ERROR'; const FATAL_ERROR_TYPE = 'FATAL_ERROR';
private $params; private $params;
private $type;
private static $template; private static $template;
public function __construct($title, $content, $params) { public function __construct($title, $content, $params) {
parent::__construct($title, $content); parent::__construct($title, $content);
$this->params = $params; $this->params = $params;
$this->type = $params['error_type'];
} }
public function display() { public function display() {
http_response_code($this->type === static::MISSING_PAGE_ERROR_TYPE ? 404 : 500); http_response_code(404);
parent::display(); parent::display();
} }
public function get_page_content() { public function get_page_content() {
if ($this->type !== static::FATAL_ERROR_TYPE && is_null(static::$template)) { include_once($this->params['theme']['error-template']);
include_once($this->params['theme']['error-template']); static::$template = new Template();
static::$template = new Template();
}
if (is_null($this->html)) { if (is_null($this->html)) {
$this->html = $this->generate_page(); $this->html = $this->generate_page();
@ -37,7 +34,6 @@ class ErrorPage extends SimplePage
} }
public function generate_page() { public function generate_page() {
if ($this->type === static::FATAL_ERROR_TYPE) return $this->content;
$params = $this->params; $params = $this->params;
$page['title'] = $this->title; $page['title'] = $this->title;
$page['theme'] = $params['theme']; $page['theme'] = $params['theme'];

Bestand weergeven

@ -4,30 +4,6 @@ use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper; use Todaymade\Daux\DauxHelper;
class Helper { 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('<base_url>', $params['base_url'], $params['image']);
return $params;
}
public static function get_request() public static function get_request()
{ {
if (isset($_SERVER['PATH_INFO'])) $uri = $_SERVER['PATH_INFO']; if (isset($_SERVER['PATH_INFO'])) $uri = $_SERVER['PATH_INFO'];

Bestand weergeven

@ -22,7 +22,7 @@ class Server {
} }
catch( NotFoundException $e ) 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(); $page->display();
@ -33,10 +33,8 @@ class Server {
} }
public function handle($url, $query = []) { public function handle($url, $query = []) {
$this->params = $this->daux->get_live_page_params(); $this->params = $this->daux->get_live_page_params();
if (!$this->params['clean_urls']) $this->params['base_page'] .= 'index.php/';
$request = Helper::get_request(); $request = Helper::get_request();
$request = urldecode($request); $request = urldecode($request);
$request_type = isset($query['method']) ? $query['method'] : ''; $request_type = isset($query['method']) ? $query['method'] : '';
@ -68,8 +66,43 @@ class Server {
} }
private function get_file_from_request($request) { private function get_file_from_request($request) {
$file = $this->daux->tree->retrieve_file($request); $tree = $this->daux->tree;
return $file; $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) { private function get_page($request) {

62
libs/Tree/Builder.php Normal file
Bestand weergeven

@ -0,0 +1,62 @@
<?php namespace Todaymade\Daux\Tree;
use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper;
class Builder {
public static function build($dir, $ignore, $mode = Daux::LIVE_MODE, $parents = null) {
if (!$dh = opendir($dir)) {
return;
}
$node = new Directory($dir, $parents);
$new_parents = $parents;
if (is_null($new_parents)) {
$new_parents = array();
} else{
$new_parents[] = $node;
}
while (($file = readdir($dh)) !== false) {
if ($file == '.' || $file == '..') {
continue;
}
$path = $dir . DIRECTORY_SEPARATOR . $file;
if ((is_dir($path) && in_array($file, $ignore['folders'])) || (!is_dir($path) && in_array($file, $ignore['files']))) {
continue;
}
$file_details = DauxHelper::pathinfo($path);
$entry = null;
if (is_dir($path)) {
$entry = static::build($path, $ignore, $mode, $new_parents);
} else if(in_array($file_details['extension'], Daux::$VALID_MARKDOWN_EXTENSIONS)) {
$entry = new Content($path, $new_parents);
if ($mode === Daux::STATIC_MODE) {
$entry->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;
}
}

19
libs/Tree/Content.php Normal file
Bestand weergeven

@ -0,0 +1,19 @@
<?php namespace Todaymade\Daux\Tree;
use Todaymade\Daux\DauxHelper;
class Content extends Entry {
public $title;
public function __construct($path = '', $parents = array()) {
parent::__construct($path, $parents);
$this->value = $this->uri;
}
protected function getFilename($file) {
$file = DauxHelper::pathinfo($file);
return $file['filename'];
}
}

43
libs/Tree/Directory.php Normal file
Bestand weergeven

@ -0,0 +1,43 @@
<?php namespace Todaymade\Daux\Tree;
class Directory extends Entry {
public $value = [];
public function sort() {
uasort($this->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;
}
}
}

86
libs/Tree/Entry.php Normal file
Bestand weergeven

@ -0,0 +1,86 @@
<?php namespace Todaymade\Daux\Tree;
use Todaymade\Daux\DauxHelper;
abstract class Entry
{
public $title;
public $name;
public $index_page;
public $first_page;
public $uri;
public $local_path;
public $last_modified;
public $parents;
function __construct($path = '', $parents = array()) {
if (!isset($path) || $path == '' || !file_exists($path)) return;
$this->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;
}
}

9
libs/Tree/Raw.php Normal file
Bestand weergeven

@ -0,0 +1,9 @@
<?php namespace Todaymade\Daux\Tree;
class Raw extends Entry {
public function __construct($path = '', $parents = array()) {
parent::__construct($path, $parents);
$this->value = $this->uri;
}
}

Bestand weergeven

Voor

Breedte:  |  Hoogte:  |  Grootte: 1.0 KiB

Na

Breedte:  |  Hoogte:  |  Grootte: 1.0 KiB

Bestand weergeven

@ -13,13 +13,14 @@
$nav = ''; $nav = '';
foreach ($tree->value as $node) { foreach ($tree->value as $node) {
$url = $node->uri; $url = $node->uri;
if ($node->type === \TodayMade\Daux\Entry::FILE_TYPE) { if ($node instanceof \Todaymade\Daux\Tree\Content) {
if ($node->value === 'index') continue; if ($node->value === 'index') continue;
$nav .= '<li'; $nav .= '<li';
$link = ($path === '') ? $url : $path . '/' . $url; $link = ($path === '') ? $url : $path . '/' . $url;
if ($current_url === $link) $nav .= ' class="active"'; if ($current_url === $link) $nav .= ' class="active"';
$nav .= '><a href="' . $base_page . $link . '">' . $node->title . '</a></li>'; $nav .= '><a href="' . $base_page . $link . '">' . $node->title . '</a></li>';
} else { }
if ($node instanceof \Todaymade\Daux\Tree\Directory) {
$nav .= '<li'; $nav .= '<li';
$link = ($path === '') ? $url : $path . '/' . $url; $link = ($path === '') ? $url : $path . '/' . $url;
if (strpos($current_url, $link) === 0) $nav .= ' class="open"'; if (strpos($current_url, $link) === 0) $nav .= ' class="open"';

Bestand weergeven

@ -12,11 +12,12 @@
private function build_navigation($tree, $path, $base_page) { private function build_navigation($tree, $path, $base_page) {
$nav = ''; $nav = '';
foreach ($tree->value as $url => $node) { foreach ($tree->value as $url => $node) {
if ($node->type === \TodayMade\Daux\Entry::FILE_TYPE) { if ($node instanceof \Todaymade\Daux\Tree\Content) {
if ($node->value === 'index') continue; if ($node->value === 'index') continue;
$link = ($path === '') ? $url : $path . '/' . $url; $link = ($path === '') ? $url : $path . '/' . $url;
$nav .= '<li><a href="' . utf8_encode($base_page . $link) . '">' . $node->title . '</a></li>'; $nav .= '<li><a href="' . utf8_encode($base_page . $link) . '">' . $node->title . '</a></li>';
} else { }
if ($node instanceof \Todaymade\Daux\Tree\Directory) {
$nav .= '<li>'; $nav .= '<li>';
$link = ($path === '') ? $url : $path . '/' . $url; $link = ($path === '') ? $url : $path . '/' . $url;
if ($node->index_page) $nav .= '<a href="' . $base_page . $link . '" class="folder">' . $node->title . '</a>'; if ($node->index_page) $nav .= '<a href="' . $base_page . $link . '" class="folder">' . $node->title . '</a>';