--- /dev/null
+// PLUGIN: popcorndoc
+
+(function (Popcorn) {
+
+ /**
+ * Github Gist popcorn plug-in
+ * Loads and allows lines of code in a gist to be highlighted, and examples run.
+ *
+ * @param {Object} options
+ *
+ * Example:
+ * var p = Popcorn('#video')
+ * .popcorndoc({
+ * start: 5, // seconds
+ * end: 15, // seconds
+ * lines: '25', // line(s) to highlight and show
+ * target: 'gist-container', // DIV in which to load gist
+ * gistUrl: 'https://gist.github.com/289467' // URL of gist
+ * })
+ * // The specified "line" can also be a list
+ * .popcorndoc({
+ * start: 20, // seconds
+ * end: 25, // seconds
+ * lines: '26,29-32', // line(s) to highlight and show
+ * target: 'gist-container', // DIV in which to load gist
+ * gistUrl: 'https://gist.github.com/289467' // URL of gist
+ * })
+ * // Highlighting can be removed
+ * .popcorndoc({
+ * start: 20, // seconds
+ * end: 25, // seconds
+ * lines: null // line(s) to highlight and show
+ * target: 'gist-container', // DIV in which to load gist
+ * gistUrl: 'https://gist.github.com/289467' // URL of gist
+ * });
+ * // Run the example. NOTE: should be last gist track.
+ * .popcorndoc({
+ * start: 29, // seconds
+ * target: 'gist-container', // DIV in which to load gist
+ * gistUrl: 'https://gist.github.com/289467', // URL of gist
+ * runIn: 'iframe-container-div' // DIV in which to load the example in an iframe
+ * });
+ */
+
+ /**
+ * script.js - https://github.com/kares/script.js
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0.html
+ * @version 0.7-SNAPSHOT
+ */
+var script=function(){function n(e,b){var d=e.loaded,a=e.complete;if(e.defer){var c=document.createElement("script");c.src=e.src;if(e.type)c.type=e.type;e.charset&&c.setAttribute("charset",e.charset);var g=function(){if(!(d&&d.call(c,e.writes?h||void 0:void 0)===!1)){var b=document.getElementById(e.id);if(e.writes&&h){var g=function(a){c.nextSibling?c.parentNode.insertBefore(a,c.nextSibling):c.parentNode.appendChild(a)};b||(b=document.createElement("div"),g(b));b.style.display="none";b.innerHTML=
+"<br/>"+h.join("");b.removeChild(b.childNodes[0]);for(var f=b.childNodes[0];f;)b.removeChild(f),g(f),f=b.childNodes[0]}b&&b.parentNode.removeChild(b);a&&a.call(c)}},f=!1;c.onload=c.onreadystatechange=function(){if(!f&&(!this.readyState||this.readyState==="loaded"||this.readyState==="complete")){f=!0;c.onload=c.onreadystatechange=null;try{g()}finally{b&&b()}}};e.append(c)}else{c=document.getElementById(e.id);try{var i;d&&(i=d.call(c));i!==!1&&a&&a.call(c)}finally{if(e.idGenerated)c.id=null;b&&b()}}}
+function i(){if(f&&!f.loading)f.loading=!0,function b(){if(f.length){var d=f.shift();if(d)if(d.writes){if(!j)j=document.write;if(!k)k=document.writeln;document.write=o;document.writeln=p;h=null;n(d,function(){if(j)document.write=j;if(k)document.writeln=k;h=k=j=null;b()})}else n(d),b();else b()}else delete f.loading}()}var h=null,o=function(){var e=arguments;e&&e[0]&&(h||(h=[]),h.push(e[0]))},p=function(e){o(e+"\n")},j,k,f=[],g,l=function(){g&&(g=null,i())};if(document.addEventListener)g=function(){document.removeEventListener("DOMContentLoaded",
+g,!1);g=null;i()},document.addEventListener("DOMContentLoaded",g,!1),window.addEventListener("load",l,!1);else if(document.attachEvent){g=function(){document.readyState==="complete"&&(document.detachEvent("onreadystatechange",g),g=null,i())};document.attachEvent("onreadystatechange",g);window.attachEvent("onload",l);l=!1;try{l=window.frameElement==null}catch(r){}document.documentElement.doScroll&&l&&function b(){if(g){try{document.documentElement.doScroll("left")}catch(d){setTimeout(b,1);return}i()}}()}var m=
+function(b){if(!b)throw"script : no arguments given";if(typeof b==="string")b={src:b};else if(!b.src)throw"script : 'src' is required";var d,a={},c=m.defaults;if(c)for(d in c)a[d]=c[d];for(d in b)a[d]=b[d];if(a.base&&a.src.substring(0,4)!=="http")d=a.base,c=a.base.length-1,d=d[c]=="/"?d.substring(0,c):d,a.src=d+"/"+a.src;if(a.onload!=null)a.onLoad=a.onLoad!=null?b.onload!=null?b.onload:b.onLoad!=null?b.onLoad:a.onload:a.onload,delete a.onload;if(typeof a.defer==="undefined")a.defer=a.onLoad;if(!a.id)a.id=
+m._generateId(),a.idGenerated=!0;var h=a.append;if(typeof h==="string")a.append=function(a){document.getElementById(h).appendChild(a)};else if(h&&h.appendChild)a.append=function(a){h.appendChild(a)};if(a.writes==null)a.writes=!0;if(g){if(a.defer){if(b='<div id="'+a.id+'"',b+=a.loadingHTML?">"+a.loadingHTML:' style="diplay: none;">',b+="</div>",!a.append)a.append=function(b){var c=document.getElementById(a.id);c.parentNode.insertBefore(b,c)}}else b='<script id="'+a.id+'" src="'+a.src+'"><\/script>';
+document.write(b);a.order==null?f.push(a):f.splice(a.order,0,a)}else{a.defer=!0;if(!a.append)a.append=function(a){document.getElementsByTagName("body")[0].appendChild(a)};a.order==null?f.push(a):f.splice(a.order,0,a);i()}},q=0;m._generateId=function(){return"_script-"+q++};m.defaults={type:"text/javascript"};return m}();
+
+ /**
+ * Inject CSS class into DOM, from http://paulirish.com/2008/bookmarklet-inject-new-css-rules/
+ */
+ var gistCssClassName;
+ function setupCss(className) {
+ if (className) {
+ gistCssClassName = className;
+ return;
+ }
+
+ gistCssClassName = "popcorn-gist-highlighted";
+ var defaultHighlightedCSS = ".popcorn-gist-highlighted { background-color: #FFC; }";
+
+ if ("\v" == "v") {
+ document.createStyleSheet().cssText = defaultHighlightedCSS;
+ } else {
+ var tag = document.createElement("style");
+ tag.type = "text/css";
+ document.getElementsByTagName("head")[0].appendChild(tag);
+ tag[(typeof document.body.style.WebkitAppearance == "string") ? "innerText" : "innerHTML"] = defaultHighlightedCSS;
+ }
+ }
+
+ function applyStyle(linesString, func) {
+ var lines = [];
+ Popcorn.forEach( linesString.split( ',' ), function ( item, i ) {
+ var range = item.split('-'),
+ lineStart = range[0] | 0,
+ lineEnd = range[1] | 0;
+
+ if (lineEnd) {
+ // Make sure the line range is valid
+ lineEnd = lineEnd > lineStart ? lineEnd : lineStart + 1;
+
+ while (lineStart <= lineEnd ) {
+ lines.push( lineStart++ );
+ }
+ } else {
+ lines.push( lineStart );
+ }
+ } );
+
+ var i = lines.length;
+ while ( i-- ) {
+ func( lines[i] );
+ }
+ }
+
+ var highlighted = {};
+
+ function getLine(num) {
+ return document.getElementById('LC' + num);
+ }
+
+ function unhighlight(line) {
+ line = getLine(line);
+ if (!line) {
+ return;
+ }
+
+ line.classList.remove('popcorn-gist-highlighted');
+ }
+
+ function highlight(line) {
+ line = getLine(line);
+ if (!line) {
+ return;
+ }
+
+ line.classList.add('popcorn-gist-highlighted');
+ }
+
+ function scrollTo(line) {
+ // todo...
+ }
+
+ var gists = {};
+
+ function loadGist(url, target) {
+ var gistId = url.match(/gist\.github\.com\/(\d+)/)[1];
+ script({
+ src: 'https://gist.github.com/' + gistId + '.js',
+ append: target
+ });
+ gists[url] = true;
+ }
+
+ var iframe;
+ function cleanUp(elem) {
+ var container = elem.parentNode;
+ if (!container) {
+ throw new Error(ERR_MSG);
+ }
+ container.removeChild(iframe);
+ iframe = null;
+ }
+
+ var ERR_MSG = "popcorndoc: runIn property must be a vaild ID of a DIV in the document.";
+
+ Popcorn.plugin( "popcorndoc" , {
+
+ manifest: {
+ about:{
+ name: "Popcorn Documentation Gist Plugin",
+ version: "0.1",
+ author: "@humphd, @rwaldron, @remixmanifesto",
+ website: "http://popcornjs.org"
+ },
+ options:{
+ start : {elem:'input', type:'text', label:'In'},
+ end : {elem:'input', type:'text', label:'Out'},
+ target : 'gist-container',
+ lines : {elem:'input', type:'text', label:'Line'},
+ gistUrl : {elem:'input', type:'text', label:'Gist URL'},
+ runIn : {elem:'input', type:'text', label:'Div to Run Example in'}
+ }
+ },
+
+ _setup: function(options) {
+ if (!gistCssClassName) {
+ setupCss(options.className);
+ }
+
+ if (!gists[options.gistUrl]) {
+ loadGist(options.gistUrl, options.target);
+ }
+ },
+
+ start: function(event, options) {
+ if (options.lines) {
+ applyStyle( options.lines, highlight );
+ return;
+ }
+
+ unhighlight();
+
+ if (options.runIn) {
+ console.log('runIn start');
+ if (iframe) {
+ cleanUp(iframe);
+ }
+
+ iframe = document.createElement( "iframe" );
+
+ var contents = document.querySelectorAll( "div.line" ),
+ container = document.getElementById(options.runIn),
+ iframeDoc;
+
+ if (!container) {
+ throw new Error(ERR_MSG);
+ }
+
+ container.style.display = 'block';
+
+ iframe.setAttribute( 'width', '100%' );
+ iframe.setAttribute( 'height', '100%' );
+
+ container.appendChild( iframe );
+ iframeDoc = ( iframe.contentWindow || iframe.contentDocument ).document;
+ iframeDoc.open();
+
+ Popcorn.forEach( contents, function( div ) {
+ var text = div.textContent;
+ if ( text ) {
+ // Remove single-line comments, being careful not to ditch URLs (http://....)
+ text = text.replace( /[^:]\/\/.+$/, '' );
+ iframeDoc.write( text + '\n' );
+ }
+ } );
+
+ iframeDoc.close();
+ }
+ },
+
+ end: function( event, options ){
+ if (options.lines) {
+ applyStyle( options.lines, unhighlight );
+ }
+
+ if (options.runIn) {
+ console.log('runIn end');
+ if (iframe) {
+ cleanUp(iframe);
+ }
+ }
+ }
+ });
+
+})( Popcorn );
--- /dev/null
+test( "popcorndoc existence", function(){
+
+ var popped = Popcorn( "#video" ).pause();
+
+ var expects = 2,
+ count = 0,
+
+ plus = function() {
+ if ( ++count === expects ) {
+ start();
+ Popcorn.destroy( popped );
+ }
+ };
+
+ expect( expects );
+ stop();
+
+ ok( popped.popcorndoc, "popcorndoc exists:" );
+ plus();
+ equals( typeof popped.popcorndoc, "function" , "Popcorn.popcorndoc is a function" );
+ plus();
+
+});
+
+test( "popcorndoc functional tests", function() {
+
+ var popped = Popcorn( "#video" );
+ popped.pause().volume( 0 );
+
+ var expects = 20,
+ count = 0,
+
+ plus = function() {
+ if ( ++count === expects ) {
+ Popcorn.destroy( popped );
+ start();
+ }
+ };
+
+ stop();
+
+ popped.popcorndoc({
+ start: 1,
+ end: 2,
+ lines: '1',
+ target: 'gistdiv',
+ gistUrl: 'https://gist.github.com/289467'
+ })
+ .popcorndoc({
+ start: 1,
+ end: 2,
+ lines: '3-5',
+ target: 'gistdiv',
+ gistUrl: 'https://gist.github.com/289467'
+ })
+ .popcorndoc({
+ start: 2,
+ end: 3,
+ lines: '2,6-8',
+ target: 'gistdiv',
+ gistUrl: 'https://gist.github.com/289467'
+ })
+ .popcorndoc({
+ start: 3,
+ end: 4,
+ lines: '15-10',
+ target: 'gistdiv',
+ gistUrl: 'https://gist.github.com/289467'
+ });
+
+ var line,
+ lines = [],
+ i = 0;
+
+ while( line = document.getElementById( "LC" + ( ++i ) ) ) {
+ lines.push( line );
+ }
+
+ popped.exec( 1.5, function() {
+ var line1 = document.getElementById( "LC1" ),
+ line3 = document.getElementById( "LC3" ),
+ line4 = document.getElementById( "LC4" ),
+ line5 = document.getElementById( "LC5" );
+
+ ok( line1.className.match( /popcorn-gist-highlighted/ ), "line 1 is highlighted" );
+ plus();
+ ok( line3.className.match( /popcorn-gist-highlighted/ ), "line 3 is highlighted" );
+ plus();
+ ok( line4.className.match( /popcorn-gist-highlighted/ ), "line 4 is highlighted" );
+ plus();
+ ok( line5.className.match( /popcorn-gist-highlighted/ ), "line 5 is highlighted" );
+ plus();
+ })
+ .exec( 2.5, function() {
+ var line2 = document.getElementById( "LC2" ),
+ line6 = document.getElementById( "LC6" ),
+ line7 = document.getElementById( "LC7" ),
+ line8 = document.getElementById( "LC8" ),
+ line1 = document.getElementById( "LC1" ),
+ line3 = document.getElementById( "LC3" ),
+ line4 = document.getElementById( "LC4" ),
+ line5 = document.getElementById( "LC5" );
+
+ ok( !line1.className.match( /popcorn-gist-highlighted/ ), "line 1 is not highlighted" );
+ plus();
+ ok( !line3.className.match( /popcorn-gist-highlighted/ ), "line 3 is not highlighted" );
+ plus();
+ ok( !line4.className.match( /popcorn-gist-highlighted/ ), "line 4 is not highlighted" );
+ plus();
+ ok( !line5.className.match( /popcorn-gist-highlighted/ ), "line 5 is not highlighted" );
+ plus();
+
+ ok( line2.className.match( /popcorn-gist-highlighted/ ), "line 2 is highlighted" );
+ plus();
+ ok( line6.className.match( /popcorn-gist-highlighted/ ), "line 6 is highlighted" );
+ plus();
+ ok( line7.className.match( /popcorn-gist-highlighted/ ), "line 7 is highlighted" );
+ plus();
+ ok( line8.className.match( /popcorn-gist-highlighted/ ), "line 8 is highlighted" );
+ plus();
+ })
+ .exec( 3.5, function() {
+ var line15 = document.getElementById( "LC15" ),
+ line16 = document.getElementById( "LC16" ),
+ line2 = document.getElementById( "LC2" ),
+ line6 = document.getElementById( "LC6" ),
+ line7 = document.getElementById( "LC7" ),
+ line8 = document.getElementById( "LC8" );
+
+ ok( !line2.className.match( /popcorn-gist-highlighted/ ), "line 2 is not highlighted" );
+ plus();
+ ok( !line6.className.match( /popcorn-gist-highlighted/ ), "line 6 is not highlighted" );
+ plus();
+ ok( !line7.className.match( /popcorn-gist-highlighted/ ), "line 7 is not highlighted" );
+ plus();
+ ok( !line8.className.match( /popcorn-gist-highlighted/ ), "line 8 is not highlighted" );
+ plus();
+
+ ok( line15.className.match( /popcorn-gist-highlighted/ ), "line 15 is highlighted, with bad range input" );
+ plus();
+ ok( line16.className.match( /popcorn-gist-highlighted/ ), "line 16 is highlighted as the fallback for an incorrect range" );
+ plus();
+ ok( !document.getElementById( "LC10" ).className.match( /popcorn-gist-highlighted/ ), "line 10 is NOT highlighted" );
+ plus();
+ })
+ .exec( 4.5, function() {
+ var highlighted = 0 ;
+ for( var i = 0, len = lines.len; i < len; i++ ) {
+ lines[ i ].className.match( /popcorn.gist.highlighted/ ) && highlighted++;
+ }
+ equals( highlighted, 0, "0 lines are highlighted" );
+ plus();
+
+ });
+
+ popped.play();
+});
\ No newline at end of file