Beispiel #1
0
 /**
  * Recursively generate the documentation
  *
  * @param Directory $tree
  * @param string $output_dir
  * @param \Todaymade\Daux\Config $params
  * @param OutputInterface $output
  * @param integer $width
  * @param string $base_url
  * @throws \Exception
  */
 private function generateRecursive(Directory $tree, $output_dir, $params, $output, $width, $base_url = '')
 {
     DauxHelper::rebaseConfiguration($params, $base_url);
     if ($base_url !== '' && empty($params['entry_page'])) {
         $params['entry_page'] = $tree->getFirstPage();
     }
     foreach ($tree->getEntries() as $key => $node) {
         if ($node instanceof Directory) {
             $new_output_dir = $output_dir . DIRECTORY_SEPARATOR . $key;
             mkdir($new_output_dir);
             $this->generateRecursive($node, $new_output_dir, $params, $output, $width, '../' . $base_url);
             // Rebase configuration again as $params is a shared object
             DauxHelper::rebaseConfiguration($params, $base_url);
         } else {
             $this->runAction("- " . $node->getUrl(), $output, $width, function () use($node, $output_dir, $key, $params) {
                 if (!$node instanceof Content) {
                     copy($node->getPath(), $output_dir . DIRECTORY_SEPARATOR . $key);
                     return;
                 }
                 $generated = $this->generateOne($node, $params);
                 file_put_contents($output_dir . DIRECTORY_SEPARATOR . $key, $generated->getContent());
             });
         }
     }
 }
Beispiel #2
0
 /**
  * @dataProvider providerRenderLink
  */
 public function testRenderLink($expected, $string, $current)
 {
     $config = new Config();
     $config['base_url'] = '';
     $config['tree'] = $this->getTree($config);
     $config->setCurrentPage(DauxHelper::getFile($config['tree'], $current));
     $converter = new CommonMarkConverter(['daux' => $config]);
     $this->assertEquals("<p>{$expected}</p>", trim($converter->convertToHtml($string)));
 }
Beispiel #3
0
 /**
  * @param string $url
  * @return Entry
  * @throws Exception
  */
 protected function resolveInternalFile($url)
 {
     $file = DauxHelper::getFile($this->daux['tree'], $url);
     if ($file) {
         return $file;
     }
     $file = DauxHelper::getFile($this->daux['tree'], $url . '.html');
     if ($file) {
         return $file;
     }
     throw new Exception("Could not locate file '{$url}'");
 }
Beispiel #4
0
 private function findImage($src, $tag, Content $file, $callback)
 {
     //for protocol relative or http requests : keep the original one
     if (substr($src, 0, strlen('http')) === 'http' || substr($src, 0, strlen('//')) === '//') {
         return $src;
     }
     //Get the path to the file, relative to the root of the documentation
     $url = DauxHelper::getCleanPath(dirname($file->getUrl()) . '/' . $src);
     //Get any file corresponding to the right one
     $file = DauxHelper::getFile($this->tree, $url);
     if ($file === false) {
         return false;
     }
     $result = $callback($src, $this->getAttributes($tag), $file);
     return $result ?: $src;
 }
Beispiel #5
0
 /**
  * @param AbstractInline|Link $inline
  * @param ElementRendererInterface $htmlRenderer
  * @return HtmlElement
  * @throws Exception
  */
 public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
 {
     // This can't be in the method type as
     // the method is an abstract and should
     // have the same interface
     if (!$inline instanceof Link) {
         throw new \RuntimeException('Wrong type passed to ' . __CLASS__ . '::' . __METHOD__ . " the expected type was 'League\\CommonMark\\Inline\\Element\\Link' but '" . get_class($inline) . "' was provided");
     }
     $element = parent::render($inline, $htmlRenderer);
     $url = $inline->getUrl();
     // Absolute urls, empty urls and anchors
     // should not go through the url resolver
     if (empty($url) || $url[0] == '#' || preg_match('|^(?:[a-z]+:)?//|', $url)) {
         return $element;
     }
     $file = $this->resolveInternalFile($url);
     $url = DauxHelper::getRelativePath($this->daux->getCurrentPage()->getUrl(), $file->getUrl());
     $element->setAttribute('href', $url);
     return $element;
 }
