mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-24 10:49:00 +02:00
Show markdown on pages.
This commit is contained in:
parent
46959b74ed
commit
7ec406d4a1
9 changed files with 462 additions and 275 deletions
87
src/Services/MarkdownParser.php
Normal file
87
src/Services/MarkdownParser.php
Normal file
|
@ -0,0 +1,87 @@
|
|||
<?php
|
||||
/**
|
||||
*
|
||||
* part-db version 0.1
|
||||
* Copyright (C) 2005 Christoph Lechner
|
||||
* http://www.cl-projects.de/
|
||||
*
|
||||
* part-db version 0.2+
|
||||
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
|
||||
* http://code.google.com/p/part-db/
|
||||
*
|
||||
* Part-DB Version 0.4+
|
||||
* Copyright (C) 2016 - 2019 Jan Böhmer
|
||||
* https://github.com/jbtronics
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* of the License, or (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*
|
||||
*/
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
|
||||
use Symfony\Contracts\Cache\CacheInterface;
|
||||
use Symfony\Contracts\Cache\ItemInterface;
|
||||
|
||||
/**
|
||||
* This class allows you to convert markdown text to HTML.
|
||||
* @package App\Services
|
||||
*/
|
||||
class MarkdownParser
|
||||
{
|
||||
protected $cache;
|
||||
/** @var \Parsedown */
|
||||
protected $parsedown;
|
||||
|
||||
public function __construct(CacheInterface $cache)
|
||||
{
|
||||
$this->cache = $cache;
|
||||
$this->initParsedown();
|
||||
}
|
||||
|
||||
protected function initParsedown()
|
||||
{
|
||||
$this->parsedown = new \Parsedown();
|
||||
$this->parsedown->setSafeMode(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts the given markdown text to HTML.
|
||||
* The result is cached.
|
||||
* @param string $markdown The markdown text that should be parsed to html.
|
||||
* @param bool $inline_mode Only allow inline markdown codes like (*bold* or **italic**), not something like tables
|
||||
* @return string The HTML version of the given text.
|
||||
* @throws \Psr\Cache\InvalidArgumentException
|
||||
*/
|
||||
public function parse(string $markdown, bool $inline_mode = false) : string
|
||||
{
|
||||
//Generate key
|
||||
if ($inline_mode) {
|
||||
$key = 'markdown_i_' . md5($markdown);
|
||||
} else {
|
||||
$key = 'markdown_' . md5($markdown);
|
||||
}
|
||||
return $this->cache->get($key, function (ItemInterface $item) use ($markdown, $inline_mode) {
|
||||
//Expire text after 2 months
|
||||
$item->expiresAfter(311040000);
|
||||
|
||||
if ($inline_mode) {
|
||||
return $this->parsedown->line($markdown);
|
||||
}
|
||||
|
||||
return '<div class="markdown">' . $this->parsedown->text($markdown) . '</div>';
|
||||
});
|
||||
}
|
||||
}
|
|
@ -36,12 +36,13 @@ use App\Entity\PriceInformations\Currency;
|
|||
use App\Services\AmountFormatter;
|
||||
use App\Services\Attachments\AttachmentURLGenerator;
|
||||
use App\Services\EntityURLGenerator;
|
||||
use App\Services\MarkdownParser;
|
||||
use App\Services\MoneyFormatter;
|
||||
use App\Services\SIFormatter;
|
||||
use App\Services\TreeBuilder;
|
||||
use Money\Currencies;
|
||||
use Symfony\Component\Cache\Adapter\AdapterInterface;
|
||||
use Symfony\Component\Serializer\SerializerInterface;
|
||||
use Twig\Cache\CacheInterface;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
use s9e\TextFormatter\Bundles\Forum as TextFormatter;
|
||||
|
@ -51,7 +52,7 @@ use Twig\TwigTest;
|
|||
class AppExtension extends AbstractExtension
|
||||
{
|
||||
protected $entityURLGenerator;
|
||||
protected $cache;
|
||||
protected $markdownParser;
|
||||
protected $serializer;
|
||||
protected $treeBuilder;
|
||||
protected $moneyFormatter;
|
||||
|
@ -59,14 +60,14 @@ class AppExtension extends AbstractExtension
|
|||
protected $amountFormatter;
|
||||
protected $attachmentURLGenerator;
|
||||
|
||||
public function __construct(EntityURLGenerator $entityURLGenerator, AdapterInterface $cache,
|
||||
public function __construct(EntityURLGenerator $entityURLGenerator, MarkdownParser $markdownParser,
|
||||
SerializerInterface $serializer, TreeBuilder $treeBuilder,
|
||||
MoneyFormatter $moneyFormatter,
|
||||
SIFormatter $SIFormatter, AmountFormatter $amountFormatter,
|
||||
AttachmentURLGenerator $attachmentURLGenerator)
|
||||
{
|
||||
$this->entityURLGenerator = $entityURLGenerator;
|
||||
$this->cache = $cache;
|
||||
$this->markdownParser = $markdownParser;
|
||||
$this->serializer = $serializer;
|
||||
$this->treeBuilder = $treeBuilder;
|
||||
$this->moneyFormatter = $moneyFormatter;
|
||||
|
@ -79,7 +80,7 @@ class AppExtension extends AbstractExtension
|
|||
{
|
||||
return [
|
||||
new TwigFilter('entityURL', [$this, 'generateEntityURL']),
|
||||
new TwigFilter('bbCode', [$this, 'parseBBCode'], ['pre_escape' => 'html', 'is_safe' => ['html']]),
|
||||
new TwigFilter('markdown', [$this->markdownParser, 'parse'], ['pre_escape' => 'html', 'is_safe' => ['html']]),
|
||||
new TwigFilter('moneyFormat', [$this, 'formatCurrency']),
|
||||
new TwigFilter('siFormat', [$this, 'siFormat']),
|
||||
new TwigFilter('amountFormat', [$this, 'amountFormat']),
|
||||
|
@ -128,22 +129,6 @@ class AppExtension extends AbstractExtension
|
|||
return $this->entityURLGenerator->getURL($entity, $method);
|
||||
}
|
||||
|
||||
public function parseBBCode(string $bbcode): string
|
||||
{
|
||||
if ('' === $bbcode) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$item = $this->cache->getItem('bbcode_'.md5($bbcode));
|
||||
if (!$item->isHit()) {
|
||||
$xml = TextFormatter::parse($bbcode);
|
||||
$item->set(TextFormatter::render($xml));
|
||||
$this->cache->save($item);
|
||||
}
|
||||
|
||||
return $item->get();
|
||||
}
|
||||
|
||||
public function formatCurrency($amount, Currency $currency = null, int $decimals = 5)
|
||||
{
|
||||
return $this->moneyFormatter->format($amount, $currency, $decimals);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue