/** * Generate the API documentation using the markdown and include files * * @param $folder * @return false|null */ public function generate($folder) { $source_dir = $folder . '/source'; if (!is_dir($source_dir)) { return false; } $parser = new Parser(); $document = $parser->parse(file_get_contents($source_dir . '/index.md')); $frontmatter = $document->getYAML(); $html = $document->getContent(); $renderer = new BladeRenderer([__DIR__ . '/../resources/views'], ['cache_path' => $source_dir . '/_tmp']); // Parse and include optional include markdown files if (isset($frontmatter['includes'])) { foreach ($frontmatter['includes'] as $include) { if (file_exists($include_file = $source_dir . '/includes/_' . $include . '.md')) { $document = $parser->parse(file_get_contents($include_file)); $html .= $document->getContent(); } } } $output = $renderer->render('index', ['page' => $frontmatter, 'content' => $html]); file_put_contents($folder . '/index.html', $output); // Copy assets rcopy($source_dir . '/assets/images/', $folder . '/images'); rcopy($source_dir . '/assets/stylus/fonts/', $folder . '/css/fonts'); }
public function __invoke(Project $project) { /** @var MarkdownFile[] $markdownFiles */ $markdownFiles = $project->findFilesByType('Couscous\\Module\\Markdown\\Model\\MarkdownFile'); foreach ($markdownFiles as $file) { $document = $this->markdownParser->parse($file->getContent()); $metadataValues = $document->getYAML(); if (is_array($metadataValues)) { $file->getMetadata()->setMany($metadataValues); } } }
public function parse($markdown) { $parsed = $this->parser->parse($markdown, false); // Get the FrontYaml content $metadata = $parsed->getYAML(); if (!is_array($metadata)) { $metadata = []; } // Get the Markdown with the FrontYaml removed $markdown = $parsed->getContent(); $document = $this->wrapped->parse($markdown); $document->metadata = array_merge($document->metadata, $metadata); return $document; }
public function __invoke(Route $route, Console $console) : int { $basePath = $route->getMatchedParam('path'); $path = realpath($basePath) . '/data/blog'; $cache = realpath($basePath) . '/data/cache/posts'; $baseUri = new Uri('https://mwop.net'); $middleware = $this->blogMiddleware; $console->writeLine('Generating static cache for blog posts', Color::GREEN); // Prepare final handler for middleware $failed = false; $done = function ($req, $res, $err = null) use(&$failed) { $failed = $err ? true : false; }; $parser = new Parser(null, new CommonMarkParser()); foreach (new MarkdownFileFilter($path) as $fileInfo) { $document = $parser->parse(file_get_contents($fileInfo->getPathname())); $metadata = $document->getYAML(); $message = ' ' . $metadata['id']; $length = strlen($message); $width = $console->getWidth(); $console->write($message, Color::BLUE); $canonical = $baseUri->withPath(sprintf('/blog/%s.html', $metadata['id'])); $request = (new Request(new PsrRequest([], [], $canonical, 'GET')))->withUri($canonical)->withAttribute('id', $metadata['id']); $failed = false; $response = $middleware($request, new Response(), $done); if (!$failed) { $this->cacheResponse($metadata['id'], $cache, $response->getBody()); } $this->reportComplete($console, $width, $length, !$failed); } $console->writeLine('ALL DONE', Color::GREEN); return 0; }
public function __invoke(Route $route, Console $console) : int { $basePath = $route->getMatchedParam('path'); $postsPath = $route->getMatchedParam('postsPath'); $authorsPath = $route->getMatchedParam('authorsPath'); $dbPath = $route->getMatchedParam('dbPath'); $message = 'Generating blog post database'; $length = strlen($message); $width = $console->getWidth(); $console->write($message, Color::BLUE); $pdo = $this->createDatabase($dbPath, $console); $path = sprintf('%s/%s', realpath($basePath), ltrim($postsPath)); $trim = strlen(realpath($basePath)) + 1; $parser = new Parser(null, new CommonMarkParser()); $statements = []; foreach (new MarkdownFileFilter($path) as $fileInfo) { $path = $fileInfo->getPathname(); $document = $parser->parse(file_get_contents($path)); $metadata = $document->getYAML(); $html = $document->getContent(); $parts = explode($this->postDelimiter, $html, 2); $body = $parts[0]; $extended = isset($parts[1]) ? $parts[1] : ''; $author = $this->getAuthor($metadata['author'], $authorsPath); $template = empty($statements) ? $this->initial : $this->item; $statements[] = sprintf($template, $pdo->quote($metadata['id']), $pdo->quote(substr($path, $trim)), (new DateTime($metadata['created']))->getTimestamp(), (new DateTime($metadata['updated']))->getTimestamp(), $pdo->quote($metadata['title']), $pdo->quote($author['id']), $metadata['draft'] ? 1 : 0, $metadata['public'] ? 1 : 0, $pdo->quote($body), $pdo->quote(sprintf('|%s|', implode('|', $metadata['tags'])))); } $pdo->exec(implode("\n", $statements)); return $this->reportSuccess($console, $width, $length); }
public static function routeMarkdown() { if ($filename = static::findFileForRoute(Route::requestUri(), Conf::get('glueExtras/RouteTools/content/path'), array('md'))) { $parser = new Parser(); if (!($document = $parser->parse(file_get_contents($filename)))) { echo "<div><strong>Error:</strong> Failed to parse markdown/front-YAML</div>"; return; } Route::any(Route::requestUri(), function () use($document) { Template::setMulti($document->getYAML()); echo $document->getContent(); }); } }
public function __invoke($req, $res, $next) { $post = $this->mapper->fetch($req->getAttribute('id', false)); if (!$post) { return $next($req, $res->withStatus(404), 'Not found'); } $parser = new Parser(null, new CommonMarkParser()); $document = $parser->parse(file_get_contents($post['path'])); $post = $document->getYAML(); $parts = explode($this->postDelimiter, $document->getContent(), 2); $post = array_merge($post, ['body' => $parts[0], 'extended' => isset($parts[1]) ? $parts[1] : '']); $original = $req->getOriginalRequest()->getUri()->getPath(); $path = substr($original, 0, -(strlen($post['id'] . '.html') + 1)); $post = new EntryView($post, $this->disqus); return new HtmlResponse($this->template->render('blog::post', $post)); }
public function __invoke(Route $route, Console $console) : int { $basePath = $route->getMatchedParam('path'); $path = realpath($basePath) . '/data/blog'; $console->writeLine('Generating search metadata', Color::BLUE); $documents = []; $parser = new Parser(); foreach (new MarkdownFileFilter($path) as $fileInfo) { $document = $parser->parse(file_get_contents($fileInfo->getPathname()), false); $metadata = $document->getYAML(); $content = $document->getContent(); $documents[] = ['id' => sprintf('/blog/%s.html', $metadata['id']), 'tags' => implode(' ', $metadata['tags']), 'title' => $metadata['title'], 'content' => $content]; } file_put_contents(realpath($basePath) . '/public/js/search_terms.json', json_encode(['docs' => $documents], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)); $console->write('[DONE]', Color::GREEN); $console->writeLine(' Generating search metadata'); return 0; }
private function generateFeed(string $type, string $fileBase, string $baseUri, string $title, string $landingRoute, string $feedRoute, array $routeOptions, Traversable $posts) { $routeOptions['type'] = $type; $landingUri = $baseUri . $this->generateUri($landingRoute, $routeOptions); $feedUri = $baseUri . $this->generateUri($feedRoute, $routeOptions); $feed = new FeedWriter(); $feed->setTitle($title); $feed->setLink($landingUri); $feed->setFeedLink($feedUri, $type); if ($type === 'rss') { $feed->setDescription($title); } $parser = new Parser(null, new CommonMarkParser()); $latest = false; $posts->setCurrentPageNumber(1); foreach ($posts as $details) { $document = $parser->parse(file_get_contents($details['path'])); $post = $document->getYAML(); $html = $document->getContent(); $author = $this->getAuthor($post['author']); if (!$latest) { $latest = $post; } $entry = $feed->createEntry(); $entry->setTitle($post['title']); // $entry->setLink($baseUri . $this->generateUri('blog.post', ['id' => $post['id']])); $entry->setLink($baseUri . sprintf('/blog/%s.html', $post['id'])); $entry->addAuthor($author); $entry->setDateModified(new DateTime($post['updated'])); $entry->setDateCreated(new DateTime($post['created'])); $entry->setContent($this->createContent($html, $post)); $feed->addEntry($entry); } // Set feed date $feed->setDateModified(new DateTime($latest['updated'])); // Write feed to file $file = sprintf('%s%s.xml', $fileBase, $type); $file = str_replace(' ', '+', $file); file_put_contents($file, $feed->export($type)); }
/** * Parse markdown with YAML headers * * This method returns an array of: content as the first member and * YAML values as the second member. * * @param string $text * * @return array */ public static function parseWithYAML($text) { $parser = new Parser(); $parsed = $parser->parse($text); return [$parsed->getContent(), $parsed->getYAML()]; }
/** * @TODO * @TODO Test * @return Document */ protected function parseContents() { $parser = new Parser(); $document = $parser->parse($this->contents, false); $this->header = $document->getYAML() ?: $this->header; $this->body = $document->getContent() ?: $this->body; return $this; }
private function renderFile(MarkdownFile $file) { $document = $this->markdownParser->parse($file->getContent()); $filename = $this->replaceExtension($file->relativeFilename); return new HtmlFile($filename, $document->getContent(), $file); }