annotate src/Database/MarkdownParser.php @ 4:84c75d9d90be

Changing usage to be bootstrap 5, not everything is reviewed but it's been started
author luka
date Tue, 19 Aug 2025 20:33:35 -0400
parents b44434aaa767
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
1 <?php
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
2
4
84c75d9d90be Changing usage to be bootstrap 5, not everything is reviewed but it's been started
luka
parents: 2
diff changeset
3 namespace Wizard\Framework\Database;
2
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
4
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
5 class MarkdownParser
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
6 {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
7 public static function toHtml(string $markdown): string
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
8 {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
9 $code_block_pattern = '/```([a-z]+)\s+(.*?)\s+```/si';
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
10 $inline_code_pattern = '/`\s*(.*?)\s*`/si';
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
11 $matches = [];
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
12
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
13 preg_match_all($code_block_pattern, $markdown, $matches, PREG_SET_ORDER);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
14 $code_blocks = [];
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
15 foreach ($matches as $index => $match) {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
16 $code_blocks[] = [
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
17 'language' => $match[1],
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
18 'code' => $match[2],
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
19 ];
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
20 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
21
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
22 foreach ($matches as $index => $match) {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
23 $markdown = preg_replace('/'.preg_quote($match[0], '/').'/', '[CODE_BLOCK_'.$index.']', $markdown, 1);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
24 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
25
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
26 preg_match_all($inline_code_pattern, $markdown, $matches, PREG_SET_ORDER);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
27 $inline_code = [];
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
28 foreach ($matches as $index => $match) {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
29 $inline_code[] = [
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
30 'code' => $match[1],
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
31 ];
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
32 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
33
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
34 foreach ($matches as $index => $match) {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
35 $markdown = preg_replace('/'.preg_quote($match[0], '/').'/', '[INLINE_CODE_'.$index.']', $markdown, 1);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
36 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
37
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
38 $markdown = preg_replace('/`(.+?)`/', '<code>$1</code>', $markdown);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
39 $markdown = preg_replace('/\#{6}\s(.+)/', '<h6>$1</h6>', $markdown);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
40 $markdown = preg_replace('/\#{5}\s(.+)/', '<h5>$1</h5>', $markdown);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
41 $markdown = preg_replace('/\#{4}\s(.+)/', '<h4>$1</h4>', $markdown);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
42 $markdown = preg_replace('/\#{3}\s(.+)/', '<h3>$1</h3>', $markdown);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
43 $markdown = preg_replace('/\#{2}\s(.+)/', '<h2>$1</h2>', $markdown);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
44 $markdown = preg_replace('/\#\s(.+)/', '<h1>$1</h1>', $markdown);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
45 $markdown = preg_replace('/\*\*\*(.+?)\*\*\*/', '<strong><em>$1</em></strong>', $markdown);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
46 $markdown = preg_replace('/\*\*(.+?)\*\*/', '<strong>$1</strong>', $markdown);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
47 $markdown = preg_replace('/\*(.+?)\*/', '<em>$1</em>', $markdown);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
48 $markdown = preg_replace('/\[(.+?)\]\((.+?)\)/', '<a href="$2">$1</a>', $markdown);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
49 $markdown = preg_replace("/\r\n|\r|\n/", '<br>', $markdown);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
50
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
51 foreach ($code_blocks as $index => $code_block) {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
52 $language = $code_block['language'];
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
53 $language = $language == 'blade' ? 'html' : $language;
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
54 $code_block['code'] = '<pre><code class="match-braces language-'.$language.'">'.htmlentities($code_block['code']).'</code></pre>';
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
55 $markdown = str_replace('[CODE_BLOCK_'.$index.']', $code_block['code'], $markdown);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
56 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
57
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
58 foreach ($inline_code as $index => $code) {
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
59 $code['code'] = '<code>'.htmlentities($code['code']).'</code>';
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
60 $markdown = str_replace('[INLINE_CODE_'.$index.']', $code['code'], $markdown);
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
61 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
62
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
63 return $markdown;
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
64 }
b44434aaa767 Moving around the components.
luka
parents:
diff changeset
65 }