daux.io/libs/Generator/Generator.php
Stéphane Goetz ecd5efe758 Extract tree builder
Add related files directly in the documentation
2015-07-20 20:49:09 +02:00

47 lines
1.9 KiB
PHP

<?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) {
$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;
//
$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 instanceof Directory) {
$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 if ($node instanceof Content) {
$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());
} else {
copy($node->local_path, $output_dir . DIRECTORY_SEPARATOR . $key);
}
}
}
}