Html Command outputs formatted document

The section titles aren’t being handled correctly, but otherwise, the
output looks good.
This commit is contained in:
Craig Davis
2014-01-12 11:00:45 -07:00
parent 842fae2b6f
commit 57fbb3dd8f
3 changed files with 281 additions and 101 deletions

View File

@@ -6,6 +6,12 @@ use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Assetic\Asset\AssetCollection;
use Assetic\Asset\FileAsset;
use Assetic\Asset\GlobAsset;
use Assetic\Filter;
use Michelf\Markdown;
use Michelf\SmartyPants;
class HtmlCommand extends Command
{
@@ -65,18 +71,69 @@ class HtmlCommand extends Command
$template = $this->app->defaultTemplate;
}
$templatePath = join(DIRECTORY_SEPARATOR, array(
$this->app->templatePath, basename($template), '/index.html'
$this->app->templatePath, basename($template)
));
if (!file_exists($templatePath)) {
$templateIndexPath = join(DIRECTORY_SEPARATOR, array(
$templatePath, 'index.html'
));
if (!file_exists($templateIndexPath)) {
$output->writeln(
sprintf(
"<error>Unable to open template file: %s</error>",
$templatePath
$templateIndexPath
),
$this->app->outputFormat
);
return false;
}
// We build these into a single string so that we can deploy this resume as a
// single file.
$cssAssetSelector = join(DIRECTORY_SEPARATOR, array($templatePath, '/css/*.css'));
$css = new AssetCollection(
array(new GlobAsset($cssAssetSelector)),
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 = Markdown::defaultTransform($resumeContent);
$resumeHtml = SmartyPants::defaultTransform($resumeHtml);
// We'll 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
);
// We'll now 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' => $refresh
)
);
// Save the fully rendered html to the final destination
file_put_contents($destination, $rendered);
$output->writeln(
sprintf(
"Wrote resume to: <info>%s</info>",
$destination
),
$this->app->outputFormat
);
}
}