1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
* part-db version 0.1 |
5
|
|
|
* Copyright (C) 2005 Christoph Lechner |
6
|
|
|
* http://www.cl-projects.de/ |
7
|
|
|
* |
8
|
|
|
* part-db version 0.2+ |
9
|
|
|
* Copyright (C) 2009 K. Jacobs and others (see authors.php) |
10
|
|
|
* http://code.google.com/p/part-db/ |
11
|
|
|
* |
12
|
|
|
* Part-DB Version 0.4+ |
13
|
|
|
* Copyright (C) 2016 - 2019 Jan Böhmer |
14
|
|
|
* https://github.com/jbtronics |
15
|
|
|
* |
16
|
|
|
* This program is free software; you can redistribute it and/or |
17
|
|
|
* modify it under the terms of the GNU General Public License |
18
|
|
|
* as published by the Free Software Foundation; either version 2 |
19
|
|
|
* of the License, or (at your option) any later version. |
20
|
|
|
* |
21
|
|
|
* This program is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* You should have received a copy of the GNU General Public License |
27
|
|
|
* along with this program; if not, write to the Free Software |
28
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
29
|
|
|
* |
30
|
|
|
*/ |
31
|
|
|
|
32
|
|
|
namespace App\Services; |
33
|
|
|
|
34
|
|
|
|
35
|
|
|
use Symfony\Contracts\Cache\CacheInterface; |
36
|
|
|
use Symfony\Contracts\Cache\ItemInterface; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* This class allows you to convert markdown text to HTML. |
40
|
|
|
* @package App\Services |
41
|
|
|
*/ |
42
|
|
|
class MarkdownParser |
43
|
|
|
{ |
44
|
|
|
protected $cache; |
45
|
|
|
/** @var \Parsedown */ |
46
|
|
|
protected $parsedown; |
47
|
|
|
|
48
|
|
|
public function __construct(CacheInterface $cache) |
49
|
|
|
{ |
50
|
|
|
$this->cache = $cache; |
51
|
|
|
$this->initParsedown(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
protected function initParsedown() |
55
|
|
|
{ |
56
|
|
|
$this->parsedown = new \Parsedown(); |
57
|
|
|
$this->parsedown->setSafeMode(true); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Converts the given markdown text to HTML. |
62
|
|
|
* The result is cached. |
63
|
|
|
* @param string $markdown The markdown text that should be parsed to html. |
64
|
|
|
* @param bool $inline_mode Only allow inline markdown codes like (*bold* or **italic**), not something like tables |
65
|
|
|
* @return string The HTML version of the given text. |
66
|
|
|
* @throws \Psr\Cache\InvalidArgumentException |
67
|
|
|
*/ |
68
|
|
|
public function parse(string $markdown, bool $inline_mode = false) : string |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
return sprintf( |
71
|
|
|
'<div class="markdown" data-markdown="%s">Markdown loading...</div>', |
72
|
|
|
htmlspecialchars($markdown) |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
//Generate key |
76
|
|
|
/*if ($inline_mode) { |
77
|
|
|
$key = 'markdown_i_' . md5($markdown); |
78
|
|
|
} else { |
79
|
|
|
$key = 'markdown_' . md5($markdown); |
80
|
|
|
} |
81
|
|
|
return $this->cache->get($key, function (ItemInterface $item) use ($markdown, $inline_mode) { |
82
|
|
|
//Expire text after 2 months |
83
|
|
|
$item->expiresAfter(311040000); |
84
|
|
|
|
85
|
|
|
if ($inline_mode) { |
86
|
|
|
return $this->parsedown->line($markdown); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return '<div class="markdown">' . $this->parsedown->text($markdown) . '</div>'; |
90
|
|
|
});*/ |
91
|
|
|
} |
92
|
|
|
} |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.