Skip to content

Syntaxhighlighter support #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ Examples:
* `[gist id=546764 file=file.txt bump=1]`
* `[gist]http://gist.github.com/546764[/gist]`

This plugin also supports using [SyntaxHighlighter Evolved](http://wordpress.org/plugins/syntaxhighlighter/)
for highlighting rather then the default gist mockup. **NOTE** for this to work, the following line must be added to the top
of the wp-settings.php file located in the root of your wordpress install: `define('EMBED_GISTHUB_SYNTAXHIGHLIGHTER', true);`.
Also, in the SyntaxHighlighter Evolved Settings (inside wp-admin), the "Load All Brushes" checkbox must be enabled.

Example Usage:
* `[gist id=546764 highlight=she]`


Cache is implemented with the Transients API to minimize delay on loading
content. Default TTL (time to live) is 86400 seconds or one day.
Expand Down
241 changes: 143 additions & 98 deletions embed-github-gist.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<?php
/*
Plugin Name: Embed GitHub Gist
Plugin URI: http://wordpress.org/extend/plugins/embed-github-gist/
Description: Embed GitHub Gists
Author: Dragonfly Development
Author URI: http://dflydev.com/
Version: 0.13
License: MIT - http://opensource.org/licenses/mit
*/
Plugin Name: Embed GitHub Gist
Plugin URI: http://wordpress.org/extend/plugins/embed-github-gist/
Description: Embed GitHub Gists
Author: Dragonfly Development
Author URI: http://dflydev.com/
Version: 0.13
License: MIT - http://opensource.org/licenses/mit
*/

// TODO Option for default ttl
// TODO Option to bypass cache
Expand All @@ -31,30 +31,43 @@
define('EMBED_GISTHUB_BYPASS_CACHE', false);
}

if( !defined('EMBED_GISTHUB_SYNTAXHIGHLIGHTER') ) {
define('EMBED_GISTHUB_SYNTAXHIGHLIGHTER', false);
}

/**
* Build a cache key
* @param int $id GitHub Gist ID
* @param string $bump Bump value to force cache expirey.
*/
function embed_github_gist_build_cache_key($id, $bump = null, $file=null) {
$key = 'embed_github_gist-' . $id;
if ( $bump ) $key .= '-' . $bump;
if ( $file ) $key .= '-' . $file;
return $key;
$key = 'embed_github_gist-' . $id;
if ( $bump ) $key .= '-' . $bump;
if ( $file ) $key .= '-' . $file;
return $key;
}

/**
* Use SyntaxHighlighter Evolved (http://wordpress.org/plugins/syntaxhighlighter/)
* for rendering instead of Github default or inline?
*/
function embed_github_gist_use_syntaxhighlighter() {
return EMBED_GISTHUB_SYNTAXHIGHLIGHTER;
}


/**
* Bypass cache?
*/
function embed_github_gist_bypass_cache() {
return EMBED_GISTHUB_BYPASS_CACHE;
return EMBED_GISTHUB_BYPASS_CACHE;
}

/**
* Prefer inline HTML over JS?
*/
function embed_github_gist_prefer_inline_html() {
return EMBED_GISTHUB_INLINE_HTML;
return EMBED_GISTHUB_INLINE_HTML;
}

