2015-05-21 17:39:11 +02:00
|
|
|
<?php namespace Todaymade\Daux\Format\HTML;
|
|
|
|
|
2015-07-19 00:55:57 +02:00
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
2015-07-14 22:06:01 +02:00
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2019-12-07 11:22:40 +01:00
|
|
|
use Todaymade\Daux\Config as GlobalConfig;
|
2015-07-23 17:44:24 +02:00
|
|
|
use Todaymade\Daux\Console\RunAction;
|
2015-05-21 17:39:11 +02:00
|
|
|
use Todaymade\Daux\Daux;
|
|
|
|
use Todaymade\Daux\DauxHelper;
|
2015-07-19 22:38:37 +02:00
|
|
|
use Todaymade\Daux\Format\Base\LiveGenerator;
|
2016-07-29 23:46:57 +02:00
|
|
|
use Todaymade\Daux\Format\HTML\ContentTypes\Markdown\ContentType;
|
2015-07-23 17:44:24 +02:00
|
|
|
use Todaymade\Daux\GeneratorHelper;
|
2016-01-31 12:00:29 +01:00
|
|
|
use Todaymade\Daux\Tree\ComputedRaw;
|
2015-07-19 12:21:09 +02:00
|
|
|
use Todaymade\Daux\Tree\Directory;
|
|
|
|
use Todaymade\Daux\Tree\Entry;
|
|
|
|
use Todaymade\Daux\Tree\Raw;
|
2015-05-21 17:39:11 +02:00
|
|
|
|
2015-07-19 22:38:37 +02:00
|
|
|
class Generator implements \Todaymade\Daux\Format\Base\Generator, LiveGenerator
|
2015-05-21 17:39:11 +02:00
|
|
|
{
|
2018-09-21 23:43:06 +02:00
|
|
|
use RunAction, HTMLUtils;
|
2015-07-14 22:06:01 +02:00
|
|
|
|
2015-07-19 12:21:09 +02:00
|
|
|
/** @var Daux */
|
|
|
|
protected $daux;
|
|
|
|
|
2019-11-28 23:32:33 +01:00
|
|
|
/** @var Template */
|
|
|
|
protected $templateRenderer;
|
|
|
|
|
2016-03-13 21:51:58 +01:00
|
|
|
protected $indexed_pages = [];
|
|
|
|
|
2015-07-19 01:05:43 +02:00
|
|
|
/**
|
2015-07-19 12:21:09 +02:00
|
|
|
* @param Daux $daux
|
2015-07-19 01:05:43 +02:00
|
|
|
*/
|
2015-07-19 12:21:09 +02:00
|
|
|
public function __construct(Daux $daux)
|
|
|
|
{
|
2019-12-05 21:25:58 +01:00
|
|
|
$config = $daux->getConfig();
|
2017-12-11 21:14:18 +01:00
|
|
|
|
2015-07-19 12:21:09 +02:00
|
|
|
$this->daux = $daux;
|
2019-12-05 21:25:58 +01:00
|
|
|
$this->templateRenderer = new Template($config);
|
|
|
|
$config->templateRenderer = $this->templateRenderer;
|
2015-07-28 17:25:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getContentTypes()
|
|
|
|
{
|
|
|
|
return [
|
2019-12-05 21:25:58 +01:00
|
|
|
'markdown' => new ContentType($this->daux->getConfig()),
|
2015-07-28 17:25:03 +02:00
|
|
|
];
|
2015-07-19 12:21:09 +02:00
|
|
|
}
|
2015-07-19 01:05:43 +02:00
|
|
|
|
2015-07-19 12:21:09 +02:00
|
|
|
public function generateAll(InputInterface $input, OutputInterface $output, $width)
|
2015-05-21 17:39:11 +02:00
|
|
|
{
|
2015-07-19 00:55:57 +02:00
|
|
|
$destination = $input->getOption('destination');
|
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
$config = $this->daux->getConfig();
|
2015-05-21 17:39:11 +02:00
|
|
|
if (is_null($destination)) {
|
2019-12-05 21:25:58 +01:00
|
|
|
$destination = $config->getLocalBase() . DIRECTORY_SEPARATOR . 'static';
|
2015-05-21 17:39:11 +02:00
|
|
|
}
|
|
|
|
|
2015-07-14 22:06:01 +02:00
|
|
|
$this->runAction(
|
2016-07-27 21:32:51 +02:00
|
|
|
'Copying Static assets ...',
|
2015-07-14 22:06:01 +02:00
|
|
|
$width,
|
2019-12-05 21:25:58 +01:00
|
|
|
function() use ($destination, $config) {
|
2016-09-06 23:11:32 +02:00
|
|
|
$this->ensureEmptyDestination($destination);
|
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
$this->copyThemes($destination, $config->getThemesPath());
|
2015-07-14 22:06:01 +02:00
|
|
|
}
|
|
|
|
);
|
2015-05-21 17:39:11 +02:00
|
|
|
|
2016-07-27 21:32:51 +02:00
|
|
|
$output->writeLn('Generating ...');
|
2016-02-15 21:14:48 +01:00
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
$this->generateRecursive($this->daux->tree, $destination, $config, $output, $width, $config->getHTML()->hasSearch());
|
2016-03-13 21:51:58 +01:00
|
|
|
|
2019-09-20 23:10:01 +02:00
|
|
|
GeneratorHelper::copyRecursive(
|
2019-12-05 21:25:58 +01:00
|
|
|
$config->getLocalBase() . DIRECTORY_SEPARATOR . 'daux_libraries' . DIRECTORY_SEPARATOR,
|
2019-09-23 22:10:07 +02:00
|
|
|
$destination . DIRECTORY_SEPARATOR . 'daux_libraries'
|
2019-09-20 23:10:01 +02:00
|
|
|
);
|
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
if ($config->getHTML()->hasSearch()) {
|
2016-03-13 21:51:58 +01:00
|
|
|
file_put_contents(
|
2019-09-23 22:10:07 +02:00
|
|
|
$destination . DIRECTORY_SEPARATOR . 'daux_search_index.json',
|
2016-03-13 21:51:58 +01:00
|
|
|
json_encode(['pages' => $this->indexed_pages])
|
|
|
|
);
|
2017-11-08 00:01:08 +01:00
|
|
|
|
|
|
|
if (json_last_error()) {
|
|
|
|
echo "Could not write search index: \n" . json_last_error_msg() . "\n";
|
|
|
|
}
|
2016-02-15 21:14:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove HTML tags, including invisible text such as style and
|
|
|
|
* script code, and embedded objects. Add line breaks around
|
|
|
|
* block-level tags to prevent word joining after tag removal.
|
|
|
|
* Also collapse whitespace to single space and trim result.
|
|
|
|
* modified from: http://nadeausoftware.com/articles/2007/09/php_tip_how_strip_html_tags_web_page
|
2016-07-29 23:46:57 +02:00
|
|
|
*
|
|
|
|
* @param string $text
|
|
|
|
* @return string
|
2016-02-15 21:14:48 +01:00
|
|
|
*/
|
2017-11-08 00:01:08 +01:00
|
|
|
private function sanitize($text)
|
2016-02-15 21:14:48 +01:00
|
|
|
{
|
|
|
|
$text = preg_replace(
|
2016-07-27 21:32:51 +02:00
|
|
|
[
|
2016-02-15 21:14:48 +01:00
|
|
|
// Remove invisible content
|
|
|
|
'@<head[^>]*?>.*?</head>@siu',
|
|
|
|
'@<style[^>]*?>.*?</style>@siu',
|
|
|
|
'@<script[^>]*?.*?</script>@siu',
|
|
|
|
'@<object[^>]*?.*?</object>@siu',
|
|
|
|
'@<embed[^>]*?.*?</embed>@siu',
|
|
|
|
'@<applet[^>]*?.*?</applet>@siu',
|
|
|
|
'@<noframes[^>]*?.*?</noframes>@siu',
|
|
|
|
'@<noscript[^>]*?.*?</noscript>@siu',
|
|
|
|
'@<noembed[^>]*?.*?</noembed>@siu',
|
|
|
|
// Add line breaks before and after blocks
|
|
|
|
'@</?((address)|(blockquote)|(center)|(del))@iu',
|
|
|
|
'@</?((div)|(h[1-9])|(ins)|(isindex)|(p)|(pre))@iu',
|
|
|
|
'@</?((dir)|(dl)|(dt)|(dd)|(li)|(menu)|(ol)|(ul))@iu',
|
|
|
|
'@</?((table)|(th)|(td)|(caption))@iu',
|
|
|
|
'@</?((form)|(button)|(fieldset)|(legend)|(input))@iu',
|
|
|
|
'@</?((label)|(select)|(optgroup)|(option)|(textarea))@iu',
|
|
|
|
'@</?((frameset)|(frame)|(iframe))@iu',
|
2016-07-27 21:32:51 +02:00
|
|
|
],
|
|
|
|
[
|
2016-02-15 21:14:48 +01:00
|
|
|
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
|
|
|
|
"\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0",
|
|
|
|
"\n\$0", "\n\$0",
|
2016-07-27 21:32:51 +02:00
|
|
|
],
|
2016-03-13 21:51:58 +01:00
|
|
|
$text
|
|
|
|
);
|
2016-07-27 21:32:51 +02:00
|
|
|
|
2017-11-08 00:01:08 +01:00
|
|
|
$text = trim(preg_replace('/\s+/', ' ', strip_tags($text)));
|
|
|
|
|
|
|
|
// Sometimes strings are detected as invalid UTF-8 and json_encode can't treat them
|
|
|
|
// iconv can fix those strings
|
2017-11-08 21:51:01 +01:00
|
|
|
$text = iconv('UTF-8', 'UTF-8//IGNORE', $text);
|
2017-11-08 00:01:08 +01:00
|
|
|
|
|
|
|
return $text;
|
2015-05-21 17:39:11 +02:00
|
|
|
}
|
|
|
|
|
2015-07-17 23:38:06 +02:00
|
|
|
/**
|
|
|
|
* Recursively generate the documentation
|
|
|
|
*
|
2015-07-18 01:10:40 +02:00
|
|
|
* @param Directory $tree
|
2015-07-17 23:38:06 +02:00
|
|
|
* @param string $output_dir
|
2019-12-07 11:22:40 +01:00
|
|
|
* @param GlobalConfig $config
|
2015-07-17 23:38:06 +02:00
|
|
|
* @param OutputInterface $output
|
2016-07-27 21:32:51 +02:00
|
|
|
* @param int $width
|
|
|
|
* @param bool $index_pages
|
2015-07-17 23:38:06 +02:00
|
|
|
* @param string $base_url
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2019-12-07 11:22:40 +01:00
|
|
|
private function generateRecursive(Directory $tree, $output_dir, GlobalConfig $config, $output, $width, $index_pages, $base_url = '')
|
2015-05-21 17:39:11 +02:00
|
|
|
{
|
2019-12-05 21:25:58 +01:00
|
|
|
DauxHelper::rebaseConfiguration($config, $base_url);
|
2015-05-21 17:39:11 +02:00
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
if ($base_url !== '' && !$config->hasEntryPage()) {
|
|
|
|
$config->setEntryPage($tree->getFirstPage());
|
2015-05-21 17:39:11 +02:00
|
|
|
}
|
2015-07-18 23:51:23 +02:00
|
|
|
|
2015-07-18 14:37:18 +02:00
|
|
|
foreach ($tree->getEntries() as $key => $node) {
|
2015-05-21 17:39:11 +02:00
|
|
|
if ($node instanceof Directory) {
|
2015-07-20 15:59:52 +02:00
|
|
|
$new_output_dir = $output_dir . DIRECTORY_SEPARATOR . $key;
|
2015-07-17 23:38:06 +02:00
|
|
|
mkdir($new_output_dir);
|
2019-12-05 21:25:58 +01:00
|
|
|
$this->generateRecursive($node, $new_output_dir, $config, $output, $width, $index_pages, '../' . $base_url);
|
2015-07-18 23:51:23 +02:00
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
// Rebase configuration again as $config is a shared object
|
|
|
|
DauxHelper::rebaseConfiguration($config, $base_url);
|
2015-05-21 17:39:11 +02:00
|
|
|
} else {
|
2015-07-14 22:06:01 +02:00
|
|
|
$this->runAction(
|
2016-07-27 21:32:51 +02:00
|
|
|
'- ' . $node->getUrl(),
|
2015-07-14 22:06:01 +02:00
|
|
|
$width,
|
2019-12-05 21:25:58 +01:00
|
|
|
function() use ($node, $output_dir, $key, $config, $index_pages) {
|
2016-01-31 12:00:29 +01:00
|
|
|
if ($node instanceof Raw) {
|
2015-07-20 15:59:52 +02:00
|
|
|
copy($node->getPath(), $output_dir . DIRECTORY_SEPARATOR . $key);
|
2016-07-27 21:32:51 +02:00
|
|
|
|
2015-07-19 12:21:09 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-01-09 18:29:52 +01:00
|
|
|
$this->daux->tree->setActiveNode($node);
|
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
$generated = $this->generateOne($node, $config);
|
2015-07-20 15:59:52 +02:00
|
|
|
file_put_contents($output_dir . DIRECTORY_SEPARATOR . $key, $generated->getContent());
|
2016-03-13 21:51:58 +01:00
|
|
|
if ($index_pages) {
|
2016-07-27 21:32:51 +02:00
|
|
|
$this->indexed_pages[] = [
|
2016-02-15 21:14:48 +01:00
|
|
|
'title' => $node->getTitle(),
|
2017-11-08 00:01:08 +01:00
|
|
|
'text' => $this->sanitize($generated->getPureContent()),
|
2016-07-27 21:32:51 +02:00
|
|
|
'tags' => '',
|
|
|
|
'url' => $node->getUrl(),
|
2016-03-13 21:51:58 +01:00
|
|
|
];
|
2016-02-15 21:14:48 +01:00
|
|
|
}
|
2015-07-14 22:06:01 +02:00
|
|
|
}
|
|
|
|
);
|
2015-05-21 17:39:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-07-19 12:21:09 +02:00
|
|
|
|
2015-07-19 22:38:37 +02:00
|
|
|
/**
|
|
|
|
* @param Entry $node
|
2019-12-07 11:22:40 +01:00
|
|
|
* @param GlobalConfig $config
|
2015-07-19 22:38:37 +02:00
|
|
|
* @return \Todaymade\Daux\Format\Base\Page
|
|
|
|
*/
|
2019-12-07 11:22:40 +01:00
|
|
|
public function generateOne(Entry $node, GlobalConfig $config)
|
2015-07-19 12:21:09 +02:00
|
|
|
{
|
|
|
|
if ($node instanceof Raw) {
|
|
|
|
return new RawPage($node->getPath());
|
|
|
|
}
|
|
|
|
|
2016-01-31 12:00:29 +01:00
|
|
|
if ($node instanceof ComputedRaw) {
|
|
|
|
return new ComputedRawPage($node);
|
|
|
|
}
|
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
$config->setRequest($node->getUrl());
|
2016-07-27 21:32:51 +02:00
|
|
|
|
2019-12-05 21:25:58 +01:00
|
|
|
$contentPage = ContentPage::fromFile($node, $config, $this->daux->getContentTypeHandler()->getType($node));
|
2017-12-11 21:14:18 +01:00
|
|
|
$contentPage->templateRenderer = $this->templateRenderer;
|
|
|
|
|
|
|
|
return $contentPage;
|
2015-07-19 12:21:09 +02:00
|
|
|
}
|
2015-05-21 17:39:11 +02:00
|
|
|
}
|