/
GistButton.subs.php
117 lines (102 loc) · 3.85 KB
/
GistButton.subs.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
<?php
/**
* @package Elk Gist Button
* @author Joshua Dickerson
* @copyright (c) 2015 Joshua Dickerson
* @license WTFPL http://www.wtfpl.net/txt/copying/
*
* @version 1.1
*
*/
if (!defined('ELK'))
die('No access...');
/**
* ibc_gist_button
*
* - Subs hook for 1.0.x, integrate_bbc_codes hook, Called from Subs.php
* - Used to add[gist][/gist] parsing values
*
* @param mixed[] $codes array of codes as defined for parse_bbc
* @param mixed[] $no_autolink_tags array of no autolink codes as defined for parse_bbc
*/
function ibc_gist_button(&$codes, &$no_autolink_tags)
{
global $modSettings;
// Only for when bbc is on
if (empty($modSettings['enableBBC']))
return;
// Make sure the admin has not disabled the gist tag
if (!empty($modSettings['disabledBBC']))
{
foreach (explode(',', $modSettings['disabledBBC']) as $tag)
{
if ($tag === 'gist')
return;
}
}
// All good, lets add our tag info to the parser, this controls how the tag will render
// with parse_bbc when found in a post
$codes[] = array(
'tag' => 'gist',
'type' => 'unparsed_content',
'content' => '<script src="https://gist.github.com/$1.js"></script>',
'validate' => function(&$tag, &$data, $disabled) {
$data = strtr($data, array('<br />' => ''));
if (strpos($data, 'http://') !== 0 && strpos($data, 'https://') !== 0)
$data = 'https://' . $data;
$data = ltrim(parse_url($data, PHP_URL_PATH), '\/');
},
);
$no_autolink_tags[] = 'gist';
}
/**
* iab_gist_button
*
* - Codes hook for 1.1.x, integrate_additional_bbc, Called from ParseWrapper getCodes()
* - Used to add[gist][/gist] parsing values
*
* @param mixed[] $codes array of codes as defined for parse_bbc
*/
function iab_gist_button(&$codes)
{
global $context;
// Ddd our tag info to the parser, this controls how the tag will render when found in a post
$codes[] =
array(
\BBC\Codes::ATTR_TAG => 'gist',
\BBC\Codes::ATTR_TYPE => \BBC\Codes::TYPE_UNPARSED_CONTENT,
\BBC\Codes::ATTR_CONTENT => '<script src="//gist.github.com/$1.js"></script>',
\BBC\Codes::ATTR_VALIDATE => function(&$tag, &$data, $disabled) {
$data = strtr($data, array('<br />' => ''));
if (strpos($data, 'http://') !== 0 && strpos($data, 'https://') !== 0)
{
$data = 'https://' . $data;
}
$data = ltrim(parse_url($data, PHP_URL_PATH), '\/');
},
\BBC\Codes::ATTR_BLOCK_LEVEL => true,
\BBC\Codes::ATTR_AUTOLINK => false,
\BBC\Codes::ATTR_LENGTH => 4,
);
$context['css_rules']['all'] = '.gist-data {max-height: 20em;}';
}
/**
* ibb_gist_button
*
* - Editor hook, integrate_bbc_buttons hook, Called from Editor.subs.php
* - Used to add buttons to the editor menu bar
*
* @param mixed[] $bbc_tags
*/
function ibb_gist_button(&$bbc_tags)
{
global $context;
// This is the group we intend to modify
$where = $bbc_tags['row2'][0];
// And here we insert the new value after code
$bbc_tags['row2'][0] = elk_array_insert($where, 'code', array('gist'), 'after', false);
// Add the javascript, this tells the editor what to do with the new button
loadJavascriptFile('GistButton.js', array(), 'GistButton');
// We need to supply the css for the button image, here we use a data-url to save an image call
$context['html_headers'] .= '<style>.sceditor-button-gist div {background: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAVpJREFUeNqM0s0rRGEUx/F7x0RKxob4A6bZKBYWFkLZqIkkC7FUsrCwoCxsZcN/IFmIP4E9ZWnyurBR3krZeH8b1/dMv5vTpDue+szzzL33nJ5znieIoihIGCGmMIt0+ctSbIUETbhHEbm/EqSD5PGOC2TwgHo04xaPv9tIHhbUoPUMXjAcx4aln9BKDcYxgRR20IJNDKEO69hCFie2JnYx3sGYJcQ5jrU2PTjEDbpwpeeXWPZN3NOLnLb8hm1UoaBAG3P6btR26pt4rblDDarRs6KOMh7fmr/idZxgAW3Y0H/r/IqCfYKU5o/yB1b7kY5tGp04Uwmh++5Vcx59PoGNWtV3pznQXK2SbLf76s8kVv09yLpGRro0SwoawIgrt1fNzPtT2FVd/WjVCdiL9qQb5k8ho3Ia8eTKea50TeMd2LZOXQmfmP9PrL/K3RjURTrAmk4lMcGPAAMAEvmJGW+ZZPAAAAAASUVORK5CYII=)}</style>';
}