/**
Expand All @@ -72,73 +85,105 @@ function embed_github_gist($id, $ttl = null, $bump = null, $file = null) {
require_once ABSPATH.WPINC.'/class-http.php';
}

$key = embed_github_gist_build_cache_key($id, $bump, $file);
if ( embed_github_gist_bypass_cache() || false === ( $gist = get_transient($key) ) ) {
$http = new WP_Http;
$args = array('sslverify' => false);
if (defined('EMBED_GISTHUB_USERNAME') && defined('EMBED_GISTHUB_PASSWORD')) {
$args['headers'] = array( 'Authorization' => 'Basic '.base64_encode(EMBED_GISTHUB_USERNAME.':'.EMBED_GISTHUB_PASSWORD) );
}
$result = $http->request('https://api.github.com/gists/' . $id, $args);
if ( is_wp_error($result) ) {
echo $result->get_error_message();
}
$json = json_decode($result['body'], true);
if (200 != $result['response']['code']) {
$html = '<div>Could not embed GitHub Gist '.$id;
if (isset($json['message'])) {
$html .= ': '. $json['message'];
}
$html .= '</div>';

return $html;
};

$files = array();
foreach ($json['files'] as $name => $fileInfo) {
if ($file === null) {
$files[$name] = $fileInfo;
} else {
if ($file == $name) {
$files[$name] = $fileInfo;
break;
}
}
}

$gist = '';

if (count($files)) {
if ( embed_github_gist_prefer_inline_html() ) {
foreach ($files as $name => $fileInfo) {
$language = strtolower($fileInfo['language']);
$gist .= '<pre><code class="language-'.$language.' '.$language.'">';
$gist .= htmlentities($fileInfo['content']);
$gist .= '</code></pre>';
}
} else {
$urlExtra = $file ? '?file='.$file : '';
$gist .= '<script src="https://gist.github.com/'.$id.'.js'.$urlExtra.'"></script>';
$gist .= '<noscript>';
foreach ($files as $name => $fileInfo) {
$language = strtolower($fileInfo['language']);
$gist .= '<pre><code class="language-'.$language.' '.$language.'">';
$gist .= htmlentities($fileInfo['content']);
$gist .= '</code></pre>';
}
$gist .= '</noscript>';
}
}

unset($result, $http);

if ( ! embed_github_gist_bypass_cache() ) {
if ( ! $ttl ) $ttl = EMBED_GISTHUB_DEFAULT_TTL;
set_transient($key, $gist, $ttl);
}
}

return $gist;
$key = embed_github_gist_build_cache_key($id, $bump, $file);
if ( embed_github_gist_bypass_cache() || false === ( $gist = get_transient($key) ) ) {
$http = new WP_Http;
$args = array('sslverify' => false);
if (defined('EMBED_GISTHUB_USERNAME') && defined('EMBED_GISTHUB_PASSWORD')) {
$args['headers'] = array( 'Authorization' => 'Basic '.base64_encode(EMBED_GISTHUB_USERNAME.':'.EMBED_GISTHUB_PASSWORD) );
}
$result = $http->request('https://api.github.com/gists/' . $id, $args);
if ( is_wp_error($result) ) {
echo $result->get_error_message();
}
$json = json_decode($result['body'], true);
if (200 != $result['response']['code']) {
$html = '<div>Could not embed GitHub Gist '.$id;
if (isset($json['message'])) {
$html .= ': '. $json['message'];
}
$html .= '</div>';

return $html;
};

$files = array();
foreach ($json['files'] as $name => $fileInfo) {
if ($file === null) {
$files[$name] = $fileInfo;
} else {
if ($file == $name) {
$files[$name] = $fileInfo;
break;
}
}
}

$gist = '';

if (count($files)) {

if( embed_github_gist_use_syntaxhighlighter() ) {

// Get available languages (brushes) available in SyntaxHighlighter
global $SyntaxHighlighter;
$brushes = $SyntaxHighlighter->brushes;

$http = new WP_Http;
foreach($files as $name => $fileInfo) {

$language = strtolower($fileInfo['language']);

// Verify the language of the Gist is supported by SyntaxHighlighter.
// If it doesn't, treat the Gist as Plain Text.
if(!array_key_exists($language, $brushes)) {
$language = "text";
}


$raw_content = $http->request($fileInfo['raw_url']);

if(is_wp_error($raw_content)) {
echo $result->get_error_message();
} else {
$gist .= "<pre class='brush: $language; title: ; notranslate' title=''>";
$gist .= $raw_content['body'];
$gist .= "</pre>";
}
}

} else if ( embed_github_gist_prefer_inline_html() ) {
foreach ($files as $name => $fileInfo) {
$language = strtolower($fileInfo['language']);
$gist .= '<pre><code class="language-'.$language.' '.$language.'">';
$gist .= htmlentities($fileInfo['content']);
$gist .= '</code></pre>';
}
} else {
$urlExtra = $file ? '?file='.$file : '';
$gist .= '<script src="https://gist.github.com/'.$id.'.js'.$urlExtra.'"></script>';
$gist .= '<noscript>';

foreach ($files as $name => $fileInfo) {
$language = strtolower($fileInfo['language']);
$gist .= '<pre><code class="language-'.$language.' '.$language.'">';
$gist .= htmlentities($fileInfo['content']);
$gist .= '</code></pre>';
}

$gist .= '</noscript>';
}
}

unset($result, $http);

if ( ! embed_github_gist_bypass_cache() ) {
if ( ! $ttl ) $ttl = EMBED_GISTHUB_DEFAULT_TTL;
set_transient($key, $gist, $ttl);
}
}

return $gist;
}

/**
Expand All @@ -147,28 +192,28 @@ function embed_github_gist($id, $ttl = null, $bump = null, $file = null) {
* @param mixed $content
*/
function handle_embed_github_gist_shortcode($atts, $content = null) {
extract(shortcode_atts(array(
'id' => null,
'file' => null,
'ttl' => null,
'bump' => null,
), $atts));

if ( ! $id ) {
if ( $content ) {
if ( preg_match('/\s*https?.+\/(\d+)/', $content, $matches) ) {
$id = $matches[1];
}
}
}
return $id ? embed_github_gist($id, $ttl, $bump, $file) : $content;
extract(shortcode_atts(array(
'id' => null,
'file' => null,
'ttl' => null,
'bump' => null,
), $atts));

if ( ! $id ) {
if ( $content ) {
if ( preg_match('/\s*https?.+\/(\d+)/', $content, $matches) ) {
$id = $matches[1];
}
}
}
return $id ? embed_github_gist($id, $ttl, $bump, $file) : $content;
}

/**
* Init the plugin.
*/
function handle_embed_github_gist_init() {
add_shortcode('gist', 'handle_embed_github_gist_shortcode');
add_shortcode('gist', 'handle_embed_github_gist_shortcode');
}

/**
Expand All @@ -179,7 +224,7 @@ function handle_embed_github_gist_init() {
function embed_github_gist_post_candidate()
{
global $posts;

foreach ($posts as $p) {
if (preg_match('/\[gist[^\]]*\]/siU', $p->post_content)) {
return true;
Expand Down