Separate generator and server

This commit is contained in:
Stéphane Goetz 2015-04-22 12:23:57 +02:00
parent 90027b2a0e
commit c79c692042
12 changed files with 458 additions and 434 deletions

View File

@ -1,6 +1,4 @@
<?php <?php
require_once("vendor/autoload.php");
/* /*
Daux.io Daux.io
@ -64,9 +62,12 @@ negligence or otherwise) arising in any way out of the use of this
software, even if advised of the possibility of such damage. software, even if advised of the possibility of such damage.
*/ */
if (isset($argv[1])) $Daux = new \Todaymade\Daux\Daux($argv[1]);
else $Daux = new \Todaymade\Daux\Daux(); require_once("vendor/autoload.php");
$Daux->initialize();
if (isset($argv[2])) $Daux->generate_static($argv[2]); $global_config = (isset($argv[1]))? $argv[1] : null;
else $Daux->generate_static(); $destination = (isset($argv[2]))? $argv[2] : null;
?>
$generator = new \Todaymade\Daux\Generator\Generator();
$generator->generate($global_config, $destination);

View File

@ -1,6 +1,4 @@
<?php <?php
require_once("vendor/autoload.php");
/* /*
Daux.io Daux.io
@ -64,8 +62,7 @@ negligence or otherwise) arising in any way out of the use of this
software, even if advised of the possibility of such damage. software, even if advised of the possibility of such damage.
*/ */
$Daux = new \Todaymade\Daux\Daux();
$Daux->initialize(); require_once("vendor/autoload.php");
$page = $Daux->handle_request($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $_REQUEST);
$page->display(); \Todaymade\Daux\Server\Server::serve($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $_REQUEST);
?>

View File