Beispiel #6
0
 /**
  * @param AbstractInline|Link $inline
  * @param ElementRendererInterface $htmlRenderer
  * @return HtmlElement
  * @throws LinkNotFoundException
  */
 public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
 {
     // This can't be in the method type as
     // the method is an abstract and should
     // have the same interface
     if (!$inline instanceof Link) {
         throw new \RuntimeException('Wrong type passed to ' . __CLASS__ . '::' . __METHOD__ . " the expected type was 'League\\CommonMark\\Inline\\Element\\Link' but '" . get_class($inline) . "' was provided");
     }
     $element = parent::render($inline, $htmlRenderer);
     $url = $inline->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', 'external');
         return $element;
     }
     // if there's a hash component in the url, ensure we
     // don't put that part through the resolver.
     $urlAndHash = explode('#', $url);
     $url = $urlAndHash[0];
     try {
         $file = $this->resolveInternalFile($url);
         $url = DauxHelper::getRelativePath($this->daux->getCurrentPage()->getUrl(), $file->getUrl());
     } catch (LinkNotFoundException $e) {
         if ($this->daux->isStatic()) {
             throw $e;
         }
         $element->setAttribute('class', 'broken');
     }
     if (isset($urlAndHash[1])) {
         $url .= '#' . $urlAndHash[1];
     }
     $element->setAttribute('href', $url);
     return $element;
 }
Beispiel #7
0
 /**
  * Recursively generate the documentation
  *
  * @param Directory $tree
  * @param string $output_dir
  * @param \Todaymade\Daux\Config $params
  * @param OutputInterface $output
  * @param int $width
  * @param bool $index_pages
  * @param string $base_url
  * @throws \Exception
  */
 private function generateRecursive(Directory $tree, $output_dir, $params, $output, $width, $index_pages, $base_url = '')
 {
     DauxHelper::rebaseConfiguration($params, $base_url);
     if ($base_url !== '' && empty($params['entry_page'])) {
         $params['entry_page'] = $tree->getFirstPage();
     }
     foreach ($tree->getEntries() as $key => $node) {
         if ($node instanceof Directory) {
             $new_output_dir = $output_dir . DIRECTORY_SEPARATOR . $key;
             mkdir($new_output_dir);
             $this->generateRecursive($node, $new_output_dir, $params, $output, $width, $index_pages, '../' . $base_url);
             // Rebase configuration again as $params is a shared object
             DauxHelper::rebaseConfiguration($params, $base_url);
         } else {
             $this->runAction('- ' . $node->getUrl(), $output, $width, function () use($node, $output_dir, $key, $params, $index_pages) {
                 if ($node instanceof Raw) {
                     copy($node->getPath(), $output_dir . DIRECTORY_SEPARATOR . $key);
                     return;
                 }
                 $generated = $this->generateOne($node, $params);
                 file_put_contents($output_dir . DIRECTORY_SEPARATOR . $key, $generated->getContent());
                 if ($index_pages) {
                     $this->indexed_pages[] = ['title' => $node->getTitle(), 'text' => utf8_encode($this->strip_html_tags($generated->getPureContent())), 'tags' => '', 'url' => $node->getUrl()];
                 }
             });
         }
     }
 }
Beispiel #8
0
 /**
  * @param string $request
  * @return \Todaymade\Daux\Format\Base\Page
  * @throws NotFoundException
  */
 private function getPage($request)
 {
     $file = DauxHelper::getFile($this->daux->tree, $request);
     if ($file === false) {
         throw new NotFoundException('The Page you requested is yet to be made. Try again later.');
     }
     $generator = $this->daux->getGenerator();
     if (!$generator instanceof LiveGenerator) {
         throw new \RuntimeException("The generator '" . get_class($generator) . "' does not implement the interface " . "'Todaymade\\Daux\\Format\\Base\\LiveGenerator' and thus doesn't support live rendering.");
     }
     return $this->daux->getGenerator()->generateOne($file, $this->params);
 }
