-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlw-soromanga-block.php
109 lines (104 loc) · 2.63 KB
/
lw-soromanga-block.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* Plugin Name: Lw Soromanga Block
* Description: Example block written with ESNext standard and JSX support – build step required.
* Requires at least: 5.7
* Requires PHP: 7.0
* Version: 0.1.0
* Author: The WordPress Contributors
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: lw-soromanga-block
*
* @package create-block
*/
$replacelist = [
[
"f" => "/===[=]*/u",
"r" => '<div class="soromanga_container">'
],
[
"f" => "/@divend/u",
"r" => "</div>"
],
[
"f" => "/@frame:([\w\- ]+)/u",
"r" => '<div class="frame $1"><div class="serif">'
],
[
"f" => "/@fend/u",
"r" => "</div></div>"
],
[
"f" => "/@センセ「([\w\- ]+)/u",
"r" => "@「yuki $1"
],
[
"f" => "/@デシ「([\w\- ]+)/u",
"r" => "@「alice $1"
],
[
"f" => "/@「([\w\- ]+)/u",
"r" => '<div class="frame $1"><div class="serif">'
],
[
"f" => "/@」/u",
"r" => "</div></div>"
],
[
"f" => "/<br>/",
"r" => "<br>"
],
[
"f" => "/@o:(.*)/",
"r" => '<div class="fukuromoji"><div class="fukurochild">$1</div></div>'
],
[
"f" => "/!\[(.*?)\]\((.*?)\)/u",
"r" => '<img alt="$1" src="$2">'
],
[
"f" => "/【センセ(.*?)】(.*)/u",
"r" => '<p class="kaiwa yuki $1"><span class="kaiwaborder">$2</span></p>'
],
[
"f" => "/【デシ(.*?)】(.*)/u",
"r" => '<p class="kaiwa alice $1"><span class="kaiwaborder">$2</span></p>'
],
];
function render_manga($attributes, $content, $block ){
global $replacelist;
// 改行で分割
$content = str_replace(["\r\n", "\r", "\n"], "\n", $content);
$lcontent = explode("\n", $content);
$testresult = '';
$error_flag = false;
foreach($replacelist as $line){
$result = preg_replace($line['f'], $line['r'], $lcontent);
if($result != null){
$lcontent = $result;
} else {
$error_flag = true;
$testresult = $testresult . $line['f'] . $line['r'] . '<br>';
}
}
$content = implode("\n", $lcontent);
if($error_flag == false){
return $content;
} else {
return $content . 'Error<br>' . $testresult;
}
}
/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/block-editor/tutorials/block-tutorial/writing-your-first-block-type/
*/
function create_block_lw_soromanga_block_block_init() {
register_block_type_from_metadata( __DIR__ ,array(
'render_callback' => 'render_manga'
));
}
add_action( 'init', 'create_block_lw_soromanga_block_block_init' );