Skip to content

Commit a44a5b2

Browse files
author
Jake Bell
committed
Adding ability to embed/link to single file within a gist (from #936740)
1 parent 7f79f76 commit a44a5b2

File tree

2 files changed

+65
-11
lines changed

2 files changed

+65
-11
lines changed

gist_filter.module

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ function gist_filter_filter_info() {
3333
*/
3434
function gist_filter_gist_filter_process($text, $filter, $format) {
3535
$display = $filter->settings['gist_filter_display_method'];
36-
return preg_replace('@\[gist\:(\d+)\]@', _gist_display('$1', $display), $text);
36+
$callback = '_gist_display_' . $display;
37+
return preg_replace_callback('@\[gist\:(\d+)\:?(.*)?\]@', $callback, $text);
3738
}
3839

39-
4040
/**
4141
* Settings callback for gist_filter.
4242
*/
@@ -63,14 +63,19 @@ function gist_filter_filter_tips($filter, $format, $long = FALSE) {
6363
}
6464

6565
/**
66-
* Replace the text with embedded script or link.
66+
* Replace the text with embedded script.
67+
*/
68+
function _gist_display_embed($matches) {
69+
$gist_url = 'http://gist.github.com/' . $matches[1];
70+
$gist_url = isset($matches[2]) && !empty($matches[2]) ? $gist_url . '.js?file=' . $matches[2] : $gist_url . '.js';
71+
return '<script src="' . $gist_url . '"></script>';
72+
}
73+
74+
/**
75+
* Replace the text with a link.
6776
*/
68-
function _gist_display($gist, $display) {
69-
$url = 'http://gist.github.com/' . $gist;
70-
switch ($display) {
71-
case 'embed':
72-
return '<script src="' . $url . '.js"></script>';
73-
case 'link':
74-
return l($url, $url);
75-
}
77+
function _gist_display_link($matches) {
78+
$gist_url = 'http://gist.github.com/' . $matches[1];
79+
$gist_url = isset($matches[2]) && !empty($matches[2])? $gist_url . '#file_' . $matches[2] : $gist_url;
80+
return l($gist_url, $gist_url);
7681
}

gist_filter.test

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,31 @@ class GistFilterTestCase extends DrupalWebTestCase {
5858

5959
}
6060

61+
/**
62+
* Testing the embedded gist option with a file parameter.
63+
*/
64+
function testEmbedStyleWithFile() {
65+
66+
// Turn on our input filter and set the option to embed.
67+
$edit = array(
68+
'filters[gist_filter][status]' => 1,
69+
'filters[gist_filter][settings][gist_filter_display_method]' => 'embed',
70+
);
71+
$this->drupalPost('admin/config/content/formats/plain_text', $edit, t('Save configuration'));
72+
73+
// Create a test node
74+
$langcode = LANGUAGE_NONE;
75+
$edit = array(
76+
"title" => $this->randomName(),
77+
"body[$langcode][0][value]" => 'Hello! [gist:865412:php_file.php]',
78+
);
79+
$result = $this->drupalPost('node/add/' . $this->contentType->type, $edit, t('Save'));
80+
$this->assertResponse(200);
81+
$this->assertRaw("Hello! ");
82+
$this->assertRaw('<script src="http://gist.github.com/865412.js?file=php_file.php"></script>');
83+
84+
}
85+
6186
/**
6287
* Testing the link option.
6388
*/
@@ -82,4 +107,28 @@ class GistFilterTestCase extends DrupalWebTestCase {
82107

83108
}
84109

110+
/**
111+
* Testing the link option.
112+
*/
113+
function testLinkStyleWithFile() {
114+
115+
// Turn on our input filter and set the option to link.
116+
$edit = array(
117+
'filters[gist_filter][status]' => 1,
118+
'filters[gist_filter][settings][gist_filter_display_method]' => 'link',
119+
);
120+
$this->drupalPost('admin/config/content/formats/plain_text', $edit, t('Save configuration'));
121+
122+
// Create a test node
123+
$langcode = LANGUAGE_NONE;
124+
$edit = array(
125+
"title" => $this->randomName(),
126+
"body[$langcode][0][value]" => 'Hello! [gist:865412:php_file.php]',
127+
);
128+
$result = $this->drupalPost('node/add/' . $this->contentType->type, $edit, t('Save'));
129+
$this->assertResponse(200);
130+
$this->assertRaw('Hello! <a href="http://gist.github.com/865412#file_php_file.php">http://gist.github.com/865412#file_php_file.php</a>');
131+
132+
}
133+
85134
}

0 commit comments

Comments
 (0)