daux.io/libs/Generator/Generator.php

64 lignes
2.1 KiB
PHP
Brut Vue normale Historique

2015-04-22 12:23:57 +02:00
<?php namespace Todaymade\Daux\Generator;
use Todaymade\Daux\Daux;
use Todaymade\Daux\DauxHelper;
2015-04-22 12:23:57 +02:00
use Todaymade\Daux\MarkdownPage;
use Todaymade\Daux\Tree\Directory;
use Todaymade\Daux\Tree\Content;
2015-04-22 12:23:57 +02:00
2015-04-23 00:32:30 +02:00
class Generator
{
public function generate($global_config, $destination)
{
2015-04-22 12:23:57 +02:00
$daux = new Daux(Daux::STATIC_MODE);
$daux->initialize($global_config);
2015-04-23 00:32:30 +02:00
$params = $daux->getParams();
if (is_null($destination)) {
$destination = $daux->local_base . DS . 'static';
}
2015-04-22 12:23:57 +02:00
echo "Copying Static assets ...\n";
2015-04-23 00:32:30 +02:00
Helper::copyAssets($destination, $daux->local_base);
echo "Generating ...\n";
2015-04-23 00:32:30 +02:00
$this->generateRecursive($daux->tree, $destination, $params);
echo "Done !\n";
2015-04-22 12:23:57 +02:00
}
2015-04-23 00:32:30 +02:00
private function generateRecursive($tree, $output_dir, $params, $base_url = '')
{
2015-04-22 12:23:57 +02:00
$params['base_url'] = $params['base_page'] = $base_url;
// Rebase Theme
$params['theme'] = DauxHelper::getTheme(
2015-04-23 21:04:00 +02:00
$params['theme-name'],
$params['base_url'],
$params['local_base'],
2015-04-23 21:04:00 +02:00
$base_url
);
2015-04-22 12:23:57 +02:00
$params['image'] = str_replace('<base_url>', $base_url, $params['image']);
2015-04-23 00:32:30 +02:00
if ($base_url !== '') {
$params['entry_page'] = $tree->getFirstPage();
}
2015-04-22 12:23:57 +02:00
foreach ($tree->value as $key => $node) {
if ($node instanceof Directory) {
2015-04-23 00:32:30 +02:00
$new_output_dir = $output_dir . DS . $key;
2015-04-22 12:23:57 +02:00
@mkdir($new_output_dir);
$this->generateRecursive($node, $new_output_dir, $params, '../' . $base_url);
2015-04-23 00:32:30 +02:00
} elseif ($node instanceof Content) {
echo "- " . $node->getUrl() . "\n";
2015-04-23 00:32:30 +02:00
$params['request'] = $node->getUrl();
$params['file_uri'] = $node->getName();
2015-04-22 12:23:57 +02:00
$page = MarkdownPage::fromFile($node, $params);
2015-04-23 00:32:30 +02:00
file_put_contents($output_dir . DS . $key, $page->getContent());
} else {
echo "- " . $node->getUrl() . "\n";
2015-04-23 00:32:30 +02:00
copy($node->getPath(), $output_dir . DS . $key);
2015-04-22 12:23:57 +02:00
}
}
}
}