forked from westonruter/syntax-highlighting-code-block
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax-highlighting-code-block.php
371 lines (317 loc) · 10.4 KB
/
syntax-highlighting-code-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
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
/**
* Plugin Name: Syntax-highlighting Code Block (with Server-side Rendering)
* Plugin URI: https://github.com/westonruter/syntax-highlighting-code-block
* Description: Extending the Code block with syntax highlighting rendered on the server, thus being AMP-compatible and having faster frontend performance.
* Version: 1.1.3
* Author: Weston Ruter
* Author URI: https://weston.ruter.net/
* License: GPL2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: syntax-highlighting-code-block
* Requires PHP: 5.6
*
* @package Syntax_Highlighting_Code_Block
*/
namespace Syntax_Highlighting_Code_Block;
const PLUGIN_VERSION = '1.1.3';
const DEVELOPMENT_MODE = true; // This is automatically rewritten to false during dist build.
const OPTION_NAME = 'syntax_highlighting';
const DEFAULT_THEME = 'default';
const BLOCK_STYLE_FILTER = 'syntax_highlighting_code_block_style';
const FRONTEND_STYLE_HANDLE = 'syntax-highlighting-code-block';
/**
* Get an array of all the options tied to this plugin.
*
* @return array
*/
function get_options() {
$options = \get_option( OPTION_NAME, [] );
return array_merge(
[
'theme_name' => DEFAULT_THEME,
],
$options
);
}
/**
* Get the single, specified plugin option.
*
* @param string $option_name The plugin option name.
*
* @return mixed
*/
function get_option( $option_name ) {
return get_options()[ $option_name ];
}
/**
* Get path to script deps file.
*
* @return string Path.
*/
function get_script_deps_path() {
return __DIR__ . '/build/index.deps.json';
}
/**
* Initialize plugin.
*/
function init() {
if ( ! function_exists( 'register_block_type' ) ) {
return;
}
if ( DEVELOPMENT_MODE && ! file_exists( get_script_deps_path() ) ) {
add_action( 'admin_notices', __NAMESPACE__ . '\print_build_required_admin_notice' );
return;
}
register_block_type(
'core/code',
[
'render_callback' => __NAMESPACE__ . '\render_block',
]
);
add_action( 'enqueue_block_editor_assets', __NAMESPACE__ . '\enqueue_editor_assets' );
add_action( 'wp_enqueue_scripts', __NAMESPACE__ . '\register_frontend_assets' );
}
add_action( 'plugins_loaded', __NAMESPACE__ . '\init' );
/**
* Print admin notice when plugin installed from source but no build being performed.
*/
function print_build_required_admin_notice() {
?>
<div class="notice notice-error">
<p>
<strong><?php esc_html_e( 'Syntax-highlighting Code Block', 'amp' ); ?>:</strong>
<?php
echo wp_kses_post(
sprintf(
/* translators: %s is the command to run */
__( 'Unable to initialize plugin due to being installed from source without running a build. Please run %s', 'syntax-highlighting-code-block' ),
'<code>composer install && npm install && npm run build</code>'
)
);
?>
</p>
</div>
<?php
}
/**
* Enqueue assets for editor.
*/
function enqueue_editor_assets() {
$handle = 'syntax-highlighting-code-block';
$script_path = '/build/index.js';
$script_deps = json_decode( file_get_contents( get_script_deps_path() ), false ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
$in_footer = true;
wp_enqueue_script(
$handle,
plugins_url( $script_path, __FILE__ ),
$script_deps,
DEVELOPMENT_MODE ? filemtime( plugin_dir_path( __FILE__ ) . $script_path ) : PLUGIN_VERSION,
$in_footer
);
wp_set_script_translations( $handle, 'syntax-highlighting-code-block' );
}
/**
* Register assets for the frontend.
*
* Asset(s) will only be enqueued if needed.
*/
function register_frontend_assets() {
if ( has_filter( BLOCK_STYLE_FILTER ) ) {
/**
* Filters the style used for the code syntax block.
*
* The string returned must correspond to the filenames found at <https://github.com/scrivo/highlight.php/tree/master/styles>,
* minus the file extension.
*
* This filter takes precedence over any settings set in the database as an option. Additionally, if this filter
* is provided, then a theme selector will not be provided in Customizer.
*
* @since 1.0.0
* @param string $style Style.
*/
$style = apply_filters( BLOCK_STYLE_FILTER, DEFAULT_THEME );
} else {
$style = get_option( 'theme_name' );
}
$default_style_path = sprintf( 'vendor/scrivo/highlight.php/styles/%s.css', 0 === validate_file( $style ) ? $style : DEFAULT_THEME );
wp_register_style(
FRONTEND_STYLE_HANDLE,
plugins_url( $default_style_path, __FILE__ ),
[],
SCRIPT_DEBUG ? filemtime( plugin_dir_path( __FILE__ ) . $default_style_path ) : PLUGIN_VERSION
);
}
/**
* Render code block.
*
* @param array $attributes Attributes.
* @param string $content Content.
* @return string Highlighted content.
*/
function render_block( $attributes, $content ) {
$pattern = '(?P<before><pre.*?><code.*?>)';
$pattern .= '(?P<code>.*)';
$after = '</code></pre>';
$pattern .= $after;
if ( ! preg_match( '#^\s*' . $pattern . '\s*$#s', $content, $matches ) ) {
return $content;
}
if ( ! isset( $attributes['language'] ) ) {
$attributes['language'] = '';
}
if ( ! isset( $attributes['showLines'] ) ) {
$attributes['showLines'] = false;
}
// Enqueue the style now that we know it will be needed.
wp_enqueue_style( FRONTEND_STYLE_HANDLE );
// Include line-number styles if requesting to show lines.
if ( $attributes['showLines'] ) {
$after_styles = wp_styles()->get_data( FRONTEND_STYLE_HANDLE, 'after' );
if ( ! is_array( $after_styles ) ) {
$after_styles = '';
} else {
$after_styles = implode( '', $after_styles );
}
// Only include line-number styles if not already included.
if ( false === strpos( $after_styles, '.hljs.line-numbers' ) ) {
wp_add_inline_style(
FRONTEND_STYLE_HANDLE,
file_get_contents( __DIR__ . '/line-numbers.css' ) // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
);
}
}
$inject_classes = function( $start_tags, $language, $show_lines ) {
$added_classes = "hljs language-$language";
if ( $show_lines ) {
$added_classes .= ' line-numbers';
}
$start_tags = preg_replace(
'/(<code[^>]*class=")/',
'$1 ' . esc_attr( $added_classes ),
$start_tags,
1,
$count
);
if ( 0 === $count ) {
$start_tags = preg_replace(
'/(?<=<code)(?=>)/',
sprintf( ' class="%s"', esc_attr( $added_classes ) ),
$start_tags,
1
);
}
return $start_tags;
};
$transient_key = 'syntax-highlighting-code-block-' . md5( $attributes['showLines'] . $attributes['language'] . $matches['code'] ) . '-v' . PLUGIN_VERSION;
$highlighted = get_transient( $transient_key );
if ( $highlighted && isset( $highlighted['code'] ) ) {
if ( isset( $highlighted['language'] ) ) {
$matches['before'] = $inject_classes( $matches['before'], $highlighted['language'], $highlighted['show_lines'] );
}
return $matches['before'] . $highlighted['code'] . $after;
}
try {
if ( ! class_exists( '\Highlight\Autoloader' ) ) {
require_once __DIR__ . '/vendor/scrivo/highlight.php/Highlight/Autoloader.php';
spl_autoload_register( 'Highlight\Autoloader::load' );
}
$highlighter = new \Highlight\Highlighter();
$language = $attributes['language'];
$code = html_entity_decode( $matches['code'], ENT_QUOTES );
// Convert from Prism.js languages names.
if ( 'clike' === $language ) {
$language = 'cpp';
} elseif ( 'git' === $language ) {
$language = 'diff'; // Best match.
} elseif ( 'markup' === $language ) {
$language = 'xml';
}
if ( $language ) {
$r = $highlighter->highlight( $language, $code );
} else {
$r = $highlighter->highlightAuto( $code );
}
$code = $r->value;
$language = $r->language;
$show_lines = $attributes['showLines'];
if ( $show_lines ) {
require_once __DIR__ . '/vendor/scrivo/highlight.php/HighlightUtilities/functions.php';
$lines = \HighlightUtilities\splitCodeIntoArray( $code );
$code = '';
// We need to wrap the line of code twice in order to let out `white-space: pre` CSS setting to be respected
// by our `table-row`.
foreach ( $lines as $line ) {
$code .= sprintf( '<div class="loc"><span>%s</span></div>%s', $line, PHP_EOL );
}
}
$highlighted = compact( 'code', 'language', 'show_lines' );
set_transient( $transient_key, compact( 'code', 'language', 'show_lines' ), MONTH_IN_SECONDS );
$matches['before'] = $inject_classes( $matches['before'], $highlighted['language'], $highlighted['show_lines'] );
return $matches['before'] . $code . $after;
} catch ( \Exception $e ) {
return sprintf(
'<!-- %s(%s): %s -->%s',
get_class( $e ),
$e->getCode(),
str_replace( '--', '', $e->getMessage() ),
$content
);
}
}
/**
* Initialize admin settings.
*/
function admin_init() {
register_setting( 'syntax_highlighting', OPTION_NAME );
}
add_action( 'admin_init', __NAMESPACE__ . '\admin_init' );
/**
* Validate the given stylesheet name against available stylesheets.
*
* @param \WP_Error $validity Validator object.
* @param string $input Incoming theme name.
*
* @return mixed
*/
function validate_theme_name( $validity, $input ) {
require_once __DIR__ . '/vendor/scrivo/highlight.php/HighlightUtilities/functions.php';
$themes = \HighlightUtilities\getAvailableStyleSheets();
if ( ! in_array( $input, $themes, true ) ) {
$validity->add( 'invalid_theme', __( 'Unrecognized theme', 'syntax-highlighting-code-block' ) );
}
return $validity;
}
/**
* Add plugin settings to Customizer.
*
* @param \WP_Customize_Manager $wp_customize The Customizer object.
*/
function customize_register( $wp_customize ) {
if ( has_filter( BLOCK_STYLE_FILTER ) ) {
return;
}
require_once __DIR__ . '/vendor/scrivo/highlight.php/HighlightUtilities/functions.php';
$themes = \HighlightUtilities\getAvailableStyleSheets();
sort( $themes );
$choices = array_combine( $themes, $themes );
$wp_customize->add_setting(
'syntax_highlighting[theme_name]',
[
'type' => 'option',
'default' => DEFAULT_THEME,
'validate_callback' => __NAMESPACE__ . '\validate_theme_name',
]
);
$wp_customize->add_control(
'syntax_highlighting[theme_name]',
[
'type' => 'select',
'section' => 'colors',
'label' => __( 'Syntax Highlighting Theme', 'syntax-highlighting-code-block' ),
'description' => __( 'Preview the theme by navigating to a page with a code block to see the different themes in action.', 'syntax-highlighting-code-block' ),
'choices' => $choices,
]
);
}
add_action( 'customize_register', __NAMESPACE__ . '\customize_register' );