@ -1,108 +1,61 @@
<?php namespace Todaymade\Daux; <?php namespace Todaymade\Daux;
use Todaymade\Daux\Server\Helper as ServerHelper;
use Todaymade\Daux\Generator\Helper as GeneratorHelper;
class Daux class Daux
{ {
const STATIC_MODE = 'DAUX_STATIC'; const STATIC_MODE = 'DAUX_STATIC';
const LIVE_MODE = 'DAUX_LIVE'; const LIVE_MODE = 'DAUX_LIVE';
public static $VALID_MARKDOWN_EXTENSIONS; public static $VALID_MARKDOWN_EXTENSIONS;
private $local_base; public $local_base;
private $base_url; public $base_url = '';
private $host; public $host;
private $docs_path; private $docs_path;
private $tree; public $tree;
private $options; public $options;
private $error_page;
private $error = false;
private $params;
private $mode; private $mode;
function __construct($global_config_file = NULL) { public function __construct($mode) {
$this->initial_setup($global_config_file); $this->mode = $mode;
}
public function initialize($config_file = 'config.json') {
if ($this->error) return;
$this->load_docs_config($config_file);
$this->generate_directory_tree();
if (!$this->error) $this->params = $this->get_page_params();
}
public function generate_static($output_dir = NULL) {
if (is_null($output_dir)) $output_dir = $this->local_base . DIRECTORY_SEPARATOR . 'static';
DauxHelper::clean_copy_assets($output_dir, $this->local_base);
$this->recursive_generate_static($this->tree, $output_dir, $this->params);
}
public function handle_request($url, $query = array()) {
if ($this->error) return $this->error_page;
if (!$this->params['clean_urls']) $this->params['base_page'] .= 'index.php/';
$request = DauxHelper::get_request();
$request = urldecode($request);
$request_type = isset($query['method']) ? $query['method'] : '';
if($request == 'first_page') {
$request = $this->tree->first_page->uri;
}
switch ($request_type) {
case 'DauxEdit':
if ($this->options['file_editor']) {
$content = isset($query['markdown']) ? $query['markdown'] : '';
return $this->save_file($request, $content);
}
return $this->generate_error_page('Editing Disabled', 'Editing is currently disabled in config',
ErrorPage::FATAL_ERROR_TYPE);
default:
return $this->get_page($request);
}
}
private function initial_setup($global_config_file) {
$this->setup_environment_variables();
$this->load_global_config($global_config_file);
}
private function setup_environment_variables() {
global $argc;
$this->local_base = dirname(dirname(__FILE__)); $this->local_base = dirname(dirname(__FILE__));
$this->base_url = ''; $this->base_url = '';
if (isset($argc)) {
$this->mode = Daux::STATIC_MODE; if ($this->mode == Daux::LIVE_MODE) {
return;
}
$this->mode = Daux::LIVE_MODE;
$this->host = $_SERVER['HTTP_HOST']; $this->host = $_SERVER['HTTP_HOST'];
$this->base_url = $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']); $this->base_url = $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
$t = strrpos($this->base_url, '/index.php'); $t = strrpos($this->base_url, '/index.php');
if ($t != FALSE) $this->base_url = substr($this->base_url, 0, $t); if ($t != FALSE) $this->base_url = substr($this->base_url, 0, $t);
if (substr($this->base_url, -1) !== '/') $this->base_url .= '/'; if (substr($this->base_url, -1) !== '/') $this->base_url .= '/';
} }
}
public function initialize($global_config_file = null, $config_file = 'config.json') {
$this->load_global_config($global_config_file);
$this->load_docs_config($config_file);
$this->generate_directory_tree();
}
private function load_global_config($global_config_file) { private function load_global_config($global_config_file) {
if (is_null($global_config_file)) $global_config_file = $this->local_base . DIRECTORY_SEPARATOR . 'global.json'; if (is_null($global_config_file)) $global_config_file = $this->local_base . DIRECTORY_SEPARATOR . 'global.json';
if (!file_exists($global_config_file)) { if (!file_exists($global_config_file)) {
$this->generate_error_page('Global Config File Missing', throw new Exception('The Global Config file is missing. Requested File : ' . $global_config_file);
'The Global Config file is missing. Requested File : ' . $global_config_file, ErrorPage::FATAL_ERROR_TYPE);
return;
} }
$global_config = json_decode(file_get_contents($global_config_file), true); $global_config = json_decode(file_get_contents($global_config_file), true);
if (!isset($global_config)) { if (!isset($global_config)) {
$this->generate_error_page('Corrupt Global Config File', throw new Exception('The Global Config file is corrupt. Check that the JSON encoding is correct');
'The Global Config file is corrupt. Check that the JSON encoding is correct', ErrorPage::FATAL_ERROR_TYPE);
return;
} }
if (!isset($global_config['docs_directory'])) { if (!isset($global_config['docs_directory'])) {
$this->generate_error_page('Docs Directory not set', 'The Global Config file does not have the docs directory set.', throw new Exception('The Global Config file does not have the docs directory set.');
ErrorPage::FATAL_ERROR_TYPE);
return;
} }
$this->docs_path = $global_config['docs_directory']; $this->docs_path = $global_config['docs_directory'];
if (!is_dir($this->docs_path) && !is_dir($this->docs_path = $this->local_base . DIRECTORY_SEPARATOR . $this->docs_path)) { if (!is_dir($this->docs_path) && !is_dir($this->docs_path = $this->local_base . DIRECTORY_SEPARATOR . $this->docs_path)) {
$this->generate_error_page('Docs Directory not found', throw new Exception('The Docs directory does not exist. Check the path again : ' . $this->docs_path);
'The Docs directory does not exist. Check the path again : ' . $this->docs_path, ErrorPage::FATAL_ERROR_TYPE);
return;
} }
if (!isset($global_config['valid_markdown_extensions'])) static::$VALID_MARKDOWN_EXTENSIONS = array('md', 'markdown'); if (!isset($global_config['valid_markdown_extensions'])) static::$VALID_MARKDOWN_EXTENSIONS = array('md', 'markdown');
@ -112,17 +65,13 @@
private function load_docs_config($config_file) { private function load_docs_config($config_file) {
$config_file = $this->docs_path . DIRECTORY_SEPARATOR . $config_file; $config_file = $this->docs_path . DIRECTORY_SEPARATOR . $config_file;
if (!file_exists($config_file)) { if (!file_exists($config_file)) {
$this->generate_error_page('Config File Missing', throw new Exception('The local config file is missing. Check path : ' . $config_file);
'The local config file is missing. Check path : ' . $config_file, ErrorPage::FATAL_ERROR_TYPE);
return;
} }
$this->options = json_decode(file_get_contents($this->local_base . DIRECTORY_SEPARATOR . 'default.json'), true); $this->options = json_decode(file_get_contents($this->local_base . DIRECTORY_SEPARATOR . 'default.json'), true);
if (is_file($config_file)) { if (is_file($config_file)) {
$config = json_decode(file_get_contents($config_file), true); $config = json_decode(file_get_contents($config_file), true);
if (!isset($config)) { if (!isset($config)) {
$this->generate_error_page('Invalid Config File', throw new Exception('There was an error parsing the Config file. Please review');
'There was an error parsing the Config file. Please review', ErrorPage::FATAL_ERROR_TYPE);
return;
} }
$this->options = array_merge($this->options, $config); $this->options = array_merge($this->options, $config);
} }
@ -139,170 +88,38 @@
} }
} }
private function recursive_generate_static($tree, $output_dir, $params, $base_url = '') { public function get_base_params() {
$params['base_url'] = $params['base_page'] = $base_url; return array(
$new_params = $params; 'local_base' => $this->local_base,
//changed this as well in order for the templates to be put in the right place
$params['theme'] = DauxHelper::rebase_theme($params['theme'], $base_url, $params['base_url'] . "templates/default/themes/" . $params['theme']['name'] . '/');
//
$params['image'] = str_replace('<base_url>', $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) {
$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 {
$params['request'] = $node->get_url();
$params['file_uri'] = $node->name;
$page = MarkdownPage::fromFile($node, $params); 'tagline' => $this->options['tagline'],
file_put_contents($output_dir . DIRECTORY_SEPARATOR . $key, $page->get_page_content()); 'title' => $this->options['title'],
} 'author' => $this->options['author'],
} 'image' => $this->options['image'],
'repo' => $this->options['repo'],
'links' => $this->options['links'],
'twitter' => $this->options['twitter'],
'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']) : '',
'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'],
);
} }
private function save_file($request, $content) { //TODO :: move to generator
$file = $this->get_file_from_request($request); public function get_page_params($mode = '') {
if ($file === false) return $this->generate_error_page('Page Not Found',
'The Page you requested is yet to be made. Try again later.', ErrorPage::MISSING_PAGE_ERROR_TYPE);
if ($file->write($content)) return new SimplePage('Success', 'Successfully Edited');
else return $this->generate_error_page('File Not Writable', 'The file you wish to write to is not writable.',
ErrorPage::FATAL_ERROR_TYPE);
}
private function generate_error_page($title, $content, $type) {
$this->error_page = new ErrorPage($title, $content, $this->get_page_params($type));
$this->error = true;
return $this->error_page;
}
private function get_page($request) {
$params = $this->params;
$file = $this->get_file_from_request($request);
if ($file === false) return $this->generate_error_page('Page Not Found',
'The Page you requested is yet to be made. Try again later.', ErrorPage::MISSING_PAGE_ERROR_TYPE);
$params['request'] = $request;
$params['file_uri'] = $file->value;
if ($request !== 'index') $params['entry_page'] = $file->first_page;
return MarkdownPage::fromFile($file, $params);
}
private function get_page_params($mode = '') {
$params = array();
$params['local_base'] = $this->local_base;
if ($mode === '') $mode = $this->mode; if ($mode === '') $mode = $this->mode;
$params = $this->get_base_params();
$params['mode'] = $mode; $params['mode'] = $mode;
switch ($mode) {
case ErrorPage::FATAL_ERROR_TYPE:
$params['error_type'] = ErrorPage::FATAL_ERROR_TYPE;
break;
case ErrorPage::NORMAL_ERROR_TYPE:
case ErrorPage::MISSING_PAGE_ERROR_TYPE:
$params['error_type'] = $mode;
$params['index_key'] = 'index';
$params['docs_path'] = $this->docs_path;
$protocol = '//';
$params['base_url'] = $protocol . $this->base_url;
$params['base_page'] = $params['base_url'];
$params['host'] = $this->host;
$params['tree'] = $this->tree;
$params['index'] = ($this->tree->index_page !== false) ? $this->tree->index_page : $this->tree->first_page;
$params['clean_urls'] = $this->options['clean_urls'];
$params['tagline'] = $this->options['tagline'];
$params['title'] = $this->options['title'];
$params['author'] = $this->options['author'];
$params['image'] = $this->options['image'];
if ($params['image'] !== '') $params['image'] = str_replace('<base_url>', $params['base_url'], $params['image']);
$params['repo'] = $this->options['repo'];
$params['links'] = $this->options['links'];
$params['twitter'] = $this->options['twitter'];
$params['google_analytics'] = ($g = $this->options['google_analytics']) ?
DauxHelper::google_analytics($g, $this->host) : '';
$params['piwik_analytics'] = ($p = $this->options['piwik_analytics']) ?
DauxHelper::piwik_analytics($p, $this->options['piwik_analytics_id']) : '';
$params['template'] = $this->options['template'];
$params['theme'] = DauxHelper::configure_theme($this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR .
$this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['theme'] . '/config.json', $params['base_url'],
$this->local_base, $params['base_url'] . "templates/" . $params['template'] . "/themes/" . $this->options['theme'] . '/');
break;
case Daux::LIVE_MODE:
$params['docs_path'] = $this->docs_path;
$params['index_key'] = 'index';
$protocol = '//';
$params['base_url'] = $protocol . $this->base_url;
$params['base_page'] = $params['base_url'];
$params['host'] = $this->host;
$params['tree'] = $this->tree;
$params['index'] = ($this->tree->index_page !== false) ? $this->tree->index_page : $this->tree->first_page;
$params['clean_urls'] = $this->options['clean_urls'];
$params['tagline'] = $this->options['tagline'];
$params['title'] = $this->options['title'];
$params['author'] = $this->options['author'];
$params['image'] = $this->options['image'];
if ($params['image'] !== '') $params['image'] = str_replace('<base_url>', $params['base_url'], $params['image']);
$params['repo'] = $this->options['repo'];
$params['links'] = $this->options['links'];
$params['twitter'] = $this->options['twitter'];
$params['google_analytics'] = ($g = $this->options['google_analytics']) ?
DauxHelper::google_analytics($g, $this->host) : '';
$params['piwik_analytics'] = ($p = $this->options['piwik_analytics']) ?
DauxHelper::piwik_analytics($p, $this->options['piwik_analytics_id']) : '';
$params['template'] = $this->options['template'];
$params['theme'] = DauxHelper::configure_theme($this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR .
$this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['theme'] . '/config.json', $params['base_url'],
$this->local_base, $params['base_url'] . "templates/" . $params['template'] . "/themes/" . $this->options['theme'] . '/', $mode);
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'];
break;
case Daux::STATIC_MODE:
$params['docs_path'] = $this->docs_path;
$params['index_key'] = 'index.html'; $params['index_key'] = 'index.html';
$params['base_url'] = ''; $params['base_url'] = '';
$params['base_page'] = $params['base_url']; $params['base_page'] = $params['base_url'];
$params['tree'] = $this->tree;
$params['index'] = ($this->tree->index_page !== false) ? $this->tree->index_page : $this->tree->first_page;
$params['tagline'] = $this->options['tagline'];
$params['title'] = $this->options['title'];
$params['author'] = $this->options['author'];
$params['image'] = $this->options['image'];
$params['repo'] = $this->options['repo'];
$params['links'] = $this->options['links'];
$params['twitter'] = $this->options['twitter'];
$params['google_analytics'] = ($g = $this->options['google_analytics']) ?
DauxHelper::google_analytics($g, $this->host) : '';
$params['piwik_analytics'] = ($p = $this->options['piwik_analytics']) ?
DauxHelper::piwik_analytics($p, $this->options['piwik_analytics_id']) : '';
$params['template'] = $this->options['template'];
$params['theme'] = DauxHelper::configure_theme($this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR .
$this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['theme'] . '/config.json', $params['base_url'],
$this->local_base, $params['base_url'] . "templates/" . $params['template'] . "/themes/" . $this->options['theme'] . '/', $mode);
if ($params['breadcrumbs'] = $this->options['breadcrumbs']) if ($params['breadcrumbs'] = $this->options['breadcrumbs'])
$params['breadcrumb_separator'] = $this->options['breadcrumb_separator']; $params['breadcrumb_separator'] = $this->options['breadcrumb_separator'];
@ -320,16 +137,56 @@
$params['float'] = $this->options['float']; $params['float'] = $this->options['float'];
$params['date_modified'] = $this->options['date_modified']; $params['date_modified'] = $this->options['date_modified'];
$params['file_editor'] = false; $params['file_editor'] = false;
break;
} $params['theme'] = GeneratorHelper::configure_theme(
$this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['theme'],
$params['base_url'],
$this->local_base,
$params['base_url'] . "templates/" . $params['template'] . "/themes/" . $this->options['theme'] . '/'
);
return $params; return $params;
} }
private function get_file_from_request($request) { //TODO :: move to server
$file = $this->tree->retrieve_file($request); public function get_live_page_params() {
return $file; $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'];
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'] = ServerHelper::configure_theme(
$this->local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $this->options['template'] . DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $this->options['theme'],
$params['base_url'],
$this->local_base,
$params['base_url'] . "templates/" . $params['template'] . "/themes/" . $this->options['theme'] . '/'
);
return $params;
}
}

