Skip to content

Commit fb29ec2

Browse files
committed
Initial commit.
0 parents  commit fb29ec2

File tree

3 files changed

+294
-0
lines changed

3 files changed

+294
-0
lines changed

blazer-six-gist-oembed.php

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
<?php
2+
/*
3+
Plugin Name: Blazer Six Gist oEmbed
4+
Version: 1.0
5+
Plugin URI: https://gist.github.com/3031280
6+
Description: Gist oEmbed and shortcode support with caching.
7+
Author: Blazer Six, Inc.
8+
Author URI: http://www.blazersix.com/
9+
License: GPLv2 or later
10+
License URI: http://www.gnu.org/licenses/gpl-2.0.html
11+
12+
Alternatives
13+
------------
14+
* oEmbed Gist by Takayuki Miyauchi - http://wordpress.org/extend/plugins/oembed-gist/
15+
* Pretty Cacheable Gists by Zach Tollman - https://gist.github.com/2864688
16+
* Spotify, Rdio, and Gist embeds for WordPress by Alex Mills - https://gist.github.com/2417309
17+
18+
------------------------------------------------------------------------
19+
Copyright 2012 Blazer Six, Inc.
20+
21+
This program is free software; you can redistribute it and/or modify
22+
it under the terms of the GNU General Public License as published by
23+
the Free Software Foundation; either version 2 of the License, or
24+
(at your option) any later version.
25+
26+
This program is distributed in the hope that it will be useful,
27+
but WITHOUT ANY WARRANTY; without even the implied warranty of
28+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29+
GNU General Public License for more details.
30+
31+
You should have received a copy of the GNU General Public License
32+
along with this program; if not, write to the Free Software
33+
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34+
*/
35+
36+
37+
add_action( 'plugins_loaded', array( 'Blazer_Six_Gist_oEmbed', 'setup' ) );
38+
39+
class Blazer_Six_Gist_oEmbed {
40+
function setup() {
41+
wp_embed_register_handler( 'gist', '#(https://gist\.github\.com/([a-z0-9]+))(?:\#file_(.*))?#i', array( __CLASS__, 'wp_embed_handler' ) );
42+
wp_register_style( 'github-gist', 'https://gist.github.com/stylesheets/gist/embed.css' );
43+
44+
add_shortcode( 'gist', array( __CLASS__, 'shortcode' ) );
45+
}
46+
47+
/**
48+
* WP Embed Handler
49+
*
50+
* Parses Gist URLs for oEmbed support. Returns the value as a shortcode
51+
* string to let the shortcode method handle processing. The value returned
52+
* also doesn't have wpautop() applied, which is nice for source code.
53+
*
54+
* If a file is specified in the hash of the URL for a multi-file Gist, it
55+
* will be picked up and only the single file will be displayed.
56+
*/
57+
function wp_embed_handler( $matches, $attr, $url, $rawattr ) {
58+
$shortcode = '[gist';
59+
60+
if ( isset( $matches[2] ) && ! empty( $matches[2] ) )
61+
$shortcode .= ' id="' . esc_attr( $matches[2] ) . '"';
62+
63+
if ( isset( $matches[3] ) && ! empty( $matches[3] ) )
64+
$shortcode .= ' file="' . esc_attr( $matches[3] ) . '"';
65+
66+
$shortcode .= ']' . $url . '[/gist]';
67+
68+
return $shortcode; // Allows html to be returned after wpautop
69+
}
70+
71+
/**
72+
* Gist Shortcode
73+
*
74+
* Works with private Gists, too (I think).
75+
*
76+
* Cache functionality mimics WP_Embed so the cache is invalidated whenever
77+
* a post is saved. @see WP_Embed->delete_oembed_caches()
78+
*
79+
* The $show_line_numbers attribute assumes custom CSS has been added. If
80+
* the embedded stylehseet is being used, this value won't do anything aside
81+
* from add a class.
82+
*
83+
* If $embed_stylesheet is set to true, the external stylesheet will be
84+
* enqueued and output in the footer. If that's too late, set the default
85+
* $embed_stylesheet value to false and enqueue the 'github-gist' style
86+
* before wp_head.
87+
*
88+
* @see WP_Embed->shortcode()
89+
*/
90+
function shortcode( $attr, $content = null ) {
91+
global $post, $wp_embed;
92+
93+
$defaults = apply_filters( 'blazersix_gist_shortcode_defaults', array(
94+
'embed_stylesheet' => apply_filters( 'blazersix_gist_embed_stylesheet_default', true ),
95+
'id' => '',
96+
'file' => '',
97+
'show_line_numbers' => true,
98+
'show_meta' => true
99+
) );
100+
101+
$attr = shortcode_atts( $defaults, $attr );
102+
$attr['embed_stylesheet'] = ( ! $attr['embed_stylesheet'] || 'false' === $attr['embed_stylesheet'] || '0' === $attr['embed_stylesheet'] ) ? false : true;
103+
$attr['show_line_numbers'] = ( ! $attr['show_line_numbers'] || 'false' === $attr['show_line_numbers'] || '0' === $attr['show_line_numbers'] ) ? false : true;
104+
$attr['show_meta'] = ( ! $attr['show_meta'] || 'false' === $attr['show_meta'] || '0' === $attr['show_meta'] ) ? false : true;
105+
106+
extract( $attr, EXTR_SKIP );
107+
108+
if ( ! empty( $id ) )
109+
$json_url = 'http://gist.github.com/' . $id . '.json';
110+
111+
// The Gist ID and desired file are picked up from the URL if not passed as shortcode attributes
112+
if ( ! empty( $content ) && ( ! isset( $json_url ) || ! isset( $file ) ) ) {
113+
preg_match( '#(https://gist\.github\.com/([a-z0-9]+))(?:\#file_(.*))?#i', $content, $matches );
114+
115+
if ( ! isset( $json_url ) && isset( $matches[1] ) && ! empty( $matches[1] ) )
116+
$json_url = $matches[1] . '.json';
117+
118+
if ( ! isset( $file ) && isset( $matches[3] ) && ! empty( $matches[3] ) )
119+
$file = $matches[3];
120+
}
121+
122+
if ( ! isset( $json_url ) )
123+
return '';
124+
125+
if ( ! empty( $file ) )
126+
$json_url = add_query_arg( 'file', urlencode( $file ), $json_url );
127+
128+
if ( $json_url && isset( $post->ID ) ) {
129+
// Check for a cached result (stored in the post meta)
130+
$cachekey = '_oembed_' . md5( $json_url );
131+
$html = get_post_meta( $post->ID, $cachekey, true );
132+
133+
// Failures are cached
134+
if ( '{{unknown}}' === $html )
135+
return $wp_embed->maybe_make_link( $url );
136+
137+
// Retrieve html from Gist json endpoint
138+
if ( empty( $html ) ) {
139+
$response = wp_remote_get( $json_url, array( 'sslverify' => false ) );
140+
if ( 200 == wp_remote_retrieve_response_code( $response ) ) {
141+
$json = json_decode( wp_remote_retrieve_body( $response ) );
142+
if ( isset( $json->div ) && ! empty( $json->div ) ) {
143+
$html = $json->div;
144+
}
145+
}
146+
147+
// Cache the result
148+
$cache = ( $html ) ? $html : '{{unknown}}';
149+
update_post_meta( $post->ID, $cachekey, $cache );
150+
}
151+
152+
// If there was a result, return it
153+
if ( $html ) {
154+
if ( $show_line_numbers )
155+
$html = str_replace( 'class="highlight"', 'class="highlight show-line-numbers"', $html );
156+
157+
if ( false === $show_meta )
158+
$html = preg_replace( '#<div class="gist-meta">.*?</div>#ms', '', $html );
159+
160+
if ( $embed_stylesheet )
161+
wp_enqueue_style( 'github-gist' ); // External stylesheet; line numbers won't work
162+
163+
return apply_filters( 'blazersix_gist_embed_html', $html, $url, $attr, $post->ID );
164+
}
165+
}
166+
}
167+
}
168+
?>

