Convert to PSR2

This commit is contained in:
Stéphane Goetz 2015-04-23 00:32:30 +02:00
parent ecd5efe758
commit de1214cbab
21 changed files with 664 additions and 493 deletions

View File

@ -65,6 +65,8 @@ software, even if advised of the possibility of such damage.
require_once("vendor/autoload.php");
\Todaymade\Daux\Daux::initConstants();
$global_config = (isset($argv[1]))? $argv[1] : null;
$destination = (isset($argv[2]))? $argv[2] : null;

View File

@ -65,4 +65,6 @@ software, even if advised of the possibility of such damage.
require_once("vendor/autoload.php");
\Todaymade\Daux\Daux::initConstants();
\Todaymade\Daux\Server\Server::serve($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $_REQUEST);

View File

@ -1,7 +1,5 @@
<?php namespace Todaymade\Daux;
use Todaymade\Daux\Server\Helper as ServerHelper;
use Todaymade\Daux\Generator\Helper as GeneratorHelper;
use Todaymade\Daux\Tree\Builder;
class Daux
@ -18,29 +16,43 @@
public $options;
private $mode;
public function __construct($mode) {
public function __construct($mode)
{
$this->mode = $mode;
$this->local_base = dirname(dirname(__FILE__));
$this->local_base = dirname(__DIR__);
$this->base_url = '';
if ($this->mode == Daux::LIVE_MODE) {
$this->host = $_SERVER['HTTP_HOST'];
$this->base_url = $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
$t = strrpos($this->base_url, '/index.php');
if ($t != FALSE) $this->base_url = substr($this->base_url, 0, $t);
if (substr($this->base_url, -1) !== '/') $this->base_url .= '/';
if ($t != false) {
$this->base_url = substr($this->base_url, 0, $t);
}
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();
public static function initConstants()
{
define("DS", DIRECTORY_SEPARATOR);
}
private function load_global_config($global_config_file) {
if (is_null($global_config_file)) $global_config_file = $this->local_base . DIRECTORY_SEPARATOR . 'global.json';
public function initialize($global_config_file = null, $config_file = 'config.json')
{
$this->loadConfig($global_config_file);
$this->loadConfigOverrides($config_file);
$this->generateTree();
}
private function loadConfig($global_config_file)
{
if (is_null($global_config_file)) {
$global_config_file = $this->local_base . DS . 'global.json';
}
if (!file_exists($global_config_file)) {
throw new Exception('The Global Config file is missing. Requested File : ' . $global_config_file);
}
@ -55,20 +67,24 @@
}
$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 . DS . $this->docs_path)) {
throw new Exception('The Docs directory does not exist. Check the path again : ' . $this->docs_path);
}
if (!isset($global_config['valid_markdown_extensions'])) static::$VALID_MARKDOWN_EXTENSIONS = array('md', 'markdown');
else static::$VALID_MARKDOWN_EXTENSIONS = $global_config['valid_markdown_extensions'];
if (!isset($global_config['valid_markdown_extensions'])) {
static::$VALID_MARKDOWN_EXTENSIONS = array('md', 'markdown');
} else {
static::$VALID_MARKDOWN_EXTENSIONS = $global_config['valid_markdown_extensions'];
}
}
private function load_docs_config($config_file) {
$config_file = $this->docs_path . DIRECTORY_SEPARATOR . $config_file;
private function loadConfigOverrides($config_file)
{
$config_file = $this->docs_path . DS . $config_file;
if (!file_exists($config_file)) {
throw new Exception('The local config file is missing. Check path : ' . $config_file);
}
$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 . DS . 'default.json'), true);
if (is_file($config_file)) {
$config = json_decode(file_get_contents($config_file), true);
if (!isset($config)) {
@ -76,12 +92,16 @@
}
$this->options = array_merge($this->options, $config);
}
if (isset($this->options['timezone'])) date_default_timezone_set($this->options['timezone']);
else if (!ini_get('date.timezone')) date_default_timezone_set('GMT');
if (isset($this->options['timezone'])) {
date_default_timezone_set($this->options['timezone']);
} elseif (!ini_get('date.timezone')) {
date_default_timezone_set('GMT');
}
}
private function generate_directory_tree() {
$this->tree = Builder::build($this->docs_path, $this->options['ignore'], $this->mode);
private function generateTree()
{
$this->tree = Builder::build($this->docs_path, $this->options['ignore'], $this->getParams());
if (!empty($this->options['languages'])) {
foreach ($this->options['languages'] as $key => $node) {
$this->tree->value[$key]->title = $node;
@ -89,7 +109,8 @@
}
}
public function get_base_params() {
public function getParams()
{
$params = array(
//Informations
'tagline' => $this->options['tagline'],
@ -101,8 +122,10 @@
'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']) : '',
'google_analytics' => ($g = $this->options['google_analytics']) ?
DauxHelper::googleAnalytics($g, $this->host) : '',
'piwik_analytics' => ($p = $this->options['piwik_analytics']) ?
DauxHelper::piwikAnalytics($p, $this->options['piwik_analytics_id']) : '',
'toggle_code' => $this->options['toggle_code'],
'float' => $this->options['float'],
'date_modified' => $this->options['date_modified'],
@ -117,52 +140,39 @@
'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'],
);
if (!$params['multilanguage']) {
if ($this->tree) {
$params['tree'] = $this->tree;
$params['index'] = ($index = $this->tree->getIndexPage()) ? $index : $this->tree->getFirstPage();
if ($params['multilanguage']) {
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]->getFirstPage();
}
} else {
$params['entry_page'] = $this->tree->first_page;
$params['entry_page'] = $this->tree->getFirstPage();
}
}
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'],
$params['base_url'],
$this->local_base,
$params['base_url'] . "templates/" . $params['template'] . "/themes/" . $this->options['theme'] . '/'
);
return $params;
}
//TODO :: move to server
public function get_live_page_params() {
$params = $this->get_base_params();
if ($this->mode == self::LIVE_MODE) {
$params['index_key'] = 'index';
$params['host'] = $this->host;
$params['base_page'] = $params['base_url'] = '//' . $this->base_url;
if (!$this->options['clean_urls']) $params['base_page'] .= 'index.php/';
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']);
}
} else {
$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'],
$params['theme'] = DauxHelper::getTheme(
$this->local_base . DS . 'templates' . DS . $this->options['template'] . DS . 'themes' . DS . $this->options['theme'],
$params['base_url'],
$this->local_base,
$params['base_url'] . "templates/" . $params['template'] . "/themes/" . $this->options['theme'] . '/'

View File

@ -1,9 +1,13 @@
<?php namespace Todaymade\Daux;
class DauxHelper {
class DauxHelper
{
public static function get_breadcrumb_title_from_request($request, $separator = 'Chevrons', $multilanguage = false) {
if ($multilanguage) $request = substr($request, strpos($request, '/') + 1);
public static function getBreadcrumbFromRequest($request, $separator = 'Chevrons', $multilanguage = false)
{
if ($multilanguage) {
$request = substr($request, strpos($request, '/') + 1);
}
$request = str_replace('_', ' ', $request);
switch ($separator) {
case 'Chevrons':
@ -22,13 +26,16 @@
return $request;
}
public static function get_theme($theme_folder, $base_url, $local_base, $theme_url) {
public static function getTheme($theme_folder, $base_url, $local_base, $theme_url)
{
$name = static::pathinfo($theme_folder);
$theme = array();
if (is_file($theme_folder . DIRECTORY_SEPARATOR . "config.json")) {
$theme = json_decode(file_get_contents($theme_folder . DIRECTORY_SEPARATOR . "config.json"), true);
if (!$theme) $theme = array();
if (is_file($theme_folder . DS . "config.json")) {
$theme = json_decode(file_get_contents($theme_folder . DS . "config.json"), true);
if (!$theme) {
$theme = array();
}
}
$theme['name'] = $name['filename'];
@ -40,8 +47,8 @@
'require-jquery' => false,
'bootstrap-js' => false,
'favicon' => '<base_url>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',
'template' => $local_base . DS . 'templates' . DS . 'default/default.php',
'error-template' => $local_base . DS . 'templates' . DS . 'default/error.php',
];
$substitutions = ['<local_base>' => $local_base, '<base_url>' => $base_url, '<theme_url>' => $theme_url];
@ -66,7 +73,8 @@
return $theme;
}
public static function google_analytics($analytics, $host) {
public static function googleAnalytics($analytics, $host)
{
$ga = <<<EOT
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
@ -80,7 +88,8 @@ EOT;
return $ga;
}
public static function piwik_analytics($analytics_url, $analytics_id) {
public static function piwikAnalytics($analytics_url, $analytics_id)
{
$pa = <<<EOT
<script type="text/javascript">
var _paq = _paq || [];
@ -92,7 +101,8 @@ EOT;
$pa .= '_paq.push(["setTrackerUrl", u+"piwik.php"]);';
$pa .= '_paq.push(["setSiteId", ' . $analytics_id . ']);';
$pa .= <<<EOT
var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0]; g.type="text/javascript";
var d=document, g=d.createElement("script"), s=d.getElementsByTagName("script")[0];
g.type="text/javascript";
g.defer=true; g.async=true; g.src=u+"piwik.js"; s.parentNode.insertBefore(g,s);
})();
</script>
@ -100,12 +110,21 @@ EOT;
return $pa;
}
public static function pathinfo($path) {
public static function pathinfo($path)
{
preg_match('%^(.*?)[\\\\/]*(([^/\\\\]*?)(\.([^\.\\\\/]+?)|))[\\\\/\.]*$%im', $path, $m);
if (isset($m[1])) $ret['dir']=$m[1];
if (isset($m[2])) $ret['basename']=$m[2];
if (isset($m[5])) $ret['extension']=$m[5];
if (isset($m[3])) $ret['filename']=$m[3];
if (isset($m[1])) {
$ret['dir']=$m[1];
}
if (isset($m[2])) {
$ret['basename']=$m[2];
}
if (isset($m[5])) {
$ret['extension']=$m[5];
}
if (isset($m[3])) {
$ret['filename']=$m[3];
}
return $ret;
}
}

View File

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

View File

@ -1,45 +1,49 @@
<?php namespace Todaymade\Daux\Generator;
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) {
class Generator
{
public function generate($global_config, $destination)
{
$daux = new Daux(Daux::STATIC_MODE);
$daux->initialize($global_config);
$this->generate_static($daux, $destination);
$params = $daux->getParams();
if (is_null($destination)) {
$destination = $daux->local_base . DS . 'static';
}
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);
Helper::copyAssets($destination, $daux->local_base);
$this->generateRecursive($daux->tree, $destination, $params);
}
private function recursive_generate_static($tree, $output_dir, $params, $base_url = '') {
private function generateRecursive($tree, $output_dir, $params, $base_url = '')
{
$params['base_url'] = $params['base_page'] = $base_url;
$new_params = $params;
//
$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->getFirstPage();
}
foreach ($tree->value as $key => $node) {
if ($node instanceof Directory) {
$new_output_dir = $output_dir . DIRECTORY_SEPARATOR . $key;
$new_output_dir = $output_dir . DS . $key;
@mkdir($new_output_dir);
$this->recursive_generate_static($node, $new_output_dir, $new_params, '../' . $base_url);
$this->generateRecursive($node, $new_output_dir, $new_params, '../' . $base_url);
} elseif ($node instanceof Content) {
$params['request'] = $node->get_url();
$params['file_uri'] = $node->name;
$params['request'] = $node->getUrl();
$params['file_uri'] = $node->getName();
$page = MarkdownPage::fromFile($node, $params);
file_put_contents($output_dir . DIRECTORY_SEPARATOR . $key, $page->get_page_content());
file_put_contents($output_dir . DS . $key, $page->getContent());
} else {
copy($node->local_path, $output_dir . DIRECTORY_SEPARATOR . $key);
copy($node->getPath(), $output_dir . DS . $key);
}
}
}

View File

@ -1,47 +1,54 @@
<?php namespace Todaymade\Daux\Generator;
use Todaymade\Daux\DauxHelper;
class Helper
{
class Helper {
public static function clean_copy_assets($path, $local_base){
public static function copyAssets($path, $local_base)
{
@mkdir($path);
static::clean_directory($path);
static::rmdir($path);
@mkdir($path . DS . 'resources');
static::copyRecursive($local_base . DS . 'resources', $path . DS . 'resources');
@mkdir($path . DS . 'js');
static::copyRecursive($local_base . DS . 'js', $path . DS . 'js');
@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
@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'
@mkdir($path . DS . 'templates');
@mkdir($path . DS . 'templates' . DS . 'default');
@mkdir($path . DS . 'templates' . DS . 'default' . DS . 'themes');
static::copyRecursive(
$local_base . DS . 'templates' . DS . 'default' . DS . 'themes',
$path . DS . 'templates' . DS . 'default' . DS . 'themes'
);
}
// Rmdir
private static function clean_directory($dir) {
private static function rmdir($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());
if ($file->getFilename() === '.' || $file->getFilename() === '..') {
continue;
}
if ($file->isDir()) {
rmdir($file->getRealPath());
} else {
unlink($file->getRealPath());
}
}
}
private static function copy_recursive($src,$dst) {
private static function copyRecursive($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 {
static::copyRecursive($src . '/' . $file, $dst . '/' . $file);
} else {
copy($src . '/' . $file, $dst . '/' . $file);
}
}

View File

@ -1,49 +1,54 @@
<?php namespace Todaymade\Daux;
use Todaymade\Daux\Tree\Content;
class MarkdownPage extends SimplePage
{
private $filename;
private $file;
private $params;
private $language;
private $mtime;
private $homepage;
private $breadcrumb_trail;
private static $template;
public function __construct() {
public function __construct()
{
}
// For Future Expansion
public static function fromFile($file, $params) {
public static function fromFile($file, $params)
{
$instance = new self();
$instance->initialize_from_file($file, $params);
$instance->initialize($file, $params);
return $instance;
}
private function initialize_from_file($file, $params) {
$this->title = $file->title;
$this->filename = $file->name;
$this->path = $file->local_path;
$this->mtime = $file->last_modified;
private function initialize(Content $file, $params)
{
$this->file = $file;
$this->params = $params;
$this->title = $file->title;
if ($this->title === 'index') {
$this->homepage = ($this->filename === '_index');
$this->homepage = ($this->file->getName() === '_index');
$minimum_parent_dir_size = ($params['multilanguage']) ? 2 : 1;
if (count($file->parents) >= $minimum_parent_dir_size) {
$parent = end($file->parents);
$this->title = $parent->title;
} else $this->title = $params['title'];
if (count($file->getParents()) >= $minimum_parent_dir_size) {
$parents = $file->getParents();
$parent = end($parents);
$this->title = $parent->getTitle();
} else {
$this->title = $params['title'];
}
} else {
$this->homepage = false;
}
if ($params['breadcrumbs'])
$this->breadcrumb_trail = $this->build_breadcrumb_trail($file->parents, $params['multilanguage']);
if ($params['breadcrumbs']) {
$this->breadcrumb_trail = $this->buildBreadcrumbTrail($file->getParents(), $params['multilanguage']);
}
$this->language = '';
if ($params['multilanguage'] && !empty($file->parents)) {
reset($file->parents);
$language_dir = current($file->parents);
if ($params['multilanguage'] && count($file->getParents())) {
reset($file->getParents());
$language_dir = current($file->getParents());
$this->language = $language_dir->name;
}
if (is_null(static::$template)) {
@ -52,63 +57,70 @@ class MarkdownPage extends SimplePage
}
}
private function build_breadcrumb_trail($parents, $multilanguage) {
if ($multilanguage && !empty($parents)) $parents = array_splice($parents, 1);
private function buildBreadcrumbTrail($parents, $multilanguage)
{
if ($multilanguage && !empty($parents)) {
$parents = array_splice($parents, 1);
}
$breadcrumb_trail = array();
if (!empty($parents)) {
foreach ($parents as $node) {
$breadcrumb_trail[$node->title] = $node->get_url();
$breadcrumb_trail[$node->getTitle()] = $node->getUrl();
}
}
return $breadcrumb_trail;
}
public function get_page_content() {
public function getContent()
{
if (is_null($this->html)) {
$this->content = file_get_contents($this->path);
$this->html = $this->generate_page();
$this->content = file_get_contents($this->file->getPath());
$this->html = $this->generatePage();
}
return $this->html;
}
private function generate_page() {
private function generatePage()
{
$params = $this->params;
$Parsedown = new \Parsedown();
$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;
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]->getUrl());
}
} else {
$entry_page['View Documentation'] = utf8_encode($params['base_page'] . $params['entry_page']->getUri());
}
} elseif ($params['file_uri'] === 'index') {
$entry_page[utf8_encode($params['entry_page']->title)] = utf8_encode($params['base_page'] . $params['entry_page']->getUrl());
}
$page['entry_page'] = $entry_page;
$page['homepage'] = $this->homepage;
$page['title'] = $this->title;
$page['title'] = $this->file->getTitle();
$page['tagline'] = $params['tagline'];
$page['author'] = $params['author'];
$page['filename'] = $this->filename;
$page['filename'] = $this->file->getName();
if ($page['breadcrumbs'] = $params['breadcrumbs']) {
$page['breadcrumb_trail'] = $this->breadcrumb_trail;
$page['breadcrumb_separator'] = $params['breadcrumb_separator'];
}
$page['language'] = $this->language;
$page['path'] = $this->path;
$page['path'] = $this->file->getPath();
$page['request'] = utf8_encode($params['request']);
$page['theme'] = $params['theme'];
$page['modified_time'] = filemtime($this->path);
$page['modified_time'] = filemtime($this->file->getPath());
$page['markdown'] = $this->content;
$page['content'] = $Parsedown->text($this->content);
$page['file_editor'] = $params['file_editor'];
$page['google_analytics'] = $params['google_analytics'];
$page['piwik_analytics'] = $params['piwik_analytics'];
$Parsedown = new \Parsedown();
$page['content'] = $Parsedown->text($this->content);
return static::$template->get_content($page, $params);
}
}

View File

@ -2,6 +2,6 @@
interface Page
{
function get_page_content();
function display();
public function getContent();
public function display();
}

View File

@ -12,28 +12,32 @@ class ErrorPage extends SimplePage
private $params;
private static $template;
public function __construct($title, $content, $params) {
public function __construct($title, $content, $params)
{
parent::__construct($title, $content);
$this->params = $params;
}
public function display() {
public function display()
{
http_response_code(404);
parent::display();
}
public function get_page_content() {
public function getContent()
{
include_once($this->params['theme']['error-template']);
static::$template = new Template();
if (is_null($this->html)) {
$this->html = $this->generate_page();
$this->html = $this->generatePage();
}
return $this->html;
}
public function generate_page() {
private function generatePage()
{
$params = $this->params;
$page['title'] = $this->title;
$page['theme'] = $params['theme'];

View File

@ -1,17 +1,21 @@
<?php namespace Todaymade\Daux\Server;
use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper;
class Helper {
public static function get_request()
class Helper
{
if (isset($_SERVER['PATH_INFO'])) $uri = $_SERVER['PATH_INFO'];
else if (isset($_SERVER['REQUEST_URI'])) {
public static function getRequest()
{
if (isset($_SERVER['PATH_INFO'])) {
$uri = $_SERVER['PATH_INFO'];
} elseif (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);
if (strpos($uri, $_SERVER['SCRIPT_NAME']) === 0) {
$uri = substr($uri, strlen($_SERVER['SCRIPT_NAME']));
} elseif (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])) {
@ -22,10 +26,13 @@ class Helper {
$_GET = array();
}
$uri = parse_url($uri, PHP_URL_PATH);
} else {
return false;
}
else return false;
$uri = str_replace(array('//', '../'), '/', trim($uri, '/'));
if ($uri == "") $uri = "first_page";
if ($uri == "") {
$uri = "first_page";
}
return $uri;
}
}

View File

@ -2,6 +2,6 @@
use Todaymade\Daux\Exception;
class NotFoundException extends Exception {
class NotFoundException extends Exception
{
}

View File

@ -4,38 +4,40 @@ use Todaymade\Daux\Daux;
use Todaymade\Daux\Exception;
use Todaymade\Daux\MarkdownPage;
use Todaymade\Daux\SimplePage;
use Todaymade\Daux\Tree\Directory;
class Server {
class Server
{
private $daux;
private $params;
public static function serve() {
public static function serve()
{
$daux = new Daux(Daux::LIVE_MODE);
try
{
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(), $daux->get_live_page_params());
$page = $server->handle($_REQUEST);
} catch (NotFoundException $e) {
$page = new ErrorPage("An error occured", $e->getMessage(), $daux->getParams());
}
$page->display();
}
public function __construct(Daux $daux) {
public function __construct(Daux $daux)
{
$this->daux = $daux;
}
public function handle($url, $query = []) {
$this->params = $this->daux->get_live_page_params();
public function handle($query = [])
{
$this->params = $this->daux->getParams();
$request = Helper::get_request();
$request = Helper::getRequest();
$request = urldecode($request);
$request_type = isset($query['method']) ? $query['method'] : '';
if ($request == 'first_page') {
@ -48,24 +50,30 @@ class Server {
}
$content = isset($query['markdown']) ? $query['markdown'] : '';
return $this->save_file($request, $content);
return $this->saveFile($request, $content);
default:
return $this->get_page($request);
return $this->getPage($request);
}
}
private function save_file($request, $content) {
$file = $this->get_file_from_request($request);
private function saveFile($request, $content)
{
$file = $this->getFile($request);
if ($file === false) throw new NotFoundException('The Page you requested is yet to be made. Try again later.');
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.');
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) {
private function getFile($request)
{
$tree = $this->daux->tree;
$request = explode('/', $request);
foreach ($request as $node) {
@ -90,7 +98,7 @@ class Server {
return false;
}
return $tree->index_page;
return $tree->getIndexPage();
}
// If the entry we found is not a directory, we're done
@ -98,21 +106,26 @@ class Server {
return $tree;
}
if ($tree->index_page){
return $tree->index_page;
if ($tree->getIndexPage()) {
return $tree->getIndexPage();
}
return ($get_first_file) ? $tree->first_page : false;
return false;
}
private function get_page($request) {
private function getPage($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.');
$file = $this->getFile($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;
if ($request !== 'index') {
$params['entry_page'] = $file->first_page;
}
return MarkdownPage::fromFile($file, $params);
}
}

View File

@ -6,29 +6,34 @@ class SimplePage implements Page
protected $content;
protected $html = null;
public function __construct($title, $content) {
$this->initialize_page($title, $content);
public function __construct($title, $content)
{
$this->initializePage($title, $content);
}
public function initialize_page($title, $content) {
$this->title = $title;
$this->content = $content;
}
public function display() {
public function display()
{
header('Content-type: text/html; charset=utf-8');
echo $this->get_page_content();
echo $this->getContent();
}
public function get_page_content() {
public function getContent()
{
if (is_null($this->html)) {
$this->html = $this->generate_page();
$this->html = $this->generatePage();
}
return $this->html;
}
private function generate_page() {
private function initializePage($title, $content)
{
$this->title = $title;
$this->content = $content;
}
private function generatePage()
{
return $this->content;
}
}

View File

@ -3,8 +3,10 @@
use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper;
class Builder {
public static function build($dir, $ignore, $mode = Daux::LIVE_MODE, $parents = null) {
class Builder
{
public static function build($dir, $ignore, $params, $parents = null)
{
if (!$dh = opendir($dir)) {
return;
}
@ -23,9 +25,12 @@ class Builder {
continue;
}
$path = $dir . DIRECTORY_SEPARATOR . $file;
$path = $dir . DS . $file;
if ((is_dir($path) && in_array($file, $ignore['folders'])) || (!is_dir($path) && in_array($file, $ignore['files']))) {
if (is_dir($path) && in_array($file, $ignore['folders'])) {
continue;
}
if (!is_dir($path) && in_array($file, $ignore['files'])) {
continue;
}
@ -33,30 +38,29 @@ class Builder {
$entry = null;
if (is_dir($path)) {
$entry = static::build($path, $ignore, $mode, $new_parents);
$entry = static::build($path, $ignore, $params, $new_parents);
} elseif (in_array($file_details['extension'], Daux::$VALID_MARKDOWN_EXTENSIONS)) {
$entry = new Content($path, $new_parents);
if ($mode === Daux::STATIC_MODE) {
$entry->uri .= '.html';
if ($params['mode'] === Daux::STATIC_MODE) {
$entry->setUri($entry->getUri() . '.html');
}
} else {
$entry = new Raw($path, $new_parents);
}
if ($entry instanceof Entry) {
$node->value[$entry->uri] = $entry;
$node->value[$entry->getUri()] = $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;
if (isset($node->value[$params['index_key']])) {
$node->value[$params['index_key']]->setFirstPage($node->getFirstPage());
$node->setIndexPage($node->value[$params['index_key']]);
} else {
$node->setIndexPage(false);
}
return $node;
}
}

View File

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

View File

@ -1,14 +1,16 @@
<?php namespace Todaymade\Daux\Tree;
class Directory extends Entry {
class Directory extends Entry
{
public $value = [];
public function sort() {
uasort($this->value, array($this, 'compare_directory_entries'));
public function sort()
{
uasort($this->value, array($this, 'compareEntries'));
}
private function compare_directory_entries($a, $b) {
private function compareEntries($a, $b)
{
$name_a = explode('_', $a->name);
$name_b = explode('_', $b->name);
if (is_numeric($name_a[0])) {
@ -18,26 +20,37 @@ class Directory extends Entry {
if (($a >= 0) == ($b >= 0)) {
$a = abs($a);
$b = abs($b);
if ($a == $b) return (strcasecmp($name_a[1], $name_b[1]));
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;
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;
if ($b >= 0) {
return 1;
}
$t = $name_a[0];
if ($t && $t[0] === '-') return 1;
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;
if (($p && $p[0] === '-') == ($q && $q[0] === '-')) {
return strcasecmp($p, $q);
} else {
return ($p[0] === '-') ? 1 : -1;
}
}
}
}

View File

@ -4,27 +4,45 @@ 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;
protected $title;
protected $name;
protected $index_page;
protected $first_page;
protected $uri;
protected $local_path;
protected $last_modified;
protected $parents;
function __construct($path = '', $parents = array()) {
if (!isset($path) || $path == '' || !file_exists($path)) return;
public 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->title = $this->getTitleInternal($this->name);
$this->uri = $this->getUrlInternal($this->getFilename($path));
$this->index_page = false;
}
public function getName()
{
return $this->name;
}
public function get_url() {
public function setUri($uri)
{
$this->uri = $uri;
}
public function getUri()
{
return $this->uri;
}
public function getUrl()
{
$url = '';
foreach ($this->parents as $node) {
$url .= $node->uri . '/';
@ -33,21 +51,59 @@ use Todaymade\Daux\DauxHelper;
return $url;
}
public function get_first_page() {
public function getIndexPage()
{
return $this->index_page;
}
public function setIndexPage($index_page)
{
$this->index_page = $index_page;
}
public function getFirstPage()
{
if ($this->first_page) {
return $this->first_page;
}
foreach ($this->value as $node) {
if ($node instanceof Content && $node->title != 'index')
if ($node instanceof Content && $node->title != 'index') {
$this->first_page = $node;
return $node;
}
}
foreach ($this->value as $node) {
if ($node instanceof Directory) {
$page = $node->get_first_page();
if ($page) return $page;
if ($node instanceof Directory && $page = $node->getFirstPage()) {
$this->first_page = $page;
return $page;
}
}
return false;
}
public function write($content) {
public function setFirstPage($first_page)
{
$this->first_page = $first_page;
}
public function getTitle()
{
return $this->title;
}
public function getParents()
{
return $this->parents;
}
public function getPath()
{
return $this->local_path;
}
public function write($content)
{
if (!is_writable($this->local_path)) {
return false;
}
@ -56,31 +112,39 @@ use Todaymade\Daux\DauxHelper;
return true;
}
protected function getFilename($file) {
protected function getFilename($file)
{
$parts = explode('/', $file);
return end($parts);
}
protected function get_title_from_filename($filename) {
protected function getTitleInternal($filename)
{
$filename = explode('_', $filename);
if ($filename[0] == '' || is_numeric($filename[0])) unset($filename[0]);
else {
if ($filename[0] == '' || is_numeric($filename[0])) {
unset($filename[0]);
} else {
$t = $filename[0];
if ($t[0] == '-') $filename[0] = substr($t, 1);
if ($t[0] == '-') {
$filename[0] = substr($t, 1);
}
}
$filename = implode(' ', $filename);
return $filename;
}
protected function get_url_from_filename($filename) {
protected function getUrlInternal($filename)
{
$filename = explode('_', $filename);
if ($filename[0] == '' || is_numeric($filename[0])) unset($filename[0]);
else {
if ($filename[0] == '' || is_numeric($filename[0])) {
unset($filename[0]);
} else {
$t = $filename[0];
if ($t[0] == '-') $filename[0] = substr($t, 1);
if ($t[0] == '-') {
$filename[0] = substr($t, 1);
}
}
$filename = implode('_', $filename);
return $filename;
}
}

View File

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

View File

@ -12,13 +12,14 @@
private function build_navigation($tree, $path, $current_url, $base_page, $mode) {
$nav = '';
foreach ($tree->value as $node) {
$url = $node->uri;
$url = $node->getUri();
if ($node instanceof \Todaymade\Daux\Tree\Content) {
if ($node->value === 'index') continue;
$nav .= '<li';
$link = ($path === '') ? $url : $path . '/' . $url;
if ($current_url === $link) $nav .= ' class="active"';
$nav .= '><a href="' . $base_page . $link . '">' . $node->title . '</a></li>';
$nav .= '><a href="' . $base_page . $link . '">' . $node->getTitle() . '</a></li>';
}
if ($node instanceof \Todaymade\Daux\Tree\Directory) {
$nav .= '<li';
@ -26,9 +27,9 @@
if (strpos($current_url, $link) === 0) $nav .= ' class="open"';
$nav .= ">";
if ($mode === \TodayMade\Daux\Daux::STATIC_MODE) $link .= "/index.html";
if ($node->index_page) $nav .= '<a href="' . $base_page . $link . '" class="folder">' .
$node->title . '</a>';
else $nav .= '<a href="#" class="aj-nav folder">' . $node->title . '</a>';
if ($node->getIndexPage()) $nav .= '<a href="' . $base_page . $link . '" class="folder">' .
$node->getTitle() . '</a>';
else $nav .= '<a href="#" class="aj-nav folder">' . $node->getTitle() . '</a>';
$nav .= '<ul class="nav nav-list">';
$new_path = ($path === '') ? $url : $path . '/' . $url;
$nav .= $this->build_navigation($node, $new_path, $current_url, $base_page, $mode);