Skip to content

Commit

Permalink
allow embedding of single files; closes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanNutt committed Oct 21, 2017
1 parent 2491d18 commit 67a6fa9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
20 changes: 9 additions & 11 deletions filter.php
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
<?php

class filter_gistembed extends moodle_text_filter {
public function filter($text, array $options = array()) {
$cnt = preg_match_all('/https?:\/\/gist\.github\.com\/(.*?)\/([a-f0-9]*)/', $text, $matches);
if ($cnt) {
for ($i=0; $i<$cnt; ++$i) {
$script = '<script src="https://gist.github.com/' . $matches[1][$i] . '/' . $matches[2][$i] . '.js"></script>';
$text = str_replace($matches[0][$i], $script, $text);

public function filter( $text, array $options = [ ] ) {
return preg_replace_callback( '/https?:\/\/gist\.github\.com\/(.*?)\/([a-f0-9]*)(\?file=[^\s<>]*)?/', function($matches) {
//echo '<pre>' . print_r($matches, true) . '</pre>';
$src = 'https://gist.github.com/' . $matches[1] . '/' . $matches[2] . '.js';
if (!empty($matches[3])) {
$src .= $matches[3];
}
}
return $text;
return '<script src="' . $src . '"></script>';
}, $text );
}

}

6 changes: 6 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ Forum button. You should see something like this.

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

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

### Settings

There really aren't any. Just paste in the link.
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

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

$plugin->version = '2015092400';
$plugin->version = '2017102100';
$plugin->requires = '2014051200'; // 2.7
$plugin->cron = 0;
$plugin->component = 'filter_gistembed';
$plugin->maturity = MATURITY_BETA;
$plugin->release = '0.1';
$plugin->release = '0.2';

0 comments on commit 67a6fa9

Please sign in to comment.