Skip to content

Commit 67a6fa9

Browse files
committed
allow embedding of single files; closes #1
1 parent 2491d18 commit 67a6fa9

File tree

3 files changed

+17
-13
lines changed

3 files changed

+17
-13
lines changed

filter.php

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
<?php
22

33
class filter_gistembed extends moodle_text_filter {
4-
5-
public function filter($text, array $options = array()) {
6-
$cnt = preg_match_all('/https?:\/\/gist\.github\.com\/(.*?)\/([a-f0-9]*)/', $text, $matches);
7-
if ($cnt) {
8-
for ($i=0; $i<$cnt; ++$i) {
9-
$script = '<script src="https://gist.github.com/' . $matches[1][$i] . '/' . $matches[2][$i] . '.js"></script>';
10-
$text = str_replace($matches[0][$i], $script, $text);
4+
5+
public function filter( $text, array $options = [ ] ) {
6+
return preg_replace_callback( '/https?:\/\/gist\.github\.com\/(.*?)\/([a-f0-9]*)(\?file=[^\s<>]*)?/', function($matches) {
7+
//echo '<pre>' . print_r($matches, true) . '</pre>';
8+
$src = 'https://gist.github.com/' . $matches[1] . '/' . $matches[2] . '.js';
9+
if (!empty($matches[3])) {
10+
$src .= $matches[3];
1111
}
12-
}
13-
return $text;
12+
return '<script src="' . $src . '"></script>';
13+
}, $text );
1414
}
15-
1615
}
17-

readme.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ Forum button. You should see something like this.
102102

103103
And that's it. You're all set.
104104

105+
#### Embedding single files
106+
If you're embedding a gist with multiple files, normally all files will embed. If you'd rather
107+
embed only a single file add `?file=Filename.ext` to the end of the URL that you paste into
108+
Moodle and only that file will embed. If that file doesn't exist in your gist nothing
109+
will embed.
110+
105111
### Settings
106112

107113
There really aren't any. Just paste in the link.

version.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
defined('MOODLE_INTERNAL') || die();
44

5-
$plugin->version = '2015092400';
5+
$plugin->version = '2017102100';
66
$plugin->requires = '2014051200'; // 2.7
77
$plugin->cron = 0;
88
$plugin->component = 'filter_gistembed';
99
$plugin->maturity = MATURITY_BETA;
10-
$plugin->release = '0.1';
10+
$plugin->release = '0.2';

0 commit comments

Comments
 (0)