-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathkotlin-shortcode.php
125 lines (102 loc) · 3.47 KB
/
kotlin-shortcode.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace Kotlin\KotlinPlaygroundWordpressPlugin;
class KotlinShortcode
{
const KOTLIN_PLAYGROUND_CND_URL = 'https://unpkg.com/kotlin-playground@1';
const SHORTCODE_NAME = 'kotlin';
private $isShortcodeWasUsed = false;
public function __construct()
{
$shortcodeName = self::SHORTCODE_NAME;
// Remove autop in shortcode
remove_filter('the_content', 'wpautop');
add_filter('the_content', 'wpautop', 99);
add_filter('the_content', 'shortcode_unautop', 100);
// Remove autotypographer in shortcode
add_filter('no_texturize_shortcodes', function($shortcodes) use ($shortcodeName) {
$shortcodes[] = $shortcodeName;
return $shortcodes;
});
add_shortcode($shortcodeName, array($this, 'doShortcode'));
}
public function doShortcode($attributes, $content = null) {
if (!$this->isShortcodeWasUsed) {
$this->isShortcodeWasUsed = true;
// Render KotlinRuncode init code in footer
$initJS = self::renderInitJs();
add_action('wp_footer', function() use ($initJS) {
echo $initJS;
}, 100);
}
$attrs = shortcode_atts(array(
'runnable' => true,
'platform' => "java",
'theme' => "idea",
'version' => "",
'min-version' => "",
'indent' => "4",
'auto-indent' => true,
'folded-button' => true
), $attributes);
$dataAttrs = array();
if ($attrs['folded-button'] === 'false') {
$dataAttrs[] = 'folded-button="false"';
}
if ($attrs['runnable'] === 'false') {
$dataAttrs[] = 'data-highlight-only="true"';
}
if ($attrs['auto-indent'] === 'false') {
$dataAttrs[] = 'auto-indent="false"';
}
if ($attrs['version']) {
$dataAttrs[] = 'data-version="'.$attrs['version'].'"';
}
if ($attrs['min-version']) {
$dataAttrs[] = 'data-min-compiler-version="'.$attrs['min-version'].'"';
}
switch ($attrs['platform']) {
case "js":
$dataAttrs[] = 'data-target-platform="js"';
break;
case "canvas":
$dataAttrs[] = 'data-target-platform="canvas"';
break;
case "junit":
$dataAttrs[] = 'data-target-platform="junit"';
break;
}
switch ($attrs['theme']) {
case "idea":
$dataAttrs[] = 'theme="idea"';
break;
case "darcula":
$dataAttrs[] = 'theme="darcula"';
break;
}
switch ($attrs['indent']) {
case "4":
$dataAttrs[] = 'indent="4"';
break;
case "2":
$dataAttrs[] = 'indent="2"';
break;
}
$dataAttrsString = join(' ', $dataAttrs);
$encoded = htmlspecialchars(html_entity_decode($content));
return '<pre class="kotlin-code" '. $dataAttrsString .' style="visibility: hidden; padding: 36px 0;">'. $encoded .'</pre>';
}
public static function renderInitJs() {
$src = self::KOTLIN_PLAYGROUND_CND_URL;
$content = <<<HTML
<script src="$src"></script>
<script>
(function() {
if (window.KotlinPlayground) {
window.KotlinPlayground('.kotlin-code');
}
})();
</script>
HTML;
return $content;
}
}