Skip to content

Commit 61aa211

Browse files
committed
Add Twig extension with md2html filter
1 parent 1bbcf89 commit 61aa211

File tree

6 files changed

+75
-20
lines changed

6 files changed

+75
-20
lines changed

app/Resources/views/default/cheatsheet.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
{% endblock %}
88

99
{% block body %}
10-
{{ html|raw }}
10+
{{ html|md2html }}
1111
{% endblock %}

app/Resources/views/default/conduct.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@
1616
{% endblock %}
1717

1818
{% block body %}
19-
{{ content|raw }}
19+
{{ content|md2html }}
2020
{% endblock %}

app/Resources/views/default/resources.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
{% endblock %}
88

99
{% block body %}
10-
{{ html|raw }}
10+
{{ html|md2html }}
1111
{% endblock %}

app/config/services.yml

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
1-
# Learn more about services, parameters and containers at
2-
# http://symfony.com/doc/current/book/service_container.html
3-
parameters:
4-
# parameter_name: value
5-
61
services:
2+
markdown:
3+
class: Mni\FrontYAML\Parser
4+
75
app.repository.post:
86
class: AppBundle\Repository\PostRepository
9-
arguments: ["%kernel.root_dir%"]
7+
arguments: ['%kernel.root_dir%']
108

119
app.repository.psr:
1210
class: AppBundle\Repository\PsrRepository
13-
arguments: ["%kernel.root_dir%"]
11+
arguments: ['%kernel.root_dir%']
12+
13+
app.twig.app_extension:
14+
class: AppBundle\Twig\AppExtension
15+
public: false
16+
arguments: ['@markdown']
17+
tags:
18+
- { name: twig.extension }

src/AppBundle/Controller/DefaultController.php

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use AppBundle\Entity\Contact;
1414
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
1515
use Symfony\Component\HttpFoundation\Response;
16-
use Mni\FrontYAML\Parser;
1716
use AppBundle\Entity\Project;
1817

1918
class DefaultController extends Controller
@@ -168,10 +167,8 @@ public function resourcesAction(Request $request)
168167
{
169168
$file = $this->get('kernel')->getRootDir().'/../vendor/symfony-si/symfony-resources/README.md';
170169
$content = (file_exists($file)) ? file_get_contents($file) : '<h1>Symfony resources</h1>';
171-
$parser = new Parser();
172-
$document = $parser->parse($content);
173170

174-
return $this->render('default/resources.html.twig', ['html' => $document->getContent()]);
171+
return $this->render('default/resources.html.twig', ['html' => $content]);
175172
}
176173

177174
/**
@@ -182,10 +179,8 @@ public function cheatsheetAction()
182179
{
183180
$file = $this->get('kernel')->getRootDir().'/../vendor/symfony-si/symfony-cheatsheet/README.md';
184181
$content = (file_exists($file)) ? file_get_contents($file) : '<h1>Symfony cheat sheet</h1>';
185-
$parser = new Parser();
186-
$document = $parser->parse($content);
187182

188-
return $this->render('default/cheatsheet.html.twig', ['html' => $document->getContent()]);
183+
return $this->render('default/cheatsheet.html.twig', ['html' => $content]);
189184
}
190185

191186
/**
@@ -315,9 +310,7 @@ public function conductAction()
315310
{
316311
$file = $this->get('kernel')->getRootDir().'/../vendor/symfony-si/conduct/README.md';
317312
$content = (file_exists($file)) ? file_get_contents($file) : '<h1>Symfony.si Code of Conduct</h1>';
318-
$parser = new Parser();
319-
$document = $parser->parse($content);
320313

321-
return $this->render('default/conduct.html.twig', ['content' => $document->getContent()]);
314+
return $this->render('default/conduct.html.twig', ['content' => $content]);
322315
}
323316
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace AppBundle\Twig;
4+
5+
use Mni\FrontYAML\Parser;
6+
7+
/**
8+
* Application Twig extension which adds 'md2html' Twig filter for converting Markdown
9+
* content to HTML.
10+
*/
11+
class AppExtension extends \Twig_Extension
12+
{
13+
/**
14+
* @var Parser
15+
*/
16+
private $parser;
17+
18+
/**
19+
* AppExtension constructor.
20+
*
21+
* @param Parser $parser
22+
*/
23+
public function __construct(Parser $parser)
24+
{
25+
$this->parser = $parser;
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function getFilters()
32+
{
33+
return array(
34+
new \Twig_SimpleFilter('md2html', [$this, 'markdownToHtml'], ['is_safe' => ['html']]),
35+
);
36+
}
37+
38+
/**
39+
* Parses the given Markdown content and converts it HTML.
40+
*
41+
* @param string $content
42+
* @return string
43+
*/
44+
public function markdownToHtml($content)
45+
{
46+
$document = $this->parser->parse($content);
47+
return $document->getContent();
48+
}
49+
50+
/**
51+
* {@inheritdoc}
52+
*/
53+
public function getName()
54+
{
55+
return 'app.extension';
56+
}
57+
}

0 commit comments

Comments
 (0)