Initial commit: apps.supersam24.ru
This commit is contained in:
commit
e955e1da36
6566 files changed
+1271095
No files matched your search
+27
@@ -0,0 +1,27 @@
|
||||
language: php
|
||||
sudo: false
|
||||
|
||||
php:
|
||||
- 5.4
|
||||
- 5.5
|
||||
- 5.6
|
||||
- 7
|
||||
|
||||
env:
|
||||
global:
|
||||
# travis encrypt CODECLIMATE_REPO_TOKEN=<https://codeclimate.com/repos/53e0fcc8e30ba02d78013d4c/coverage_setup>
|
||||
- secure: "n4I9GwRCxi9UGjhDGe79SV6Nr/fRKpA8N+qqqbkveJv01Z4IRcAsRBRxKrSAX8zJUoFhlJK73VeRnBfdSBddsL3RqgDMbHilZFy6yC7Dj/vVyNe90RPWxc5J7w3sidT2g1OsgWueiUis8y3dGOQd2sewfcb2pZa29/eQusBV5i0="
|
||||
|
||||
install:
|
||||
- composer install
|
||||
|
||||
before_script:
|
||||
- mkdir -p build/logs
|
||||
|
||||
script:
|
||||
- vendor/bin/phpunit --coverage-clover build/logs/clover.xml
|
||||
|
||||
after_script:
|
||||
- php vendor/bin/coveralls -v
|
||||
- ./vendor/bin/test-reporter --stdout > codeclimate.json
|
||||
- "curl -X POST -d @codeclimate.json -H 'Content-Type: application/json' -H 'User-Agent: Code Climate (PHP Test Reporter v1.0.1-dev)' https://codeclimate.com/test_reports"
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
CHANGELOG
|
||||
=========
|
||||
|
||||
2.0.0
|
||||
-----
|
||||
|
||||
- **BC**: Require Twig v1.12, in order to replace deprecated Twig_Filter_Method with Twig_SimpleFilter
|
||||
- **Added**: Add support for ParsedownEnging
|
||||
|
||||
1.2.0
|
||||
-----
|
||||
|
||||
- **Added**: Add support for GitHub's markdown engine
|
||||
|
||||
1.1.0
|
||||
-----
|
||||
|
||||
- **Added**: Add support for PHP League CommonMark engine
|
||||
|
||||
1.0.0
|
||||
-----
|
||||
|
||||
- **BC**: Remove deprecated dflydev-markdown parser
|
||||
+171
@@ -0,0 +1,171 @@
|
||||
Twig Markdown Extension
|
||||
=======================
|
||||
|
||||
[](http://travis-ci.org/aptoma/twig-markdown)
|
||||
[](https://coveralls.io/r/aptoma/twig-markdown)
|
||||
|
||||
Twig Markdown extension provides a new filter and a tag to allow parsing of
|
||||
content as Markdown in [Twig][1] templates.
|
||||
|
||||
This extension could be integrated with several Markdown parser as it provides an interface, which allows you to customize your Markdown parser.
|
||||
|
||||
### Supported parsers
|
||||
|
||||
* [michelf/php-markdown](https://github.com/michelf/php-markdown) (+ MarkdownExtra)
|
||||
* [league/commonmark](http://commonmark.thephpleague.com/)
|
||||
* [KnpLabs/php-github-api](https://github.com/KnpLabs/php-github-api)
|
||||
* [erusev/parsedown](https://github.com/erusev/parsedown)
|
||||
|
||||
## Features
|
||||
|
||||
* Filter support `{{ "# Heading Level 1"|markdown }}`
|
||||
* Tag support `{% markdown %}{% endmarkdown %}`
|
||||
|
||||
When used as a tag, the indentation level of the first line sets the default indentation level for the rest of the tag content.
|
||||
From this indentation level, all same indentation or outdented levels text will be transformed as regular text.
|
||||
|
||||
This feature allows you to write your Markdown content at any indentation level without caring of Markdown internal transformation:
|
||||
|
||||
```php
|
||||
<div>
|
||||
<h1 class="someClass">{{ title }}</h1>
|
||||
|
||||
{% markdown %}
|
||||
This is a list that is indented to match the context around the markdown tag:
|
||||
|
||||
* List item 1
|
||||
* List item 2
|
||||
* Sub List Item
|
||||
* Sub Sub List Item
|
||||
|
||||
The following block will be transformed as code, as it is indented more than the
|
||||
surrounding content:
|
||||
|
||||
$code = "good";
|
||||
|
||||
{% endmarkdown %}
|
||||
|
||||
</div>
|
||||
```
|
||||
|
||||
## Installation
|
||||
|
||||
Run the composer command to install the latest stable version:
|
||||
|
||||
```bash
|
||||
composer require aptoma/twig-markdown
|
||||
```
|
||||
|
||||
Or update your `composer.json`:
|
||||
|
||||
```json
|
||||
{
|
||||
"require": {
|
||||
"aptoma/twig-markdown": "~1.1"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
You can choose to provide your own Markdown engine, although we recommend
|
||||
using [michelf/php-markdown](https://github.com/michelf/php-markdown):
|
||||
|
||||
```bash
|
||||
composer require michelf/php-markdown ~1.3
|
||||
```
|
||||
|
||||
```json
|
||||
{
|
||||
"require": {
|
||||
"michelf/php-markdown": "~1.3"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
You may also use the [PHP League CommonMark engine](http://commonmark.thephpleague.com/):
|
||||
|
||||
```bash
|
||||
composer require league/commonmark ~0.5
|
||||
```
|
||||
|
||||
```json
|
||||
{
|
||||
"require": {
|
||||
"league/commonmark": "~0.5"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Twig Extension
|
||||
|
||||
The Twig extension provides the `markdown` tag and filter support.
|
||||
|
||||
Assuming that you are using [composer](http://getcomposer.org) autoloading,
|
||||
add the extension to the Twig environment:
|
||||
|
||||
```php
|
||||
|
||||
use Aptoma\Twig\Extension\MarkdownExtension;
|
||||
use Aptoma\Twig\Extension\MarkdownEngine;
|
||||
|
||||
$engine = new MarkdownEngine\MichelfMarkdownEngine();
|
||||
|
||||
$twig->addExtension(new MarkdownExtension($engine));
|
||||
```
|
||||
|
||||
### Twig Token Parser
|
||||
|
||||
The Twig token parser provides the `markdown` tag only!
|
||||
|
||||
```php
|
||||
use Aptoma\Twig\Extension\MarkdownEngine;
|
||||
use Aptoma\Twig\TokenParser\MarkdownTokenParser;
|
||||
|
||||
$engine = new MarkdownEngine\MichelfMarkdownEngine();
|
||||
|
||||
$twig->addTokenParser(new MarkdownTokenParser($engine));
|
||||
```
|
||||
|
||||
### GitHub Markdown Engine
|
||||
|
||||
`MarkdownEngine\GitHubMarkdownEngine` provides an interface to GitHub's markdown engine using their public API via [`KnpLabs\php-github-api`][2]. To reduce API calls, rendered documents are hashed and cached in the filesystem. You can pass a GitHub repository and the path to be used for caching to the constructor:
|
||||
|
||||
```php
|
||||
use Aptoma\Twig\Extension\MarkdownEngine;
|
||||
|
||||
$engine = new MarkdownEngine\GitHubMarkdownEngine(
|
||||
'aptoma/twig-markdown', // The GitHub repository to use as a context
|
||||
true, // Whether to use GitHub's Flavored Markdown (GFM)
|
||||
'/tmp/markdown-cache', // Path on filesystem to store rendered documents
|
||||
);
|
||||
```
|
||||
|
||||
In order to authenticate the API client (for instance), it's possible to pass an own instance of `\GitHub\Client` instead of letting the engine create one itself:
|
||||
|
||||
```php
|
||||
$client = new \GitHub\Client;
|
||||
$client->authenticate('GITHUB_CLIENT_ID', 'GITHUB_CLIENT_SECRET', \Github\Client::AUTH_URL_CLIENT_ID);
|
||||
|
||||
$engine = new MarkdownEngine\GitHubMarkdownEngine('aptoma/twig-markdown', true, '/tmp/markdown-cache', $client);
|
||||
```
|
||||
|
||||
### Using a different Markdown parser engine
|
||||
|
||||
If you want to use a different Markdown parser, you need to create an adapter
|
||||
that implements `Aptoma\Twig\Extension\MarkdownEngineInterface.php`. Have
|
||||
a look at `Aptoma\Twig\Extension\MarkdownEngine\MichelfMarkdownEngine` for an
|
||||
example.
|
||||
|
||||
## Tests
|
||||
|
||||
The test suite uses PHPUnit:
|
||||
|
||||
$ phpunit
|
||||
|
||||
## License
|
||||
|
||||
Twig Markdown Extension is licensed under the MIT license.
|
||||
|
||||
[1]: http://twig.sensiolabs.org
|
||||
[2]: https://github.com/knplabs/php-github-api
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
{
|
||||
"name": "aptoma/twig-markdown",
|
||||
"description": "Twig extension to work with Markdown content",
|
||||
"keywords": ["twig", "markdown"],
|
||||
"license": "MIT",
|
||||
"authors": [
|
||||
{
|
||||
"name": "Gunnar Lium",
|
||||
"email": "gunnar@aptoma.com"
|
||||
},
|
||||
{
|
||||
"name": "Joris Berthelot",
|
||||
"email": "joris@berthelot.tel"
|
||||
}
|
||||
],
|
||||
"require": {
|
||||
"twig/twig": "~1.12"
|
||||
},
|
||||
"require-dev": {
|
||||
"phpunit/phpunit": "~4.0",
|
||||
"satooshi/php-coveralls": "~0.6",
|
||||
"codeclimate/php-test-reporter": "dev-master",
|
||||
"michelf/php-markdown": "~1",
|
||||
"league/commonmark": "~0.5",
|
||||
"knplabs/github-api": "~1.2",
|
||||
"erusev/parsedown": "^1.6"
|
||||
},
|
||||
"suggest": {
|
||||
"michelf/php-markdown": "Original Markdown engine with MarkdownExtra.",
|
||||
"knplabs/github-api": "Needed for using GitHub's Markdown engine provided through their API."
|
||||
},
|
||||
"autoload": {
|
||||
"psr-0": { "Aptoma": "src/" }
|
||||
}
|
||||
}
|
||||
+1772
File diff suppressed because it is too large.
Load diff
+18
@@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit backupGlobals="false"
|
||||
backupStaticAttributes="false"
|
||||
colors="true"
|
||||
convertErrorsToExceptions="true"
|
||||
convertNoticesToExceptions="true"
|
||||
convertWarningsToExceptions="true"
|
||||
processIsolation="false"
|
||||
stopOnFailure="false"
|
||||
syntaxCheck="false"
|
||||
bootstrap="vendor/autoload.php"
|
||||
>
|
||||
<testsuites>
|
||||
<testsuite name="Aptoma Markdown Test Suite">
|
||||
<directory>./tests/</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
</phpunit>
|
||||
Vendored
+76
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
namespace Aptoma\Twig\Extension\MarkdownEngine;
|
||||
|
||||
use Aptoma\Twig\Extension\MarkdownEngineInterface;
|
||||
|
||||
/**
|
||||
* GithubMarkdownEngine.php
|
||||
*
|
||||
* Maps GitHub's Markdown engine API to Aptoma\Twig Markdown Extension using
|
||||
* KnpLabs\php-github-api.
|
||||
*
|
||||
* @author Lukas W <lukaswhl@gmail.com>
|
||||
*/
|
||||
class GitHubMarkdownEngine implements MarkdownEngineInterface
|
||||
{
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $contextRepo The repository context. Pass a GitHub repo
|
||||
* such as 'aptoma/twig-markdown' to render e.g. issues #23 in the
|
||||
* context of the repo.
|
||||
* @param bool $gfm Whether to use GitHub's Flavored Markdown or the
|
||||
* standard markdown. Default is true.
|
||||
* @param string $cacheDir Location on disk where rendered documents should
|
||||
* be stored.
|
||||
* @param \Github\Client $client Client object to use. A new Github\Client()
|
||||
* object is constructed automatically if $client is null.
|
||||
*/
|
||||
public function __construct($contextRepo = null, $gfm = true, $cacheDir = '/tmp/github-markdown-cache', \GitHub\Client $client=null)
|
||||
{
|
||||
$this->repo = $contextRepo;
|
||||
$this->mode = $gfm ? 'gfm' : 'markdown';
|
||||
$this->cacheDir = rtrim($cacheDir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
|
||||
if (!is_dir($this->cacheDir)) {
|
||||
@mkdir($this->cacheDir, 0777, true);
|
||||
}
|
||||
|
||||
if ($client === null) {
|
||||
$client = new \Github\Client();
|
||||
}
|
||||
$this->api = $client->api('markdown');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function transform($content)
|
||||
{
|
||||
$cacheFile = $this->getCachePath($content);
|
||||
if (file_exists($cacheFile)) {
|
||||
return file_get_contents($cacheFile);;
|
||||
}
|
||||
|
||||
$response = $this->api->render($content, $this->mode, $this->repo);
|
||||
file_put_contents($cacheFile, $response);
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'KnpLabs\php-github-api';
|
||||
}
|
||||
|
||||
private function getCachePath($content)
|
||||
{
|
||||
return $this->cacheDir . md5($content) . '_' . $this->mode. '_' . str_replace('/', '.', $this->repo);
|
||||
}
|
||||
|
||||
private $api;
|
||||
private $cacheDir;
|
||||
private $repo;
|
||||
private $mode;
|
||||
}
|
||||
Vendored
+32
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Aptoma\Twig\Extension\MarkdownEngine;
|
||||
|
||||
use Aptoma\Twig\Extension\MarkdownEngineInterface;
|
||||
use Michelf\MarkdownExtra;
|
||||
|
||||
/**
|
||||
* MichelfMarkdownEngine.php
|
||||
*
|
||||
* Maps Michelf\MarkdownExtra to Aptoma\Twig Markdown Extension
|
||||
*
|
||||
* @author Joris Berthelot <joris@berthelot.tel>
|
||||
*/
|
||||
class MichelfMarkdownEngine implements MarkdownEngineInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function transform($content)
|
||||
{
|
||||
return MarkdownExtra::defaultTransform($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'Michelf\Markdown';
|
||||
}
|
||||
}
|
||||
Vendored
+49
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace Aptoma\Twig\Extension\MarkdownEngine;
|
||||
|
||||
use Aptoma\Twig\Extension\MarkdownEngineInterface;
|
||||
use League\CommonMark\CommonMarkConverter;
|
||||
|
||||
/**
|
||||
* PHPLeagueCommonMarkEngine.php
|
||||
*
|
||||
* Maps League\CommonMark\CommonMarkConverter to Aptoma\Twig Markdown Extension
|
||||
*
|
||||
* @author Casey McLaughlin <caseyamcl@gmail.com>
|
||||
*/
|
||||
class PHPLeagueCommonMarkEngine implements MarkdownEngineInterface
|
||||
{
|
||||
/**
|
||||
* @var \League\CommonMark\CommonMarkConverter
|
||||
*/
|
||||
private $converter;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Accepts CommonMarkConverter or creates one automatically
|
||||
*
|
||||
* @param \League\CommonMark\CommonMarkConverter $converter
|
||||
*/
|
||||
public function __construct(CommonMarkConverter $converter = null)
|
||||
{
|
||||
$this->converter = $converter ?: new CommonMarkConverter();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function transform($content)
|
||||
{
|
||||
return $this->converter->convertToHtml($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'League\CommonMark';
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
<?php
|
||||
|
||||
namespace Aptoma\Twig\Extension\MarkdownEngine;
|
||||
|
||||
use Aptoma\Twig\Extension\MarkdownEngineInterface;
|
||||
use Parsedown;
|
||||
|
||||
/**
|
||||
* ParsedownEngine.php
|
||||
*
|
||||
* Maps erusev/parsedown to Aptoma\Twig Markdown Extension
|
||||
*
|
||||
* @author Sébastien Lourseau <https://github.com/SebLours>
|
||||
*/
|
||||
class ParsedownEngine implements MarkdownEngineInterface
|
||||
{
|
||||
/**
|
||||
* @var Parsedown
|
||||
*/
|
||||
protected $engine;
|
||||
|
||||
/**
|
||||
* @param string|null $instanceName
|
||||
*/
|
||||
public function __construct($instanceName = null)
|
||||
{
|
||||
$this->engine = Parsedown::instance($instanceName);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function transform($content)
|
||||
{
|
||||
return $this->engine->parse($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'erusev/parsedown';
|
||||
}
|
||||
}
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace Aptoma\Twig\Extension;
|
||||
|
||||
/**
|
||||
* MarkdownEngineInterface.php
|
||||
*
|
||||
* Provide software interface to maps various Markdown engines
|
||||
*
|
||||
* @author Joris Berthelot <joris@berthelot.tel>
|
||||
*/
|
||||
interface MarkdownEngineInterface
|
||||
{
|
||||
/**
|
||||
* Transforms the given markdown data in HTML
|
||||
*
|
||||
* @param $content Markdown data
|
||||
* @return string
|
||||
*/
|
||||
public function transform($content);
|
||||
|
||||
/**
|
||||
* Return Markdown engine vendor ID
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName();
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
<?php
|
||||
|
||||
namespace Aptoma\Twig\Extension;
|
||||
|
||||
use Aptoma\Twig\TokenParser\MarkdownTokenParser;
|
||||
|
||||
/**
|
||||
* MarkdownExtension provides support for Markdown.
|
||||
*
|
||||
* @author Gunnar Lium <gunnar@aptoma.com>
|
||||
* @author Joris Berthelot <joris@berthelot.tel>
|
||||
*/
|
||||
class MarkdownExtension extends \Twig_Extension
|
||||
{
|
||||
|
||||
/**
|
||||
* @var MarkdownEngineInterface $markdownEngine
|
||||
*/
|
||||
private $markdownEngine;
|
||||
|
||||
/**
|
||||
* @param MarkdownEngineInterface $markdownEngine The Markdown parser engine
|
||||
*/
|
||||
public function __construct(MarkdownEngineInterface $markdownEngine)
|
||||
{
|
||||
$this->markdownEngine = $markdownEngine;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFilters()
|
||||
{
|
||||
return array(
|
||||
new \Twig_SimpleFilter(
|
||||
'markdown',
|
||||
array($this, 'parseMarkdown'),
|
||||
array('is_safe' => array('html'))
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform Markdown content to HTML
|
||||
*
|
||||
* @param $content The Markdown content to be transformed
|
||||
* @return string The result of the Markdown engine transformation
|
||||
*/
|
||||
public function parseMarkdown($content)
|
||||
{
|
||||
return $this->markdownEngine->transform($content);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getTokenParsers()
|
||||
{
|
||||
return array(new MarkdownTokenParser($this->markdownEngine));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getName()
|
||||
{
|
||||
return 'markdown';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Aptoma\Twig\Node;
|
||||
|
||||
/**
|
||||
* Represents a markdown node.
|
||||
*
|
||||
* It parses content as Markdown.
|
||||
*
|
||||
* @author Gunnar Lium <gunnar@aptoma.com>
|
||||
* @author Joris Berthelot <joris@berthelot.tel>
|
||||
*/
|
||||
class MarkdownNode extends \Twig_Node
|
||||
{
|
||||
public function __construct(\Twig_NodeInterface $body, $lineno, $tag = 'markdown')
|
||||
{
|
||||
parent::__construct(array('body' => $body), array(), $lineno, $tag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compiles the node to PHP.
|
||||
*
|
||||
* @param \Twig_Compiler A Twig_Compiler instance
|
||||
*/
|
||||
public function compile(\Twig_Compiler $compiler)
|
||||
{
|
||||
$compiler
|
||||
->addDebugInfo($this)
|
||||
->write('ob_start();' . PHP_EOL)
|
||||
->subcompile($this->getNode('body'))
|
||||
->write('$content = ob_get_clean();' . PHP_EOL)
|
||||
->write('preg_match("/^\s*/", $content, $matches);' . PHP_EOL)
|
||||
->write('$lines = explode("\n", $content);' . PHP_EOL)
|
||||
->write('$content = preg_replace(\'/^\' . $matches[0]. \'/\', "", $lines);' . PHP_EOL)
|
||||
->write('$content = join("\n", $content);' . PHP_EOL)
|
||||
->write('echo $this->env->getTokenParsers()->getTokenParser(\'markdown\')->getEngine()->transform($content);' . PHP_EOL);
|
||||
}
|
||||
}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
<?php
|
||||
|
||||
namespace Aptoma\Twig\TokenParser;
|
||||
|
||||
use Aptoma\Twig\Node\MarkdownNode;
|
||||
use Aptoma\Twig\Extension\MarkdownEngineInterface;
|
||||
|
||||
/**
|
||||
* @author Gunnar Lium <gunnar@aptoma.com>
|
||||
* @author Joris Berthelot <joris@berthelot.tel>
|
||||
*/
|
||||
class MarkdownTokenParser extends \Twig_TokenParser
|
||||
{
|
||||
/**
|
||||
* @var The Markdown engine
|
||||
*/
|
||||
protected $markdownEngine;
|
||||
|
||||
/**
|
||||
* @param MarkdownEngineInterface $markdownEngine The Markdown parser engine
|
||||
*/
|
||||
public function __construct(MarkdownEngineInterface $markdownEngine)
|
||||
{
|
||||
$this->markdownEngine = $markdownEngine;
|
||||
}
|
||||
|
||||
/**
|
||||
* Markdown parser engine getter
|
||||
*
|
||||
* @return MarkdownEngineInterface
|
||||
*/
|
||||
public function getEngine()
|
||||
{
|
||||
return $this->markdownEngine;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses a token and returns a node.
|
||||
*
|
||||
* @param \Twig_Token $token A \Twig_Token instance
|
||||
*
|
||||
* @throws \Twig_Error_Syntax
|
||||
* @return \Twig_NodeInterface A \Twig_NodeInterface instance
|
||||
*/
|
||||
public function parse(\Twig_Token $token)
|
||||
{
|
||||
$lineno = $token->getLine();
|
||||
|
||||
$this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
|
||||
$body = $this->parser->subparse(array($this, 'decideMarkdownEnd'), true);
|
||||
$this->parser->getStream()->expect(\Twig_Token::BLOCK_END_TYPE);
|
||||
|
||||
return new MarkdownNode($body, $lineno, $this->getTag());
|
||||
}
|
||||
|
||||
/**
|
||||
* Decide if current token marks end of Markdown block.
|
||||
*
|
||||
* @param \Twig_Token $token
|
||||
* @return bool
|
||||
*/
|
||||
public function decideMarkdownEnd(\Twig_Token $token)
|
||||
{
|
||||
return $token->test('endmarkdown');
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the tag name associated with this token parser.
|
||||
*
|
||||
* @return string The tag name
|
||||
*/
|
||||
public function getTag()
|
||||
{
|
||||
return 'markdown';
|
||||
}
|
||||
}
|
||||
Vendored
+31
@@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Aptoma\Twig\Extension\MarkdownEngine;
|
||||
|
||||
use Aptoma\Twig\Extension\MarkdownExtensionTest;
|
||||
|
||||
require_once(__DIR__ . '/../MarkdownExtensionTest.php');
|
||||
|
||||
/**
|
||||
* Class GitHubMarkdownEngineTest
|
||||
*
|
||||
* @author Lukas W <lukaswhl@gmail.com>
|
||||
*/
|
||||
class GitHubMarkdownEngineTest extends MarkdownExtensionTest
|
||||
{
|
||||
public function getParseMarkdownTests()
|
||||
{
|
||||
return array(
|
||||
array('{{ "# Main Title"|markdown }}', '<h1>Main Title</h1>'),
|
||||
array('{{ content|markdown }}', '<h1>Main Title</h1>', array('content' => '# Main Title')),
|
||||
// Check if GFM is working
|
||||
array('{{ "@aptoma"|markdown }}',
|
||||
'<p><a href="https://github.com/aptoma" class="user-mention">@aptoma</a></p>'),
|
||||
);
|
||||
}
|
||||
|
||||
protected function getEngine()
|
||||
{
|
||||
return new GitHubMarkdownEngine();
|
||||
}
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Aptoma\Twig\Extension\MarkdownEngine;
|
||||
|
||||
use Aptoma\Twig\Extension\MarkdownExtensionTest;
|
||||
|
||||
// Require parent class if not autoloaded
|
||||
if (!class_exists('\Aptoma\Twig\Extension\MarkdownExtensionTest')) {
|
||||
require_once(__DIR__ . '/../MarkdownExtensionTest.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* Class PHPLeagueCommonMarkEngineTest
|
||||
*
|
||||
* @author Casey McLaughlin <caseyamcl@gmail.com>
|
||||
*/
|
||||
class PHPLeagueCommonMarkEngineTest extends MarkdownExtensionTest
|
||||
{
|
||||
protected function getEngine()
|
||||
{
|
||||
return new PHPLeagueCommonMarkEngine();
|
||||
}
|
||||
}
|
||||
Vendored
+32
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace Aptoma\Twig\Extension\MarkdownEngine;
|
||||
|
||||
use Aptoma\Twig\Extension\MarkdownExtensionTest;
|
||||
|
||||
// Require parent class if not autoloaded
|
||||
if (!class_exists('\Aptoma\Twig\Extension\MarkdownExtensionTest')) {
|
||||
require_once(__DIR__ . '/../MarkdownExtensionTest.php');
|
||||
}
|
||||
|
||||
/**
|
||||
* Class ParsedownEngineTest
|
||||
*
|
||||
* @author Sébastien Lourseau <https://github.com/SebLours>
|
||||
*/
|
||||
class ParsedownEngineTest extends MarkdownExtensionTest
|
||||
{
|
||||
public function getParseMarkdownTests()
|
||||
{
|
||||
return array(
|
||||
array('{{ "# Main Title"|markdown }}', '<h1>Main Title</h1>'),
|
||||
array('{{ content|markdown }}', '<h1>Main Title</h1>', array('content' => '# Main Title')),
|
||||
array('{% markdown %}{{ content }}{% endmarkdown %}', '<h1>Main Title</h1>', array('content' => '# Main Title'))
|
||||
);
|
||||
}
|
||||
|
||||
protected function getEngine()
|
||||
{
|
||||
return new ParsedownEngine();
|
||||
}
|
||||
}
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Aptoma\Twig\Extension;
|
||||
|
||||
use Aptoma\Twig\Extension\MarkdownEngine\MichelfMarkdownEngine;
|
||||
|
||||
/**
|
||||
* @author Gunnar Liun <gunnar@aptoma.com>
|
||||
*/
|
||||
class MarkdownExtensionTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider getParseMarkdownTests
|
||||
*/
|
||||
public function testParseMarkdown($template, $expected, $context = array())
|
||||
{
|
||||
$this->assertEquals($expected, $this->getTemplate($template)->render($context));
|
||||
}
|
||||
|
||||
public function getParseMarkdownTests()
|
||||
{
|
||||
return array(
|
||||
array('{{ "# Main Title"|markdown }}', '<h1>Main Title</h1>' . PHP_EOL),
|
||||
array('{{ content|markdown }}', '<h1>Main Title</h1>' . PHP_EOL, array('content' => '# Main Title'))
|
||||
);
|
||||
}
|
||||
|
||||
protected function getEngine()
|
||||
{
|
||||
return new MichelfMarkdownEngine();
|
||||
}
|
||||
|
||||
protected function getTemplate($template)
|
||||
{
|
||||
$loader = new \Twig_Loader_Array(array('index' => $template));
|
||||
$twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
|
||||
$twig->addExtension(new MarkdownExtension($this->getEngine()));
|
||||
|
||||
return $twig->loadTemplate('index');
|
||||
}
|
||||
}
|
||||
+109
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
namespace Aptoma\Twig\TokenParser;
|
||||
|
||||
use Aptoma\Twig\Extension\MarkdownEngine\MichelfMarkdownEngine;
|
||||
use Aptoma\Twig\Node\MarkdownNode;
|
||||
|
||||
/**
|
||||
* @author Gunnar Lium <gunnar@aptoma.com>
|
||||
*/
|
||||
class MarkdownTokenParserTest extends \Twig_Test_NodeTestCase
|
||||
{
|
||||
public function testConstructor()
|
||||
{
|
||||
$body = new \Twig_Node(array(new \Twig_Node_Text("#Title\n\nparagraph\n", 1)));
|
||||
$node = new MarkdownNode($body, 1);
|
||||
|
||||
$this->assertEquals($body, $node->getNode('body'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the generated code actually do what we expect
|
||||
*
|
||||
* The contents of this test is the same that we write in the compile method.
|
||||
* This requires manual synchronization, which we should probably not rely on.
|
||||
*/
|
||||
public function testMarkdownPrepareBehavior()
|
||||
{
|
||||
$body = " #Title\n\n paragraph\n\n code";
|
||||
$bodyPrepared = "#Title\n\nparagraph\n\n code";
|
||||
|
||||
ob_start();
|
||||
echo $body;
|
||||
$content = ob_get_clean();
|
||||
preg_match("/^\s*/", $content, $matches);
|
||||
$lines = explode("\n", $content);
|
||||
$content = preg_replace('/^' . $matches[0]. '/', "", $lines);
|
||||
$content = join("\n", $content);
|
||||
|
||||
// Assert prepared content looks right
|
||||
$this->assertEquals($bodyPrepared, $content);
|
||||
|
||||
// Assert Markdown output
|
||||
$expectedOutput = "<h1>Title</h1>\n\n<p>paragraph</p>\n\n<pre><code>code\n</code></pre>\n";
|
||||
$this->assertEquals($expectedOutput, $this->getEngine()->transform($content));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that the generated code looks as expected
|
||||
*
|
||||
* @dataProvider getTests
|
||||
*/
|
||||
public function testCompile($node, $source, $environment = null)
|
||||
{
|
||||
parent::testCompile($node, $source, $environment);
|
||||
}
|
||||
|
||||
protected function getEngine()
|
||||
{
|
||||
return new MichelfMarkdownEngine();
|
||||
}
|
||||
|
||||
public function getTests()
|
||||
{
|
||||
$tests = array();
|
||||
|
||||
$body = new \Twig_Node(array(new \Twig_Node_Text("#Title\n\nparagraph\n", 1)));
|
||||
$node = new MarkdownNode($body, 1);
|
||||
|
||||
$tests['simple text'] = array($node, <<<EOF
|
||||
// line 1
|
||||
ob_start();
|
||||
echo "#Title
|
||||
|
||||
paragraph
|
||||
";
|
||||
\$content = ob_get_clean();
|
||||
preg_match("/^\s*/", \$content, \$matches);
|
||||
\$lines = explode("\\n", \$content);
|
||||
\$content = preg_replace('/^' . \$matches[0]. '/', "", \$lines);
|
||||
\$content = join("\\n", \$content);
|
||||
echo \$this->env->getTokenParsers()->getTokenParser('markdown')->getEngine()->transform(\$content);
|
||||
EOF
|
||||
);
|
||||
|
||||
$body = new \Twig_Node(array(new \Twig_Node_Text(" #Title\n\n paragraph\n\n code\n", 1)));
|
||||
$node = new MarkdownNode($body, 1);
|
||||
|
||||
$tests['text with leading indent'] = array($node, <<<EOF
|
||||
// line 1
|
||||
ob_start();
|
||||
echo " #Title
|
||||
|
||||
paragraph
|
||||
|
||||
code
|
||||
";
|
||||
\$content = ob_get_clean();
|
||||
preg_match("/^\s*/", \$content, \$matches);
|
||||
\$lines = explode("\\n", \$content);
|
||||
\$content = preg_replace('/^' . \$matches[0]. '/', "", \$lines);
|
||||
\$content = join("\\n", \$content);
|
||||
echo \$this->env->getTokenParsers()->getTokenParser('markdown')->getEngine()->transform(\$content);
|
||||
EOF
|
||||
);
|
||||
|
||||
return $tests;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user