-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtinymce_codehighlight.module
More file actions
175 lines (157 loc) · 5.34 KB
/
tinymce_codehighlight.module
File metadata and controls
175 lines (157 loc) · 5.34 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
<?php
/**
* @file
* Callbacks and hook implementations for the TinyMCE Code Highlight module.
*/
/**
* Implements hook_library_info().
*/
function tinymce_codehighlight_library_info() {
$module_path = backdrop_get_path('module', 'tinymce_codehighlight');
$path = $module_path . '/libraries/prismjs';
$js_options = array(
'type' => 'file',
'weight' => -1,// Make sure this loads before TinyMCE.
);
$libraries['Prism.js_local'] = array(
'title' => 'Prism.js (local)',
'website' => 'https://prismjs.com/',
'version' => '1.30.0',
'js' => array(
$path . '/components/prism-core.min.js' => $js_options,
$path . '/plugins/autoloader/prism-autoloader.min.js' => $js_options,
$module_path . '/js/tinymce-codehighlight.js' => $js_options,
),
'css' => array(
$module_path . '/css/tinymce-codehighlight-common.css' => array(),
),
);
// @see js/tinymce-codehighlight.js
$languages_path = base_path() . $module_path . '/libraries/prismjs/components/';
$libraries['Prism.js_local']['js'][] = array(
'type' => 'setting',
'data' => array('prismjsAutoloaderLangPath' => $languages_path),
);
// Optional copy to clipboard button.
if (config_get('tinymce_codehighlight.settings', 'copybutton')) {
$libraries['Prism.js_local']['js'][$path . '/plugins/toolbar/prism-toolbar.min.js'] = $js_options;
$libraries['Prism.js_local']['js'][$path . '/plugins/copy-to-clipboard/prism-copy-to-clipboard.min.js'] = $js_options;
}
$theme = _tinymce_codehighlight_get_active_theme();
$stylesheet = $theme['stylesheet_path'];
$libraries['Prism.js_local']['css'][$stylesheet] = array('type' => 'file');
return $libraries;
}
/**
* Callback to get the active theme.
*
* @return array
*/
function _tinymce_codehighlight_get_active_theme() {
$themes = _tinymce_codehighlight_get_all_themes();
$active_theme = config_get('tinymce_codehighlight.settings', 'theme');
if ($active_theme && array_key_exists($active_theme, $themes)) {
return $themes[$active_theme];
}
// Fallback if not found.
return $themes['default'];
}
/**
* Callback to collect theme info.
*
* @return array
*/
function _tinymce_codehighlight_get_all_themes() {
$themes = &backdrop_static(__FUNCTION__, NULL);
if (!isset($themes)) {
$themes = module_invoke_all('tinymce_codehighlight_theme_info');
}
return $themes;
}
/**
* Implements hook_tinymce_codehighlight_theme_info().
*/
function tinymce_codehighlight_tinymce_codehighlight_theme_info() {
// Some custom default themes, as none of the Prism.js themes is fully
// compatible with Backdrop, they usually conflict with theme's styles, and
// don't play nicely with the markup inside CKEditor5 (half styled is worse
// than not styled at all).
$path = backdrop_get_path('module', 'tinymce_codehighlight');
$themes = array();
$themes['default'] = array(
'name' => 'default',
'label' => t('Default'),
'stylesheet_path' => $path . '/themes/default/prismjs-styles.css',
'screenshot_path' => $path . '/themes/default/screenshot-default.png',
);
$themes['dark'] = array(
'name' => 'dark',
'label' => t('Dark'),
'stylesheet_path' => $path . '/themes/dark/prismjs-styles.css',
'screenshot_path' => $path . '/themes/dark/screenshot-dark.png',
);
$themes['striped'] = array(
'name' => 'striped',
'label' => t('Striped'),
'stylesheet_path' => $path . '/themes/striped/prismjs-styles.css',
'screenshot_path' => $path . '/themes/striped/screenshot-striped.png',
);
return $themes;
}
/**
* Implements hook_menu().
*/
function tinymce_codehighlight_menu() {
$items['admin/config/content/tinymce-codehighlight'] = array(
'title' => 'TinyMCE Code Highlight',
'page callback' => 'backdrop_get_form',
'page arguments' => array('tinymce_codehighlight_settings_form'),
'access arguments' => array('administer filters'),
'file' => 'tinymce_codehighlight.admin.inc',
);
return $items;
}
/**
* Implements hook_preprocess_HOOK().
*/
function tinymce_codehighlight_preprocess_page(&$variables) {
if (!backdrop_is_html()) {
return;
}
backdrop_add_library('tinymce_codehighlight', 'Prism.js_local');
if (config_get('tinymce_codehighlight.settings', 'copybutton')) {
backdrop_add_icons(array(
'copy',
'check',
));
}
}
/**
* Implements hook_tinymce_options_alter().
*/
function tinymce_codehighlight_tinymce_options_alter(array &$options, $format) {
$options['tiny_options']['codesample_global_prismjs'] = TRUE;
$theme = _tinymce_codehighlight_get_active_theme();
$stylesheet = $theme['stylesheet_path'];
$options['tiny_options']['content_css'][] = base_path() . backdrop_get_path('module', 'tinymce_codehighlight') . '/css/tinymce-codehighlight-common.css';
$options['tiny_options']['content_css'][] = base_path() . $stylesheet;
$enabled = config_get('tinymce_codehighlight.settings', 'enabled_with_labels');
$tiny_options = array();
foreach ($enabled as $name => $label) {
$tiny_options[] = array(
'text' => $label,
'value' => $name,
);
}
$options['tiny_options']['codesample_languages'] = $tiny_options;
}
/**
* Implements hook_config_info().
*/
function tinymce_codehighlight_config_info() {
$prefixes['tinymce_codehighlight.settings'] = array(
'label' => t('TinyMCE Code Highlight settings'),
'group' => t('Configuration'),
);
return $prefixes;
}