forked from shazahm1/WP-Syntax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-syntax-OLD.php
259 lines (210 loc) · 7.11 KB
/
wp-syntax-OLD.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
<?php
/*
Plugin Name: WP-Syntax
Plugin URI: http://wordpress.org/extend/plugins/wp-syntax/
Description: Syntax highlighting using <a href="http://qbnz.com/highlighter/">GeSHi</a> supporting a wide range of popular languages. Wrap code blocks with <code><pre lang="LANGUAGE" line="1"></code> and <code></pre></code> where <code>LANGUAGE</code> is a geshi supported language syntax. The <code>line</code> attribute is optional.
Author: Steven A. Zahm
Version: 0.9.13
Author URI: http://connections-pro.com
*/
# Original Author: Ryan McGeary
# Author: Steven A. Zahm
# Author: Steffen Vogel <[email protected]>
#
# Copyright (c) 2007-2013 Ryan McGeary; Steven A. Zahm
#
# This file is part of WP-Syntax.
#
# WP-Syntax is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# WP-Syntax is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with WP-Syntax; if not, write to the Free Software Foundation, Inc., 59
# Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
// Override allowed attributes for pre tags in order to use <pre lang=""> in
// comments. For more info see wp-includes/kses.php
/*if (!CUSTOM_TAGS) {
$allowedposttags['pre'] = array(
'lang' => array(),
'line' => array(),
'escaped' => array(),
'style' => array(),
'width' => array(),
'highlight' => array(),
'src' => array()
);
//Allow plugin use in comments
$allowedtags['pre'] = array(
'lang' => array(),
'line' => array(),
'escaped' => array(),
'highlight' => array()
);
}*/
include_once("geshi/geshi.php");
if (!defined("WP_CONTENT_URL")) define("WP_CONTENT_URL", get_option("siteurl") . "/wp-content");
if (!defined("WP_PLUGIN_URL")) define("WP_PLUGIN_URL", WP_CONTENT_URL . "/plugins");
function wp_syntax_change_mce_options($init) {
$ext = 'pre[id|name|class|style|lang|line|escaped|highlight|src]';
if ( isset($init["extended_valid_elements"]) )
{
$init["extended_valid_elements"] .= "," . $ext;
} else {
$init["extended_valid_elements"] = $ext;
}
return $init;
}
function wp_syntax_style()
{
$url = WP_PLUGIN_URL . "/wp-syntax/wp-syntax.css";
if ( file_exists(STYLESHEETPATH . "/wp-syntax.css") )
{
$url = get_bloginfo("stylesheet_directory") . "/wp-syntax.css";
}
wp_enqueue_style( 'wp-syntax-css', $url );
}
function wp_syntax_scripts()
{
$url = WP_PLUGIN_URL . "/wp-syntax/wp-syntax.js";
wp_enqueue_script( 'wp-syntax-js', $url );
}
// special ltrim b/c leading whitespace matters on 1st line of content
function wp_syntax_code_trim($code)
{
$code = preg_replace("/^\s*\n/siU", "", $code);
$code = rtrim($code);
return $code;
}
function wp_syntax_substitute(&$match)
{
global $wp_syntax_token, $wp_syntax_matches;
$i = count($wp_syntax_matches);
$wp_syntax_matches[$i] = $match;
return "\n\n<p>" . $wp_syntax_token . sprintf('%03d', $i) . "</p>\n\n";
}
function wp_syntax_line_numbers($code, $start)
{
$line_count = count(explode("\n", $code));
$output = '<pre>';
for ($i = 0; $i < $line_count; $i++)
{
$output .= ($start + $i) . "\n";
}
$output .= '</pre>';
return $output;
}
function wp_syntax_caption($url) {
$parsed = parse_url($url);
$path = pathinfo($parsed['path']);
$caption = '';
if ( ! isset($path['filename']) )
{
return;
}
if ( isset($parsed['scheme']) )
{
$caption .= '<a href="' . $url . '">';
}
if ( isset( $parsed["host"] ) && $parsed["host"] == 'github.com' )
{
$caption .= substr($parsed['path'], strpos($parsed['path'], '/', 1)); /* strip github.com username */
} else {
$caption .= $parsed['path'];
}
/* $caption . $path["filename"];
if (isset($path["extension"])) {
$caption .= "." . $path["extension"];
}*/
if ( isset($parsed['scheme']) )
{
$caption .= '</a>';
}
return $caption;
}
function wp_syntax_highlight($match)
{
global $wp_syntax_matches;
$i = intval($match[1]);
$match = $wp_syntax_matches[$i];
$language = strtolower(trim($match[1]));
$line = trim($match[2]);
$escaped = trim($match[3]);
$caption = wp_syntax_caption($match[5]);
$code = wp_syntax_code_trim($match[6]);
if ($escaped == 'true') $code = htmlspecialchars_decode($code);
$geshi = new GeSHi($code, $language);
$geshi->enable_keyword_links(false);
do_action_ref_array('wp_syntax_init_geshi', array(&$geshi));
if ( ! empty($match[4]) )
{
$linespecs = strpos($match[4], ",") == false ? array($match[4]) : explode(",", $match[4]);
$lines = array();
foreach ($linespecs as $spec)
{
$range = explode("-", $spec);
$lines = array_merge($lines, (count($range) == 2) ? range($range[0], $range[1]) : $range);
}
$geshi->highlight_lines_extra($lines);
}
$output = "\n" . '<div class="wp_syntax">';
$output .= '<table>';
if ( ! empty($caption) )
{
$output .= '<caption>' . $caption . '</caption>';
}
$output .= '<tr>';
if ($line)
{
$output .='<td class="line_numbers">' . wp_syntax_line_numbers($code, $line) . '</td>';
}
$output .= '<td class="code">';
$output .= $geshi->parse_code();
$output .= '</td></tr></table>';
$output .= '</div>' . "\n";
return $output;
}
function wp_syntax_before_filter($content)
{
return preg_replace_callback(
"/\s*<pre(?:lang=[\"']([\w-]+)[\"']|line=[\"'](\d*)[\"']|escaped=[\"'](true|false)?[\"']|highlight=[\"']((?:\d+[,-])*\d+)[\"']|src=[\"']([^\"']+)[\"']|\s)+>(.*)<\/pre>\s*/siU",
"wp_syntax_substitute",
$content
);
}
function wp_syntax_after_filter($content)
{
global $wp_syntax_token;
$content = preg_replace_callback(
'/<p>\s*' . $wp_syntax_token . '(\d{3})\s*<\/p>/si',
'wp_syntax_highlight',
$content
);
return $content;
}
$wp_syntax_token = md5(uniqid(rand()));
$wp_syntax_matches = array();
// Add styling
add_action('wp_print_styles', 'wp_syntax_style');
// Enqueue the JS
//add_action('wp_print_scripts', 'wp_syntax_scripts');
// Update config for WYSIWYG editor
add_filter('tiny_mce_before_init', 'wp_syntax_change_mce_options');
// We want to run before other filters; hence, a priority of 0 was chosen.
// The lower the number, the higher the priority. 10 is the default and
// several formatting filters run at or around 6.
add_filter('the_content', 'wp_syntax_before_filter', 0);
add_filter('the_excerpt', 'wp_syntax_before_filter', 0);
add_filter('comment_text', 'wp_syntax_before_filter', 0);
// We want to run after other filters; hence, a priority of 99.
add_filter('the_content', 'wp_syntax_after_filter', 99);
add_filter('the_excerpt', 'wp_syntax_after_filter', 99);
add_filter('comment_text', 'wp_syntax_after_filter', 99);
?>