Skip to content

Instantly share code, notes, and snippets.

@mloberg
Created November 14, 2011 17:13
Show Gist options
  • Save mloberg/1364481 to your computer and use it in GitHub Desktop.
Save mloberg/1364481 to your computer and use it in GitHub Desktop.
Popcorn Source Code Plugin
/**
* Popcorn SourceCode Plugin v0.1
* http://mattwritescode.com/2011/11/popcorn-source-code-plugin/
*
* Copyright 2011 Matthew Loberg (http://mloberg.com)
* Licensed under the MIT license
*
* Description:
* Add source code pre/code element to the page.
* If Google Code Prettify <http://code.google.com/p/google-code-prettify/>
* is available, it will style the code.
*
* Options:
* - Start: time you want the code to display
* - End: time you want the code to hide
* - Target: the id of the element you want the code to appear in
* - Code: the source code to display
* - Lang: the source code language (optional)
*
* Example:
var p = Popcorn("#video")
.sourceCode({
start: 5, // seconds
end: 15, // seconds
target: 'codeExample',
code: '<?php echo "foo";?>',
lang: 'php'
})
*/
(function(Popcorn){
Popcorn.plugin("sourceCode", {
_setup: function(options){
var target = document.getElementById(options.target),
code = options.code.toString().replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');
options._container = document.createElement("pre");
options._container.style.display = "none";
options._container.innerHTML = '<code class="prettyprint ' + options.lang + '">' + code + '</code>';
if(!target && Popcorn.plugin.debug){
throw new Error("target container doesn't exist");
}
target.appendChild(options._container);
},
start: function(event, options){
if(typeof prettyPrint == 'function') prettyPrint();
options._container.style.display = "block";
},
end: function(event, options){
options._container.style.display = "none";
},
_teardown: function(options){
document.getElementById(options.target).removeChild(options._container);
}
});
})(Popcorn);
/**
* Popcorn SourceCode Plugin v0.1
* http://mattwritescode.com/2011/11/popcorn-source-code-plugin/
*
* Copyright 2011 Matthew Loberg (http://mloberg.com)
* Licensed under the MIT license
*/
(function(x){x.plugin("sourceCode",{_setup:function(a){var b=document.getElementById(a.target),c=a.code.toString().replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;");a._container=document.createElement("pre");a._container.style.display="none";a._container.innerHTML='<code class="prettyprint '+a.lang+'">'+c+"</code>";if(!b&&Popcorn.plugin.debug){throw new Error("target container doesn't exist")}b.appendChild(a._container)},start:function(a,b){if(typeof prettyPrint=="function")prettyPrint();b._container.style.display="block"},end:function(a,b){b._container.style.display="none"},_teardown:function(a){document.getElementById(a.target).removeChild(a._container)}})})(Popcorn);
var p = Popcorn("#video");
p.sourceCode({
start: 5, // display at 5 seconds
end: 15, // hide at 15 seconds
target: 'source-code', // target element's id
code: "<?php echo 'foo';?>", // code to display
lang: 'php' // source code language
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment