setName('html') ->setDescription('Generate an HTML resume from a markdown file') ->addArgument( 'source', InputArgument::REQUIRED, 'Source markdown document' ) ->addArgument( 'destination', InputArgument::REQUIRED, 'Output destination folder' ) ->addOption( 'template', 't', InputOption::VALUE_REQUIRED, 'Which of the templates to use' ) ->addOption( 'refresh', 'r', InputOption::VALUE_REQUIRED, 'Regenerate the html and include a meta command to refresh the ' . 'document every periodically. Measured in seconds.' ); } protected function execute(InputInterface $input, OutputInterface $output) { $this->app = $this->getApplication(); $source = $input->getArgument('source'); $destination = rtrim($input->getArgument('destination'), DIRECTORY_SEPARATOR); $template = $input->getOption('template'); $refresh = $input->getOption('refresh'); $destFilename = join(DIRECTORY_SEPARATOR, array($destination, pathinfo($source, PATHINFO_FILENAME) . '.html')); $rendered = $this->generateHtml($source, $template, $refresh); file_put_contents($destFilename, $rendered); $output->writeln( sprintf( 'Wrote resume to: %s', $destFilename ), $this->app->outputFormat ); return true; } protected function generateHtml($source, $template, $refresh) { // Check that the source file is sane if (!file_exists($source)) { throw new \Exception("Unable to open source file: $source"); } // Check that our template is sane, or set to the default one if (!$template) { $template = $this->app->defaultTemplate; } $templatePath = join(DIRECTORY_SEPARATOR, array($this->app->templatePath, basename($template))); $templateIndexPath = join(DIRECTORY_SEPARATOR, array($templatePath, 'index.html')); if (!file_exists($templateIndexPath)) { throw new \Exception("Unable to open template file: $templateIndexPath"); } // We build these into a single string so that we can deploy this resume as a // single file. $cssAssetPath = join(DIRECTORY_SEPARATOR, array($templatePath, '/css')); $cssAssets = array(); // Our PHAR deployment can't handle the GlobAsset typically used here foreach (new \DirectoryIterator($cssAssetPath) as $fileInfo) { if ($fileInfo->isDot() || !$fileInfo->isFile()) { continue; } array_push($cssAssets, new FileAsset($fileInfo->getPathname())); } $css = new AssetCollection( $cssAssets, array(new Filter\LessphpFilter()) ); $style = $css->dump(); $templateContent = file_get_contents($templateIndexPath); $resumeContent = file_get_contents($source); // Process with Markdown, and then use SmartyPants to clean up punctuation. $resumeHtml = MarkdownExtra::defaultTransform($resumeContent); $resumeHtml = SmartyPants::defaultTransform($resumeHtml); // Construct the title for the html document from the h1 and h2 tags $simpleDom = new \simple_html_dom(); $simpleDom->load($resumeHtml); $title = sprintf( '%s | %s', $simpleDom->find('h1', 0)->innertext, $simpleDom->find('h2', 0)->innertext ); // Render the Markdown into an html file with Mustache Templates $m = new \Mustache_Engine; $rendered = $m->render($templateContent, array( 'title' => $title, 'style' => $style, 'resume' => $resumeHtml, 'reload' => (bool) $refresh, 'refresh_rate' => $refresh )); return $rendered; } } /* End of file HtmlCommand.php */