View File

@ -54,127 +54,34 @@
return $filename; return $filename;
} }
public static function build_directory_tree($dir, $ignore, $mode) { public static function get_theme($theme_folder, $local_base) {
return static::directory_tree_builder($dir, $ignore, $mode); $name = static::pathinfo($theme_folder);
}
$theme = array();
//Depreciated if (is_file($theme_folder . DIRECTORY_SEPARATOR . "config.json")) {
public static function get_request_from_url($url, $base_url) { $theme = json_decode(file_get_contents($theme_folder . DIRECTORY_SEPARATOR . "config.json"), true);
$url = substr($url, strlen($base_url));
if (strpos($url, 'index.php') === 0) {
$request = (($i = strpos($url, 'request=')) !== false) ? $request = substr($url, $i + 8) : '';
if ($end = strpos($request, '&')) $request = substr($request, 0, $end);
$request = ($request === '') ? 'index' : $request;
} else {
$request = ($url == '') ? 'index' : $url;
$request = ($end = strpos($request, '?')) ? substr($request, 0, $end) : $request;
}
return $request;
}
public static function get_request($prefix_slash = false)
{
if (isset($_SERVER['PATH_INFO'])) $uri = $_SERVER['PATH_INFO'];
else if (isset($_SERVER['REQUEST_URI'])) {
$uri = $_SERVER['REQUEST_URI'];
if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
else if (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0) $uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
if (strncmp($uri, '?/', 2) === 0) $uri = substr($uri, 2);
$parts = preg_split('#\?#i', $uri, 2);
$uri = $parts[0];
if (isset($parts[1])) {
$_SERVER['QUERY_STRING'] = $parts[1];
parse_str($_SERVER['QUERY_STRING'], $_GET);
} else {
$_SERVER['QUERY_STRING'] = '';
$_GET = array();
}
$uri = parse_url($uri, PHP_URL_PATH);
}
else return false;
$uri = str_replace(array('//', '../'), '/', trim($uri, '/'));
if ($uri == "") $uri = "first_page";
return $uri;
}
public static function configure_theme($theme, $base_url, $local_base, $theme_url, $mode = Daux::LIVE_MODE) {
$name = static::pathinfo($theme);
if (is_file($theme)) {
$theme = file_get_contents($theme);
$theme = json_decode($theme, true);
if (!$theme) $theme = array(); if (!$theme) $theme = array();
} else $theme = array(); }
$theme['name'] = $name['filename']; $theme['name'] = $name['filename'];
if ($mode === Daux::LIVE_MODE) { $theme += ['css' => [], 'js' => [], 'fonts' => [], 'require-jquery' => false, 'bootstrap-js' => false];
if (!isset($theme['favicon'])) $theme['favicon'] = utf8_encode($base_url . 'img/favicon.png');
else {
$theme['favicon'] = utf8_encode(str_replace('<base_url>', $base_url, $theme['favicon']));
$theme['favicon'] = str_replace('<theme_url>', $theme_url, $theme['favicon']);
}
if (!isset($theme['css'])) $theme['css'] = array();
else { if (!isset($theme['template'])){
foreach ($theme['css'] as $key => $css) { $theme['template'] = $local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default/default.tpl';
$theme['css'][$key] = utf8_encode(str_replace('<base_url>', $base_url, $css)); } else{
$theme['css'][$key] = utf8_encode(str_replace('<theme_url>', $theme_url, $css)); $theme['template'] = str_replace('<local_base>', $local_base, $theme['template']);
}
}
if (!isset($theme['fonts'])) $theme['fonts'] = array();
else {
foreach ($theme['fonts'] as $key => $font) {
$theme['fonts'][$key] = utf8_encode(str_replace('<base_url>', $base_url, $font));
$theme['fonts'][$key] = utf8_encode(str_replace('<theme_url>', $theme_url, $font));
}
}
if (!isset($theme['js'])) $theme['js'] = array();
else {
foreach ($theme['js'] as $key => $js) {
$theme['js'][$key] = utf8_encode(str_replace('<base_url>', $base_url, $js));
$theme['js'][$key] = utf8_encode(str_replace('<theme_url>', $theme_url, $js));
}
} }
if (!isset($theme['error-template'])) {
$theme['error-template'] = $local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default/error.tpl';
} else { } else {
if (!isset($theme['favicon'])) $theme['favicon'] = '<base_url>img/favicon.png'; $theme['error-template'] = str_replace('<local_base>', $local_base, $theme['error-template']);
if (!isset($theme['css'])) $theme['css'] = array();
if (!isset($theme['fonts'])) $theme['fonts'] = array();
if (!isset($theme['js'])) $theme['js'] = array();
} }
if (!isset($theme['template'])) $theme['template'] = $local_base . DIRECTORY_SEPARATOR . 'templates' .
DIRECTORY_SEPARATOR . 'default/default.tpl';
else $theme['template'] = str_replace('<local_base>', $local_base, $theme['template']);
if (!isset($theme['error-template'])) $theme['error-template'] = $local_base . DIRECTORY_SEPARATOR . 'templates' .
DIRECTORY_SEPARATOR . 'default/error.tpl';
else $theme['error-template'] = str_replace('<local_base>', $local_base, $theme['error-template']);
if (!isset($theme['require-jquery'])) $theme['require-jquery'] = false;
if (!isset($theme['bootstrap-js'])) $theme['bootstrap-js'] = false;
return $theme; return $theme;
} }
public static function rebase_theme($theme, $base_url, $theme_url) {
$theme['favicon'] = utf8_encode(str_replace('<base_url>', $base_url, $theme['favicon']));
$theme['favicon'] = str_replace('<theme_url>', $theme_url, $theme['favicon']);
foreach ($theme['css'] as $key => $css) {
$theme['css'][$key] = utf8_encode(str_replace('<base_url>', $base_url, $css));
$theme['css'][$key] = utf8_encode(str_replace('<theme_url>', $theme_url, $css));
}
foreach ($theme['fonts'] as $key => $font) {
$theme['fonts'][$key] = utf8_encode(str_replace('<base_url>', $base_url, $font));
$theme['fonts'][$key] = utf8_encode(str_replace('<theme_url>', $theme_url, $font));
}
foreach ($theme['js'] as $key => $js) {
$theme['js'][$key] = utf8_encode(str_replace('<base_url>', $base_url, $js));
$theme['js'][$key] = utf8_encode(str_replace('<theme_url>', $theme_url, $js));
}
return $theme;
}
public static function google_analytics($analytics, $host) { public static function google_analytics($analytics, $host) {
$ga = <<<EOT $ga = <<<EOT
@ -210,7 +117,7 @@ EOT;
return $pa; return $pa;
} }
private static function directory_tree_builder($dir, $ignore, $mode = Daux::LIVE_MODE, $parents = null) { public static function build_directory_tree($dir, $ignore, $mode = Daux::LIVE_MODE, $parents = null) {
if ($dh = opendir($dir)) { if ($dh = opendir($dir)) {
$node = new Entry($dir, $parents); $node = new Entry($dir, $parents);
$new_parents = $parents; $new_parents = $parents;
@ -223,7 +130,7 @@ EOT;
if (!is_dir($path) && in_array($entry, $ignore['files'])) continue; if (!is_dir($path) && in_array($entry, $ignore['files'])) continue;
$file_details = static::pathinfo($path); $file_details = static::pathinfo($path);
if (is_dir($path)) $entry = static::directory_tree_builder($path, $ignore, $mode, $new_parents); 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)) else if (in_array($file_details['extension'], Daux::$VALID_MARKDOWN_EXTENSIONS))
{ {
$entry = new Entry($path, $new_parents); $entry = new Entry($path, $new_parents);
@ -250,51 +157,4 @@ EOT;
if (isset($m[3])) $ret['filename']=$m[3]; if (isset($m[3])) $ret['filename']=$m[3];
return $ret; return $ret;
} }
public static function clean_copy_assets($path, $local_base){
@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 . '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
@mkdir($path . DIRECTORY_SEPARATOR . 'templates');
@mkdir($path . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default');
@mkdir($path . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'themes');
static::copy_recursive($local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR .
'default' . DIRECTORY_SEPARATOR . 'themes', $path . DIRECTORY_SEPARATOR . 'templates' .
DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'themes');
//
}
// Rmdir
private static function clean_directory($dir) {
$it = new \RecursiveDirectoryIterator($dir);
$files = new \RecursiveIteratorIterator($it,
\RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $file) {
if ($file->getFilename() === '.' || $file->getFilename() === '..') continue;
if ($file->isDir()) rmdir($file->getRealPath());
else unlink($file->getRealPath());
}
}
private static function copy_recursive($src,$dst) {
$dir = opendir($src);
@mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
static::copy_recursive($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
} }

View File

@ -3,6 +3,7 @@
{ {
const FILE_TYPE = 'FILE_TYPE'; const FILE_TYPE = 'FILE_TYPE';
const DIRECTORY_TYPE = 'DIRECTORY_TYPE'; const DIRECTORY_TYPE = 'DIRECTORY_TYPE';
public $name; public $name;
public $title; public $title;
public $type; public $type;

5
libs/Exception.php Normal file
View File

@ -0,0 +1,5 @@
<?php namespace Todaymade\Daux;
class Exception extends \Exception {
}

View File

@ -0,0 +1,46 @@
<?php namespace Todaymade\Daux\Generator;
use Todaymade\Daux\Daux;
use Todaymade\Daux\Entry;
use Todaymade\Daux\MarkdownPage;
class Generator {
public function generate($global_config, $destination) {
$daux = new Daux(Daux::STATIC_MODE);
$daux->initialize($global_config);
$this->generate_static($daux, $destination);
}
public function generate_static(Daux $daux, $output_dir = NULL) {
$params = $daux->get_page_params();
if (is_null($output_dir)) $output_dir = $daux->local_base . DIRECTORY_SEPARATOR . 'static';
Helper::clean_copy_assets($output_dir, $daux->local_base);
$this->recursive_generate_static($daux->tree, $output_dir, $params);
}
private function recursive_generate_static($tree, $output_dir, $params, $base_url = '') {
$params['base_url'] = $params['base_page'] = $base_url;
$new_params = $params;
//changed this as well in order for the templates to be put in the right place
$params['theme'] = Helper::rebase_theme($params['theme'], $base_url, $params['base_url'] . "templates/default/themes/" . $params['theme']['name'] . '/');
//
$params['image'] = str_replace('<base_url>', $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) {
$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 {
$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());
}
}
}
}

79
libs/Generator/Helper.php Normal file
View File

@ -0,0 +1,79 @@
<?php namespace Todaymade\Daux\Generator;
use Todaymade\Daux\DauxHelper;
class Helper {
public static function clean_copy_assets($path, $local_base){
@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 . '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
@mkdir($path . DIRECTORY_SEPARATOR . 'templates');
@mkdir($path . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default');
@mkdir($path . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'themes');
static::copy_recursive(
$local_base . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'themes',
$path . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . 'default' . DIRECTORY_SEPARATOR . 'themes'
);
}
// Rmdir
private static function clean_directory($dir) {
$it = new \RecursiveDirectoryIterator($dir);
$files = new \RecursiveIteratorIterator($it, \RecursiveIteratorIterator::CHILD_FIRST);
foreach($files as $file) {
if ($file->getFilename() === '.' || $file->getFilename() === '..') continue;
if ($file->isDir()) rmdir($file->getRealPath());
else unlink($file->getRealPath());
}
}
private static function copy_recursive($src,$dst) {
$dir = opendir($src);
@mkdir($dst);
while(false !== ( $file = readdir($dir)) ) {
if (( $file != '.' ) && ( $file != '..' )) {
if ( is_dir($src . '/' . $file) ) {
static::copy_recursive($src . '/' . $file,$dst . '/' . $file);
}
else {
copy($src . '/' . $file,$dst . '/' . $file);
}
}
}
closedir($dir);
}
public static function rebase_theme($theme, $base_url, $theme_url) {
$theme['favicon'] = utf8_encode(str_replace('<base_url>', $base_url, $theme['favicon']));
$theme['favicon'] = str_replace('<theme_url>', $theme_url, $theme['favicon']);
foreach ($theme['css'] as $key => $css) {
$theme['css'][$key] = utf8_encode(str_replace('<base_url>', $base_url, $css));
$theme['css'][$key] = utf8_encode(str_replace('<theme_url>', $theme_url, $css));
}
foreach ($theme['fonts'] as $key => $font) {
$theme['fonts'][$key] = utf8_encode(str_replace('<base_url>', $base_url, $font));
$theme['fonts'][$key] = utf8_encode(str_replace('<theme_url>', $theme_url, $font));
}
foreach ($theme['js'] as $key => $js) {
$theme['js'][$key] = utf8_encode(str_replace('<base_url>', $base_url, $js));
$theme['js'][$key] = utf8_encode(str_replace('<theme_url>', $theme_url, $js));
}
return $theme;
}
public static function configure_theme($theme, $base_url, $local_base, $theme_url) {
$theme = DauxHelper::get_theme($theme, $local_base);
if (!isset($theme['favicon'])) $theme['favicon'] = '<base_url>img/favicon.png';
return $theme;
}
}

View File

@ -1,4 +1,6 @@
<?php namespace Todaymade\Daux; <?php namespace Todaymade\Daux\Server;
use Todaymade\Daux\SimplePage;
class ErrorPage extends SimplePage class ErrorPage extends SimplePage
{ {

84
libs/Server/Helper.php Normal file
View File

@ -0,0 +1,84 @@
<?php namespace Todaymade\Daux\Server;
use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper;
class Helper {
public static function configure_theme($theme_file, $base_url, $local_base, $theme_url) {
$theme = DauxHelper::get_theme($theme_file, $local_base);
if (!isset($theme['favicon'])){
$theme['favicon'] = utf8_encode($base_url . 'img/favicon.png');
} else {
$theme['favicon'] = utf8_encode(str_replace('<base_url>', $base_url, $theme['favicon']));
$theme['favicon'] = str_replace('<theme_url>', $theme_url, $theme['favicon']);
}
foreach ($theme['css'] as $key => $css) {
$theme['css'][$key] = utf8_encode(str_replace('<base_url>', $base_url, $css));
$theme['css'][$key] = utf8_encode(str_replace('<theme_url>', $theme_url, $css));
}
foreach ($theme['fonts'] as $key => $font) {
$theme['fonts'][$key] = utf8_encode(str_replace('<base_url>', $base_url, $font));
$theme['fonts'][$key] = utf8_encode(str_replace('<theme_url>', $theme_url, $font));
}
foreach ($theme['js'] as $key => $js) {
$theme['js'][$key] = utf8_encode(str_replace('<base_url>', $base_url, $js));
$theme['js'][$key] = utf8_encode(str_replace('<theme_url>', $theme_url, $js));
}
return $theme;
}
public static function get_error_params(Daux $daux)
{
$params = $daux->get_base_params();
$params['theme'] = Helper::configure_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()
{
if (isset($_SERVER['PATH_INFO'])) $uri = $_SERVER['PATH_INFO'];
else if (isset($_SERVER['REQUEST_URI'])) {
$uri = $_SERVER['REQUEST_URI'];
if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) $uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
else if (strpos($uri, dirname($_SERVER['SCRIPT_NAME'])) === 0) $uri = substr($uri, strlen(dirname($_SERVER['SCRIPT_NAME'])));
if (strncmp($uri, '?/', 2) === 0) $uri = substr($uri, 2);
$parts = preg_split('#\?#i', $uri, 2);
$uri = $parts[0];
if (isset($parts[1])) {
$_SERVER['QUERY_STRING'] = $parts[1];
parse_str($_SERVER['QUERY_STRING'], $_GET);
} else {
$_SERVER['QUERY_STRING'] = '';
$_GET = array();
}
$uri = parse_url($uri, PHP_URL_PATH);
}
else return false;
$uri = str_replace(array('//', '../'), '/', trim($uri, '/'));
if ($uri == "") $uri = "first_page";
return $uri;
}
}

View File

@ -0,0 +1,7 @@
<?php namespace Todaymade\Daux\Server;
use Todaymade\Daux\Exception;
class NotFoundException extends Exception {
}

85
libs/Server/Server.php Normal file
View File

@ -0,0 +1,85 @@
<?php namespace Todaymade\Daux\Server;
use Todaymade\Daux\Daux;
use Todaymade\Daux\Exception;
use Todaymade\Daux\MarkdownPage;
use Todaymade\Daux\SimplePage;
class Server {
private $daux;
private $params;
public static function serve() {
$daux = new Daux(Daux::LIVE_MODE);
try
{
$daux->initialize();
$server = new static($daux);
$page = $server->handle($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $_REQUEST);
}
catch( NotFoundException $e )
{
$page = new ErrorPage("An error occured", $e->getMessage(), Helper::get_error_params($daux));
}
$page->display();
}
public function __construct(Daux $daux) {
$this->daux = $daux;
}
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'] : '';
if($request == 'first_page') {
$request = $this->daux->tree->first_page->uri;
}
switch ($request_type) {
case 'DauxEdit':
if (!$this->daux->options['file_editor']) {
throw new Exception('Editing is currently disabled in config');
}
$content = isset($query['markdown']) ? $query['markdown'] : '';
return $this->save_file($request, $content);
default:
return $this->get_page($request);
}
}
private function save_file($request, $content) {
$file = $this->get_file_from_request($request);
if ($file === false) throw new NotFoundException('The Page you requested is yet to be made. Try again later.');
if (!$file->write($content)) throw new Exception('The file you wish to write to is not writable.');
return new SimplePage('Success', 'Successfully Edited');
}
private function get_file_from_request($request) {
$file = $this->daux->tree->retrieve_file($request);
return $file;
}
private function get_page($request) {
$params = $this->params;
$file = $this->get_file_from_request($request);
if ($file === false) throw new NotFoundException('The Page you requested is yet to be made. Try again later.');
$params['request'] = $request;
$params['file_uri'] = $file->value;
if ($request !== 'index') $params['entry_page'] = $file->first_page;
return MarkdownPage::fromFile($file, $params);
}
}