From 94613dc674a4e8508eb2843ef404950af8c8b3ec Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Wed, 26 Jan 2011 19:28:13 +0530 Subject: [PATCH] code syntax highlighting --- pandora/templates/api.html | 2 ++ static/css/highlight.css | 67 +++++++++++++++++++++++++++++++++++++ static/js/highlight.pack.js | 1 + static/js/pandora.api.js | 16 +++++---- 4 files changed, 80 insertions(+), 6 deletions(-) create mode 100644 static/css/highlight.css create mode 100644 static/js/highlight.pack.js diff --git a/pandora/templates/api.html b/pandora/templates/api.html index fb67a3f9d..c0022bfa0 100644 --- a/pandora/templates/api.html +++ b/pandora/templates/api.html @@ -6,9 +6,11 @@ + + diff --git a/static/css/highlight.css b/static/css/highlight.css new file mode 100644 index 000000000..39be6f7ac --- /dev/null +++ b/static/css/highlight.css @@ -0,0 +1,67 @@ +pre code { + display: block; + background: #F0F0F0; +} + +pre code, +.xml .title { + color: black; +} + +.string, +.title, +.parent, +.tag .attribute .value, +.rules .value, +.rules .value .number, +.preprocessor, +.instancevar, +.aggregate, +.template_tag, +.django .variable, +.addition, +.flow, +.stream { + color: #800; +} + +.comment, +.annotation, +.template_comment, +.diff .header, +.chunk { + color: #888; +} + +.number, +.regexp, +.literal, +.change { + color: #080; +} + +.decorator, +.filter .argument, +.localvars, +.array, +.attr_selector, +.pi, +.doctype, +.deletion, +.envvar, +.shebang { + color: #88F; +} + +.keyword, +.id, +.title, +.aggregate { + font-weight: bold; +} + +.html .css, +.html .javascript, +.html .vbscript { + opacity: 0.5; +} diff --git a/static/js/highlight.pack.js b/static/js/highlight.pack.js new file mode 100644 index 000000000..d2aa56306 --- /dev/null +++ b/static/js/highlight.pack.js @@ -0,0 +1 @@ +var hljs=new function(){var p={};var a={};function n(c){return c.replace(/&/gm,"&").replace(//gm,">")}function k(s,r){if(!s){return false}for(var c=0;c"}function B(C){return""}while(z.length||A.length){var w=v().splice(0,1)[0];x+=n(y.substr(s,w.offset-s));s=w.offset;if(w.event=="start"){x+=t(w.node);u.push(w.node)}else{if(w.event=="stop"){var r=u.length;do{r--;var c=u[r];x+=B(c)}while(c!=w.node);u.splice(r,1);while(r'+n(M[0])+""}else{O+=n(M[0])}R=Q.lR.lastIndex;M=Q.lR.exec(N)}O+=n(N.substr(R,N.length-R));return O}function L(r,N){if(N.subLanguage&&a[N.subLanguage]){var M=g(N.subLanguage,r);u+=M.keyword_count;C+=M.r;return M.value}else{return G(r,N)}}function J(N,r){var M=N.nM?"":'';if(N.rB){s+=M;N.buffer=""}else{if(N.eB){s+=n(r)+M;N.buffer=""}else{s+=M;N.buffer=r}}D[D.length]=N}function F(M,O,R){var P=D[D.length-1];if(R){s+=L(P.buffer+M,P);return false}var S=A(O,P);if(S){s+=L(P.buffer+M,P);J(S,O);C+=S.r;return S.rB}var r=x(D.length-1,O);if(r){var T=P.nM?"":"";if(P.rE){s+=L(P.buffer+M,P)+T}else{if(P.eE){s+=L(P.buffer+M,P)+T+n(O)}else{s+=L(P.buffer+M+O,P)+T}}while(r>1){T=D[D.length-2].nM?"":"";s+=T;r--;D.length--}var Q=D[D.length-1];D.length--;D[D.length-1].buffer="";if(Q.starts){for(var N=0;N1){throw"Illegal"}return{language:K,r:C,keyword_count:u,value:s}}catch(H){if(H=="Illegal"){return{language:null,r:0,keyword_count:0,value:n(E)}}else{throw H}}}function i(){function r(y,x){if(y.compiled){return}if(y.b){y.bR=e(x,"^"+y.b)}if(y.e){y.eR=e(x,"^"+y.e)}if(y.i){y.iR=e(x,"^(?:"+y.i+")")}if(y.r==undefined){y.r=1}if(!y.displayClassName){y.displayClassName=y.cN}if(!y.cN){y.nM=true}for(var w in y.k){if(!y.k.hasOwnProperty(w)){continue}if(y.k[w] instanceof Object){y.keywordGroups=y.k}else{y.keywordGroups={keyword:y.k}}break}y.sm=[];if(y.c){for(var v=0;vz.keyword_count+z.r){z=w}if(w.keyword_count+w.r>y.keyword_count+y.r){z=y;y=w}}}var u=v.className;if(!u.match(y.language)){u=u?(u+" "+y.language):y.language}var c=d(v);if(c.length){var s=document.createElement("pre");s.innerHTML=y.value;y.value=m(c,d(s),C)}if(A){y.value=y.value.replace(/^((<[^>]+>|\t)+)/gm,function(D,G,F,E){return G.replace(/\t/g,A)})}if(r){y.value=y.value.replace(/\n/g,"
")}if(/MSIE [678]/.test(navigator.userAgent)&&v.tagName=="CODE"&&v.parentNode.tagName=="PRE"){var s=v.parentNode;var x=document.createElement("div");x.innerHTML="
"+y.value+"
";v=x.firstChild.firstChild;x.firstChild.cN=s.cN;s.parentNode.replaceChild(x.firstChild,s)}else{v.innerHTML=y.value}v.className=u;v.dataset={};v.dataset.result={language:y.language,kw:y.keyword_count,re:y.r};if(z&&z.language){v.dataset.second_best={language:z.language,kw:z.keyword_count,re:z.r}}}function l(){if(l.called){return}l.called=true;f();if(arguments.length){for(var c=0;c|>=|>>|>>=|>>>|>>>=|\\?|\\[|\\{|\\(|\\^|\\^=|\\||\\|=|\\|\\||~";this.ASM={cN:"string",b:"'",e:"'",i:"\\n",c:["escape"],r:0};this.QSM={cN:"string",b:'"',e:'"',i:"\\n",c:["escape"],r:0};this.BE={cN:"escape",b:"\\\\.",e:this.IMR,nM:true,r:0};this.CLCM={cN:"comment",b:"//",e:"$",r:0};this.CBLCLM={cN:"comment",b:"/\\*",e:"\\*/"};this.HCM={cN:"comment",b:"#",e:"$"};this.NUMBER_MODE={cN:"number",b:this.NR,e:this.IMR,r:0};this.CNM={cN:"number",b:this.CNR,e:this.IMR,r:0};this.inherit=function(c,t){var s={};for(var r in c){s[r]=c[r]}if(t){for(var r in t){s[r]=t[r]}}return s}}();var initHighlightingOnLoad=hljs.initHighlightingOnLoad;hljs.LANGUAGES.javascript={dM:{l:[hljs.UIR],c:["string","comment","number","regexp_container","function"],k:{keyword:{"in":1,"if":1,"for":1,"while":1,"finally":1,"var":1,"new":1,"function":1,"do":1,"return":1,"void":1,"else":1,"break":1,"catch":1,"instanceof":1,"with":1,"throw":1,"case":1,"default":1,"try":1,"this":1,"switch":1,"continue":1,"typeof":1,"delete":1},literal:{"true":1,"false":1,"null":1}}},m:[hljs.CLCM,hljs.CBLCLM,hljs.CNM,hljs.ASM,hljs.QSM,hljs.BE,{cN:"regexp_container",b:"("+hljs.RSR+"|case|return|throw)\\s*",e:hljs.IMR,nM:true,l:[hljs.IR],k:{"return":1,"throw":1,"case":1},c:["comment",{cN:"regexp",b:"/.*?[^\\\\/]/[gim]*",e:hljs.IMR}],r:0},{cN:"function",b:"\\bfunction\\b",e:"{",l:[hljs.UIR],k:{"function":1},c:[{cN:"title",b:"[A-Za-z$_][0-9A-Za-z$_]*",e:hljs.IMR},{cN:"params",b:"\\(",e:"\\)",c:["string","comment"]}]}]};hljs.LANGUAGES.python={dM:{l:[hljs.UIR],i:"(|\\?)",c:["comment","string","function","class","number","decorator"],k:{keyword:{and:1,elif:1,is:1,global:1,as:1,"in":1,"if":1,from:1,raise:1,"for":1,except:1,"finally":1,print:1,"import":1,pass:1,"return":1,exec:1,"else":1,"break":1,not:1,"with":1,"class":1,assert:1,yield:1,"try":1,"while":1,"continue":1,del:1,or:1,def:1,lambda:1,nonlocal:10},built_in:{None:1,True:1,False:1,Ellipsis:1,NotImplemented:1}}},m:[{cN:"function",l:[hljs.UIR],b:"\\bdef ",e:":",i:"$",k:{def:1},c:["title","params"],r:10},{cN:"class",l:[hljs.UIR],b:"\\bclass ",e:":",i:"[${]",k:{"class":1},c:["title","params"],r:10},{cN:"title",b:hljs.UIR,e:hljs.IMR},{cN:"params",b:"\\(",e:"\\)",c:["string"]},hljs.HCM,hljs.CNM,{cN:"string",b:"u?r?'''",e:"'''",r:10},{cN:"string",b:'u?r?"""',e:'"""',r:10},hljs.ASM,hljs.QSM,hljs.BE,{cN:"string",b:"(u|r|ur)'",e:"'",c:["escape"],r:10},{cN:"string",b:'(u|r|ur)"',e:'"',c:["escape"],r:10},{cN:"decorator",b:"@",e:"$"}]}; \ No newline at end of file diff --git a/static/js/pandora.api.js b/static/js/pandora.api.js index ac48ee9b4..84060531b 100755 --- a/static/js/pandora.api.js +++ b/static/js/pandora.api.js @@ -119,10 +119,12 @@ function constructList() { if(data.ids.length) $.each(data.ids, function(v, k) { info.append($("

").html(k)); - info.append($('
').html(app.actions[k].doc.replace('/\n/
\n/g'))); - - var $code = $('
').html(app.actions[k].code.replace('/\n/
\n/g')) - .hide(); + var $doc =$('
')
+                           .html(app.actions[k].doc.replace('/\n/
\n/g')) + .appendTo(info); + var $code = $('') + .html(app.actions[k].code.replace('/\n/
\n/g')) + .hide(); var $button = new Ox.Button({ title: [ {id: "one", title: "expand"}, @@ -133,8 +135,10 @@ function constructList() { .addClass("margin") .click(function() { $code.toggle()}) .appendTo(info) - $('').html(' View Python Source').appendTo(info) - $code.appendTo(info) + $('').html(' View Python Source').appendTo(info) + $('
').append($code).appendTo(info) 
+                hljs.highlightBlock($code[0], '    ');
+
                 hash += k + ','
               });
             else