Beispiel #9
0
 /**
  * @param Directory $parent
  * @param string $path
  * @return ContentAbstract
  */
 public static function getOrCreatePage(Directory $parent, $path)
 {
     $extension = pathinfo($path, PATHINFO_EXTENSION);
     // If the file doesn't have an extension, set .md as a default
     if ($extension == '') {
         $extension = 'md';
         $path .= '.md';
     }
     $raw = !in_array($extension, $parent->getConfig()['valid_content_extensions']);
     $title = $uri = $path;
     if (!$raw) {
         $title = static::getName($path);
         $uri = DauxHelper::slug($title);
         if ($parent->getConfig()->isStatic()) {
             $uri .= '.html';
         }
     }
     if (array_key_exists($uri, $parent->getEntries())) {
         return $parent->getEntries()[$uri];
     }
     $page = $raw ? new ComputedRaw($parent, $uri) : new Content($parent, $uri);
     $page->setContent('-');
     //set an almost empty content to avoid problems
     $page->setName($path);
     $page->setTitle($title);
     if ($title == 'index' || $title == '_index') {
         $page->setTitle($parent->getTitle());
     }
     return $page;
 }
Beispiel #10
0
 protected function addId(Heading $node)
 {
     // If the node has an ID, no need to generate it
     $attributes = $node->getData('attributes', []);
     if (array_key_exists('id', $attributes) && !empty($attributes['id'])) {
         // TODO :: check for uniqueness
         return $attributes['id'];
     }
     // Well, seems we have to generate an ID
     $walker = $node->walker();
     $inside = [];
     while ($event = $walker->next()) {
         $insideNode = $event->getNode();
         if ($insideNode instanceof Heading) {
             continue;
         }
         $inside[] = $insideNode;
     }
     $text = '';
     foreach ($inside as $other) {
         if ($other instanceof Text) {
             $text .= ' ' . $other->getContent();
         }
     }
     $text = 'page_' . DauxHelper::slug(trim($text));
     // TODO :: check for uniqueness
     $node->data['attributes']['id'] = $text;
 }
Beispiel #11
0
 /**
  * @param Directory $parent
  * @param string $path
  * @return Content
  */
 public static function getOrCreatePage(Directory $parent, $path)
 {
     $title = static::getName($path);
     // If the file doesn't have an extension, set .md as a default
     if (pathinfo($path, PATHINFO_EXTENSION) == '') {
         $path .= '.md';
     }
     $uri = $slug = DauxHelper::slug($title);
     if ($parent->getConfig()['mode'] === Daux::STATIC_MODE) {
         $uri = $slug . ".html";
     }
     if (array_key_exists($uri, $parent->getEntries())) {
         return $parent->getEntries()[$uri];
     }
     $page = new Content($parent, $uri);
     $page->setContent("-");
     //set an almost empty content to avoid problems
     if ($title == 'index') {
         // TODO :: clarify the difference between 'index' and '_index'
         $page->setName('_index' . pathinfo($path, PATHINFO_EXTENSION));
         $page->setTitle($parent->getTitle());
     } else {
         $page->setName($path);
         $page->setTitle($title);
     }
     return $page;
 }
Beispiel #12
0
 public function normalizeDocumentationPath($path)
 {
     // When running through `daux --serve` we set an environment variable to know where we started from
     $env = getenv('DAUX_SOURCE');
     if ($env && is_dir($env)) {
         return $env;
     }
     if (is_dir($path)) {
         if (DauxHelper::isAbsolutePath($path)) {
             return $path;
         }
         return getcwd() . '/' . $path;
     }
     throw new Exception('The Docs directory does not exist. Check the path again : ' . $path);
 }