forked from b2evolution/b2evolution
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_markdown.plugin.php
More file actions
635 lines (575 loc) · 19 KB
/
_markdown.plugin.php
File metadata and controls
635 lines (575 loc) · 19 KB
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
<?php
/**
* This file implements the Markdown plugin for b2evolution
*
* Markdown
*
* b2evolution - {@link http://b2evolution.net/}
* Released under GNU GPL License - {@link http://b2evolution.net/about/gnu-gpl-license}
* @copyright (c)2003-2015 by Francois Planque - {@link http://fplanque.com/}
*
* @package plugins
* @ignore
*/
if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
/**
* @package plugins
*/
class markdown_plugin extends Plugin
{
var $code = 'b2evMark';
var $name = 'Markdown';
var $priority = 20;
var $version = '5.0.0';
var $group = 'rendering';
var $short_desc;
var $long_desc;
var $help_topic = 'markdown-plugin';
var $number_of_installs = 1;
/**
* Init
*/
function PluginInit( & $params )
{
require_once( dirname( __FILE__ ).'/_parsedown.inc.php' );
$this->short_desc = T_('Markdown');
$this->long_desc = T_('Accepted formats:<br />
# h1<br />
## h2<br />
### h3<br />
#### h4<br />
##### h5<br />
###### h6<br />
--- (horizontal rule)<br />
* * * (horizontal rule)<br />
- - - - (horizontal rule)<br />
`code spans`<br />
> blockquote');
}
/**
* Define here default collection/blog settings that are to be made available in the backoffice.
*
* @param array Associative array of parameters.
* @return array See {@link Plugin::get_coll_setting_definitions()}.
*/
function get_coll_setting_definitions( & $params )
{
$default_params = array_merge( $params, array(
'default_comment_rendering' => 'never',
'default_post_rendering' => 'opt-in'
) );
return array_merge( parent::get_coll_setting_definitions( $default_params ),
array(
'links' => array(
'label' => T_( 'Links' ),
'type' => 'checkbox',
'note' => T_( 'Detect and convert markdown link markup.' ),
'defaultvalue' => 0,
),
'images' => array(
'label' => T_( 'Images' ),
'type' => 'checkbox',
'note' => T_( 'Detect and convert markdown image markup.' ),
'defaultvalue' => 0,
),
'text_styles' => array(
'label' => T_( 'Italic & Bold styles' ),
'type' => 'checkbox',
'note' => T_( 'Detect and convert markdown italics and bold markup.' ),
'defaultvalue' => 0,
),
'min_h_level' => array(
'label' => T_( 'Top Heading Level' ),
'type' => 'integer',
'size' => 1,
'maxlength' => 1,
'note' => T_( 'This plugin will adjust headings so they always start at the level you want: 1 for <H1>, 2 for <H2>, etc.' ),
'defaultvalue' => 1,
'valid_range' => array(
'min' => 1, // from <h1>
'max' => 6, // to <h6>
),
),
)
);
}
/**
* Define here default message settings that are to be made available in the backoffice.
*
* @param array Associative array of parameters.
* @return array See {@link Plugin::GetDefaultSettings()}.
*/
function get_msg_setting_definitions( & $params )
{
// set params to allow rendering for messages by default
$default_params = array_merge( $params, array( 'default_msg_rendering' => 'opt-in' ) );
return parent::get_msg_setting_definitions( $default_params );
}
/**
* Perform rendering
*
* @param array Associative array of parameters
* 'data': the data (by reference). You probably want to modify this.
* 'format': see {@link format_to_output()}. Only 'htmlbody' and 'entityencoded' will arrive here.
* @return boolean true if we can render something for the required output format
*/
function RenderItemAsHtml( & $params )
{
$content = & $params['data'];
if( !empty( $params['Item'] ) )
{ // Get Item from params
$Item = & $params['Item'];
}
elseif( !empty( $params['Comment'] ) )
{ // Get Item from Comment
$Comment = & $params['Comment'];
$Item = & $Comment->get_Item();
}
if( ! empty( $Item ) )
{ // We are rendering Item or Comment now, Get the settings depending on Blog
$item_Blog = & $Item->get_Blog();
$text_styles_enabled = $this->get_coll_setting( 'text_styles', $item_Blog );
$links_enabled = $this->get_coll_setting( 'links', $item_Blog );
$images_enabled = $this->get_coll_setting( 'images', $item_Blog );
$min_h_level = $this->get_coll_setting( 'min_h_level', $item_Blog );
}
elseif( ! empty( $params['Message'] ) )
{ // We are rendering Message now, Use FALSE by default because we don't have the settings
$text_styles_enabled = false;
$links_enabled = false;
$images_enabled = false;
$min_h_level = 1; // Use default value
}
else
{ // Unknown call, Don't render this case
return;
}
// Init parser class with blog settings
$Parsedown = Parsedown::instance();
$Parsedown->parse_font_styles = $text_styles_enabled;
$Parsedown->parse_links = $links_enabled;
$Parsedown->parse_images = $images_enabled;
$Parsedown->min_h_level = $min_h_level;
// Parse markdown code to HTML
if( stristr( $content, '<code' ) !== false || stristr( $content, '<pre' ) !== false )
{ // Call replace_content() on everything outside code/pre:
$content = callback_on_non_matching_blocks( $content,
'~<(code|pre)[^>]*>.*?</\1>~is',
array( $Parsedown, 'parse' ) );
}
else
{ // No code/pre blocks, replace on the whole thing
$content = $Parsedown->parse( $content );
}
return true;
}
/**
* Event handler: Defines blog settings by its kind. Use {@link get_collection_kinds()} to return
* an array of available blog kinds and their names.
* Define new blog kinds in {@link Plugin::GetCollectionKinds()} method of your plugin.
*
* Note: You have to change $params['Blog'] (which gets passed by reference).
*
* @param array Associative array of parameters
* - 'Blog': created Blog (by reference)
* - 'kind': the kind of created blog (by reference)
*/
function InitCollectionKinds( & $params )
{
if( empty( $params['Blog'] ) || empty( $params['kind'] ) )
{ // Invalid data, Exit here
return;
}
switch( $params['kind'] )
{
case 'forum':
case 'manual':
$params['Blog']->set_setting( 'plugin'.$this->ID.'_coll_apply_rendering', 'opt-out' );
$params['Blog']->set_setting( 'plugin'.$this->ID.'_coll_apply_comment_rendering', 'opt-out' );
$params['Blog']->set_setting( 'plugin'.$this->ID.'_images', '1' );
$params['Blog']->set_setting( 'plugin'.$this->ID.'_links', '1' );
break;
}
}
/**
* Display a toolbar
*
* @todo dh> This seems to be a lot of Javascript. Please try exporting it in a
* (dynamically created) .js src file. Then we could use cache headers
* to let the browser cache it.
* @param array Associative array of parameters
* @return boolean did we display a toolbar?
*/
function AdminDisplayToolbar( & $params )
{
if( ! empty( $params['Item'] ) )
{ // Item is set, get Blog from post
$edited_Item = & $params['Item'];
$Blog = & $edited_Item->get_Blog();
}
if( empty( $Blog ) )
{ // Item is not set, try global Blog
global $Blog;
if( empty( $Blog ) )
{ // We can't get a Blog, this way "apply_rendering" plugin collection setting is not available
return false;
}
}
$coll_setting_name = ( $params['target_type'] == 'Comment' ) ? 'coll_apply_comment_rendering' : 'coll_apply_rendering';
$apply_rendering = $this->get_coll_setting( $coll_setting_name, $Blog );
if( empty( $apply_rendering ) || $apply_rendering == 'never' )
{ // Plugin is not enabled for current case, so don't display a toolbar:
return false;
}
// Print toolbar on screen
return $this->DisplayCodeToolbar( $Blog );
}
/**
* Event handler: Called when displaying editor toolbars.
*
* @param array Associative array of parameters
* @return boolean did we display a toolbar?
*/
function DisplayCommentToolbar( & $params )
{
if( ! empty( $params['Comment'] ) )
{ // Comment is set, get Blog from comment
$Comment = & $params['Comment'];
if( !empty( $Comment->item_ID ) )
{
$comment_Item = & $Comment->get_Item();
$Blog = & $comment_Item->get_Blog();
}
}
if( empty( $Blog ) )
{ // Comment is not set, try global Blog
global $Blog;
if( empty( $Blog ) )
{ // We can't get a Blog, this way "apply_comment_rendering" plugin collection setting is not available
return false;
}
}
$apply_rendering = $this->get_coll_setting( 'coll_apply_comment_rendering', $Blog );
if( empty( $apply_rendering ) || $apply_rendering == 'never' )
{ // Plugin is not enabled for current case, so don't display a toolbar:
return false;
}
// Print toolbar on screen
return $this->DisplayCodeToolbar( $Blog );
}
/**
* Event handler: Called when displaying editor toolbars for message.
*
* @param array Associative array of parameters
* @return boolean did we display a toolbar?
*/
function DisplayMessageToolbar( & $params )
{
$apply_rendering = $this->get_msg_setting( 'msg_apply_rendering' );
if( ! empty( $apply_rendering ) && $apply_rendering != 'never' )
{ // Print toolbar on screen
return $this->DisplayCodeToolbar( NULL, $params );
}
return false;
}
/**
* Display Toolbar
*
* @param object Blog
*/
function DisplayCodeToolbar( $Blog = NULL, $params = array() )
{
global $Hit;
if( $Hit->is_lynx() )
{ // let's deactivate toolbar on Lynx, because they don't work there.
return false;
}
if( empty( $Blog ) )
{ // Use FALSE by default because we don't have the settings for Message
$text_styles_enabled = false;
$links_enabled = false;
$images_enabled = false;
}
else
{ // Get plugin setting values depending on Blog
$text_styles_enabled = $this->get_coll_setting( 'text_styles', $Blog );
$links_enabled = $this->get_coll_setting( 'links', $Blog );
$images_enabled = $this->get_coll_setting( 'images', $Blog );
}
// Load js to work with textarea
require_js( 'functions.js', 'blog', true, true );
?><script type="text/javascript">
//<![CDATA[
var markdown_btns = new Array();
var markdown_open_tags = new Array();
function markdown_btn( id, text, title, tag_start, tag_end, style, open, grp_pos )
{
this.id = id; // used to name the toolbar button
this.text = text; // label on button
this.title = title; // title
this.tag_start = tag_start; // open tag
this.tag_end = tag_end; // close tag
this.style = style; // style on button
this.open = open; // set to -1 if tag does not need to be closed
this.grp_pos = grp_pos; // position in the group, e.g. 'last'
}
<?php
if( $text_styles_enabled )
{ // Show thess buttons only when plugin setting "Italic & Bold styles" is enabled ?>
markdown_btns[markdown_btns.length] = new markdown_btn(
'mrkdwn_bold','bold', '<?php echo TS_('Bold') ?>',
'**','**',
'font-weight:bold'
);
markdown_btns[markdown_btns.length] = new markdown_btn(
'mrkdwn_italic','italic', '<?php echo TS_('Italic') ?>',
'*','*',
'font-style:italic', -1, 'last'
);
<?php
}
if( $links_enabled )
{ // Show this button only when plugin setting "Links" is enabled ?>
markdown_btns[markdown_btns.length] = new markdown_btn(
'mrkdwn_link', 'link','<?php echo TS_('Link') ?>',
'','',
'text-decoration:underline', -1<?php echo ! $images_enabled ? ', \'last\'' : '' ?>
);
<?php
}
if( $images_enabled )
{ // Show this button only when plugin setting "Images" is enabled ?>
markdown_btns[markdown_btns.length] = new markdown_btn(
'mrkdwn_img', 'img','<?php echo TS_('Image') ?>',
'','',
'', -1, 'last'
);
<?php
} ?>
markdown_btns[markdown_btns.length] = new markdown_btn(
'mrkdwn_h1','H1', '<?php echo TS_('Header 1') ?>',
'\n# ','',
'', -1
);
markdown_btns[markdown_btns.length] = new markdown_btn(
'mrkdwn_h1','H2', '<?php echo TS_('Header 2') ?>',
'\n## ','',
'', -1
);
markdown_btns[markdown_btns.length] = new markdown_btn(
'mrkdwn_h1','H3', '<?php echo TS_('Header 3') ?>',
'\n### ','',
'', -1
);
markdown_btns[markdown_btns.length] = new markdown_btn(
'mrkdwn_h1','H4', '<?php echo TS_('Header 4') ?>',
'\n#### ','',
'', -1
);
markdown_btns[markdown_btns.length] = new markdown_btn(
'mrkdwn_h1','H5', '<?php echo TS_('Header 5') ?>',
'\n##### ','',
'', -1
);
markdown_btns[markdown_btns.length] = new markdown_btn(
'mrkdwn_h1','H6', '<?php echo TS_('Header 6') ?>',
'\n###### ','',
'', -1, 'last'
);
markdown_btns[markdown_btns.length] = new markdown_btn(
'mrkdwn_li','li', '<?php echo TS_('Unordered list item') ?>',
'\n* ','',
'', -1
);
markdown_btns[markdown_btns.length] = new markdown_btn(
'mrkdwn_ol','ol', '<?php echo TS_('Ordered list item') ?>',
'\n1. ','',
'', -1
);
markdown_btns[markdown_btns.length] = new markdown_btn(
'mrkdwn_li','blockquote', '<?php echo TS_('Blockquote') ?>',
'\n> ','',
'', -1, 'last'
);
markdown_btns[markdown_btns.length] = new markdown_btn(
'mrkdwn_codespan','codespan', '<?php echo TS_('Codespan') ?>',
'`','`',
'', -1
);
markdown_btns[markdown_btns.length] = new markdown_btn(
'mrkdwn_preblock','preblock', '<?php echo TS_('Preformatted code block') ?>',
'\n\t','',
'', -1, 'last'
);
markdown_btns[markdown_btns.length] = new markdown_btn(
'mrkdwn_codeblock','codeblock', '<?php echo TS_('Highlighted code block') ?>',
'\n```\n','\n```\n',
'', -1, 'last'
);
markdown_btns[markdown_btns.length] = new markdown_btn(
'mrkdwn_hr','hr', '<?php echo TS_('Horizontal Rule') ?>',
'\n---\n','',
'', -1
);
markdown_btns[markdown_btns.length] = new markdown_btn(
'mrkdwn_br','<br>', '<?php echo TS_('Line Break') ?>',
' \n','',
'', -1
);
function markdown_show_btn( button, i )
{
if( button.id == 'mrkdwn_img' )
{ // Image
document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" title="' + button.title
+ '" style="' + button.style + '" class="<?php echo $this->get_template( 'toolbar_button_class' ); ?>" data-func="markdown_insert_lnkimg|b2evoCanvas|img" value="' + button.text + '" />');
}
else if( button.id == 'mrkdwn_link' )
{ // Link
document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" title="' + button.title
+ '" style="' + button.style + '" class="<?php echo $this->get_template( 'toolbar_button_class' ); ?>" data-func="markdown_insert_lnkimg|b2evoCanvas" value="' + button.text + '" />');
}
else
{ // Normal buttons:
document.write('<input type="button" id="' + button.id + '" accesskey="' + button.access + '" title="' + button.title
+ '" style="' + button.style + '" class="<?php echo $this->get_template( 'toolbar_button_class' ); ?>" data-func="markdown_insert_tag|b2evoCanvas|'+i+'" value="' + button.text + '" />');
}
}
// Memorize a new open tag
function markdown_add_tag( button )
{
if( markdown_btns[button].tag_end != '' )
{
markdown_open_tags[markdown_open_tags.length] = button;
document.getElementById( markdown_btns[button].id ).value = '/' + document.getElementById( markdown_btns[button].id ).value;
}
}
// Forget about an open tag
function markdown_remove_tag( button )
{
for( i = 0; i < markdown_open_tags.length; i++ )
{
if( markdown_open_tags[i] == button )
{
markdown_open_tags.splice( i, 1 );
document.getElementById( markdown_btns[button].id ).value = document.getElementById( markdown_btns[button].id ).value.replace( '/', '' );
}
}
}
function markdown_check_open_tags( button )
{
var tag = 0;
for( i = 0; i < markdown_open_tags.length; i++ )
{
if( markdown_open_tags[i] == button )
{
tag++;
}
}
if( tag > 0 )
{
return true; // tag found
}
else
{
return false; // tag not found
}
}
function markdown_close_all_tags()
{
var count = markdown_open_tags.length;
for( o = 0; o < count; o++ )
{
markdown_insert_tag( b2evoCanvas, markdown_open_tags[markdown_open_tags.length - 1] );
}
}
function markdown_toolbar( title )
{
document.write( '<?php echo $this->get_template( 'toolbar_title_before' ); ?>' + title + '<?php echo $this->get_template( 'toolbar_title_after' ); ?>' );
document.write( '<?php echo $this->get_template( 'toolbar_group_before' ); ?>' );
for( var i = 0; i < markdown_btns.length; i++ )
{
markdown_show_btn( markdown_btns[i], i );
if( markdown_btns[i].grp_pos == 'last' && i > 0 && i < markdown_btns.length - 1 )
{ // Separator between groups
document.write( '<?php echo $this->get_template( 'toolbar_group_after' ).$this->get_template( 'toolbar_group_before' ); ?>' );
}
}
document.write( '<?php echo $this->get_template( 'toolbar_group_after' ).$this->get_template( 'toolbar_group_before' ); ?>' );
document.write( '<input type="button" id="mrkdwn_close" class="<?php echo $this->get_template( 'toolbar_button_class' ); ?>" data-func="markdown_close_all_tags" title="<?php echo format_to_output( T_('Close all tags'), 'htmlattr' ); ?>" value="X" />' );
document.write( '<?php echo $this->get_template( 'toolbar_group_after' ); ?>' );
}
function markdown_insert_tag( field, i )
{
// we need to know if something is selected.
// First, ask plugins, then try IE and Mozilla.
var sel_text = b2evo_Callbacks.trigger_callback( "get_selected_text_for_" + field.id );
var focus_when_finished = false; // used for IE
if( sel_text == null )
{ // detect selection:
//IE support
if( document.selection )
{
field.focus();
var sel = document.selection.createRange();
sel_text = sel.text;
focus_when_finished = true;
}
//MOZILLA/NETSCAPE support
else if( field.selectionStart || field.selectionStart == '0' )
{
var startPos = field.selectionStart;
var endPos = field.selectionEnd;
sel_text = ( startPos != endPos );
}
}
if( sel_text )
{ // some text selected
textarea_wrap_selection( field, markdown_btns[i].tag_start, markdown_btns[i].tag_end, 0 );
}
else
{
if( !markdown_check_open_tags(i) || markdown_btns[i].tag_end == '' )
{
textarea_wrap_selection( field, markdown_btns[i].tag_start, '', 0 );
markdown_add_tag(i);
}
else
{
textarea_wrap_selection( field, '', markdown_btns[i].tag_end, 0 );
markdown_remove_tag(i);
}
}
if( focus_when_finished )
{
field.focus();
}
}
function markdown_insert_lnkimg( field, type )
{
var url = prompt( '<?php echo TS_('URL') ?>:', 'http://' );
if( url )
{
url = '[' + prompt('<?php echo TS_('Text') ?>:', '') + ']'
+ '(' + url;
var title = prompt( '<?php echo TS_('Title') ?>:', '' );
if( title != '' )
{
url += ' "' + title + '"';
}
url += ')';
if( typeof( type ) != 'undefined' && type == 'img' )
{ // for <img> tag
url = '!' + url;
}
textarea_wrap_selection( field, url, '', 1 );
}
}
//]]>
</script><?php
echo $this->get_template( 'toolbar_before', array( '$toolbar_class$' => $this->code.'_toolbar' ) );
?><script type="text/javascript">markdown_toolbar( '<?php echo T_('Markdown').': '; ?>' );</script><?php
echo $this->get_template( 'toolbar_after' );
return true;
}
}
?>