Skip to content

Commit 834ea93

Browse files
committed
Update Parser.php
1 parent ec31dcd commit 834ea93

1 file changed

Lines changed: 66 additions & 77 deletions

File tree

app/Content/Parser.php

Lines changed: 66 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
use Djot\Extension\MentionsExtension;
1717
use Djot\SafeMode;
1818

19-
use MediaEmbed\MediaEmbed;
20-
2119
use App\Models\User\UserModel;
2220

2321
class Parser
@@ -26,79 +24,71 @@ public static function text(string $content, string $type)
2624
{
2725
$text = self::parse($content);
2826
$text = self::gif($text);
29-
$text = self::video($text);
27+
$text = self::video($text);
3028

3129
return LitEmoji::encodeUnicode($text);
3230
}
3331

3432
public static function parse(string $content)
3533
{
3634
$content = str_replace('{cut}', '', $content);
37-
$content = str_replace('[^1]', '', $content);
38-
39-
// https://github.com/php-collective/djot-php/tree/master
40-
$converter = new DjotConverter(safeMode: SafeMode::strict());
41-
42-
$admonitionIcons = [
43-
'note' => 'ℹ️',
44-
'tip' => '💡',
45-
'warning' => '⚠️',
46-
'danger' => '🚨',
47-
'success' => '',
48-
];
49-
50-
$converter->on('render.div', function (RenderEvent $event) use ($admonitionIcons): void {
51-
$div = $event->getNode();
52-
if (!$div instanceof Div) {
53-
return;
54-
}
55-
56-
$class = $div->getAttribute('class') ?? '';
57-
foreach ($admonitionIcons as $type => $icon) {
58-
if (str_contains($class, $type)) {
59-
$div->setAttribute('class', 'admonition ' . $class);
60-
$div->setAttribute('data-icon', $icon);
61-
62-
return;
63-
}
64-
}
65-
});
66-
67-
$parser = $converter->getParser();
68-
$parser->addBlockPattern('/^:::spoiler\s*$/', function($lines, $start, $parent, $parser) {
69-
$endPattern = '/^:::\s*$/';
70-
$content = [];
71-
$i = $start + 1;
72-
while ($i < count($lines) && !preg_match($endPattern, $lines[$i])) {
73-
$content[] = $lines[$i];
74-
$i++;
75-
}
76-
$div = new Div();
77-
$div->setAttribute('class', 'spoiler');
78-
// Parse content inside
79-
$parser->parseBlockContent($div, $content);
80-
$parent->appendChild($div);
81-
return $i - $start + 1; // +1 for closing :::
82-
});
83-
84-
// Ссылка на темы, например: #libarea
85-
$parser = $converter->getParser()->getInlineParser();
86-
$parser->addInlinePattern('/#([a-zA-Z][a-zA-Z0-9_]*)/', function ($match, $groups, $p) {
87-
$tag = $groups[1];
88-
$link = new Link('/topic/' . strtolower($tag));
89-
$link->appendChild(new Text('#' . $tag));
90-
$link->setAttribute('class', 'green');
91-
return $link;
92-
});
93-
94-
$text = $converter
95-
->addExtension(new AutolinkExtension())
96-
->addExtension(new ExternalLinksExtension())
97-
->addExtension(new SmartQuotesExtension(locale: config('general', 'lang')))
98-
->addExtension(new MentionsExtension(urlTemplate: '/@{username}', cssClass: 'green',))
99-
->convert($content);
100-
101-
return $text;
35+
$content = str_replace('[^1]', '', $content);
36+
37+
// https://github.com/php-collective/djot-php/tree/master
38+
$converter = new DjotConverter(safeMode: SafeMode::strict());
39+
40+
self::reminders($converter);
41+
42+
self::topic($converter);
43+
44+
$text = $converter
45+
->addExtension(new AutolinkExtension())
46+
->addExtension(new ExternalLinksExtension())
47+
->addExtension(new SmartQuotesExtension(locale: config('general', 'lang')))
48+
->addExtension(new MentionsExtension(urlTemplate: '/@{username}', cssClass: 'green',))
49+
->convert($content);
50+
51+
return $text;
52+
}
53+
54+
public static function reminders($converter)
55+
{
56+
$admonitionIcons = [
57+
'note' => 'ℹ️',
58+
'tip' => '💡',
59+
'warning' => '⚠️',
60+
'danger' => '🚨',
61+
'success' => '',
62+
];
63+
64+
$converter->on('render.div', function (RenderEvent $event) use ($admonitionIcons): void {
65+
$div = $event->getNode();
66+
if (!$div instanceof Div) {
67+
return;
68+
}
69+
70+
$class = $div->getAttribute('class') ?? '';
71+
foreach ($admonitionIcons as $type => $icon) {
72+
if (str_contains($class, $type)) {
73+
$div->setAttribute('class', 'admonition ' . $class);
74+
$div->setAttribute('data-icon', $icon);
75+
76+
return;
77+
}
78+
}
79+
});
80+
}
81+
82+
public static function topic($converter)
83+
{
84+
$parser = $converter->getParser()->getInlineParser();
85+
$parser->addInlinePattern('/#([a-zA-Z][a-zA-Z0-9_]*)/', function ($match, $groups, $p) {
86+
$tag = $groups[1];
87+
$link = new Link('/topic/' . strtolower($tag));
88+
$link->appendChild(new Text('#' . $tag));
89+
$link->setAttribute('class', 'green');
90+
return $link;
91+
});
10292
}
10393

10494
public static function gif($content)
@@ -134,10 +124,10 @@ public static function gif($content)
134124

135125
public static function video($content)
136126
{
137-
// TODO
127+
// TODO
138128
return $content;
139-
}
140-
129+
}
130+
141131
// TODO: Let's check the simple version for now.
142132
public static function cut($text, $length = 800)
143133
{
@@ -165,8 +155,8 @@ public static function cut($text, $length = 800)
165155
// Content management
166156
public static function noHTML(string $content, int $lenght = 150)
167157
{
168-
$converter = new DjotConverter(safeMode: SafeMode::strict());
169-
$text = $converter->convert($content);
158+
$converter = new DjotConverter(safeMode: SafeMode::strict());
159+
$text = $converter->convert($content);
170160

171161
$content = str_replace(["\r\n", "\r", "\n", "#"], ' ', $text);
172162

@@ -177,8 +167,8 @@ public static function noHTML(string $content, int $lenght = 150)
177167

178168
public static function fragment(string $text, int $lenght = 150, string $charset = 'UTF-8'): string
179169
{
180-
$text = LitEmoji::encodeUnicode($text);
181-
170+
$text = LitEmoji::encodeUnicode($text);
171+
182172
if (mb_strlen($text, $charset) >= $lenght) {
183173
$wrap = wordwrap($text, $lenght, '~');
184174
$ret = mb_strpos($wrap, '~', 0, $charset);
@@ -190,7 +180,7 @@ public static function fragment(string $text, int $lenght = 150, string $charset
190180

191181
return $text;
192182
}
193-
183+
194184
public static function parseUsers($content, $with_user = false, $to_uid = false)
195185
{
196186
preg_match_all('/(?<=^|\s|>)@([a-z0-9_]+)/i', strip_tags($content), $matchs);
@@ -245,5 +235,4 @@ public static function parseUsers($content, $with_user = false, $to_uid = false)
245235

246236
return $content;
247237
}
248-
249238
}

0 commit comments

Comments
 (0)