daux.io/libs/Generator/Generator.php

43 regels
1.7 KiB
PHP

2015-04-22 12:23:57 +02:00
<?php namespace Todaymade\Daux\Generator;
use Todaymade\Daux\Daux;
use Todaymade\Daux\Entry;
use Todaymade\Daux\MarkdownPage;
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->type === Entry::DIRECTORY_TYPE) {
$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 {
$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());
}
}
}
}