Skip to content

Commit ec28ff1

Browse files
author
Jake Bell
committed
Adding display style for embedding gist in <code> tags.
1 parent a44a5b2 commit ec28ff1

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

gist_filter.module

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@
99
* with a link to the gist or embedding the gist itself.
1010
*/
1111

12+
/**
13+
* Implementation of hook_theme().
14+
*/
15+
function gist_filter_theme() {
16+
return array(
17+
'gist_filter_code' => array(
18+
'variables' => array('file' => array()),
19+
),
20+
);
21+
}
1222

1323
/**
1424
* Implementation of hook_filter_info().
@@ -45,6 +55,7 @@ function gist_filter_gist_filter_settings($form, $form_state, $filter, $format,
4555
'#title' => t('Gist display method'),
4656
'#type' => 'select',
4757
'#options' => array(
58+
'code' => t('Code tags'),
4859
'embed' => t('Embed'),
4960
'link' => t('Link'),
5061
),
@@ -62,6 +73,44 @@ function gist_filter_filter_tips($filter, $format, $long = FALSE) {
6273
return t('Use [gist:####] where #### is your gist number to %action.', array('%action' => $action));
6374
}
6475

76+
/**
77+
* Theme function to render the contents of a gist file in <code> tags.
78+
*/
79+
function theme_gist_filter_code($vars) {
80+
$file = $vars['file'];
81+
82+
return '<div class="github-file"><code>' . check_plain($file['content']) . '</code></div>';
83+
}
84+
85+
/**
86+
* Replace the text with the content of the Gist, wrapped in <code> tags.
87+
*/
88+
function _gist_display_code($matches) {
89+
// Get the Gist from the Github API.
90+
$url = 'https://api.github.com/gists/' . $matches[1];
91+
$response = drupal_http_request($url, array('headers' => array('Content-Type' => 'application/json')));
92+
$data = drupal_json_decode($response->data);
93+
94+
$output = '';
95+
96+
// If a file was specified, just render that one file.
97+
if (isset($matches[2]) && !empty($matches[2]) && isset($data['files'][$matches[2]])) {
98+
$output = theme('gist_filter_code', array(
99+
'file' => $data['files'][$matches[2]],
100+
));
101+
}
102+
// Otherwise, render all files.
103+
else {
104+
foreach ($data['files'] as $file) {
105+
$output .= theme('gist_filter_code', array(
106+
'file' => $file,
107+
));
108+
}
109+
}
110+
111+
return $output;
112+
}
113+
65114
/**
66115
* Replace the text with embedded script.
67116
*/

0 commit comments

Comments
 (0)