|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* balloon |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copryright (c) 2012-2019 gyselroth GmbH (https://gyselroth.com) |
|
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Balloon\App\Markdown\Converter\Adapter; |
|
13
|
|
|
|
|
14
|
|
|
use Balloon\App\Office\Converter\Adapter\Office; |
|
15
|
|
|
use Balloon\Converter\Exception; |
|
16
|
|
|
use Balloon\Filesystem\Node\File; |
|
17
|
|
|
use GuzzleHttp\ClientInterface as GuzzleHttpClientInterface; |
|
18
|
|
|
use Parsedown; |
|
19
|
|
|
use Psr\Log\LoggerInterface; |
|
20
|
|
|
|
|
21
|
|
|
class Markdown extends Office |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Parsedown. |
|
25
|
|
|
* |
|
26
|
|
|
* @var \Parsedown |
|
27
|
|
|
*/ |
|
28
|
|
|
protected $parser; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Formats. |
|
32
|
|
|
* |
|
33
|
|
|
* @var array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected $source_formats = [ |
|
36
|
|
|
'markdown' => 'text/markdown', |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Initialize. |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(Parsedown $parser, GuzzleHttpClientInterface $client, LoggerInterface $logger, array $config = []) |
|
43
|
|
|
{ |
|
44
|
|
|
parent::__construct($client, $logger, $config); |
|
45
|
|
|
$this->parser = $parser; |
|
46
|
|
|
$this->parser->setSafeMode(true); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
*/ |
|
52
|
|
|
public function match(File $file): bool |
|
53
|
|
|
{ |
|
54
|
|
|
foreach ($this->source_formats as $format => $mimetype) { |
|
55
|
|
|
if ($file->getContentType() === $mimetype) { |
|
56
|
|
|
return true; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* {@inheritdoc} |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getSupportedFormats(File $file): array |
|
67
|
|
|
{ |
|
68
|
|
View Code Duplication |
if (in_array($file->getContentType(), $this->locked_formats, true)) { |
|
|
|
|
|
|
69
|
|
|
return [ |
|
70
|
|
|
array_search($file->getContentType(), $this->locked_formats, true), |
|
71
|
|
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return array_keys($this->formats['text']); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* {@inheritdoc} |
|
79
|
|
|
*/ |
|
80
|
|
|
public function createPreview(File $file) |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->createPreviewFromStream( |
|
83
|
|
|
$this::getStreamFromString($this->parseMarkdownToHtml($file)) |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* {@inheritdoc} |
|
89
|
|
|
*/ |
|
90
|
|
|
public function convert(File $file, string $format) |
|
91
|
|
|
{ |
|
92
|
|
|
$tmpHtmlFile = $this::getStreamFromString($this->parseMarkdownToHtml($file)); |
|
93
|
|
|
if ('html' === $format) { |
|
94
|
|
|
$dest = stream_get_meta_data($tmpHtmlFile)['uri']; |
|
95
|
|
|
|
|
96
|
|
|
if (!file_exists($dest) || filesize($dest) <= 0) { |
|
97
|
|
|
throw new Exception('failed get '.$format); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $tmpHtmlFile; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
return $this->convertFromStream($tmpHtmlFile, $format); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Turn string into a stream. |
|
108
|
|
|
*/ |
|
109
|
|
|
protected static function getStreamFromString(string $string) |
|
110
|
|
|
{ |
|
111
|
|
|
$stream = tmpfile(); |
|
112
|
|
|
fwrite($stream, $string); |
|
113
|
|
|
rewind($stream); |
|
114
|
|
|
|
|
115
|
|
|
return $stream; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Parse Markdown file to HTML. |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function parseMarkdownToHtml(File $file): string |
|
122
|
|
|
{ |
|
123
|
|
|
return $this->parser->text(\stream_get_contents($file->get())); |
|
124
|
|
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.