99 * the gist itself.
1010 */
1111
12+ /**
13+ * Implements hook_filter_info().
14+ */
15+ function gist_filter_filter_info() {
16+ $filters['gist_filter'] = array(
17+ 'title' => t('Gist filter (Github Gists)'),
18+ 'description' => t('Substitutes [gist:xx] tags with the gist located at https://gist.github.com/user/xx.'),
19+ 'process callback' => 'gist_filter_gist_filter_process',
20+ 'default settings' => array(
21+ 'gist_filter_display_method' => 'embed',
22+ ),
23+ 'settings callback' => 'gist_filter_gist_filter_settings',
24+ 'tips callback' => 'gist_filter_filter_tips',
25+ 'cache' => TRUE,
26+ 'weight' => 0,
27+ );
28+ return $filters;
29+ }
30+
31+ /**
32+ * Implements hook_filter_info_alter().
33+ */
34+ function gist_filter_filter_info_alter(&$info) {
35+ if (module_exists('token_filter')) {
36+ $info['filter_tokens']['weight'] = 1;
37+ }
38+ }
39+
40+
1241 /**
1342 * Implements hook_menu().
1443 *
1544 */
1645function gist_filter_menu() {
1746 $items = array();
1847 $items['admin/config/content/gist-filter'] = array(
19- 'title' => 'Gist Filter settings',
48+ 'title' => 'Gist Filter Global settings',
2049 'description' => 'Enter global settings for the Gist Filter module',
2150 'page callback' => 'backdrop_get_form',
2251 'page arguments' => array('gist_filter_config_form'),
@@ -62,25 +91,7 @@ function gist_filter_theme() {
6291}
6392
6493/**
65- * Implements hook_filter_info().
66- */
67- function gist_filter_filter_info() {
68- $filters['gist_filter'] = array(
69- 'title' => t('Gist filter (Github Gists)'),
70- 'description' => t('Substitutes [gist:xx] tags with the gist located at https://gist.github.com/user/xx.'),
71- 'process callback' => 'gist_filter_gist_filter_process',
72- 'default settings' => array(
73- 'gist_filter_display_method' => 'embed',
74- ),
75- 'settings callback' => 'gist_filter_gist_filter_settings',
76- 'tips callback' => 'gist_filter_filter_tips',
77- 'cache' => TRUE,
78- );
79- return $filters;
80- }
81-
82- /**
83- * Process callback for hook_filter_info().
94+ * Process callback for gist_filter.
8495 */
8596function gist_filter_gist_filter_process($text, $filter, $format) {
8697 // Get the default display method for the text format.
@@ -110,11 +121,13 @@ function gist_filter_gist_filter_process($text, $filter, $format) {
110121}
111122
112123 /**
113- * Settings callback for gist_filter_config_form
124+ * Global settings page callback for gist_filter.
114125 */
115126function gist_filter_config_form($form, &$form_state) {
116127 $form = array();
117- $form['#config'] = 'gist_filter.settings';
128+ $config_file = 'gist_filter.settings';
129+ $form['#config'] = $config_file;
130+ $config = config($config_file);
118131
119132 // Container for 'code' display settings.
120133 $form['code'] = array(
@@ -137,7 +150,7 @@ function gist_filter_config_form($form, &$form_state) {
137150 $form['code']['github_token'] = array(
138151 '#type' => 'textfield',
139152 '#title' => 'GitHub Personal Access Token',
140- '#default_value' => config_get('gist_filter.settings', 'github_token', ''),
153+ '#default_value' => $config->get( 'github_token', ''),
141154 '#description' => $description,
142155 );
143156
@@ -170,13 +183,21 @@ function gist_filter_config_form($form, &$form_state) {
170183 '#type' => 'select',
171184 '#title' => 'Gist Embed Theme',
172185 '#options' => $gist_themes,
173- '#default_value' => config_get('gist_filter.settings', 'gist_embed_theme'),
186+ '#default_value' => $config->get( 'gist_embed_theme'),
174187 '#description' => t("Themes are from <a href='@developer'>Will Boyd</a> and can be previewed on <a href='@preview-page'>this page</a>.", array(
175188 '@developer' => 'https://github.com/lonekorean/gist-syntax-themes',
176189 '@preview-page' => 'https://lonekorean.github.io/gist-syntax-themes/',
177190 )),
178191 );
179192
193+ // Allow disabling the 'noscript' display of code block to save on API calls.
194+ $form['embed']['enable_noscript_display'] = array(
195+ '#type' => 'checkbox',
196+ '#title' => "Display 'code' version if Javascript is disabled.",
197+ '#description' => t("If Javascript is disabled, then enabling this option will use 'code' display of the gist. This may cause additional calls to the GitHub API if the gist is not in the cache."),
198+ '#default_value' => $config->get('enable_noscript_display'),
199+ );
200+
180201 return system_settings_form($form);
181202}
182203
@@ -198,7 +219,7 @@ function gist_filter_gist_filter_settings($form, $form_state, $filter, $format)
198219}
199220
200221/**
201- * Implements hook_filter_tips() .
222+ * Filter tip callback for gist_filter .
202223 */
203224function gist_filter_filter_tips($filter, $format, $long = FALSE) {
204225 $display = $filter->settings['gist_filter_display_method'];
@@ -229,6 +250,7 @@ function gist_filter_filter_tips($filter, $format, $long = FALSE) {
229250 // Define the tip for the default display method.
230251 $default_tip = array(t('Use [gist:####] where #### is your gist number to %action.</br>To add a specific file use [gist:123abc456def7890:myfile.sh] or the
231252 equivalent from the examples below.', array('%action' => $default_action)));
253+ // Merge and return the tips.
232254 $tips = array_merge($default_tip, $override_tips);
233255 return implode('</br>', $tips);
234256}
@@ -281,15 +303,17 @@ function _gist_display_code($matches) {
281303 */
282304function _gist_display_embed($matches) {
283305 // Get the embed theme setting.
284- $theme = config_get('gist_filter.settings', 'gist_embed_theme');
306+ $config_file = 'gist_filter.settings';
307+ $config = config($config_file);
308+ $theme = $config->get('gist_embed_theme');
285309
286310 // Make the url agnostic to the scheme so gist will embed on http and https.
287311 $gist_url = '//gist.github.com/' . $matches[1];
288312 $gist_url = isset($matches[2]) && !empty($matches[2]) ? $gist_url . '.js?file=' . $matches[2] : $gist_url . '.js';
289313
290314 // Also grab the content and display it in code tags (in case the user does
291- // not have Javascript).
292- $output = ' <noscript>' . _gist_display_code($matches) . '</noscript>';
315+ // not have Javascript) if enabled .
316+ $output = ($config->get('enable_noscript_display')) ? ' <noscript>' . _gist_display_code($matches) . '</noscript>' : ' ';
293317
294318 // If the theme is not default then use javascript to add the CSS.
295319 if ($theme != 'default') {
@@ -341,7 +365,7 @@ function gist_filter_get_gist($id) {
341365 $gist = $cached->data;
342366 }
343367 else {
344- // Not available in the cache, so retrive the gist from Github.
368+ // Not available in the cache, so retrieve the gist from Github.
345369 // Define standard header.
346370 $headers = array();
347371 $headers['Content-Type'] = 'application/json';
@@ -358,7 +382,7 @@ function gist_filter_get_gist($id) {
358382 $gist = backdrop_json_decode($response->data);
359383
360384 // Cache the gist until the next cache flush.
361- cache_set($cid, $gist, 'cache', CACHE_TEMPORARY );
385+ cache_set($cid, $gist, 'cache', CACHE_PERMANENT );
362386 }
363387 // Return the cached or retrieved gist.
364388 $gists[$id] = $gist;
0 commit comments