daux.io/libs/Generator/Generator.php

51 lines
1.7 KiB
PHP
Raw Normal View History

2015-04-22 12:23:57 +02:00
<?php namespace Todaymade\Daux\Generator;
use Todaymade\Daux\Daux;
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
2015-04-23 00:32:30 +02:00
Helper::copyAssets($destination, $daux->local_base);
$this->generateRecursive($daux->tree, $destination, $params);
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;
$new_params = $params;
//
$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);
2015-04-23 00:32:30 +02:00
$this->generateRecursive($node, $new_output_dir, $new_params, '../' . $base_url);
} elseif ($node instanceof Content) {
$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 {
2015-04-23 00:32:30 +02:00
copy($node->getPath(), $output_dir . DS . $key);
2015-04-22 12:23:57 +02:00
}
}
}
}