diff --git a/daux.phar b/daux.phar old mode 100644 new mode 100755 index 3621bae..20e5f6b Binary files a/daux.phar and b/daux.phar differ diff --git a/generate b/generate index 146dd5b..9c5d154 100755 --- a/generate +++ b/generate @@ -66,7 +66,5 @@ software, even if advised of the possibility of such damage. require_once("vendor/autoload.php"); -\Todaymade\Daux\Daux::initConstants(); - $application = new \Todaymade\Daux\Generator\Application(); $application->run(); diff --git a/index.php b/index.php index 5dd44fe..350e1f4 100644 --- a/index.php +++ b/index.php @@ -84,6 +84,4 @@ if (file_exists('vendor/autoload.php')) { throw new Exception("Impossible to load Daux, missing vendor and phar"); } -\Todaymade\Daux\Daux::initConstants(); - \Todaymade\Daux\Server\Server::serve($_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], $_REQUEST); diff --git a/libs/Daux.php b/libs/Daux.php index c654ba2..14815ba 100644 --- a/libs/Daux.php +++ b/libs/Daux.php @@ -53,22 +53,32 @@ class Daux } } - public static function initConstants() - { - define("DS", DIRECTORY_SEPARATOR); - } - /** * @param string $override_file * @throws Exception */ public function initialize($override_file = 'config.json') { - //global.json (docs dir, markdown files) - $this->loadConfig(); + // global.json + $this->loadBaseConfiguration(); - //config.json - $this->loadConfigOverrides($override_file); + // Check the documentation path + $this->docs_path = $this->options['docs_directory']; + if (!is_dir($this->docs_path) && + !is_dir($this->docs_path = $this->local_base . DIRECTORY_SEPARATOR . $this->docs_path) + ) { + throw new Exception('The Docs directory does not exist. Check the path again : ' . $this->docs_path); + } + + // /config.json, + $this->loadConfigurationOverrides($override_file); + + // Set a valid default timezone + if (isset($this->options['timezone'])) { + date_default_timezone_set($this->options['timezone']); + } elseif (!ini_get('date.timezone')) { + date_default_timezone_set('GMT'); + } } /** @@ -76,31 +86,18 @@ class Daux * * @throws Exception */ - private function loadConfig() + protected function loadBaseConfiguration() { - $default_config = [ + $this->options = new Config(); + + // Set the default configuration + $this->options->merge([ 'docs_directory' => 'docs', 'valid_content_extensions' => ['md', 'markdown'] - ]; + ]); - $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); - } - - $default_config = array_merge($default_config, json_decode(file_get_contents($global_config_file), true)); - if (!isset($default_config)) { - throw new Exception('The Global Config file is corrupt. Check that the JSON encoding is correct'); - } - - $this->docs_path = $default_config['docs_directory']; - 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); - } - - $this->options = new Config(); - $this->options->merge($default_config); + // Load the global configuration + $this->loadConfiguration($this->local_base . DIRECTORY_SEPARATOR . 'global.json', false); } /** @@ -111,33 +108,37 @@ class Daux * @param string $override_file * @throws Exception */ - private function loadConfigOverrides($override_file) + protected function loadConfigurationOverrides($override_file) { // Read documentation overrides - $config_file = $this->docs_path . DS . 'config.json'; - if (file_exists($config_file)) { - $config = json_decode(file_get_contents($config_file), true); - if (!isset($config)) { - throw new Exception('The local config file is missing. Check path : ' . $config_file); - } - $this->options->merge($config); - } + $this->loadConfiguration($this->docs_path . DIRECTORY_SEPARATOR . 'config.json'); // Read command line overrides - $config_file = $this->local_base . DS . $override_file; - if (!is_null($override_file) && file_exists($config_file)) { - $config = json_decode(file_get_contents($config_file), true); - if (!isset($config)) { - throw new Exception('The local config file is missing. Check path : ' . $config_file); + if (!is_null($override_file)) { + $this->loadConfiguration($this->local_base . DIRECTORY_SEPARATOR . $override_file); + } + } + + /** + * @param string $config_file + * @param bool $optional + * @throws Exception + */ + protected function loadConfiguration($config_file, $optional = true) + { + if (!file_exists($config_file)) { + if ($optional) { + return; } - $this->options->merge($config); + + throw new Exception('The configuration file is missing. Check path : ' . $config_file); } - if (isset($this->options['timezone'])) { - date_default_timezone_set($this->options['timezone']); - } elseif (!ini_get('date.timezone')) { - date_default_timezone_set('GMT'); + $config = json_decode(file_get_contents($config_file), true); + if (!isset($config)) { + throw new Exception('The configuration file "' . $config_file . '" is corrupt. Is your JSON well-formed ?'); } + $this->options->merge($config); } /** @@ -169,7 +170,7 @@ class Daux 'mode' => $this->mode, 'local_base' => $this->local_base, 'docs_path' => $this->docs_path, - 'templates' => $this->internal_base . DS . 'templates', + 'templates' => $this->internal_base . DIRECTORY_SEPARATOR . 'templates', ]; $this->options->conservativeMerge($default); diff --git a/libs/DauxHelper.php b/libs/DauxHelper.php index 1671442..f826fd1 100644 --- a/libs/DauxHelper.php +++ b/libs/DauxHelper.php @@ -4,6 +4,12 @@ use Todaymade\Daux\Tree\Directory; class DauxHelper { + /** + * Set a new base_url for the configuration + * + * @param Config $config + * @param string $base_url + */ public static function rebaseConfiguration(Config $config, $base_url) { // Avoid changing the url if it is already correct @@ -24,12 +30,13 @@ class DauxHelper */ public static function getTheme($params, $current_url) { - $theme_folder = $params['local_base'] . DS . 'resources' . DS . 'themes' . DS . $params['html']['theme']; + $theme_folder = $params['local_base'] . DIRECTORY_SEPARATOR . 'resources' . + DIRECTORY_SEPARATOR . 'themes' . DIRECTORY_SEPARATOR . $params['html']['theme']; $theme_url = $params['base_url'] . "resources/themes/" . $params['html']['theme'] . '/'; $theme = array(); - if (is_file($theme_folder . DS . "config.json")) { - $theme = json_decode(file_get_contents($theme_folder . DS . "config.json"), true); + 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(); } @@ -44,7 +51,7 @@ class DauxHelper 'require-jquery' => false, 'bootstrap-js' => false, 'favicon' => 'resources/img/favicon.png', - 'templates' => $theme_folder . DS . 'templates', + 'templates' => $theme_folder . DIRECTORY_SEPARATOR . 'templates', ]; $substitutions = [ diff --git a/libs/Format/Base/CommonMark/LinkRenderer.php b/libs/Format/Base/CommonMark/LinkRenderer.php index 3411dfd..06c7ebd 100644 --- a/libs/Format/Base/CommonMark/LinkRenderer.php +++ b/libs/Format/Base/CommonMark/LinkRenderer.php @@ -49,6 +49,17 @@ class LinkRenderer extends \League\CommonMark\Inline\Renderer\LinkRenderer */ public function render(AbstractInline $inline, HtmlRendererInterface $htmlRenderer) { + // This can't be in the method type as + // the method is an abstract and should + // have the same interface + if (!$inline instanceof Link) { + throw new \RuntimeException( + "Wrong type passed to " . __CLASS__ . "::" . __METHOD__ . + " the expected type was 'League\\CommonMark\\Inline\\Element\\Link' but '" . + get_class($inline) . "' was provided" + ); + } + $element = parent::render($inline, $htmlRenderer); $url = $inline->getUrl(); diff --git a/libs/Format/Confluence/CommonMark/LinkRenderer.php b/libs/Format/Confluence/CommonMark/LinkRenderer.php index 2ae066f..5b6bfd2 100644 --- a/libs/Format/Confluence/CommonMark/LinkRenderer.php +++ b/libs/Format/Confluence/CommonMark/LinkRenderer.php @@ -15,6 +15,17 @@ class LinkRenderer extends \Todaymade\Daux\Format\Base\CommonMark\LinkRenderer */ public function render(AbstractInline $inline, HtmlRendererInterface $htmlRenderer) { + // This can't be in the method type as + // the method is an abstract and should + // have the same interface + if (!$inline instanceof Link) { + throw new \RuntimeException( + "Wrong type passed to " . __CLASS__ . "::" . __METHOD__ . + " the expected type was 'League\\CommonMark\\Inline\\Element\\Link' but '" . + get_class($inline) . "' was provided" + ); + } + // Default handling $element = parent::render($inline, $htmlRenderer); $url = $inline->getUrl(); diff --git a/libs/Format/Confluence/Generator.php b/libs/Format/Confluence/Generator.php index d159e10..779220a 100644 --- a/libs/Format/Confluence/Generator.php +++ b/libs/Format/Confluence/Generator.php @@ -31,6 +31,9 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator $this->converter = new CommonMarkConverter(['daux' => $this->daux->getParams()]); } + /** + * {@inheritdoc} + */ public function generateAll(InputInterface $input, OutputInterface $output, $width) { $params = $this->daux->getParams(); diff --git a/libs/Format/Confluence/Publisher.php b/libs/Format/Confluence/Publisher.php index 03e63df..8da36ce 100644 --- a/libs/Format/Confluence/Publisher.php +++ b/libs/Format/Confluence/Publisher.php @@ -25,7 +25,7 @@ class Publisher protected $previous_title; /** - * @var string terminal width + * @var integer terminal width */ public $width; diff --git a/libs/Format/HTML/Generator.php b/libs/Format/HTML/Generator.php index 9be5276..de00af6 100644 --- a/libs/Format/HTML/Generator.php +++ b/libs/Format/HTML/Generator.php @@ -39,14 +39,14 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator, LiveGenerator $params = $this->daux->getParams(); if (is_null($destination)) { - $destination = $this->daux->local_base . DS . 'static'; + $destination = $this->daux->local_base . DIRECTORY_SEPARATOR . 'static'; } $this->runAction( "Copying Static assets ...", $output, $width, - function () use ($destination) { + function() use ($destination) { Helper::copyAssets($destination, $this->daux->local_base); } ); @@ -76,7 +76,7 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator, LiveGenerator foreach ($tree->getEntries() as $key => $node) { if ($node instanceof Directory) { - $new_output_dir = $output_dir . DS . $key; + $new_output_dir = $output_dir . DIRECTORY_SEPARATOR . $key; mkdir($new_output_dir); $this->generateRecursive($node, $new_output_dir, $params, $output, $width, '../' . $base_url); @@ -87,14 +87,14 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator, LiveGenerator "- " . $node->getUrl(), $output, $width, - function () use ($node, $output_dir, $key, $params) { + function() use ($node, $output_dir, $key, $params) { if (!$node instanceof Content) { - copy($node->getPath(), $output_dir . DS . $key); + copy($node->getPath(), $output_dir . DIRECTORY_SEPARATOR . $key); return; } $generated = $this->generateOne($node, $params); - file_put_contents($output_dir . DS . $key, $generated->getContent()); + file_put_contents($output_dir . DIRECTORY_SEPARATOR . $key, $generated->getContent()); } ); } diff --git a/libs/Generator/Command.php b/libs/Generator/Command.php index 312df2c..620f0c1 100644 --- a/libs/Generator/Command.php +++ b/libs/Generator/Command.php @@ -5,8 +5,6 @@ use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Todaymade\Daux\Daux; -use Todaymade\Daux\Format\HTML\Generator as HTMLGenerator; -use Todaymade\Daux\Format\Confluence\Generator as ConfluenceGenerator; class Command extends SymfonyCommand { @@ -53,7 +51,7 @@ class Command extends SymfonyCommand } $class = $daux->getProcessorClass(); - if ($class) { + if (!empty($class)) { $daux->setProcessor(new $class($daux, $output, $width)); } } diff --git a/libs/Generator/Helper.php b/libs/Generator/Helper.php index 167dead..48f50d6 100644 --- a/libs/Generator/Helper.php +++ b/libs/Generator/Helper.php @@ -16,8 +16,11 @@ class Helper mkdir($path); } - mkdir($path . DS . 'resources'); - static::copyRecursive($local_base . DS . 'resources', $path . DS . 'resources'); + mkdir($path . DIRECTORY_SEPARATOR . 'resources'); + static::copyRecursive( + $local_base . DIRECTORY_SEPARATOR . 'resources', + $path . DIRECTORY_SEPARATOR . 'resources' + ); } /** diff --git a/libs/Server/Server.php b/libs/Server/Server.php index bf98728..f1ee380 100644 --- a/libs/Server/Server.php +++ b/libs/Server/Server.php @@ -25,7 +25,7 @@ class Server $daux->initialize(); $class = $daux->getProcessorClass(); - if ($class) { + if (!empty($class)) { $daux->setProcessor(new $class($daux, new NullOutput(), 0)); } @@ -101,7 +101,7 @@ class Server * Handle an incoming request * * @param array $query - * @return \Todaymade\Daux\Tree\Entry + * @return \Todaymade\Daux\Format\Base\Page * @throws Exception * @throws NotFoundException */ @@ -120,7 +120,7 @@ class Server /** * @param string $request - * @return \Todaymade\Daux\Tree\Entry + * @return \Todaymade\Daux\Format\Base\Page * @throws NotFoundException */ private function getPage($request) diff --git a/libs/Tree/Builder.php b/libs/Tree/Builder.php index 230264a..6d1e88d 100644 --- a/libs/Tree/Builder.php +++ b/libs/Tree/Builder.php @@ -27,7 +27,7 @@ class Builder continue; } - $path = $node->getPath() . DS . $file; + $path = $node->getPath() . DIRECTORY_SEPARATOR . $file; if (is_dir($path) && in_array($file, $ignore['folders'])) { continue; @@ -37,9 +37,9 @@ class Builder } if (is_dir($path)) { - $new = new Directory($node, static::getUriFromFilename(static::getFilename($path)), $path); + $new = new Directory($node, static::removeSortingInformations(static::getFilename($path)), $path); $new->setName(DauxHelper::pathinfo($path)['filename']); - $new->setTitle(static::getTitleFromFilename($new->getName())); + $new->setTitle(static::removeSortingInformations($new->getName(), ' ')); static::build($new, $ignore); } else { static::createContent($node, $path); @@ -61,14 +61,14 @@ class Builder $config = $parent->getConfig(); if (!in_array(pathinfo($path, PATHINFO_EXTENSION), $config['valid_content_extensions'])) { - $entry = new Raw($parent, static::getUriFromFilename(static::getFilename($path)), $path, filemtime($path)); - $entry->setTitle(static::getTitleFromFilename($name)); + $entry = new Raw($parent, static::removeSortingInformations(static::getFilename($path)), $path, filemtime($path)); + $entry->setTitle(static::removeSortingInformations($name, ' ')); $entry->setName($name); return $entry; } - $uri = static::getUriFromFilename($name); + $uri = static::removeSortingInformations($name); if ($config['mode'] === Daux::STATIC_MODE) { $uri .= '.html'; } @@ -82,7 +82,7 @@ class Builder $entry->setTitle($parent->getTitle()); } } else { - $entry->setTitle(static::getTitleFromFilename($name)); + $entry->setTitle(static::removeSortingInformations($name, ' ')); } $entry->setName($name); @@ -104,7 +104,7 @@ class Builder * @param string $filename * @return string */ - protected static function getTitleFromFilename($filename) + protected static function removeSortingInformations($filename, $separator = '_') { $filename = explode('_', $filename); if ($filename[0] == '' || is_numeric($filename[0])) { @@ -115,26 +115,7 @@ class Builder $filename[0] = substr($t, 1); } } - $filename = implode(' ', $filename); - return $filename; - } - - /** - * @param string $filename - * @return string - */ - protected static function getUriFromFilename($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); + $filename = implode($separator, $filename); return $filename; } diff --git a/libs/Tree/Directory.php b/libs/Tree/Directory.php index 1e1851c..a994b79 100644 --- a/libs/Tree/Directory.php +++ b/libs/Tree/Directory.php @@ -58,7 +58,7 @@ class Directory extends Entry } /** - * @return Content|false + * @return Content|null */ public function getFirstPage() { @@ -87,7 +87,7 @@ class Directory extends Entry } } - return false; + return null; } /** diff --git a/templates/content.php b/templates/content.php index 585b92a..525165c 100644 --- a/templates/content.php +++ b/templates/content.php @@ -3,27 +3,33 @@ - + diff --git a/templates/error.php b/templates/error.php index 67d16a4..b56c671 100644 --- a/templates/error.php +++ b/templates/error.php @@ -2,8 +2,8 @@
- +
diff --git a/templates/home.php b/templates/home.php index 3975562..a2ffff9 100644 --- a/templates/home.php +++ b/templates/home.php @@ -5,19 +5,23 @@ - Fork me on GitHub + Fork me on GitHub
- ' . $params['tagline'] . ''; ?> + ' . $params['tagline'] . ''; + } ?>
- '; ?> + '; + } ?>
@@ -28,8 +32,12 @@
View On GitHub'; - foreach ($page['entry_page'] as $key => $node) echo '' . $key . ''; + if ($params['html']['repo']) { + echo 'View On GitHub'; + } + foreach ($page['entry_page'] as $key => $node) { + echo '' . $key . ''; + } ?>
@@ -40,7 +48,7 @@
- +
@@ -52,7 +60,9 @@
@@ -60,10 +70,10 @@
- <?php echo $page['title']; ?> - - + <?= $page['title']; ?> + + - + - "; ?> + "; + } ?> - "; ?> + "; + } ?> '; - if ($params['theme']['bootstrap-js']) echo ''; + if ($params['theme']['require-jquery']) { + echo ''; + } + if ($params['theme']['bootstrap-js']) { + echo ''; + } ?> - + - '; ?> + '; + } ?> - + diff --git a/templates/layout/05_page.php b/templates/layout/05_page.php index b34b276..bae70d7 100644 --- a/templates/layout/05_page.php +++ b/templates/layout/05_page.php @@ -1,7 +1,7 @@ layout('theme::layout/00_layout') ?> - Fork me on GitHub + Fork me on GitHub
-
+
section('content'); ?>
diff --git a/templates/partials/navbar_content.php b/templates/partials/navbar_content.php index 7932a42..9017004 100644 --- a/templates/partials/navbar_content.php +++ b/templates/partials/navbar_content.php @@ -1,2 +1,2 @@ - +