gist.css

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/**
2+
* Blazer Six Gist oEmbed Styles
3+
*
4+
* Optional styles for including in a local stylesheet (based on
5+
* the GitHub stylesheet). Be sure to hook into the
6+
* "blazersix_gist_shortcode_defaults" filter to set the
7+
* "embed_stylesheet" argument to false.
8+
-------------------------------------------------------------- */
9+
.gist { overflow: hidden; width: 100%; max-width: 100%; color: #000; font-size: 14px; line-height: 18px;}
10+
.gist div { margin: 0; padding: 0;}
11+
.gist .gist-file { margin-bottom: 1em; font-family: Monaco, "Courier New", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", monospace; border: 1px solid #dedede;}
12+
.gist .gist-file .gist-data { overflow: auto; word-wrap: normal; width: 100%; max-width: 100%; font-size: 100%; background-color: #f8f8ff;}
13+
.gist .gist-file .gist-data .highlight { background: transparent;}
14+
.gist .gist-file .gist-data pre { overflow: auto; margin: 0; padding: .25em 0 .5em 0; font-family: 'Bitstream Vera Sans Mono', 'Courier', monospace; font-size: 14px; line-height: 21px;
15+
white-space: pre;
16+
word-wrap: normal;
17+
-moz-tab-size: 4;
18+
-o-tab-size: 4;
19+
tab-size: 4;}
20+
.gist .gist-file .gist-data pre .line { padding: 0 10px;}
21+
.gist .gist-file .gist-data pre .line:even { background: #f3f3f3;}
22+
.gist .gist-file .gist-meta { overflow: hidden; padding: .5em; color: #666; font-size: 85%; background-color: #eaeaea; border-top: 1px solid #ddd;}
23+
.gist .gist-file .gist-meta a { color: #369;}
24+
.gist .gist-file .gist-meta a:visited { color: #737;}
25+
26+
/* Line numbers support: https://gist.github.com/1243028 */
27+
.gist .show-line-numbers { position: relative; border-left: 5ex solid #eee;}
28+
.gist .show-line-numbers pre { counter-reset: linenumbers;}
29+
.gist .show-line-numbers pre div:before { content: counter(linenumbers); counter-increment: linenumbers; position: absolute; left: -4.5ex; width: 4ex; color: #aaa; text-align: right;
30+
-khtml-user-select: none;
31+
-moz-user-select: none;
32+
-ms-user-select: none;
33+
-webkit-touch-callout: none;
34+
-webkit-user-select: none;
35+
user-select: none;}
36+
37+
.gist-syntax { background: #ffffff; }
38+
.gist-syntax .c { color: #999988; font-style: italic } /* Comment */
39+
.gist-syntax .err { color: #a61717; background-color: #e3d2d2 } /* Error */
40+
.gist-syntax .k { color: #000000; font-weight: bold } /* Keyword */
41+
.gist-syntax .o { color: #000000; font-weight: bold } /* Operator */
42+
.gist-syntax .cm { color: #999988; font-style: italic } /* Comment.Multiline */
43+
.gist-syntax .cp { color: #999999; font-weight: bold } /* Comment.Preproc */
44+
.gist-syntax .c1 { color: #999988; font-style: italic } /* Comment.Single */
45+
.gist-syntax .cs { color: #999999; font-weight: bold; font-style: italic } /* Comment.Special */
46+
.gist-syntax .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */
47+
.gist-syntax .gd .x { color: #000000; background-color: #ffaaaa } /* Generic.Deleted.Specific */
48+
.gist-syntax .ge { color: #000000; font-style: italic } /* Generic.Emph */
49+
.gist-syntax .gr { color: #aa0000 } /* Generic.Error */
50+
.gist-syntax .gh { color: #999999 } /* Generic.Heading */
51+
.gist-syntax .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */
52+
.gist-syntax .gi .x { color: #000000; background-color: #aaffaa } /* Generic.Inserted.Specific */
53+
.gist-syntax .go { color: #888888 } /* Generic.Output */
54+
.gist-syntax .gp { color: #555555 } /* Generic.Prompt */
55+
.gist-syntax .gs { font-weight: bold } /* Generic.Strong */
56+
.gist-syntax .gu { color: #aaaaaa } /* Generic.Subheading */
57+
.gist-syntax .gt { color: #aa0000 } /* Generic.Traceback */
58+
.gist-syntax .kc { color: #000000; font-weight: bold } /* Keyword.Constant */
59+
.gist-syntax .kd { color: #000000; font-weight: bold } /* Keyword.Declaration */
60+
.gist-syntax .kp { color: #000000; font-weight: bold } /* Keyword.Pseudo */
61+
.gist-syntax .kr { color: #000000; font-weight: bold } /* Keyword.Reserved */
62+
.gist-syntax .kt { color: #445588; font-weight: bold } /* Keyword.Type */
63+
.gist-syntax .m { color: #009999 } /* Literal.Number */
64+
.gist-syntax .s { color: #d14 } /* Literal.String */
65+
.gist-syntax .na { color: #008080 } /* Name.Attribute */
66+
.gist-syntax .nb { color: #0086B3 } /* Name.Builtin */
67+
.gist-syntax .nc { color: #445588; font-weight: bold } /* Name.Class */
68+
.gist-syntax .no { color: #008080 } /* Name.Constant */
69+
.gist-syntax .ni { color: #800080 } /* Name.Entity */
70+
.gist-syntax .ne { color: #990000; font-weight: bold } /* Name.Exception */
71+
.gist-syntax .nf { color: #990000; font-weight: bold } /* Name.Function */
72+
.gist-syntax .nn { color: #555555 } /* Name.Namespace */
73+
.gist-syntax .nt { color: #000080 } /* Name.Tag */
74+
.gist-syntax .nv { color: #008080 } /* Name.Variable */
75+
.gist-syntax .ow { color: #000000; font-weight: bold } /* Operator.Word */
76+
.gist-syntax .w { color: #bbbbbb } /* Text.Whitespace */
77+
.gist-syntax .mf { color: #009999 } /* Literal.Number.Float */
78+
.gist-syntax .mh { color: #009999 } /* Literal.Number.Hex */
79+
.gist-syntax .mi { color: #009999 } /* Literal.Number.Integer */
80+
.gist-syntax .mo { color: #009999 } /* Literal.Number.Oct */
81+
.gist-syntax .sb { color: #d14 } /* Literal.String.Backtick */
82+
.gist-syntax .sc { color: #d14 } /* Literal.String.Char */
83+
.gist-syntax .sd { color: #d14 } /* Literal.String.Doc */
84+
.gist-syntax .s2 { color: #d14 } /* Literal.String.Double */
85+
.gist-syntax .se { color: #d14 } /* Literal.String.Escape */
86+
.gist-syntax .sh { color: #d14 } /* Literal.String.Heredoc */
87+
.gist-syntax .si { color: #d14 } /* Literal.String.Interpol */
88+
.gist-syntax .sx { color: #d14 } /* Literal.String.Other */
89+
.gist-syntax .sr { color: #009926 } /* Literal.String.Regex */
90+
.gist-syntax .s1 { color: #d14 } /* Literal.String.Single */
91+
.gist-syntax .ss { color: #990073 } /* Literal.String.Symbol */
92+
.gist-syntax .bp { color: #999999 } /* Name.Builtin.Pseudo */
93+
.gist-syntax .vc { color: #008080 } /* Name.Variable.Class */
94+
.gist-syntax .vg { color: #008080 } /* Name.Variable.Global */
95+
.gist-syntax .vi { color: #008080 } /* Name.Variable.Instance */
96+
.gist-syntax .il { color: #009999 } /* Literal.Number.Integer.Long */

readme.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== Blazer Six Gist oEmbed ===
2+
Contributors: blazersix, bradyvercher
3+
Tags: gist, github, oembed
4+
Requires at least: 3.4
5+
Tested up to: 3.4.1
6+
Stable tag: 1.0
7+
License: GPLv2 or later
8+
License URI: http://www.gnu.org/licenses/gpl-2.0.html
9+
10+
Gist oEmbed and shortcode support with caching.
11+
12+
== Description ==
13+
14+
Enable oEmbed-like support for embedding Gists in your post content.
15+
16+
Allows for removing the external stylesheet dependency, so the styles can be added directly to your local stylesheet. Go ahead, style your Gists. Give them line numbers and alternating line background colors (check the included example stylesheet).
17+
18+
== Installation ==
19+
20+
Installing Blazer Six Gist oEmbed is just like installing most other plugins. [Check out the codex](http://codex.wordpress.org/Managing_Plugins#Installing_Plugins) if you have any questions.
21+
22+
== Frequently Asked Questions ==
23+
24+
= How get updates to my Gist to show up? =
25+
Gists are cached when they're first loaded. Update the post to invalidate the cache and fetch any changes.
26+
27+
== Changelog ==
28+
29+
= 1.0 =
30+
* Initial release.

0 commit comments

Comments
 (0)