diff --git a/README.txt b/README.txt index ba7dd38..c9b79cd 100644 --- a/README.txt +++ b/README.txt @@ -1,9 +1,9 @@ === WP-Git-Embed === -Contributors: gbaptistas +Contributors: gbaptistas, willybahuaud Tags: github, gist, bitbucket, git, embed, code, highlight, source, sourcecode, syntax, syntax highlighting Requires at least: 3.0.1 -Tested up to: 3.4 -Stable tag: 0.4 +Tested up to: 3.6.0 +Stable tag: 0.5 License: MIT License URI: http://opensource.org/licenses/MIT @@ -21,6 +21,9 @@ See: https://github.com/gbaptista/wp-git-embed == Changelog == += 0.5 = +* Add Prism support. + = 0.4 = * Fix file names. diff --git a/css/prism-dark.css b/css/prism-dark.css new file mode 100644 index 0000000..b3b40b9 --- /dev/null +++ b/css/prism-dark.css @@ -0,0 +1,108 @@ +/** + * prism.js Dark theme for JavaScript, CSS and HTML + * Based on the slides of the talk “/Reg(exp){2}lained/” + * @author Lea Verou + */ + +code[class*="language-"], +pre[class*="language-"] { + color: white; + text-shadow: 0 -.1em .2em black; + font-family: Consolas, Monaco, 'Andale Mono', monospace; + direction: ltr; + text-align: left; + white-space: pre; + word-spacing: normal; + + -moz-tab-size: 4; + -o-tab-size: 4; + tab-size: 4; + + -webkit-hyphens: none; + -moz-hyphens: none; + -ms-hyphens: none; + hyphens: none; +} + +pre[class*="language-"], +:not(pre) > code[class*="language-"] { + background: hsl(30,20%,25%); +} + +/* Code blocks */ +pre[class*="language-"] { + padding: 1em; + margin: .5em 0; + overflow: auto; + border: .3em solid hsl(30,20%,40%); + border-radius: .5em; + box-shadow: 1px 1px .5em black inset; +} + +/* Inline code */ +:not(pre) > code[class*="language-"] { + padding: .15em .2em .05em; + border-radius: .3em; + border: .13em solid hsl(30,20%,40%); + box-shadow: 1px 1px .3em -.1em black inset; +} + +.token.comment, +.token.prolog, +.token.doctype, +.token.cdata { + color: hsl(30,20%,50%); +} + +.token.punctuation { + opacity: .7; +} + +.namespace { + opacity: .7; +} + +.token.property, +.token.tag, +.token.boolean, +.token.number { + color: hsl(350, 40%, 70%); +} + +.token.selector, +.token.attr-name, +.token.string { + color: hsl(75, 70%, 60%); +} + +.token.operator, +.token.entity, +.token.url, +.language-css .token.string, +.style .token.string { + color: hsl(40, 90%, 60%); +} + +.token.atrule, +.token.attr-value, +.token.keyword, +.token.function, +.token.constant { + color: hsl(350, 40%, 70%); +} + + +.token.regex, +.token.important, +.token.variable { + color: #e90; +} + +.token.important, +.token.deliminator { + font-weight: bold; +} + +.token.entity { + cursor: help; +} diff --git a/css/prism-funky.css b/css/prism-funky.css new file mode 100644 index 0000000..c81c82d --- /dev/null +++ b/css/prism-funky.css @@ -0,0 +1,104 @@ +/** + * prism.js Funky theme + * Based on “Polyfilling the gaps” talk slides http://lea.verou.me/polyfilling-the-gaps/ + * @author Lea Verou + */ + +code[class*="language-"], +pre[class*="language-"] { + font-family: Consolas, Monaco, 'Andale Mono', monospace; + direction: ltr; + text-align: left; + white-space: pre; + word-spacing: normal; + + -moz-tab-size: 4; + -o-tab-size: 4; + tab-size: 4; + + -webkit-hyphens: none; + -moz-hyphens: none; + -ms-hyphens: none; + hyphens: none; +} + +/* Code blocks */ +pre[class*="language-"] { + padding: .4em .8em; + margin: .5em 0; + overflow: auto; + background: url('data:image/svg+xml;charset=utf-8,%0D%0A%0D%0A%0D%0A<%2Fsvg>'); + background-size: 1em 1em; +} + +code[class*="language-"] { + background: black; + color: white; + box-shadow: -.3em 0 0 .3em black, .3em 0 0 .3em black; +} + +/* Inline code */ +:not(pre) > code[class*="language-"] { + padding: .2em; + border-radius: .3em; + box-shadow: none; +} + +.token.comment, +.token.prolog, +.token.doctype, +.token.cdata { + color: #aaa; +} + +.token.punctuation { + color: #999; +} + +.namespace { + opacity: .7; +} + +.token.property, +.token.tag, +.token.boolean, +.token.number { + color: #0cf; +} + +.token.selector, +.token.attr-name, +.token.string { + color: yellow; +} + +.token.operator, +.token.entity, +.token.url, +.language-css .token.string { + color: yellowgreen; +} + +.token.atrule, +.token.attr-value, +.token.keyword, +.token.function, +.token.constant { + color: deeppink; +} + + +.token.regex, +.token.important, +.token.variable { + color: orange; +} + +.token.important, +.token.deliminator { + font-weight: bold; +} + +.token.entity { + cursor: help; +} \ No newline at end of file diff --git a/css/prism-willy2.css b/css/prism-willy2.css new file mode 100644 index 0000000..eb76e1c --- /dev/null +++ b/css/prism-willy2.css @@ -0,0 +1,110 @@ +/** + * prism.js default theme for JavaScript, CSS and HTML + * Based on dabblet (http://dabblet.com) + * @author Lea Verou + */ + +code[class*="language-"], +pre[class*="language-"] { + color: white; + text-shadow: 0 1px black; + font-family: Consolas, Monaco, 'Andale Mono', monospace; + direction: ltr; + text-align: left; + white-space: pre; + word-spacing: normal; + + -moz-tab-size: 4; + -o-tab-size: 4; + tab-size: 4; + + -webkit-hyphens: none; + -moz-hyphens: none; + -ms-hyphens: none; + hyphens: none; +} + +/* Code blocks */ +pre[class*="language-"] { + padding: 1em; + margin: .5em 0; + overflow: auto; +} + +:not(pre) > code[class*="language-"], +pre[class*="language-"] { + background: #0F0F0F; +} + +/* Inline code */ +:not(pre) > code[class*="language-"] { + padding: .1em; + border-radius: .3em; +} + +.token.comment, +.token.prolog, +.token.doctype, +.token.cdata { + color: #536377; +} + +.token.punctuation { + color: #63CFC5; +} + +.namespace { + opacity: .7; +} + +.token.property, +.token.tag, +.token.boolean, +.token.number { + color: #FB44B7; +} + +.token.selector, +.token.attr-name, +.token.string { + color: #EFFA72; +} + +.token.operator, +.token.entity, +.token.url, +.language-css .token.string, +.style .token.string { + color: #C14F3E; + /*background: hsla(0,0%,0%,.5);*/ +} + +.token.atrule, +.token.attr-value, +.token.keyword, +.token.function, +.token.constant { + color: #07a; +} + +.token.deliminator { + font-weight:bold; +} + +.token.regex, +.token.important, +.token.variable { + color: #2B36C4; +} + +.token.important { + font-weight: bold; +} + +.token.entity { + cursor: help; +} + +.token.deliminator { + font-weight:bold; +} diff --git a/css/prism.css b/css/prism.css new file mode 100644 index 0000000..4cca3c8 --- /dev/null +++ b/css/prism.css @@ -0,0 +1,110 @@ +/** + * prism.js default theme for JavaScript, CSS and HTML + * Based on dabblet (http://dabblet.com) + * @author Lea Verou + */ + +code[class*="language-"], +pre[class*="language-"] { + color: black; + text-shadow: 0 1px white; + font-family: Consolas, Monaco, 'Andale Mono', monospace; + direction: ltr; + text-align: left; + white-space: pre; + word-spacing: normal; + + -moz-tab-size: 4; + -o-tab-size: 4; + tab-size: 4; + + -webkit-hyphens: none; + -moz-hyphens: none; + -ms-hyphens: none; + hyphens: none; +} + +/* Code blocks */ +pre[class*="language-"] { + padding: 1em; + margin: .5em 0; + overflow: auto; +} + +:not(pre) > code[class*="language-"], +pre[class*="language-"] { + background: #f5f2f0; +} + +/* Inline code */ +:not(pre) > code[class*="language-"] { + padding: .1em; + border-radius: .3em; +} + +.token.comment, +.token.prolog, +.token.doctype, +.token.cdata { + color: slategray; +} + +.token.punctuation { + color: #999; +} + +.namespace { + opacity: .7; +} + +.token.property, +.token.tag, +.token.boolean, +.token.number { + color: #905; +} + +.token.selector, +.token.attr-name, +.token.string { + color: #690; +} + +.token.operator, +.token.entity, +.token.url, +.language-css .token.string, +.style .token.string { + color: #a67f59; + background: hsla(0,0%,100%,.5); +} + +.token.atrule, +.token.attr-value, +.token.keyword, +.token.function, +.token.constant { + color: #07a; +} + +.token.deliminator { + font-weight:bold; +} + +.token.regex, +.token.important, +.token.variable { + color: #e90; +} + +.token.important { + font-weight: bold; +} + +.token.entity { + cursor: help; +} + +.token.deliminator { + font-weight:bold; +} diff --git a/js/prefixfree.min.js b/js/prefixfree.min.js new file mode 100644 index 0000000..94fc408 --- /dev/null +++ b/js/prefixfree.min.js @@ -0,0 +1,5 @@ +/** + * StyleFix 1.0.3 & PrefixFree 1.0.7 + * @author Lea Verou + * MIT license + */(function(){function t(e,t){return[].slice.call((t||document).querySelectorAll(e))}if(!window.addEventListener)return;var e=window.StyleFix={link:function(t){try{if(t.rel!=="stylesheet"||t.hasAttribute("data-noprefix"))return}catch(n){return}var r=t.href||t.getAttribute("data-href"),i=r.replace(/[^\/]+$/,""),s=t.parentNode,o=new XMLHttpRequest,u;o.onreadystatechange=function(){o.readyState===4&&u()};u=function(){var n=o.responseText;if(n&&t.parentNode&&(!o.status||o.status<400||o.status>600)){n=e.fix(n,!0,t);if(i){n=n.replace(/url\(\s*?((?:"|')?)(.+?)\1\s*?\)/gi,function(e,t,n){return/^([a-z]{3,10}:|\/|#)/i.test(n)?e:'url("'+i+n+'")'});var r=i.replace(/([\\\^\$*+[\]?{}.=!:(|)])/g,"\\$1");n=n.replace(RegExp("\\b(behavior:\\s*?url\\('?\"?)"+r,"gi"),"$1")}var u=document.createElement("style");u.textContent=n;u.media=t.media;u.disabled=t.disabled;u.setAttribute("data-href",t.getAttribute("href"));s.insertBefore(u,t);s.removeChild(t);u.media=t.media}};try{o.open("GET",r);o.send(null)}catch(n){if(typeof XDomainRequest!="undefined"){o=new XDomainRequest;o.onerror=o.onprogress=function(){};o.onload=u;o.open("GET",r);o.send(null)}}t.setAttribute("data-inprogress","")},styleElement:function(t){if(t.hasAttribute("data-noprefix"))return;var n=t.disabled;t.textContent=e.fix(t.textContent,!0,t);t.disabled=n},styleAttribute:function(t){var n=t.getAttribute("style");n=e.fix(n,!1,t);t.setAttribute("style",n)},process:function(){t('link[rel="stylesheet"]:not([data-inprogress])').forEach(StyleFix.link);t("style").forEach(StyleFix.styleElement);t("[style]").forEach(StyleFix.styleAttribute)},register:function(t,n){(e.fixers=e.fixers||[]).splice(n===undefined?e.fixers.length:n,0,t)},fix:function(t,n,r){for(var i=0;i-1&&(e=e.replace(/(\s|:|,)(repeating-)?linear-gradient\(\s*(-?\d*\.?\d*)deg/ig,function(e,t,n,r){r=Math.abs(r-450)%360;return t+(n||"")+"linear-gradient("+r+"deg"}));e=t("functions","(\\s|:|,)","\\s*\\(","$1"+s+"$2(",e);e=t("keywords","(\\s|:)","(\\s|;|\\}|$)","$1"+s+"$2$3",e);e=t("properties","(^|\\{|\\s|;)","\\s*:","$1"+s+"$2:",e);if(n.properties.length){var o=RegExp("\\b("+n.properties.join("|")+")(?!:)","gi");e=t("valueProperties","\\b",":(.+?);",function(e){return e.replace(o,s+"$1")},e)}if(r){e=t("selectors","","\\b",n.prefixSelector,e);e=t("atrules","@","\\b","@"+s+"$1",e)}e=e.replace(RegExp("-"+s,"g"),"-");e=e.replace(/-\*-(?=[a-z]+)/gi,n.prefix);return e},property:function(e){return(n.properties.indexOf(e)?n.prefix:"")+e},value:function(e,r){e=t("functions","(^|\\s|,)","\\s*\\(","$1"+n.prefix+"$2(",e);e=t("keywords","(^|\\s)","(\\s|$)","$1"+n.prefix+"$2$3",e);return e},prefixSelector:function(e){return e.replace(/^:{1,2}/,function(e){return e+n.prefix})},prefixProperty:function(e,t){var r=n.prefix+e;return t?StyleFix.camelCase(r):r}};(function(){var e={},t=[],r={},i=getComputedStyle(document.documentElement,null),s=document.createElement("div").style,o=function(n){if(n.charAt(0)==="-"){t.push(n);var r=n.split("-"),i=r[1];e[i]=++e[i]||1;while(r.length>3){r.pop();var s=r.join("-");u(s)&&t.indexOf(s)===-1&&t.push(s)}}},u=function(e){return StyleFix.camelCase(e)in s};if(i.length>0)for(var a=0;a/g, '>').replace(/\u00a0/g, ' '); + //console.time(code.slice(0,50)); + + var env = { + element: element, + language: language, + grammar: grammar, + code: code + }; + + _.hooks.run('before-highlight', env); + + if (async && self.Worker) { + var worker = new Worker(_.filename); + + worker.onmessage = function(evt) { + env.highlightedCode = Token.stringify(JSON.parse(evt.data)); + env.element.innerHTML = env.highlightedCode; + + callback && callback.call(env.element); + //console.timeEnd(code.slice(0,50)); + _.hooks.run('after-highlight', env); + }; + + worker.postMessage(JSON.stringify({ + language: env.language, + code: env.code + })); + } + else { + env.highlightedCode = _.highlight(env.code, env.grammar) + env.element.innerHTML = env.highlightedCode; + + callback && callback.call(element); + + _.hooks.run('after-highlight', env); + //console.timeEnd(code.slice(0,50)); + } + }, + + highlight: function (text, grammar) { + return Token.stringify(_.tokenize(text, grammar)); + }, + + tokenize: function(text, grammar) { + var Token = _.Token; + + var strarr = [text]; + + var rest = grammar.rest; + + if (rest) { + for (var token in rest) { + grammar[token] = rest[token]; + } + + delete grammar.rest; + } + + tokenloop: for (var token in grammar) { + if(!grammar.hasOwnProperty(token) || !grammar[token]) { + continue; + } + + var pattern = grammar[token], + inside = pattern.inside, + lookbehind = !!pattern.lookbehind || 0; + + pattern = pattern.pattern || pattern; + + for (var i=0; i text.length) { + // Something went terribly wrong, ABORT, ABORT! + break tokenloop; + } + + if (str instanceof Token) { + continue; + } + + pattern.lastIndex = 0; + + var match = pattern.exec(str); + + if (match) { + if(lookbehind) { + lookbehind = match[1].length; + } + + var from = match.index - 1 + lookbehind, + match = match[0].slice(lookbehind), + len = match.length, + to = from + len, + before = str.slice(0, from + 1), + after = str.slice(to + 1); + + var args = [i, 1]; + + if (before) { + args.push(before); + } + + var wrapped = new Token(token, inside? _.tokenize(match, inside) : match); + + args.push(wrapped); + + if (after) { + args.push(after); + } + + Array.prototype.splice.apply(strarr, args); + } + } + } + + return strarr; + }, + + hooks: { + all: {}, + + add: function (name, callback) { + var hooks = _.hooks.all; + + hooks[name] = hooks[name] || []; + + hooks[name].push(callback); + }, + + run: function (name, env) { + var callbacks = _.hooks.all[name]; + + if (!callbacks || !callbacks.length) { + return; + } + + for (var i=0, callback; callback = callbacks[i++];) { + callback(env); + } + } + } +}; + +var Token = _.Token = function(type, content) { + this.type = type; + this.content = content; +}; + +Token.stringify = function(o) { + if (typeof o == 'string') { + return o; + } + + if (Object.prototype.toString.call(o) == '[object Array]') { + for (var i=0; i' + env.content + ''; + +}; + +if (!self.document) { + // In worker + self.addEventListener('message', function(evt) { + var message = JSON.parse(evt.data), + lang = message.language, + code = message.code; + + self.postMessage(JSON.stringify(_.tokenize(code, _.languages[lang]))); + self.close(); + }, false); + + return; +} + +// Get current script and highlight +var script = document.getElementsByTagName('script'); + +script = script[script.length - 1]; + +if (script) { + _.filename = script.src; + + if (document.addEventListener && !script.hasAttribute('data-manual')) { + document.addEventListener('DOMContentLoaded', _.highlightAll); + } +} + +})(); + +/*********************************************** + Begin prism-markup.js +***********************************************/ + +Prism.languages.markup = { + 'comment': /<!--[\w\W]*?--(>|>)/g, + 'prolog': /<\?.+?\?>/, + 'doctype': /<!DOCTYPE.+?>/, + 'cdata': /<!\[CDATA\[[\w\W]+?]]>/i, + 'tag': { + pattern: /<\/?[\w:-]+\s*[\w\W]*?>/gi, + inside: { + 'tag': { + pattern: /^<\/?[\w:-]+/i, + inside: { + 'punctuation': /^<\/?/, + 'namespace': /^[\w-]+?:/ + } + }, + 'attr-value': { + pattern: /=(('|")[\w\W]*?(\2)|[^\s>]+)/gi, + inside: { + 'punctuation': /=/g + } + }, + 'punctuation': /\/?>/g, + 'attr-name': { + pattern: /[\w:-]+/g, + inside: { + 'namespace': /^[\w-]+?:/ + } + } + + } + }, + 'entity': /&#?[\da-z]{1,8};/gi +}; + +// Plugin to make entity title show the real entity, idea by Roman Komarov +Prism.hooks.add('wrap', function(env) { + + if (env.type === 'entity') { + env.attributes['title'] = env.content.replace(/&/, '&'); + } +}); + +/*********************************************** + Begin prism-php.js +***********************************************/ + +Prism.languages.php = { + 'ignore': /&(lt|gt|amp);/gi, + 'comment': { + pattern: /(^|[^\\])(\/\*[\w\W]*?\*\/|\/\/.*?(\r?\n|$))/g, + lookbehind: true + }, + 'deliminator': /(\?>|\?>|<\?php|<\?php)/ig, + 'variable': /(\$[a-z_]\w+)((-(\>|>)\$?[a-z_]\w+(?!\()))?\b/ig, + 'string': /(("|')(\\?.)*?\2)|((<<<|<<<)([a-zA-Z_])[\w\W]+\4;)/g, + 'regex': { + pattern: /(^|[^/])\/(?!\/)(\[.+?]|\\.|[^/\r\n])+\/[gim]{0,3}(?=\s*($|[\r\n,.;})]))/g, + lookbehind: true + }, + 'keyword': /\b(and|or|xor|array|as|break|case|cfunction|class|const|continue|declare|default|die|do|else|elseif|enddeclare|endfor|endforeach|endif|endswitch|endwhile|extends|for|foreach|function|include|include_once|global|if|new|return|static|switch|use|require|require_once|var|while|abstract|interface|pu blic|implements|extends|private|protected|throw|exit|echo)\b/g, + 'function': /(((-(\>|>)\w+))(?=\())|(\b(abs|acos|acosh|addcslashes|addslashes|array_change_key_case|array_chunk|array_combine|array_count_values|array_diff|array_diff_assoc|array_diff_key|array_diff_uassoc|array_diff_ukey|array_fill|array_filter|array_flip|array_intersect|array_intersect_assoc|array_intersect_key|array_intersect_uassoc|array_intersect_ukey|array_key_exists|array_keys|array_map|array_merge|array_merge_recursive|array_multisort|array_pad|array_pop|array_product|array_push|array_rand|array_reduce|array_reverse|array_search|array_shift|array_slice|array_splice|array_sum|array_udiff|array_udiff_assoc|array_udiff_uassoc|array_uintersect|array_uintersect_assoc|array_uintersect_uassoc|array_unique|array_unshift|array_values|array_walk|array_walk_recursive|atan|atan2|atanh|base64_decode|base64_encode|base_convert|basename|bcadd|bccomp|bcdiv|bcmod|bcmul|bindec|bindtextdomain|bzclose|bzcompress|bzdecompress|bzerrno|bzerror|bzerrstr|bzflush|bzopen|list|bzread|bzwrite|ceil|chdir|checkdate|checkdnsrr|chgrp|chmod|chop|chown|chr|chroot|chunk_split|class_exists|closedir|closelog|copy|cos|cosh|count|count_chars|date|decbin|dechex|decoct|deg2rad|delete|ebcdic2ascii|empty|end|ereg|ereg_replace|eregi|eregi_replace|error_log|error_reporting|escapeshellarg|escapeshellcmd|eval|exec|exp|explode|extension_loaded|feof|fflush|fgetc|fgetcsv|fgets|fgetss|file_exists|file_get_contents|file_put_contents|fileatime|filectime|filegroup|fileinode|filemtime|fileowner|fileperms|filesize|filetype|floatval|flock|floor|flush|fmod|fnmatch|fopen|fpassthru|fprintf|fputcsv|fputs|fread|fscanf|fseek|fsockopen|fstat|ftell|ftok|getallheaders|getcwd|getdate|getenv|gethostbyaddr|gethostbyname|gethostbynamel|getimagesize|getlastmod|getmxrr|getmygid|getmyinode|getmypid|getmyuid|getopt|getprotobyname|getprotobynumber|getrandmax|getrusage|getservbyname|getservbyport|gettext|gettimeofday|gettype|glob|gmdate|gmmktime|ini_alter|ini_get|ini_get_all|ini_restore|ini_set|interface_exists|intval|ip2long|is_a|is_array|is_bool|is_callable|is_dir|is_double|is_executable|is_file|is_finite|is_float|is_infinite|is_int|is_integer|is_link|is_long|is_nan|is_null|is_numeric|is_object|is_readable|is_real|is_resource|is_scalar|is_soap_fault|is_string|is_subclass_of|is_uploaded_file|is_writable|is_writeable|mkdir|mktime|nl2br|parse_ini_file|parse_str|parse_url|passthru|pathinfo|readlink|realpath|rewind|rewinddir|rmdir|round|str_ireplace|str_pad|str_repeat|str_replace|str_rot13|str_shuffle|str_split|str_word_count|strcasecmp|strchr|strcmp|strcoll|strcspn|strftime|strip_tags|stripcslashes|stripos|stripslashes|stristr|strlen|strnatcasecmp|strnatcmp|strncasecmp|strncmp|strpbrk|strpos|strptime|strrchr|strrev|strripos|strrpos|strspn|strstr|strtok|strtolower|strtotime|strtoupper|strtr|strval|substr|substr_compare|unset|print|isset)\b)/g, + 'boolean': /\b(true|false)\b/g, + 'number': /\b-?(0x)?\d*\.?\d+\b/g, + 'operator': /[-+]{1,2}|!|=?\<|=?\>;|={1,2}(?!>)|(\&|\&){1,2}|\|?\||\?|\*|\//g, + 'punctuation': /[{}[\];(),.:]/g, + 'constant': /(\b[A-Z_]+\b)|(__FILE__|__DIR__|__LINE__|__METHOD__|__NAMESPACE__|__FUNCTION__|__CLASS__)\b/g, +}; + +if (Prism.languages.markup) { + Prism.languages.insertBefore('php', 'deliminator', { + 'markup': { + pattern: /(\?>|\?>)[\w\W]*?(?=(<\?php|<\?php))/ig, + lookbehind : true, + inside: { + 'tag': { + pattern: /<\/?[\w:-]+\s*[\w\W]*?>/gi, + inside: Prism.languages.markup.tag.inside + }, + rest: Prism.languages.php + } + } + }); +} + +if (Prism.languages.markup) { + Prism.languages.insertBefore('markup', 'prolog', { + 'php': { + pattern: /(<\?php|<\?php)[\w\W]*?(\?>|\?>)/g, + inside: { + 'php': { + pattern: /(<\?php|<\?php)[\w\W]*?(\?>|\?>)/g, + inside: Prism.languages.php + }, + rest: Prism.languages.markup.tag + } + } + }); +} + + +/*********************************************** + Begin prism-css.js +***********************************************/ + +Prism.languages.css = { + 'comment': /\/\*[\w\W]*?\*\//g, + 'atrule': /@[\w-]+?(\s+.+)?(?=\s*{|\s*;)/gi, + 'url': /url\((["']?).*?\1\)/gi, + 'selector': /[^\{\}\s][^\{\}]*(?=\s*\{)/g, + 'property': /(\b|\B)[a-z-]+(?=\s*:)/ig, + 'string': /("|')(\\?.)*?\1/g, + 'important': /\B!important\b/gi, + 'ignore': /&(lt|gt|amp);/gi, + 'punctuation': /[\{\};:]/g +}; + +if (Prism.languages.markup) { + Prism.languages.insertBefore('markup', 'tag', { + 'style': { + pattern: /(<|<)style[\w\W]*?(>|>)[\w\W]*?(<|<)\/style(>|>)/ig, + inside: { + 'tag': { + pattern: /(<|<)style[\w\W]*?(>|>)|(<|<)\/style(>|>)/ig, + inside: Prism.languages.markup.tag.inside + }, + rest: Prism.languages.css + } + } + }); +} + +/*********************************************** + Begin prism-javascript.js +***********************************************/ + +Prism.languages.javascript = { + 'comment': { + pattern: /(^|[^\\])(\/\*[\w\W]*?\*\/|\/\/.*?(\r?\n|$))/g, + lookbehind: true + }, + 'string': /("|')(\\?.)*?\1/g, + 'regex': { + pattern: /(^|[^/])\/(?!\/)(\[.+?]|\\.|[^/\r\n])+\/[gim]{0,3}(?=\s*($|[\r\n,.;})]))/g, + lookbehind: true + }, + 'keyword': /\b(var|let|if|else|while|do|for|return|in|instanceof|function|new|with|typeof|try|catch|finally|null|break|continue)\b/g, + 'boolean': /\b(true|false)\b/g, + 'number': /\b-?(0x)?\d*\.?\d+\b/g, + 'operator': /[-+]{1,2}|!|=?<|=?>|={1,2}|(&){1,2}|\|?\||\?|\*|\//g, + 'ignore': /&(lt|gt|amp);/gi, + 'punctuation': /[{}[\];(),.:]/g +}; + +if (Prism.languages.markup) { + Prism.languages.insertBefore('markup', 'tag', { + 'script': { + pattern: /(<|<)script[\w\W]*?(>|>)[\w\W]*?(<|<)\/script(>|>)/ig, + inside: { + 'tag': { + pattern: /(<|<)script[\w\W]*?(>|>)|(<|<)\/script(>|>)/ig, + inside: Prism.languages.markup.tag.inside + }, + rest: Prism.languages.javascript + } + } + }); +} \ No newline at end of file diff --git a/wp-git-embed.php b/wp-git-embed.php index f88fbcd..9f05075 100644 --- a/wp-git-embed.php +++ b/wp-git-embed.php @@ -4,12 +4,12 @@ Plugin Name: WP-Git-Embed Plugin URI: http://wordpress.org/extend/plugins/wp-git-embed/ Description: Embed GitHub, Gist or Bitbucket files. -Version: 0.4 -Author: Guilherme Baptista +Version: 0.5 +Author: Guilherme Baptista, Willy Bahuaud Author URI: http://gbaptista.com License: MIT */ - +DEFINE( 'GITPLUGINURL', trailingslashit( WP_PLUGIN_URL ) . basename( dirname( __FILE__ ) ) ); if(!class_exists('WP_Git_Embed')) { class WP_Git_Embed { @@ -27,8 +27,17 @@ private function __construct() { add_filter('comment_text', array(__CLASS__, 'beforeFilter'), -1); } + + private static function raw($code) { + /** + */ + $transientname = 'code_wge_' . substr( md5( $code ), 0, 29 ); // I CHANGED TRANSIENT PREFIXE + if( false === apply_filters( 'cache_embeded_code', ( $raw = get_transient( $transientname ) ) ) ) { + /** + + */ if(preg_match('/\{[0-9].*\}/', $code, $lines)) { list($s_line, $e_line) = explode(':', preg_replace('/\{|\}/', '', $lines[0])); if(empty($e_line)) $e_line = $s_line; @@ -44,24 +53,8 @@ private static function raw($code) { } - // GitHub (Custom Ruby Immersion) - https://github.com/gbaptista/ruby-immersion - if(preg_match('/^\[ruby_code:.*\]/', $code)) { - $path = explode('[ruby_code:', $code); - $path = explode('/', preg_replace('/\]$/', '', $path[1])); - if(strlen($path[1]) == 1) $path[1] = '0'.$path[1]; - $file = '[git:https://github.com/gbaptista/ruby-immersion/blob/master/lib/'.$path[0].'/'.$path[1].'.rb]'; - } - - // GitHub (Custom Ruby Immersion Test) - https://github.com/gbaptista/ruby-immersion - elseif(preg_match('/^\[ruby_test:.*\]/', $code)) { - $path = explode('[ruby_test:', $code); - $path = explode('/', preg_replace('/\]$/', '', $path[1])); - if(strlen($path[1]) == 1) $path[1] = '0'.$path[1]; - $file = '[git:https://github.com/gbaptista/ruby-immersion/blob/master/test/'.$path[0].'/test_'.$path[1].'.rb]'; - } - // Default - else $file = $code; + $file = $code; // GitHub - https://github.com/ if(preg_match('/:\/\/github.com/', $file)) { @@ -96,85 +89,78 @@ private static function raw($code) { $link = $raw; - $raw = file_get_contents($raw); + $raw = file_get_contents($raw); - // GitHub (Custom Ruby Immersion) - https://github.com/gbaptista/ruby-immersion - if(preg_match('/^\[ruby_code:.*\]/', $code)) { - $raw = preg_replace("/\# encoding\: utf\-8\n{2,}|^\n{1,}/", '', $raw); - } - - // GitHub (Custom Ruby Immersion Test) - https://github.com/gbaptista/ruby-immersion - elseif(preg_match('/^\[ruby_test:.*\]/', $code)) { - $raw = str_replace("require 'test/unit'", '', $raw); - $raw = str_replace("require 'include_file'", '', $raw); - $raw = str_replace("IncludeFile::inject __FILE__", '', $raw); - $raw = preg_replace("/class LoveTest.* \< Test\:\:Unit\:\:TestCase/", '', $raw); - $raw = str_replace("def test_with_love", '', $raw); - $raw = preg_replace("/end$/", '', trim($raw)); - $raw = preg_replace("/end$/", '', trim($raw)); - $raw = preg_replace("/ /", '', trim($raw)); - $raw = preg_replace("/\n{2,}/", "\n\n", trim($raw)); - $raw = preg_replace("/\# encoding\: utf\-8/", '', trim($raw)); - $raw = preg_replace("/^\n{1,}/", '', trim($raw)); - } + $raw = preg_replace("/\/", '>', trim($raw)); - if(!empty($s_line)) - $raw = implode("\n", array_slice(preg_split('/\r\n|\r|\n/', $raw), $s_line-1, ($e_line-$s_line)+1)); - else { - $s_line = 1; - } - - if(!empty($format)) { - if(preg_match('/^pre.*/', $format)) { - $format = explode('_', $format); - $raw = '
' . $raw . '
'; - $links = TRUE; - $format = 'pre'; - } elseif(preg_match('/^sourcecode.*/', $format)) { - $format = explode('_', $format); - $raw = '[sourcecode language="'.$format[1].'"]' . $raw . '[/sourcecode]'; - $links = TRUE; - $format = 'sourcecode'; - } else { - $links = FALSE; + if(!empty($s_line)) + $raw = implode("\n", array_slice(preg_split('/\r\n|\r|\n/', $raw), $s_line-1, ($e_line-$s_line)+1)); + else { + $s_line = 1; } - } else $links = FALSE; - if($links) { + if(!empty($format)) { + if(preg_match('/^precode.*/', $format)) { + $format = explode('_', $format); + $raw = '
' . $raw . '
'; + $links = TRUE; + $format = 'pre'; + } elseif(preg_match('/^pre.*/', $format)) { + $format = explode('_', $format); + $raw = '
' . $raw . '
'; + $links = TRUE; + $format = 'pre'; + } elseif(preg_match('/^sourcecode.*/', $format)) { + $format = explode('_', $format); + $raw = '[sourcecode language="'.$format[1].'"]' . $raw . '[/sourcecode]'; + $links = TRUE; + $format = 'sourcecode'; + } else { + $links = FALSE; + } + } else $links = FALSE; - $file_name = preg_replace('/#.*/', '', end(preg_split('/\/|\\\/', $link))); - $file_name = preg_replace('/\?.*/', '', $file_name); + if($links) { - //echo $source . '
' . $link; exit; + $file_name = preg_replace('/#.*/', '', end(preg_split('/\/|\\\/', $link))); + $file_name = preg_replace('/\?.*/', '', $file_name); - if($format == 'pre') - $raw .= '
'.$file_name.''; - else - $raw .= '
'.$file_name.''; + if($format == 'pre' || $format == 'precode') + $raw .= '
'.$file_name.''; + else + $raw .= '
'.$file_name.''; - if(preg_match('/^http.*:/', $link)) { + if(preg_match('/^http.*:/', $link)) { - if(empty($source)) - $raw .= 'download file'; - else { - $raw .= 'view raw'; - $raw .= 'view file on '; + if(empty($source)) + $raw .= 'download file'; + else { + $raw .= 'view raw'; + $raw .= 'view file on '; - if($service == 'github') $raw .= 'GitHub'; - elseif($service == 'gist') $raw .= 'GitHub Gist'; - elseif($service == 'bitbucket') $raw .= ' Bitbucket'; + if($service == 'github') $raw .= 'GitHub'; + elseif($service == 'gist') $raw .= 'GitHub Gist'; + elseif($service == 'bitbucket') $raw .= ' Bitbucket'; + } + } + + $raw .= '
'; } - - $raw .= '
'; - - } - + set_transient( $transientname, htmlspecialchars( $raw ) ); // I REMOVED TIME ARGUMENT + return $raw; # return $raw .= "\n\n# $source"; # Todo. + /** + + */ } else return $code; + } else { + return htmlspecialchars_decode( $raw ); + } } @@ -206,4 +192,29 @@ public static function beforeFilter( $content ) { function WP_Git_Embed() { return WP_Git_Embed::getInstance(); } add_action( 'plugins_loaded', 'WP_Git_Embed' ); + function enqueue_prismjs(){ + wp_register_script( 'prismjs', GITPLUGINURL.'/js/prism.js', array('jquery'), '0.9', true ); + wp_register_style( 'prismcss', GITPLUGINURL.'/css/' . apply_filters( 'prism_css', 'prism-okaidia' ) . '.css', false, '0.95', 'all' ); + wp_enqueue_style( 'prismcss' ); + wp_enqueue_script( 'jquery' ); + wp_enqueue_script( 'prismjs' ); + } + add_action( 'wp_enqueue_scripts', 'enqueue_prismjs' ); +} + +add_action('emptying_code_transient', 'drop_wge_transients'); + +/** +false cron :-) +*/ +function transient_cleaning() { + if ( ! wp_next_scheduled( 'emptying_code_transient' ) ) { + wp_schedule_event( time(), apply_filters( 'emptying_code_transient', 'weekly' ), 'emptying_code_transient' ); + } +} +add_action('wp', 'transient_cleaning'); + +function drop_wge_transients() { + global $wpdb; + $wpdb->query( $wpdb->prepare("DELETE FROM {$wpdb->options} WHERE option_name LIKE '_transient_code_wge_%'") ); } \ No newline at end of file