From afed50301a5810d91fda2ba72ead38192dcba8b0 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 22 Jun 2013 16:10:31 +0000 Subject: [PATCH 1/6] Basic SyntaxHighlighter Evolved support --- embed-github-gist.php | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/embed-github-gist.php b/embed-github-gist.php index fc2dde0..a6f9a93 100644 --- a/embed-github-gist.php +++ b/embed-github-gist.php @@ -28,7 +28,11 @@ } if ( !defined('EMBED_GISTHUB_BYPASS_CACHE') ) { - define('EMBED_GISTHUB_BYPASS_CACHE', false); + define('EMBED_GISTHUB_BYPASS_CACHE', true); //todo change back to false +} + +if( !defined('EMBED_GITHUB_SYNTAXHIGHLIGHTER') ) { + define('EMBED_GITHUB_SYNTAXHIGHLIGHTER', true); } /** @@ -43,6 +47,15 @@ function embed_github_gist_build_cache_key($id, $bump = null, $file=null) { 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_GITHUB_SYNTAXHIGHLIGHTER; +} + + /** * Bypass cache? */ @@ -109,7 +122,22 @@ function embed_github_gist($id, $ttl = null, $bump = null, $file = null) { $gist = ''; if (count($files)) { - if ( embed_github_gist_prefer_inline_html() ) { + + if( embed_github_gist_use_syntaxhighlighter() ) { + + $http = new WP_Http; + + foreach($files as $name => $fileInfo) { + $language = strtolower($fileInfo['language']); + $language = "php"; + $raw_content = $http->request($fileInfo['raw_url']); + $gist .= "
";
+					$gist .= $raw_content['body'];
+					$gist .= "
"; + } + + + } else if ( embed_github_gist_prefer_inline_html() ) { foreach ($files as $name => $fileInfo) { $language = strtolower($fileInfo['language']); $gist .= '
';

From 1b3d3f580d05a53ee1e68d493b7eaa4002310026 Mon Sep 17 00:00:00 2001
From: root 
Date: Sat, 22 Jun 2013 20:16:09 +0000
Subject: [PATCH 2/6] Added check to make sure language was available in

---
 embed-github-gist.php | 269 ++++++++++++++++++++++--------------------
 1 file changed, 143 insertions(+), 126 deletions(-)

diff --git a/embed-github-gist.php b/embed-github-gist.php
index a6f9a93..c63a7f3 100644
--- a/embed-github-gist.php
+++ b/embed-github-gist.php
@@ -1,13 +1,13 @@
  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 = '
Could not embed GitHub Gist '.$id; - if (isset($json['message'])) { - $html .= ': '. $json['message']; - } - $html .= '
'; - - 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() ) { + $gist = ''; + + if ( !class_exists('WP_Http') ) { + 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 = '
Could not embed GitHub Gist '.$id; + if (isset($json['message'])) { + $html .= ': '. $json['message']; + } + $html .= '
'; + + 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"; + } - foreach($files as $name => $fileInfo) { - $language = strtolower($fileInfo['language']); - $language = "php"; - $raw_content = $http->request($fileInfo['raw_url']); - $gist .= "
";
-					$gist .= $raw_content['body'];
-					$gist .= "
"; + + $raw_content = $http->request($fileInfo['raw_url']); + + if(is_wp_error($raw_content)) { + echo $result->get_error_message(); + } else { + $gist .= "
";
+												$gist .= $raw_content['body'];
+												$gist .= "
"; + } + } + + } else if ( embed_github_gist_prefer_inline_html() ) { + foreach ($files as $name => $fileInfo) { + $language = strtolower($fileInfo['language']); + $gist .= '
';
+										$gist .= htmlentities($fileInfo['content']);
+										$gist .= '
'; + } + } else { + $urlExtra = $file ? '?file='.$file : ''; + $gist .= ''; + $gist .= ''; + } } - - } else if ( embed_github_gist_prefer_inline_html() ) { - foreach ($files as $name => $fileInfo) { - $language = strtolower($fileInfo['language']); - $gist .= '
';
-                    $gist .= htmlentities($fileInfo['content']);
-                    $gist .= '
'; - } - } else { - $urlExtra = $file ? '?file='.$file : ''; - $gist .= ''; - $gist .= ''; - } - } - - unset($result, $http); - - if ( ! embed_github_gist_bypass_cache() ) { - if ( ! $ttl ) $ttl = EMBED_GISTHUB_DEFAULT_TTL; - set_transient($key, $gist, $ttl); - } - } - - return $gist; + unset($result, $http); + + if ( ! embed_github_gist_bypass_cache() ) { + if ( ! $ttl ) $ttl = EMBED_GISTHUB_DEFAULT_TTL; + set_transient($key, $gist, $ttl); + } + } + + return $gist; } /** @@ -175,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'); } /** @@ -206,19 +223,19 @@ 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; + global $posts; + + foreach ($posts as $p) { + if (preg_match('/\[gist[^\]]*\]/siU', $p->post_content)) { + return true; + } } - } - return false; + return false; } add_action('init', 'handle_embed_github_gist_init'); if ( !is_admin()) { - add_action('wp', 'embed_github_gist_post_candidate'); + add_action('wp', 'embed_github_gist_post_candidate'); } From e34027deff16b124ebadef422c7a2c48c2aa7af4 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 22 Jun 2013 20:22:15 +0000 Subject: [PATCH 3/6] VIM went a little extreme on the reindenting. Fixed. --- embed-github-gist.php | 240 +++++++++++++++++++++--------------------- 1 file changed, 120 insertions(+), 120 deletions(-) diff --git a/embed-github-gist.php b/embed-github-gist.php index c63a7f3..4494356 100644 --- a/embed-github-gist.php +++ b/embed-github-gist.php @@ -20,19 +20,19 @@ * @var int */ if ( !defined('EMBED_GISTHUB_DEFAULT_TTL') ) { - define('EMBED_GISTHUB_DEFAULT_TTL', 86400); // 60*60*24 (1 day) + define('EMBED_GISTHUB_DEFAULT_TTL', 86400); // 60*60*24 (1 day) } if ( !defined('EMBED_GISTHUB_INLINE_HTML') ) { - define('EMBED_GISTHUB_INLINE_HTML', false); + define('EMBED_GISTHUB_INLINE_HTML', false); } if ( !defined('EMBED_GISTHUB_BYPASS_CACHE') ) { - define('EMBED_GISTHUB_BYPASS_CACHE', true); //todo change back to false + define('EMBED_GISTHUB_BYPASS_CACHE', true); //todo change back to false } if( !defined('EMBED_GITHUB_SYNTAXHIGHLIGHTER') ) { - define('EMBED_GITHUB_SYNTAXHIGHLIGHTER', true); + define('EMBED_GITHUB_SYNTAXHIGHLIGHTER', true); } /** @@ -41,10 +41,10 @@ * @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; } /** @@ -52,7 +52,7 @@ function embed_github_gist_build_cache_key($id, $bump = null, $file=null) { * for rendering instead of Github default or inline? */ function embed_github_gist_use_syntaxhighlighter() { - return EMBED_GITHUB_SYNTAXHIGHLIGHTER; + return EMBED_GITHUB_SYNTAXHIGHLIGHTER; } @@ -60,14 +60,14 @@ function embed_github_gist_use_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; } /** @@ -79,111 +79,111 @@ function embed_github_gist_prefer_inline_html() { * @param string $file Name of file */ function embed_github_gist($id, $ttl = null, $bump = null, $file = null) { - $gist = ''; - - if ( !class_exists('WP_Http') ) { - require_once ABSPATH.WPINC.'/class-http.php'; + $gist = ''; + + if ( !class_exists('WP_Http') ) { + 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) ); } - - $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 = '
Could not embed GitHub Gist '.$id; - if (isset($json['message'])) { - $html .= ': '. $json['message']; - } - $html .= '
'; - - return $html; - }; - - $files = array(); - foreach ($json['files'] as $name => $fileInfo) { - if ($file === null) { - $files[$name] = $fileInfo; - } else { - if ($file == $name) { - $files[$name] = $fileInfo; - break; - } - } + $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 = '
Could not embed GitHub Gist '.$id; + if (isset($json['message'])) { + $html .= ': '. $json['message']; + } + $html .= '
'; + + 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() ) { + $gist = ''; - // Get available languages (brushes) available in SyntaxHighlighter - global $SyntaxHighlighter; - $brushes = $SyntaxHighlighter->brushes; + if (count($files)) { - $http = new WP_Http; - foreach($files as $name => $fileInfo) { + if( embed_github_gist_use_syntaxhighlighter() ) { - $language = strtolower($fileInfo['language']); + // Get available languages (brushes) available in SyntaxHighlighter + global $SyntaxHighlighter; + $brushes = $SyntaxHighlighter->brushes; - // 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"; - } + $http = new WP_Http; + foreach($files as $name => $fileInfo) { + $language = strtolower($fileInfo['language']); - $raw_content = $http->request($fileInfo['raw_url']); + // 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"; + } - if(is_wp_error($raw_content)) { - echo $result->get_error_message(); - } else { - $gist .= "
";
-												$gist .= $raw_content['body'];
-												$gist .= "
"; - } - } - } else if ( embed_github_gist_prefer_inline_html() ) { - foreach ($files as $name => $fileInfo) { - $language = strtolower($fileInfo['language']); - $gist .= '
';
-										$gist .= htmlentities($fileInfo['content']);
-										$gist .= '
'; - } - } else { - $urlExtra = $file ? '?file='.$file : ''; - $gist .= ''; - $gist .= ''; - } + } else if ( embed_github_gist_prefer_inline_html() ) { + foreach ($files as $name => $fileInfo) { + $language = strtolower($fileInfo['language']); + $gist .= '
';
+					$gist .= htmlentities($fileInfo['content']);
+					$gist .= '
'; + } + } else { + $urlExtra = $file ? '?file='.$file : ''; + $gist .= ''; + $gist .= ''; + } + } - if ( ! embed_github_gist_bypass_cache() ) { - if ( ! $ttl ) $ttl = EMBED_GISTHUB_DEFAULT_TTL; - set_transient($key, $gist, $ttl); - } + unset($result, $http); + + if ( ! embed_github_gist_bypass_cache() ) { + if ( ! $ttl ) $ttl = EMBED_GISTHUB_DEFAULT_TTL; + set_transient($key, $gist, $ttl); } + } - return $gist; + return $gist; } /** @@ -192,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]; - } - } + 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; + } + 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'); } /** @@ -223,19 +223,19 @@ function handle_embed_github_gist_init() { */ function embed_github_gist_post_candidate() { - global $posts; + global $posts; - foreach ($posts as $p) { - if (preg_match('/\[gist[^\]]*\]/siU', $p->post_content)) { - return true; - } + foreach ($posts as $p) { + if (preg_match('/\[gist[^\]]*\]/siU', $p->post_content)) { + return true; } + } - return false; + return false; } add_action('init', 'handle_embed_github_gist_init'); if ( !is_admin()) { - add_action('wp', 'embed_github_gist_post_candidate'); + add_action('wp', 'embed_github_gist_post_candidate'); } From d3b6aed8800c243ca93d4d09151914bc4d8e3a4c Mon Sep 17 00:00:00 2001 From: root Date: Sat, 22 Jun 2013 20:43:15 +0000 Subject: [PATCH 4/6] Updated README to document SyntaxHighlighter addition --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index 1f888d9..aabdc6f 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,13 @@ Examples: * `[gist id=546764 file=file.txt bump=1]` * `[gist]http://gist.github.com/546764[/gist]` +This Wordpress 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);`. + +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. From c99c05c76a5a30ea811afeb61edf8aa301219184 Mon Sep 17 00:00:00 2001 From: root Date: Sat, 22 Jun 2013 20:44:13 +0000 Subject: [PATCH 5/6] Updated README to document SyntaxHighlighter addition --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index aabdc6f..c320ce7 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,10 @@ Examples: * `[gist id=546764 file=file.txt bump=1]` * `[gist]http://gist.github.com/546764[/gist]` -This Wordpress Plugin also supports using [SyntaxHighlighter Evolved](http://wordpress.org/plugins/syntaxhighlighter/) +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]` From 1d0366b91cfff319fbce76df7e37009e100fa13a Mon Sep 17 00:00:00 2001 From: root Date: Sat, 22 Jun 2013 20:47:28 +0000 Subject: [PATCH 6/6] Fixing Spelling --- embed-github-gist.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/embed-github-gist.php b/embed-github-gist.php index 4494356..f79d8b1 100644 --- a/embed-github-gist.php +++ b/embed-github-gist.php @@ -28,11 +28,11 @@ } if ( !defined('EMBED_GISTHUB_BYPASS_CACHE') ) { - define('EMBED_GISTHUB_BYPASS_CACHE', true); //todo change back to false + define('EMBED_GISTHUB_BYPASS_CACHE', false); } -if( !defined('EMBED_GITHUB_SYNTAXHIGHLIGHTER') ) { - define('EMBED_GITHUB_SYNTAXHIGHLIGHTER', true); +if( !defined('EMBED_GISTHUB_SYNTAXHIGHLIGHTER') ) { + define('EMBED_GISTHUB_SYNTAXHIGHLIGHTER', false); } /** @@ -52,7 +52,7 @@ function embed_github_gist_build_cache_key($id, $bump = null, $file=null) { * for rendering instead of Github default or inline? */ function embed_github_gist_use_syntaxhighlighter() { - return EMBED_GITHUB_SYNTAXHIGHLIGHTER; + return EMBED_GISTHUB_SYNTAXHIGHLIGHTER; }