-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathgenerateLiveDemo.php
executable file
·175 lines (141 loc) · 6.02 KB
/
generateLiveDemo.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/php
<?php
include __DIR__ . '/../vendor/autoload.php';
$configurator = new s9e\TextFormatter\Configurator;
$configurator->enableJavaScript();
$configurator->rootRules->enableAutoLineBreaks();
$configurator->BBCodes->addFromRepository('*');
$configurator->BBCodes->addFromRepository('B');
$configurator->BBCodes->addFromRepository('C');
$configurator->BBCodes->addFromRepository('CODE');
$configurator->BBCodes->addFromRepository('COLOR');
$configurator->BBCodes->addFromRepository('FLOAT');
$configurator->BBCodes->addFromRepository('HR');
$configurator->BBCodes->addFromRepository('I');
$configurator->BBCodes->addFromRepository('LIST');
$configurator->BBCodes->addFromRepository('QUOTE');
$configurator->BBCodes->addFromRepository('S');
$configurator->BBCodes->addFromRepository('U');
$configurator->BBCodes->addFromRepository('URL');
$configurator->Censor->add('apple', 'banana');
$configurator->Emoticons->add(':)', '<img src="s.png" alt=":)"/>');
$configurator->Preg->replace(
'/#(?<tag>[a-z0-9]+)/i',
'<a href="https://twitter.com/#!/search/%23{@tag}"><xsl:apply-templates/></a>'
);
$configurator->HTMLElements->allowElement('a');
$configurator->HTMLElements->allowElement('b');
$configurator->HTMLElements->allowAttribute('a', 'href');
$configurator->HTMLElements->allowAttribute('a', 'title');
$configurator->Autolink;
$configurator->HTMLEntities;
$configurator->FancyPants;
$configurator->MediaEmbed->add('youtube');
$configurator->tags['YOUTUBE']->template = '<iframe width="240" height="180" src="https://www.youtube.com/embed/{@id}" allowfullscreen=""/>';
$filepath = __DIR__ . '/../vendor/node_modules/google-closure-compiler-linux/compiler';
$minifier = $configurator->javascript->setMinifier('ClosureCompilerApplication', $filepath);
$minifier->cacheDir = __DIR__ . '/../tests/.cache';
$minifier->options .= ' --jscomp_error "*" --jscomp_off "strictCheckTypes"';
$configurator->javascript->exports = ['disablePlugin', 'enablePlugin', 'preview'];
extract($configurator->finalize());
ob_start();
?><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Security-Policy" content="script-src '$integrity' cdn.jsdelivr.net">
<title>s9e\TextFormatter • Demo</title>
<base href="https://s9e.github.io/TextFormatter/demo.html" />
<style type="text/css">
#preview
{
font-family: sans;
padding: 5px;
background-color: #f8f8f8;
border: dashed 1px #ddd;
border-radius: 5px;
}
code
{
padding: 2px;
background-color: #fff;
border-radius: 3px;
border: solid 1px #ddd;
}
</style>
</head>
<body>
<div style="float:left;width:80%;max-width:800px">
<form>
<textarea style="width:99%" rows="15">[float=right]https://www.youtube.com/watch?v=QH2-TGUlwu4[/float]
This is a demo of the JavaScript port of [url=https://github.com/s9e/TextFormatter title="s9e\TextFormatter at GitHub.com"]s9e\TextFormatter[/url].
The following plugins have been enabled:
[list]
[*][b]Autolink[/b] --- loose URLs such as https://github.com are automatically turned into links
[*][b]BBCodes[/b]
[list=circle]
[*][b]bold[/b], [i]italic[/i], [u]underline[/u], [s]strikethrough[/s],
[*][color=#f05]co[/color][color=#2f2]lo[/color][color=#02f]r,[/color]
[*][C][URL][/C], [C:123][C][/C:123], [C][FLOAT][/C], and [C][LIST][/C]
[*][C][CODE][/C] with real-time syntax highlighting via [url=https://highlightjs.org/]highlight.js[/url]
[code=php]$who = "world";
printf("Hello %s\n", $who);[/code]
[/list]
[*][b]Censor[/b] --- the word "apple" is censored and automatically replaced with "banana"
[*][b]Emoticons[/b] --- one emoticon :) has been added
[*][b]FancyPants[/b] --- some typography is enhanced, e.g. (c) (tm) and "quotes"
[*][b]MediaEmbed[/b] --- links to YouTube are transformed into playable videos
[*][b]Preg[/b] --- the Preg plugin provides a way to perform generic regexp-based replacements that are HTML-safe. Here, text that matches [C]/#(?<tag>[a-z0-9]+)/i[/C] is replaced with the template [C]<a href="https://twitter.com/#!/search/%23{@tag}"><xsl:apply-templates/></a>[/C] -- For example: #PHP, #fml
[*][b]HTMLElements[/b] --- [C]<a>[/C] and [C]<b>[/C] tags are allowed, with two whitelisted attributes for [C]<a>[/C]: [C]href[/C] and [C]title[/C]. Example: <a href="https://github.com" title="GitHub - Social Coding"><b>GitHub</b></a>
[*][b]HTMLEntities[/b] --- HTML entities such as &hearts; are decoded
[/list]
[hr]
The parser/renderer used on this page has been generated by [url=https://github.com/s9e/TextFormatter/blob/master/scripts/generateLiveDemo.php]this script[/url]. It's been minified with Google Closure Compiler to <?php printf('%.1f', strlen($js) / 1024); ?> KB (<?php printf('%.1f', strlen(gzcompress($js, 9)) / 1024); ?> KB compressed)</textarea>
</form>
</div>
<div style="float:left">
<form><?php
$list = [];
foreach ($configurator->plugins as $pluginName => $plugin)
{
$list[$pluginName] = '<input type="checkbox" id="' . $pluginName . '" checked><label for="' . $pluginName . '"> '. $pluginName . '</label>';
}
ksort($list);
echo implode('<br>', $list);
?></form>
</div>
<div style="clear:both"></div>
<div id="preview"></div>
<script><?php echo $js; ?>
var text,
textareaEl = document.getElementsByTagName('textarea')[0],
previewEl = document.getElementById('preview');
setInterval(function()
{
if (textareaEl.value === text)
{
return;
}
text = textareaEl.value;
s9e.TextFormatter.preview(text, previewEl);
}, 20);
document.querySelectorAll('input[type="checkbox"]').forEach(
(el) => el.addEventListener(
'change',
function ()
{
(el.checked) ? s9e.TextFormatter.enablePlugin(el.id)
: s9e.TextFormatter.disablePlugin(el.id);
text = '';
}
)
);
</script>
</body>
</html><?php
$html = ob_get_clean();
preg_match('(<script>(.*?)</script>)s', $html, $m);
$integrity = 'sha384-' . base64_encode(hash('sha384', $m[1], true));
$html = str_replace('$integrity', $integrity, $html);
file_put_contents(__DIR__ . '/../../s9e.github.io/TextFormatter/demo.html', $html);
echo "Done.\n";