mirror of
https://github.com/KnpLabs/KnpMarkdownBundle.git
synced 2024-06-16 12:45:15 +02:00
Compare commits
101 commits
Author | SHA1 | Date | |
---|---|---|---|
9a8e23f69f | |||
091cff2698 | |||
dc113af854 | |||
9e2d994506 | |||
80e724bb6c | |||
7dd53fca23 | |||
129a09ce3f | |||
4f439a0d56 | |||
59ab364807 | |||
68aaf27531 | |||
54c72caaf7 | |||
2e716d1b64 | |||
643203872b | |||
b22717883f | |||
c018169d4e | |||
2b779a4eb3 | |||
63c755a578 | |||
74ded9e121 | |||
664c3bf82b | |||
b839e4358f | |||
cdd1aeec0c | |||
98710f8f2a | |||
972faeb8cf | |||
547f137ea8 | |||
b9e547fb67 | |||
83d78eb5c7 | |||
289f0327bb | |||
a87b16a141 | |||
f782aae4f7 | |||
823f0d8d89 | |||
7e0b145307 | |||
6decbb73f9 | |||
88932cc74d | |||
9de7a9f400 | |||
7afd756126 | |||
90ff78064e | |||
24f6c74239 | |||
86433a9433 | |||
69862cc6f6 | |||
5fb3d1c269 | |||
fa322f2a92 | |||
fadc9120b8 | |||
da45d3c76d | |||
eb31781090 | |||
7074f295ce | |||
5baa80f676 | |||
843b3d0c9b | |||
5e83a6a9ba | |||
d575814dd3 | |||
7238cc264e | |||
6dba9b29bc | |||
a81befb20f | |||
bd574d776b | |||
a63663cce6 | |||
a172d73960 | |||
dd17fd825c | |||
182b7cc670 | |||
a960290f36 | |||
d462ba15b9 | |||
1cb9e62d4d | |||
b23e8d965b | |||
25663d724b | |||
0f8ec028e9 | |||
f93e8da815 | |||
c055def2e6 | |||
d2824faf99 | |||
c2b7faa646 | |||
881585be15 | |||
a97046ec63 | |||
8947cfb930 | |||
4e998d807c | |||
912796833a | |||
02d9f8b7a4 | |||
91a268692e | |||
77d1412005 | |||
816b0ca898 | |||
424be918cd | |||
6ed1387fc0 | |||
5057219910 | |||
814ac965d1 | |||
6f42bee012 | |||
1adfd1bd8c | |||
51c54c813c | |||
175144c0bd | |||
9432453ab9 | |||
4c3a367997 | |||
fd1a6d20eb | |||
24dea61a36 | |||
3581dabfc5 | |||
4ec8a70d92 | |||
8956d7e71b | |||
c74ca069f9 | |||
4fb2747817 | |||
bb19335618 | |||
aaa0ec473f | |||
48f1ed6e5b | |||
ecf5581606 | |||
21576fce48 | |||
000c4b11ed | |||
4e890d540a | |||
c5f5046c8e |
91
.github/workflows/phpunit.yaml
vendored
Normal file
91
.github/workflows/phpunit.yaml
vendored
Normal file
|
@ -0,0 +1,91 @@
|
|||
name: PHPUnit
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
|
||||
jobs:
|
||||
phpunit:
|
||||
name: "PHPUnit - PHP ${{ matrix.php-version }}"
|
||||
runs-on: ubuntu-20.04
|
||||
continue-on-error: false
|
||||
env:
|
||||
SYMFONY_REQUIRE: ${{matrix.symfony-require}}
|
||||
SYMFONY_DEPRECATIONS_HELPER: ${{matrix.symfony-deprecations-helper}}
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
php-version:
|
||||
- "7.4"
|
||||
- "8.0"
|
||||
- "8.1"
|
||||
deps:
|
||||
- "stable"
|
||||
symfony-require:
|
||||
- "5.4.*"
|
||||
symfony-deprecations-helper:
|
||||
- "5"
|
||||
include:
|
||||
- symfony-require: "4.4.*"
|
||||
php-version: "7.4"
|
||||
deps: "low"
|
||||
symfony-deprecations-helper: ""
|
||||
|
||||
- symfony-require: "4.4.*"
|
||||
php-version: "7.4"
|
||||
deps: "stable"
|
||||
symfony-deprecations-helper: "5"
|
||||
|
||||
- symfony-require: "6.0.*"
|
||||
php-version: "8.0"
|
||||
deps: "stable"
|
||||
symfony-deprecations-helper: "5"
|
||||
|
||||
- symfony-require: "6.0.*"
|
||||
php-version: "8.1"
|
||||
deps: "stable"
|
||||
symfony-deprecations-helper: "5"
|
||||
|
||||
fail-fast: true
|
||||
|
||||
steps:
|
||||
- name: Checkout code
|
||||
uses: actions/checkout@v2
|
||||
|
||||
- name: Install PHP with extensions
|
||||
uses: shivammathur/setup-php@v2
|
||||
with:
|
||||
coverage: none
|
||||
php-version: ${{ matrix.php-version }}
|
||||
extensions: mbstring, intl, pdo, pdo_sqlite, sqlite3
|
||||
ini-values: date.timezone=UTC
|
||||
|
||||
- name: Validate composer.json and composer.lock
|
||||
run: composer validate --strict
|
||||
|
||||
- name: Install dependencies with Composer
|
||||
run: composer update --no-interaction --prefer-dist --optimize-autoloader --prefer-stable
|
||||
|
||||
- name: Cache dependencies installed with Composer
|
||||
uses: actions/cache@v2
|
||||
with:
|
||||
path: ~/.composer/cache
|
||||
key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}"
|
||||
restore-keys: "php-${{ matrix.php-version }}-composer-locked-"
|
||||
|
||||
- name: Install stable dependencies with Composer
|
||||
run: composer update --no-interaction --prefer-dist --prefer-stable
|
||||
if: "${{ matrix.deps == 'stable' }}"
|
||||
|
||||
- name: Install dev dependencies with Composer
|
||||
run: composer update --no-interaction --prefer-dist
|
||||
if: "${{ matrix.deps == 'dev' }}"
|
||||
|
||||
- name: Install lowest possible dependencies with Composer
|
||||
run: composer update --no-interaction --prefer-dist --prefer-stable --prefer-lowest
|
||||
if: "${{ matrix.deps == 'low' }}"
|
||||
|
||||
- name: Install PHPUnit
|
||||
run: composer run-script test install
|
||||
|
||||
- name: Run tests
|
||||
run: composer run-script test -v
|
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -1,4 +1,6 @@
|
|||
.phpunit.result.cache
|
||||
phpunit.xml
|
||||
var/
|
||||
vendor/*
|
||||
!vendor/parser
|
||||
composer.lock
|
||||
Tests/fixtures/app/cache
|
||||
|
|
47
.travis.yml
47
.travis.yml
|
@ -1,36 +1,39 @@
|
|||
language: php
|
||||
|
||||
sudo: false
|
||||
|
||||
cache:
|
||||
directories:
|
||||
- $HOME/.composer/cache/files
|
||||
- $HOME/symfony-bridge/.phpunit
|
||||
|
||||
php:
|
||||
- 5.3
|
||||
- 5.4
|
||||
- 5.5
|
||||
- 5.6
|
||||
- 7.0
|
||||
- hhvm
|
||||
env:
|
||||
global:
|
||||
- PHPUNIT_FLAGS="-v"
|
||||
- SYMFONY_PHPUNIT_DIR="$HOME/symfony-bridge/.phpunit"
|
||||
|
||||
matrix:
|
||||
fast_finish: true
|
||||
include:
|
||||
# Test against lowest bounds of dependencies to ensure they are right
|
||||
- php: 5.6
|
||||
env: COMPOSER_FLAGS='--prefer-lowest --prefer-stable'
|
||||
# Test against dev versions of dependencies
|
||||
- php: 5.6
|
||||
env: deps=dev
|
||||
- php: 7.1
|
||||
- php: 7.2
|
||||
- php: 7.3
|
||||
env: deps=low
|
||||
- php: 7.4
|
||||
- php: 8.0
|
||||
|
||||
before_install:
|
||||
- if [ "$deps" = 'dev' ]; then perl -pi -e 's/^}$/,"minimum-stability":"dev"}/' composer.json; fi;
|
||||
- phpenv config-rm xdebug.ini || true
|
||||
- composer global require --no-progress --no-scripts --no-plugins symfony/flex dev-main
|
||||
|
||||
install:
|
||||
- composer update $COMPOSER_FLAGS
|
||||
- |
|
||||
if [[ $deps = low ]]; then
|
||||
export SYMFONY_DEPRECATIONS_HELPER=weak
|
||||
composer update --prefer-dist --prefer-lowest --prefer-stable
|
||||
else
|
||||
composer update --prefer-dist
|
||||
fi
|
||||
- ./vendor/bin/simple-phpunit install
|
||||
|
||||
script: phpunit --coverage-text
|
||||
|
||||
notifications:
|
||||
email:
|
||||
- travis@knplabs.com
|
||||
script:
|
||||
- composer validate --strict --no-check-lock
|
||||
- ./vendor/bin/simple-phpunit $PHPUNIT_FLAGS
|
6
CHANGELOG.md
Normal file
6
CHANGELOG.md
Normal file
|
@ -0,0 +1,6 @@
|
|||
1.7
|
||||
===
|
||||
|
||||
* The MarkdownHelper Templating component integration (and corresponding
|
||||
`templating.helper.markdown` services) are no longer added unless you
|
||||
have the `symfony/templating` component installed.
|
|
@ -10,7 +10,7 @@ class ParsersCompilerPass implements CompilerPassInterface
|
|||
{
|
||||
public function process(ContainerBuilder $container)
|
||||
{
|
||||
if (!$container->hasDefinition('templating.helper.markdown')) {
|
||||
if (!$container->hasDefinition('markdown.parser.parser_manager')) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -24,7 +24,7 @@ class ParsersCompilerPass implements CompilerPassInterface
|
|||
$defaultAlias = isset($defaultAlias['alias']) ? $defaultAlias['alias'] : null;
|
||||
}
|
||||
|
||||
$definition = $container->getDefinition('templating.helper.markdown');
|
||||
$definition = $container->getDefinition('markdown.parser.parser_manager');
|
||||
if (empty($defaultAlias)) {
|
||||
$definition->addMethodCall('addParser', array(new Reference('markdown.parser'), 'default'));
|
||||
}
|
||||
|
|
|
@ -12,11 +12,12 @@ class Configuration implements ConfigurationInterface
|
|||
*
|
||||
* @return TreeBuilder
|
||||
*/
|
||||
public function getConfigTreeBuilder()
|
||||
public function getConfigTreeBuilder(): TreeBuilder
|
||||
{
|
||||
$treeBuilder = new TreeBuilder();
|
||||
$treeBuilder = new TreeBuilder('knp_markdown');
|
||||
$rootNode = $treeBuilder->getRootNode();
|
||||
|
||||
$treeBuilder->root('knp_markdown', 'array')
|
||||
$rootNode
|
||||
->addDefaultsIfNotSet()
|
||||
->children()
|
||||
->arrayNode('parser')
|
||||
|
|
|
@ -2,11 +2,15 @@
|
|||
|
||||
namespace Knp\Bundle\MarkdownBundle\DependencyInjection;
|
||||
|
||||
use Knp\Bundle\MarkdownBundle\MarkdownParserInterface;
|
||||
use Michelf\MarkdownInterface;
|
||||
use Symfony\Component\Config\FileLocator;
|
||||
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
|
||||
use Symfony\Component\DependencyInjection\Alias;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
|
||||
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
|
||||
use Symfony\Component\Templating\EngineInterface;
|
||||
|
||||
class KnpMarkdownExtension extends Extension
|
||||
{
|
||||
|
@ -26,7 +30,10 @@ class KnpMarkdownExtension extends Extension
|
|||
|
||||
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
|
||||
$loader->load('parser.xml');
|
||||
$loader->load('helper.xml');
|
||||
// BC to support the PHP templates in the Templating component
|
||||
if (interface_exists(EngineInterface::class)) {
|
||||
$loader->load('helper.xml');
|
||||
}
|
||||
$loader->load('twig.xml');
|
||||
|
||||
if ('markdown.parser.sundown' == $config['parser']['service']) {
|
||||
|
@ -43,6 +50,8 @@ class KnpMarkdownExtension extends Extension
|
|||
$container->setParameter('markdown.sundown.render_flags', $config['sundown']['render_flags']);
|
||||
}
|
||||
|
||||
$container->setAlias('markdown.parser', $config['parser']['service']);
|
||||
$container->setAlias('markdown.parser', new Alias($config['parser']['service'], true));
|
||||
$container->setAlias(MarkdownParserInterface::class, 'markdown.parser');
|
||||
$container->setAlias(MarkdownInterface::class, 'markdown.parser');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,24 +2,20 @@
|
|||
|
||||
namespace Knp\Bundle\MarkdownBundle\Helper;
|
||||
|
||||
use Knp\Bundle\MarkdownBundle\Parser\ParserManager;
|
||||
use Symfony\Component\Templating\Helper\HelperInterface;
|
||||
use Knp\Bundle\MarkdownBundle\MarkdownParserInterface;
|
||||
|
||||
/**
|
||||
* @deprecated The MarkdownHelper was deprecated in 1.7 and will be removed in 2.0.
|
||||
*/
|
||||
class MarkdownHelper implements HelperInterface
|
||||
{
|
||||
/**
|
||||
* @var MarkdownParserInterface[]
|
||||
*/
|
||||
private $parsers = array();
|
||||
private $parserManager;
|
||||
private $charset = 'UTF-8';
|
||||
|
||||
/**
|
||||
* @param MarkdownParserInterface $parser
|
||||
* @param string $alias
|
||||
*/
|
||||
public function addParser(MarkdownParserInterface $parser, $alias)
|
||||
public function __construct(ParserManager $parserManager)
|
||||
{
|
||||
$this->parsers[$alias] = $parser;
|
||||
$this->parserManager = $parserManager;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -34,17 +30,9 @@ class MarkdownHelper implements HelperInterface
|
|||
*/
|
||||
public function transform($markdownText, $parserName = null)
|
||||
{
|
||||
if (null === $parserName) {
|
||||
$parserName = 'default';
|
||||
}
|
||||
trigger_error('The MarkdownHelper was deprecated in 1.7 and will be removed in KnpMarkdownBundle 2.0.', E_USER_DEPRECATED);
|
||||
|
||||
if (!isset($this->parsers[$parserName])) {
|
||||
throw new \RuntimeException(sprintf('Unknown parser selected ("%s"), available are: %s', $parserName, implode(', ', array_keys($this->parsers))));
|
||||
}
|
||||
|
||||
$parser = $this->parsers[$parserName];
|
||||
|
||||
return $parser->transformMarkdown($markdownText);
|
||||
return $this->parserManager->transform($markdownText, $parserName);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -62,7 +50,7 @@ class MarkdownHelper implements HelperInterface
|
|||
*
|
||||
* @return string The default charset
|
||||
*/
|
||||
public function getCharset()
|
||||
public function getCharset(): string
|
||||
{
|
||||
return $this->charset;
|
||||
}
|
||||
|
|
|
@ -124,7 +124,7 @@ class MarkdownParser extends MarkdownExtra implements MarkdownParserInterface
|
|||
/**
|
||||
* Simplify detab
|
||||
*/
|
||||
public function detab($text)
|
||||
public function detab($text): string
|
||||
{
|
||||
return str_replace("\t", str_repeat(' ', $this->tab_width), $text);
|
||||
}
|
||||
|
@ -216,6 +216,7 @@ class MarkdownParser extends MarkdownExtra implements MarkdownParserInterface
|
|||
[ \n]* # ignore any spaces/tabs between closing quote and )
|
||||
)? # title is optional
|
||||
\)
|
||||
(?:[ ]? '.$this->id_class_attr_catch_re.' )? # $8 = id/class attributes
|
||||
)
|
||||
}xs',
|
||||
array(&$this, '_doAnchors_inline_callback'), $text);
|
||||
|
|
47
Parser/ParserManager.php
Normal file
47
Parser/ParserManager.php
Normal file
|
@ -0,0 +1,47 @@
|
|||
<?php
|
||||
|
||||
namespace Knp\Bundle\MarkdownBundle\Parser;
|
||||
|
||||
use Knp\Bundle\MarkdownBundle\MarkdownParserInterface;
|
||||
|
||||
class ParserManager
|
||||
{
|
||||
/**
|
||||
* @var MarkdownParserInterface[]
|
||||
*/
|
||||
private $parsers = array();
|
||||
|
||||
/**
|
||||
* @param MarkdownParserInterface $parser
|
||||
* @param string $alias
|
||||
*/
|
||||
public function addParser(MarkdownParserInterface $parser, $alias)
|
||||
{
|
||||
$this->parsers[$alias] = $parser;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms markdown syntax to HTML
|
||||
*
|
||||
* @param string $markdownText The markdown syntax text
|
||||
* @param null|string $parserName
|
||||
*
|
||||
* @return string The HTML code
|
||||
*
|
||||
* @throws \RuntimeException
|
||||
*/
|
||||
public function transform($markdownText, $parserName = null)
|
||||
{
|
||||
if (null === $parserName) {
|
||||
$parserName = 'default';
|
||||
}
|
||||
|
||||
if (!isset($this->parsers[$parserName])) {
|
||||
throw new \RuntimeException(sprintf('Unknown parser selected ("%s"), available are: %s', $parserName, implode(', ', array_keys($this->parsers))));
|
||||
}
|
||||
|
||||
$parser = $this->parsers[$parserName];
|
||||
|
||||
return $parser->transformMarkdown($markdownText);
|
||||
}
|
||||
}
|
|
@ -15,7 +15,7 @@ class Min extends MarkdownParser
|
|||
$this->features[$name] = false;
|
||||
}
|
||||
|
||||
return parent::__construct($features);
|
||||
parent::__construct($features);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,48 +1,69 @@
|
|||
Provide markdown conversion (based on [Michel Fortin work](https://github.com/michelf/php-markdown)) to your Symfony2 projects.
|
||||
> **WARNING:** This bundle is **abandoned** in favor of [markdown_to_html](https://twig.symfony.com/doc/3.x/filters/markdown_to_html.html)
|
||||
> Twig filter. Please, consider using it instead. Migration should be easy because it also supports
|
||||
> `michelf/php-markdown` Markdown parser that is used in this project under the hood.
|
||||
|
||||
[![Build Status](https://secure.travis-ci.org/KnpLabs/KnpMarkdownBundle.png)](http://travis-ci.org/KnpLabs/KnpMarkdownBundle)
|
||||
Provide markdown conversion (based on [Michel Fortin work](https://github.com/michelf/php-markdown)) to your Symfony projects.
|
||||
|
||||
[![Build Status](https://secure.travis-ci.org/KnpLabs/KnpMarkdownBundle.svg)](http://travis-ci.org/KnpLabs/KnpMarkdownBundle)
|
||||
|
||||
![ci.yml](https://github.com/tacman/KnpMarkdownBundle/actions/workflows/ci.yml/badge.svg)
|
||||
![php.yml](https://github.com/tacman/KnpMarkdownBundle/actions/workflows/php.yml/badge.svg)
|
||||
|
||||
## INSTALLATION
|
||||
|
||||
Add KnpMarkdownBundle to your `composer.json`
|
||||
Add KnpMarkdownBundle to your project via [Composer](https://getcomposer.org/):
|
||||
|
||||
```yaml
|
||||
{
|
||||
"require": {
|
||||
"knplabs/knp-markdown-bundle": "~1.3"
|
||||
}
|
||||
}
|
||||
```
|
||||
or simply run ```composer require knplabs/knp-markdown-bundle:~1.3```
|
||||
|
||||
Register the bundle in ``app/AppKernel.php``
|
||||
|
||||
```php
|
||||
$bundles = array(
|
||||
// ...
|
||||
new Knp\Bundle\MarkdownBundle\KnpMarkdownBundle(),
|
||||
);
|
||||
composer require knplabs/knp-markdown-bundle
|
||||
```
|
||||
|
||||
If you're *not* using Symfony Flex, you will also need to enable
|
||||
the bundle in your `app/AppKernel.php` file
|
||||
(`new Knp\Bundle\MarkdownBundle\KnpMarkdownBundle()`).
|
||||
|
||||
That's it! Start using it!
|
||||
|
||||
## USAGE
|
||||
|
||||
Once the bundle is installed, you can autowire a `MarkdownParserInterface`
|
||||
into any service or controller:
|
||||
|
||||
```php
|
||||
// Use the service
|
||||
$html = $this->container->get('markdown.parser')->transformMarkdown($text);
|
||||
use Knp\Bundle\MarkdownBundle\MarkdownParserInterface;
|
||||
|
||||
// Use the helper with default parser
|
||||
echo $view['markdown']->transform($text);
|
||||
// from inside a controller
|
||||
public function index(MarkdownParserInterface $parser)
|
||||
{
|
||||
$html = $parser->transformMarkdown($text);
|
||||
}
|
||||
|
||||
// Use the helper and a select specific parser
|
||||
echo $view['markdown']->transform($text, $parserName);
|
||||
// or from inside a service
|
||||
private $parser;
|
||||
|
||||
public function __construct(MarkdownParserInterface $parser)
|
||||
{
|
||||
$this->parser = $parser;
|
||||
}
|
||||
|
||||
public function someMethod()
|
||||
{
|
||||
$html = $this->parser->transformMarkdown($text);
|
||||
}
|
||||
```
|
||||
|
||||
If you have enabled the Twig markdown filter, you can use the following in your Twig templates:
|
||||
There is also a public `markdown.parser` service you can use.
|
||||
|
||||
In Twig, you can use the `markdown` filter:
|
||||
|
||||
```twig
|
||||
{# Use default parser #}
|
||||
{{ my_data|markdown }}
|
||||
|
||||
{# If my_data is entered by a user, escape HTML tags before printing it #}
|
||||
{{ my_data|escape|markdown }}
|
||||
{# or strip HTML tags #}
|
||||
{{ my_data|striptags|markdown }}
|
||||
|
||||
{# Or select specific parser #}
|
||||
{{ my_data|markdown('parserName') }}
|
||||
```
|
||||
|
@ -53,6 +74,8 @@ Create a service implementing `Knp\Bundle\MarkdownBundle\MarkdownParserInterface
|
|||
then configure the bundle to use it:
|
||||
|
||||
```yaml
|
||||
# Symfony 3: app/config/config.yml
|
||||
# Symfony 4: config/packages/knp_markdown.yaml (you'll need to create this)
|
||||
knp_markdown:
|
||||
parser:
|
||||
service: my.markdown.parser
|
||||
|
@ -62,7 +85,7 @@ Alternatively if you are using the ``markdown.parser.sundown`` there are
|
|||
options for enabling sundown extensions and render flags, see the
|
||||
default Configuration with:
|
||||
|
||||
php app/console config:dump-reference knp_markdown
|
||||
php bin/console config:dump-reference knp_markdown
|
||||
|
||||
This bundle comes with 5 parser services, 4 based on the same algorithm
|
||||
but providing different levels of compliance to the markdown specification,
|
||||
|
@ -77,7 +100,3 @@ and one which is uses the php sundown extension:
|
|||
``markdown.parser.sundown`` requires the [php sundown extension](https://github.com/chobie/php-sundown).
|
||||
|
||||
For more details, see the implementations in Parser/Preset.
|
||||
|
||||
## TEST
|
||||
|
||||
phpunit -c myapp vendor/bundles/Knp/Bundle/MarkdownBundle
|
||||
|
|
|
@ -9,7 +9,7 @@
|
|||
|
||||
<services>
|
||||
<service id="templating.helper.markdown" class="%templating.helper.markdown.class%">
|
||||
<argument type="service" id="markdown.parser" />
|
||||
<argument type="service" id="markdown.parser.parser_manager" />
|
||||
<tag name="templating.helper" alias="markdown" />
|
||||
</service>
|
||||
</services>
|
||||
|
|
|
@ -19,5 +19,8 @@
|
|||
<service id="markdown.parser.flavored" class="Knp\Bundle\MarkdownBundle\Parser\Preset\Flavored" public="false">
|
||||
<tag name="markdown.parser" alias="flavored" />
|
||||
</service>
|
||||
|
||||
<!-- parsers will be injected with a compiler pass -->
|
||||
<service id="markdown.parser.parser_manager" class="Knp\Bundle\MarkdownBundle\Parser\ParserManager" public="false" />
|
||||
</services>
|
||||
</container>
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
<services>
|
||||
<service id="twig.extension.exercise.twig" class="Knp\Bundle\MarkdownBundle\Twig\Extension\MarkdownTwigExtension" public="false">
|
||||
<argument type="service" id="templating.helper.markdown" />
|
||||
<argument type="service" id="markdown.parser.parser_manager" />
|
||||
<tag name="twig.extension" />
|
||||
</service>
|
||||
</services>
|
||||
|
|
|
@ -2,30 +2,32 @@
|
|||
|
||||
namespace Knp\Bundle\MarkdownBundle\Twig\Extension;
|
||||
|
||||
use Knp\Bundle\MarkdownBundle\Helper\MarkdownHelper;
|
||||
use Knp\Bundle\MarkdownBundle\Parser\ParserManager;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
|
||||
class MarkdownTwigExtension extends \Twig_Extension
|
||||
class MarkdownTwigExtension extends AbstractExtension
|
||||
{
|
||||
protected $helper;
|
||||
private $parserManager;
|
||||
|
||||
public function __construct(MarkdownHelper $helper)
|
||||
public function __construct(ParserManager $parserManager)
|
||||
{
|
||||
$this->helper = $helper;
|
||||
$this->parserManager = $parserManager;
|
||||
}
|
||||
|
||||
public function getFilters()
|
||||
public function getFilters(): array
|
||||
{
|
||||
return array(
|
||||
new \Twig_SimpleFilter('markdown', array($this, 'markdown'), array('is_safe' => array('html'))),
|
||||
new TwigFilter('markdown', array($this, 'markdown'), array('is_safe' => array('html'))),
|
||||
);
|
||||
}
|
||||
|
||||
public function markdown($text, $parser = null)
|
||||
public function markdown($text, $parser = null): string
|
||||
{
|
||||
return $this->helper->transform($text, $parser);
|
||||
return $this->parserManager->transform($text, $parser);
|
||||
}
|
||||
|
||||
public function getName()
|
||||
public function getName(): string
|
||||
{
|
||||
return 'markdown';
|
||||
}
|
||||
|
|
|
@ -18,13 +18,18 @@
|
|||
],
|
||||
|
||||
"require": {
|
||||
"php": ">=5.3.9",
|
||||
"symfony/framework-bundle": "~2.3|~3.0",
|
||||
"michelf/php-markdown": "~1.4"
|
||||
"php": "^7.4|^8.0",
|
||||
"symfony/framework-bundle": "^4.4|^5.0|^6.0",
|
||||
"symfony/dependency-injection": "^4.4|^5.0|^6.0",
|
||||
"michelf/php-markdown": "^1.9"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "~4.5"
|
||||
"symfony/phpunit-bridge": "^4.4.11|^5.0|^6.0",
|
||||
"symfony/templating": "^4.4|^5.0|^6.0",
|
||||
"phpstan/phpstan": "^1.2",
|
||||
"phpstan/phpstan-symfony": "^1.0"
|
||||
},
|
||||
"abandoned": "twig/markdown-extra",
|
||||
"suggest": {
|
||||
"symfony/twig-bundle": "to use the Twig markdown filter",
|
||||
"ext-sundown": "to use optional support for php-sundown extension instead of php implementation"
|
||||
|
@ -32,7 +37,19 @@
|
|||
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "1.4.x-dev"
|
||||
"dev-master": "1.5.x-dev"
|
||||
}
|
||||
},
|
||||
|
||||
"scripts": {
|
||||
"test": [
|
||||
"php ./vendor/bin/simple-phpunit"
|
||||
]
|
||||
},
|
||||
|
||||
"autoload-dev": {
|
||||
"psr-4": {
|
||||
"Knp\\Bundle\\MarkdownBundle\\Tests\\": "tests/"
|
||||
}
|
||||
},
|
||||
|
||||
|
|
|
@ -1,30 +1,31 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
|
||||
<phpunit backupGlobals="false"
|
||||
backupStaticAttributes="false"
|
||||
<!-- http://phpunit.de/manual/4.1/en/appendixes.configuration.html -->
|
||||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
|
||||
backupGlobals="false"
|
||||
bootstrap="vendor/autoload.php"
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
processIsolation="false"
|
||||
stopOnFailure="false"
|
||||
syntaxCheck="false"
|
||||
bootstrap="./vendor/autoload.php"
|
||||
failOnRisky="true"
|
||||
failOnWarning="true"
|
||||
failOnIncomplete="false"
|
||||
>
|
||||
<php>
|
||||
<server name="SYMFONY_PHPUNIT_VERSION" value="9.5" />
|
||||
</php>
|
||||
|
||||
<coverage processUncoveredFiles="true">
|
||||
<include>
|
||||
<directory>.</directory>
|
||||
</include>
|
||||
<exclude>
|
||||
<directory>tests</directory>
|
||||
<directory>vendor</directory>
|
||||
</exclude>
|
||||
</coverage>
|
||||
<testsuites>
|
||||
<testsuite name="Markdown Test Suite">
|
||||
<directory>./Tests/</directory>
|
||||
<directory>tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<filter>
|
||||
<whitelist>
|
||||
<directory>./</directory>
|
||||
<exclude>
|
||||
<directory>./Resources</directory>
|
||||
<directory>./Tests</directory>
|
||||
<directory>./vendor</directory>
|
||||
</exclude>
|
||||
</whitelist>
|
||||
</filter>
|
||||
</phpunit>
|
||||
|
|
|
@ -2,13 +2,14 @@
|
|||
|
||||
namespace Knp\Bundle\MarkdownBundle\Tests;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Knp\Bundle\MarkdownBundle\Parser\MarkdownParser as Parser;
|
||||
|
||||
class EscapingTest extends \PHPUnit_Framework_TestCase
|
||||
class EscapingTest extends TestCase
|
||||
{
|
||||
protected $parser;
|
||||
|
||||
public function setUp()
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->parser = new Parser();
|
||||
}
|
|
@ -3,8 +3,9 @@
|
|||
namespace Knp\Bundle\MarkdownBundle\Tests;
|
||||
|
||||
use Knp\Bundle\MarkdownBundle\Parser\MarkdownParser as Parser;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class FeatureTest extends \PHPUnit_Framework_TestCase
|
||||
class FeatureTest extends TestCase
|
||||
{
|
||||
public function testParser()
|
||||
{
|
||||
|
@ -749,23 +750,15 @@ That's some text with a footnote.[^1]
|
|||
[^1]: And that's the footnote.
|
||||
EOF;
|
||||
|
||||
$html = <<<EOF
|
||||
<p>That's some text with a footnote.<sup id="fnref:1"><a href="#fn:1" class="footnote-ref">1</a></sup></p>
|
||||
$actualHtml = $parser->transform($text);
|
||||
|
||||
<div class="footnotes">
|
||||
<hr />
|
||||
<ol>
|
||||
|
||||
<li id="fn:1">
|
||||
<p>And that's the footnote. <a href="#fnref:1" class="footnote-backref">↩</a></p>
|
||||
</li>
|
||||
|
||||
</ol>
|
||||
</div>
|
||||
|
||||
EOF;
|
||||
|
||||
$this->assertEquals($html, $parser->transform($text));
|
||||
// asserting a few things instead of comparing full final HTML
|
||||
// because a few minor things have changed over versions of Michelf
|
||||
// With assertContains(), tests will pass across all versions
|
||||
$this->assertStringContainsString('<p>That\'s some text with a footnote.<sup id="fnref:1"><a href="#fn:1"', $actualHtml);
|
||||
$this->assertStringContainsString('<div class="footnotes"', $actualHtml);
|
||||
$this->assertStringContainsString('<li id="fn:1"', $actualHtml);
|
||||
$this->assertStringContainsString('<p>And that\'s the footnote. <a href="#fnref:1" class="footnote-backref"', $actualHtml);
|
||||
}
|
||||
|
||||
/**
|
63
tests/IntegrationTest.php
Normal file
63
tests/IntegrationTest.php
Normal file
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
namespace Knp\Bundle\MarkdownBundle\Tests;
|
||||
|
||||
use Knp\Bundle\MarkdownBundle\KnpMarkdownBundle;
|
||||
use Knp\Bundle\MarkdownBundle\Parser\MarkdownParser;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
|
||||
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
|
||||
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\HttpKernel\Kernel;
|
||||
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
|
||||
use Symfony\Component\Routing\RouteCollectionBuilder;
|
||||
|
||||
class IntegrationTest extends TestCase
|
||||
{
|
||||
public function testServicesAvailable()
|
||||
{
|
||||
$kernel = new IntegrationKernel('dev', true);
|
||||
$kernel->boot();
|
||||
$container = $kernel->getContainer();
|
||||
|
||||
$this->assertInstanceOf(MarkdownParser::class, $container->get('markdown.parser'));
|
||||
}
|
||||
}
|
||||
|
||||
class IntegrationKernel extends Kernel
|
||||
{
|
||||
use MicroKernelTrait;
|
||||
|
||||
private $cacheDir;
|
||||
|
||||
public function registerBundles(): iterable
|
||||
{
|
||||
return [
|
||||
new FrameworkBundle(),
|
||||
new KnpMarkdownBundle(),
|
||||
];
|
||||
}
|
||||
|
||||
protected function configureRoutes(RoutingConfigurator $routes)
|
||||
{
|
||||
}
|
||||
|
||||
protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader)
|
||||
{
|
||||
|
||||
$c->loadFromExtension('framework', [
|
||||
'secret' => 'F00',
|
||||
'router' => ['utf8' => true]
|
||||
]);
|
||||
}
|
||||
|
||||
public function getCacheDir(): string
|
||||
{
|
||||
if (null === $this->cacheDir) {
|
||||
$this->cacheDir = sys_get_temp_dir().'/'.rand(100, 999);
|
||||
}
|
||||
|
||||
return $this->cacheDir;
|
||||
}
|
||||
}
|
29
tests/Parser/ParserManagerTest.php
Normal file
29
tests/Parser/ParserManagerTest.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Knp\Bundle\MarkdownBundle\Tests\Parser;
|
||||
|
||||
use Knp\Bundle\MarkdownBundle\Helper\MarkdownHelper;
|
||||
use Knp\Bundle\MarkdownBundle\Tests\fixtures\app\TestKernel;
|
||||
use Knp\Bundle\MarkdownBundle\Parser\ParserManager;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ParserManagerTest extends TestCase
|
||||
{
|
||||
public function testIntegration()
|
||||
{
|
||||
require_once __DIR__.'/../fixtures/app/TestKernel.php';
|
||||
|
||||
$kernel = new TestKernel('dev', true);
|
||||
$kernel->boot();
|
||||
$container = $kernel->getContainer();
|
||||
|
||||
/** @var ParserManager $parserManager */
|
||||
$parserManager = $container->get('markdown.parser.parser_manager.public');
|
||||
|
||||
$actual = $parserManager->transform('*hi*');
|
||||
$this->assertEquals("<p><em>hi</em></p>\n", $actual, 'There is a default parser');
|
||||
|
||||
$actual = $parserManager->transform('*hi*', 'light');
|
||||
$this->assertEquals("<p><em>hi</em></p>\n", $actual, 'Specific parsers are registered');
|
||||
}
|
||||
}
|
|
@ -3,8 +3,9 @@
|
|||
namespace Knp\Bundle\MarkdownBundle\Tests;
|
||||
|
||||
use Knp\Bundle\MarkdownBundle\Parser\Preset as Preset;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class PresetTest extends \PHPUnit_Framework_TestCase
|
||||
class PresetTest extends TestCase
|
||||
{
|
||||
public function testMax()
|
||||
{
|
34
tests/fixtures/app/TestKernel.php
vendored
Normal file
34
tests/fixtures/app/TestKernel.php
vendored
Normal file
|
@ -0,0 +1,34 @@
|
|||
<?php
|
||||
|
||||
namespace Knp\Bundle\MarkdownBundle\Tests\fixtures\app;
|
||||
|
||||
use Knp\Bundle\MarkdownBundle\KnpMarkdownBundle;
|
||||
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
|
||||
use Symfony\Component\Config\Loader\LoaderInterface;
|
||||
use Symfony\Component\DependencyInjection\Alias;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Symfony\Component\DependencyInjection\Reference;
|
||||
use Symfony\Component\HttpKernel\Kernel;
|
||||
|
||||
class TestKernel extends Kernel
|
||||
{
|
||||
public function registerBundles(): iterable
|
||||
{
|
||||
return array(
|
||||
new FrameworkBundle(),
|
||||
new KnpMarkdownBundle(),
|
||||
);
|
||||
}
|
||||
|
||||
public function registerContainerConfiguration(LoaderInterface $loader)
|
||||
{
|
||||
$loader->load(function(ContainerBuilder $c) {
|
||||
$c->loadFromExtension('framework', array(
|
||||
'secret' => 'MarkdownTesting'
|
||||
));
|
||||
|
||||
// add a public alias so we can fetch for testing
|
||||
$c->setAlias('markdown.parser.parser_manager.public', new Alias('markdown.parser.parser_manager', true));
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue