-
Notifications
You must be signed in to change notification settings - Fork 11
/
markdown.php
421 lines (363 loc) · 13.3 KB
/
markdown.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php
/**
Plugin Name: GitHub Flavored Markdown for WordPress
Plugin URI: https://github.com/makotokw/wp-gfm
Version: 0.11
Description: Converts block in GitHub Flavored Markdown by using shortcode <code>[gfm]</code> and support PHP-Markdown by using shortcode <code>[markdown]</code>
Author: makoto_kw
Author URI: http://makotokw.com/
License: MIT
*/
class WP_GFM {
const NAME = 'WP_GFM';
const VERSION = '0.11';
const DEFAULT_RENDER_URL = 'https://api.github.com/markdown/raw';
// google-code-prettify: https://code.google.com/p/google-code-prettify/
const FENCED_CODE_BLOCKS_TEMPLATE_FOR_GOOGLE_CODE_PRETTIFY = '<pre class="prettyprint lang-{{lang}}" title="{{title}}">{{codeblock}}</pre>';
public $agent = '';
public $url = '';
public $has_converter = false;
public $gfm_options = array();
public $ad_html = '';
static function get_instance() {
static $plugin = null;
if ( ! $plugin ) {
$plugin = new WP_GFM();
}
return $plugin;
}
private function __construct() {
$this->agent = self::NAME . '/' . self::VERSION;
$this->url = plugins_url( '', __FILE__ );
$this->gfm_options = wp_parse_args(
(array) get_option( 'gfm' ),
array(
'general_ad' => false,
'php_md_always_convert' => false,
'php_md_use_autolink' => false,
'php_md_fenced_code_blocks_template' => self::FENCED_CODE_BLOCKS_TEMPLATE_FOR_GOOGLE_CODE_PRETTIFY,
'render_url' => self::DEFAULT_RENDER_URL,
)
);
if ( is_admin() ) {
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_print_footer_scripts', array( $this, 'admin_quicktags' ) );
} else {
add_action( 'wp_enqueue_scripts', array( $this, 'wp_enqueue_styles' ) );
}
$this->ad_html = '<div class="wp-gfm-ad"><span class="wp-gfm-powered-by">Markdown with by <img alt="❤" src="https://s.w.org/images/core/emoji/72x72/2764.png" width="10" height="10"> <a href="https://github.com/makotokw/wp-gfm" target="_blank" rel="nofollow noopener" title="makotokw/wp-gfm">wp-gfm</a></span></div>';
}
function wp_enqueue_styles() {
wp_enqueue_style( 'wp-gfm', $this->url . '/css/markdown.css', array(), self::VERSION );
}
function php_markdown_init() {
if ( class_exists( '\Gfm\Markdown\Extra' ) ) {
$this->has_converter = true;
\Gfm\Markdown\Extra::setElementCssPrefix( 'wp-gfm-' );
// @codingStandardsIgnoreStart
\Gfm\Markdown\Extra::$useAutoLinkExtras = true == $this->gfm_options['php_md_use_autolink'];
\Gfm\Markdown\Extra::$fencedCodeBlocksTemplate = $this->gfm_options['php_md_fenced_code_blocks_template'];
// @codingStandardsIgnoreEnd
}
if ( $this->gfm_options['php_md_always_convert'] ) {
add_action( 'the_content', array( $this, 'force_convert' ), 7 );
} else {
add_action( 'the_content', array( $this, 'the_content' ), 7 );
}
if ( $this->gfm_options['general_ad'] ) {
add_action( 'the_content', array( $this, 'the_content_ad' ), 8 );
}
add_shortcode( 'embed_markdown', array( $this, 'shortcode_embed_markdown' ) );
add_filter( 'pre_comment_content', array( $this, 'pre_comment_content' ), 5 );
}
function admin_init() {
register_setting( 'gfm_option_group', 'gfm_array', array( $this, 'option_sanitize_gfm' ) );
add_settings_section(
'setting_section_general',
'General',
array( $this, 'setting_section_general' ),
'gfm-setting-admin'
);
add_settings_field(
'general_ad',
'',
array( $this, 'create_gfm_general_ad_field' ),
'gfm-setting-admin',
'setting_section_general'
);
add_settings_section(
'setting_section_php_markdown',
'PHP Markdown',
array( $this, 'print_section_php_markdown' ),
'gfm-setting-admin'
);
add_settings_field(
'php_md_always_convert',
'',
array( $this, 'create_gfm_php_md_always_convert_field' ),
'gfm-setting-admin',
'setting_section_php_markdown'
);
add_settings_field(
'php_md_use_autolink',
'',
array( $this, 'create_gfm_php_md_use_autolink_field' ),
'gfm-setting-admin',
'setting_section_php_markdown'
);
add_settings_field(
'php_md_fenced_code_blocks_template',
'Fenced Code Blocks Template',
array( $this, 'create_gfm_php_md_fenced_code_blocks_template_field' ),
'gfm-setting-admin',
'setting_section_php_markdown'
);
add_settings_section(
'setting_section_gfm',
'GitHub Flavored Markdown',
array( $this, 'print_section_gfm' ),
'gfm-setting-admin'
);
add_settings_field(
'render_url',
'Render URL',
array( $this, 'create_gfm_render_url_field' ),
'gfm-setting-admin',
'setting_section_gfm'
);
}
function admin_menu() {
if ( function_exists( 'add_options_page' ) ) {
add_options_page(
'GFM Plugin Settings',
'WP GFM',
'manage_options',
'wp-gfm',
array( $this, 'options_page' )
);
}
}
function options_page() {
?>
<div class="wrap wrap-wp-gfm">
<h2>WP GFM Settings</h2>
<!--suppress HtmlUnknownTarget -->
<form method="post" action="options.php">
<?php
settings_fields( 'gfm_option_group' );
do_settings_sections( 'gfm-setting-admin' );
?>
<?php submit_button(); ?>
</form>
</div>
<?php
}
function option_sanitize_gfm( $input ) {
if ( get_option( 'gfm' ) === false ) {
add_option( 'gfm', $input );
} else {
update_option( 'gfm', $input );
}
return $input;
}
function setting_section_general() {
}
function create_gfm_general_ad_field() {
echo '<input type="checkbox" id="general_ad" name="gfm_array[general_ad]" value="1" class="code" '
. checked( 1, $this->gfm_options['general_ad'], false ) . ' /> Add a link of wp-gfm plugin to content';
}
function print_section_php_markdown() {
}
function create_gfm_php_md_always_convert_field() {
echo '<input type="checkbox" id="php_md_always_convert" name="gfm_array[php_md_always_convert]" value="1" class="code" '
. checked( 1, $this->gfm_options['php_md_always_convert'], false ) . ' /> All contents are markdown!'
. '<p class="description">The plugin converts content even if it is not surrounded by [markdown]</p>';
}
function create_gfm_php_md_use_autolink_field() {
echo '<input type="checkbox" id="gfm_php_md_use_autolink" name="gfm_array[php_md_use_autolink]" value="1" class="code" '
. checked( 1, $this->gfm_options['php_md_use_autolink'], false ) . ' /> Use AutoLink';
}
function create_gfm_php_md_fenced_code_blocks_template_field() {
$value = esc_attr( $this->gfm_options['php_md_fenced_code_blocks_template'] );
echo '<textarea id="gfm_php_md_fenced_code_blocks_template" name="gfm_array[php_md_fenced_code_blocks_template]" class="large-text">' . $value . '</textarea>'
. '<p class="description">'
. '{{lang}}, {{title}}, {{codeblock}}<br/>'
. 'For <a href="https://code.google.com/p/google-code-prettify/" target="_blank" rel="noopener">google-code-prettify</a>: <code>' . esc_attr( self::FENCED_CODE_BLOCKS_TEMPLATE_FOR_GOOGLE_CODE_PRETTIFY ) . '</code><br/>'
. '</p>';
}
function print_section_gfm() {
}
function create_gfm_render_url_field() {
$value = esc_attr( $this->gfm_options['render_url'] );
echo '<input type="text" id="gfm_render_url" name="gfm_array[render_url]" value="' . $value . '" class="regular-text"/>';
}
function shortcode_gfm( /** @noinspection PhpUnusedParameterInspection */ $atts, $content = '' ) {
return '<div class="markdown-body gfm-content">' . $this->convert_html_by_render_url( $this->gfm_options['render_url'], $content ) . '</div>';
}
function shortcode_markdown( /** @noinspection PhpUnusedParameterInspection */ $atts, $content = '' ) {
if ( $this->has_converter ) {
return '<div class="markdown-body markdown-content">' . \Gfm\Markdown\Extra::defaultTransform( $content ) . '</div>';
}
return $content;
}
/**
* @param $use_gfm
* @param $atts
* @param $content
* @return string
*/
function shortcode_embed( $use_gfm, $atts, /** @noinspection PhpUnusedParameterInspection */ $content ) {
/**
* @var string $url
*/
$defaults = array( 'url' => '' );
extract( shortcode_atts( $defaults, $atts ) );
if ( empty( $url ) ) {
return '';
}
$args = array();
$response = wp_remote_get( $url, $args );
if ( ! is_wp_error( $response ) ) {
$body = wp_remote_retrieve_body( $response );
// https://raw.githubusercontent.com/makotokw/wp-gfm/master/README.md ->
// https://github.com/makotokw/wp-gfm/blob/master/README.md
$r = '/^https?:\/\/raw\.githubusercontent\.com/';
if ( preg_match( $r, $url ) ) {
$url = preg_replace( $r, 'https://github.com', $url );
$url = '<a href="' . $url . '">' . $url . '</a>';
}
return '<div class="markdown-file">'
. ($use_gfm ? $this->shortcode_gfm( $atts, $body ) : $this->shortcode_markdown( $atts, $body ))
. '<div class="markdown-meta">' . $url . $this->ad_html . '</div>'
. '</div>';
}
return '';
}
function shortcode_embed_gfm( $atts, /** @noinspection PhpUnusedParameterInspection */ $content ) {
return $this->shortcode_embed( true, $atts, $content );
}
function shortcode_embed_markdown( $atts, /** @noinspection PhpUnusedParameterInspection */ $content ) {
return $this->shortcode_embed( false, $atts, $content );
}
function force_convert( $content ) {
$content = preg_replace( '{\[/?markdown\]}', '', $content );
return wp_markdown( $content );
}
function the_content( $content ) {
if ( class_exists( '\Gfm\Markdown\Extra' ) ) {
if ( isset( $GLOBALS['post'] ) ) {
if ( isset( $GLOBALS['post']->ID ) ) {
\Gfm\Markdown\Extra::setElementIdPrefix( 'post-' . $GLOBALS['post']->ID . '-md-' );
}
}
}
$content = preg_replace_callback( '/\[markdown\](.*?)\[\/markdown\]/s', create_function( '$matches', 'return wp_markdown($matches[1]);' ), $content );
$content = preg_replace_callback( '/\[gfm\](.*?)\[\/gfm\]/s', create_function( '$matches', 'return wp_fgm($matches[1]);' ), $content );
return $content;
}
function the_content_ad( $context ) {
if ( strpos( $context, '<div class="markdown-body markdown-content">' ) !== false ) {
return $context . '<div class="wp-gfm-footer">' . $this->ad_html . '</div>';
}
return $context;
}
function pre_comment_content( $comment ) {
$comment = stripslashes( $comment );
$comment = $this->the_content( $comment );
$comment = addslashes( $comment );
return $comment;
}
function convert_html_by_render_url( $render_url, $text ) {
$response = wp_remote_request(
$render_url,
array(
'method' => 'POST',
'timeout' => 10,
'user-agent' => $this->agent,
'headers' => array( 'Content-Type' => 'text/plain; charset=UTF-8' ),
'body' => $text,
)
);
if ( is_wp_error( $response ) ) {
$msg = self::NAME . ' HttpError: ' . $response->get_error_message();
error_log( $msg . ' on ' . $render_url );
return $msg;
}
if ( $response && isset( $response['response']['code'] ) && 200 != $response['response']['code'] ) {
$msg = sprintf( self::NAME . ' HttpError: %s %s', $response['response']['code'], $response['response']['message'] );
error_log( $msg . ' on ' . $render_url );
return $msg;
}
return wp_remote_retrieve_body( $response );
}
function admin_quicktags() {
// http://wordpress.stackexchange.com/questions/37849/add-custom-shortcode-button-to-editor
/* Add custom Quicktag buttons to the editor Wordpress ver. 3.3 and above only
*
* Params for this are:
* - Button HTML ID (required)
* - Button display, value="" attribute (required)
* - Opening Tag (required)
* - Closing Tag (required)
* - Access key, accesskey="" attribute for the button (optional)
* - Title, title="" attribute (optional)
* - Priority/position on bar, 1-9 = first, 11-19 = second, 21-29 = third, etc. (optional)
*/
?>
<script type="text/javascript">
(function ($) {
if (typeof(QTags) != 'undefined') {
var ids = ['markdown', 'gfm'];
$.each(ids, function (index, c) {
QTags.addButton(c, '[' + c + ']', '[' + c + ']', '[/' + c + ']');
});
}
})(jQuery);
</script>
<?php
}
}
add_action( 'init', 'wp_gfm_init' );
function wp_gfm_init() {
$plugin = WP_GFM::get_instance();
// use Michelf/Markdown if PHP 5.3+
if ( defined( 'PHP_VERSION_ID' ) ) {
if ( PHP_VERSION_ID >= 50300 ) {
if ( file_exists( dirname( __FILE__ ) . '/vendor/autoload.php' ) ) {
require_once dirname( __FILE__ ) . '/vendor/autoload.php';
$plugin->php_markdown_init();
}
}
}
include_once 'updater.php';
if ( is_admin() && class_exists( 'WP_GitHub_Updater' ) ) {
/** @noinspection PhpUnusedLocalVariableInspection */
$updater = new WP_GitHub_Updater(
array(
'slug' => plugin_basename( __FILE__ ),
'proper_folder_name' => 'wp-gfm',
'api_url' => 'https://api.github.com/repos/makotokw/wp-gfm',
'raw_url' => 'https://raw.github.com/makotokw/wp-gfm/master',
'github_url' => 'https://github.com/makotokw/wp-gfm',
'zip_url' => 'https://github.com/makotokw/wp-gfm/archive/master.zip',
'sslverify' => true,
'requires' => '3.1',
'tested' => '4.3.1',
'readme' => 'README.md',
)
);
}
}
function wp_markdown( $content ) {
$p = WP_GFM::get_instance();
return $p->shortcode_markdown( null, $content );
}
function wp_fgm( $content ) {
$p = WP_GFM::get_instance();
if ( ! empty( $p->gfm_options['render_url'] ) ) {
return $p->shortcode_gfm( null, $content );
} else {
return $p->shortcode_markdown( null, $content );
}
}