Skip to content

Commit f4b093c

Browse files
Port complete. Tests working. Initial enhancements
1 parent 08754de commit f4b093c

File tree

5 files changed

+213
-167
lines changed

5 files changed

+213
-167
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ Gist Filter allows gists from Github to be embedded anywhere in the text using
1818
a token format to embed as either a link, a 'code' block or embedded using one
1919
of a range of themes.
2020

21-
2221
Installation
2322
------------
2423
<!--
@@ -29,6 +28,11 @@ necessary.
2928
- Install this module using the official Backdrop CMS instructions at
3029
https://docs.backdropcms.org/documentation/extend-with-modules.
3130
- Enable the Gist Filter in each text format where you want to use it.
31+
- In the order of filters (weight) within each text format you use it, Gist
32+
Filter must come BEFORE
33+
[Token Filter](https://backdropcms.org/project/token_filter) and AFTER 'Limit
34+
allowed HTML tags'. The module will attempt to set this but double check this
35+
if you run into problems.
3236

3337
Usage
3438
-------------
@@ -47,13 +51,15 @@ following patterns:
4751
- `[gistlink:123abc456def7890]` - embed the gist as a link.
4852
- `[gistembed:123abc456def7890]` - embed the gist using a script.
4953
The gist ID is the string after the gist owner. For the examples above the gist
50-
URL would be `https://gist.github.com/yorkshire-pudding/123abc456def7890`.
54+
URL might be `https://gist.github.com/yorkshire-pudding/123abc456def7890`.
5155
3. To add a specific file use `[gist:123abc456def7890:myfile.sh]` or the
5256
equivalent with a `gistcode`, `gistlink` or `gistembed` prefix.
5357
4. Go to `/admin/config/content/gist-filter` where you can select a theme for
5458
the embed display (you will need to clear the page cache to apply a change
5559
here), and also add a GitHub Personal Access token to give your site a higher
5660
limit for retrieving gists.
61+
5. Clearing the 'Page and else' cache may be necessary to see the effects of a
62+
change.
5763

5864
Issues
5965
------

config/gist_filter.settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"_config_name": "gist_filter.settings",
33
"github_token": "",
4-
"gist_embed_theme": "default"
4+
"gist_embed_theme": "default",
5+
"enable_noscript_display": 1
56
}

gist_filter.info

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ type = module
66
package = Input filters
77
tags[] = Filters
88
tags[] = Content
9-
10-
files[] = gist_filter.test
9+
tags[] = GitHub
10+
tags[] = Gist

gist_filter.module

Lines changed: 54 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,43 @@
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
*/
1645
function 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
*/
8596
function 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
*/
115126
function 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
*/
203224
function 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
*/
282304
function _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

Comments
 (0)