diff --git a/libs/Format/HTML/Generator.php b/libs/Format/HTML/Generator.php index 64fc3d0..5c94903 100755 --- a/libs/Format/HTML/Generator.php +++ b/libs/Format/HTML/Generator.php @@ -16,7 +16,7 @@ use Todaymade\Daux\Tree\Raw; class Generator implements \Todaymade\Daux\Format\Base\Generator, LiveGenerator { - use RunAction; + use RunAction, HTMLUtils; /** @var Daux */ protected $daux; @@ -45,30 +45,6 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator, LiveGenerator ]; } - protected function ensureEmptyDestination($destination) - { - if (is_dir($destination)) { - GeneratorHelper::rmdir($destination); - } else { - mkdir($destination); - } - } - - /** - * Copy all files from $local to $destination - * - * @param string $destination - * @param string $local_base - */ - protected function copyThemes($destination, $local_base) - { - mkdir($destination . DIRECTORY_SEPARATOR . 'themes'); - GeneratorHelper::copyRecursive( - $local_base, - $destination . DIRECTORY_SEPARATOR . 'themes' - ); - } - public function generateAll(InputInterface $input, OutputInterface $output, $width) { $destination = $input->getOption('destination'); diff --git a/libs/Format/HTML/HTMLUtils.php b/libs/Format/HTML/HTMLUtils.php new file mode 100644 index 0000000..0c960be --- /dev/null +++ b/libs/Format/HTML/HTMLUtils.php @@ -0,0 +1,29 @@ +' . file_get_contents('themes/daux_singlepage/css/main.min.css') . ''; } - protected function getSectionId(Content $node) + protected function getPageUrl($page) { - foreach ($this->pages as $id => $page) { - if ($page['page'] == $node) { - return $id; - } - } - - throw new RuntimeException('Could not find the content page'); + return "file_" . str_replace('/', '_', $page->getUrl()); } protected function buildNavigation(Directory $tree) @@ -44,7 +38,7 @@ class Book $nav[] = [ 'title' => $node->getTitle(), - 'href' => '#section_' . $this->getSectionId($node), + 'href' => "#" . $this->getPageUrl($node), ]; } elseif ($node instanceof Directory) { if (!$node->hasContent()) { @@ -55,7 +49,7 @@ class Book $nav[] = [ 'title' => $node->getTitle(), - 'href' => '#section_' . $this->getSectionId($page_index), + 'href' => "#" . $this->getPageUrl($page_index), 'children' => $this->buildNavigation($node), ]; } @@ -104,8 +98,8 @@ class Book protected function generatePages() { $content = ''; - foreach ($this->pages as $section => $page) { - $content .= ''; + foreach ($this->pages as $page) { + $content .= ''; $content .= '

' . $page['page']->getTitle() . '

'; $content .= '
' . $page['content'] . '
'; $content .= '
 
'; diff --git a/libs/Format/HTMLFile/ContentTypes/Markdown/CommonMarkConverter.php b/libs/Format/HTMLFile/ContentTypes/Markdown/CommonMarkConverter.php new file mode 100644 index 0000000..cf97727 --- /dev/null +++ b/libs/Format/HTMLFile/ContentTypes/Markdown/CommonMarkConverter.php @@ -0,0 +1,13 @@ +getConfig('daux')); + } +} diff --git a/libs/Format/HTMLFile/ContentTypes/Markdown/ContentType.php b/libs/Format/HTMLFile/ContentTypes/Markdown/ContentType.php new file mode 100644 index 0000000..00e2157 --- /dev/null +++ b/libs/Format/HTMLFile/ContentTypes/Markdown/ContentType.php @@ -0,0 +1,12 @@ +config = $config; + $this->converter = new CommonMarkConverter(['daux' => $config]); + } +} diff --git a/libs/Format/HTMLFile/ContentTypes/Markdown/LinkRenderer.php b/libs/Format/HTMLFile/ContentTypes/Markdown/LinkRenderer.php new file mode 100644 index 0000000..f4ccc67 --- /dev/null +++ b/libs/Format/HTMLFile/ContentTypes/Markdown/LinkRenderer.php @@ -0,0 +1,74 @@ +getUrl(); + + // empty urls and anchors should + // not go through the url resolver + if (!$this->isValidUrl($url)) { + return $element; + } + + // Absolute urls, shouldn't either + if ($this->isExternalUrl($url)) { + $element->setAttribute('class', 'Link--external'); + + return $element; + } + + // if there's a hash component in the url, we can directly use it as all pages are in the same file + $urlAndHash = explode('#', $url); + if (isset($urlAndHash[1])) { + $element->setAttribute('href', '#' . $urlAndHash[1]); + + return $element; + } + + try { + $file = $this->resolveInternalFile($url); + $url = $file->getUrl(); + } catch (LinkNotFoundException $e) { + if ($this->daux->isStatic()) { + throw $e; + } + + $element->setAttribute('class', 'Link--broken'); + } + + $url = str_replace('/', '_', $url); + $element->setAttribute('href', "#file_$url"); + + return $element; + } +} \ No newline at end of file diff --git a/libs/Format/HTMLFile/Generator.php b/libs/Format/HTMLFile/Generator.php index 29d3bff..4d4f27b 100644 --- a/libs/Format/HTMLFile/Generator.php +++ b/libs/Format/HTMLFile/Generator.php @@ -5,11 +5,12 @@ use Symfony\Component\Console\Output\OutputInterface; use Todaymade\Daux\Console\RunAction; use Todaymade\Daux\Daux; use Todaymade\Daux\Format\HTML\Template; -use Todaymade\Daux\Format\HTML\ContentTypes\Markdown\ContentType; +use Todaymade\Daux\Format\HTML\HTMLUtils; +use Todaymade\Daux\Format\HTMLFile\ContentTypes\Markdown\ContentType; class Generator implements \Todaymade\Daux\Format\Base\Generator { - use RunAction; + use RunAction, HTMLUtils; /** @var Daux */ protected $daux; @@ -36,52 +37,31 @@ class Generator implements \Todaymade\Daux\Format\Base\Generator ]; } - protected function initPDF() - { - // create new PDF document - $pdf = new Book(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); - - $params = $this->daux->getParams(); - - // set document information - $pdf->SetCreator(PDF_CREATOR); - - - // set default header data - $pdf->SetHeaderData('', 0, $params['title'], $params['tagline']); - - // set header and footer fonts - $pdf->setHeaderFont([PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN]); - $pdf->setFooterFont([PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA]); - - // set default monospaced font - $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED); - - // set margins - $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); - $pdf->SetHeaderMargin(PDF_MARGIN_HEADER); - $pdf->SetFooterMargin(PDF_MARGIN_FOOTER); - - // set auto page breaks - $pdf->SetAutoPageBreak(true, PDF_MARGIN_BOTTOM); - - // set image scale factor - $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO); - - // set font - $pdf->SetFont('helvetica', '', 10); - - return $pdf; - } - /** * {@inheritdoc} */ public function generateAll(InputInterface $input, OutputInterface $output, $width) { - $params = $this->daux->getParams(); + $destination = $input->getOption('destination'); - $data = ['author' => $params['author'], 'title' => $params['title'], 'subject' => $params['tagline']]; + $params = $this->daux->getParams(); + if (is_null($destination)) { + $destination = $this->daux->local_base . DIRECTORY_SEPARATOR . 'static'; + } + + $this->runAction( + 'Cleaning destination folder ...', + $width, + function() use ($destination, $params) { + $this->ensureEmptyDestination($destination); + } + ); + + $data = [ + 'author' => $params['author'], + 'title' => $params['title'], + 'subject' => $params['tagline'] + ]; $book = new Book($this->daux->tree, $data);