Skip to content

Instantly share code, notes, and snippets.

@lwiesel
Created November 19, 2014 15:09
Show Gist options
  • Select an option

  • Save lwiesel/7b70c53dec8bb25c74c6 to your computer and use it in GitHub Desktop.

Select an option

Save lwiesel/7b70c53dec8bb25c74c6 to your computer and use it in GitHub Desktop.
Markdown documentation
<?php
namespace ChessyWeb\Bundle\CatteryBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Yaml\Parser;
use Parsedown;
use ParsedownExtra;
class DocumentationController extends Controller
{
public function pageAction($page)
{
$parsedown = new ParsedownExtra();
$parser = $this->container->get('chessy_web_base.fileparser');
$fileContent = $parser->parse($page.'.md');
if (!empty($fileContent['frontMatter']['next'])) {
$nextFile = $fileContent['frontMatter']['next'].'.md';
$nextFileContent = $parser->parse($nextFile);
$fileContent['frontMatter']['nextTitle'] = $nextFileContent['frontMatter']['title'];
}
if (!empty($fileContent['frontMatter']['prev'])) {
$prevFile = $fileContent['frontMatter']['prev'].'.md';
$prevFileContent = $parser->parse($prevFile);
$fileContent['frontMatter']['prevTitle'] = $prevFileContent['frontMatter']['title'];
}
return $this->render('ChessyWebCatteryBundle:Documentation:index.html.twig',array(
'content' => $parsedown->text($fileContent['content']),
'data' => $fileContent['frontMatter'],
));
}
}
<?php
namespace ChessyWeb\Bundle\BaseBundle\Service\FileParser;
use Symfony\Component\DependencyInjection\Container;
use Symfony\Component\Yaml\Parser;
use Parsedown;
class FileParser
{
private $container;
public function __construct(Container $container)
{
$this->container = $container;
}
public function parse($page)
{
$yaml = new Parser();
$kernel = $this->container->get('kernel');
$path = $kernel->locateResource('@ChessyWebCatteryBundle/Resources/doc/'.$page);
$content = file_get_contents($path);
$split = preg_split("/[\n]*[-]{3}[\n]*/", $content, 3);
$data = $yaml->parse($split[1]);
$content = $split[2];
return array(
'content' => $content,
'frontMatter' => $data,
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment