@loftysoul/soul
Version:
The professional Content Management System, based ghost
42 lines (41 loc) • 7.05 kB
JavaScript
(function(t){"object"==typeof exports&&"object"==typeof module?t(require("../../lib/codemirror"),require("../htmlmixed/htmlmixed")):"function"==typeof define&&define.amd?define(["../../lib/codemirror","../htmlmixed/htmlmixed"],t):t(CodeMirror)})((function(t){"use strict"
var e={noEndTag:!0,soyState:"param-def"},a={alias:{noEndTag:!0},delpackage:{noEndTag:!0},namespace:{noEndTag:!0,soyState:"namespace-def"},"@param":e,"@param?":e,"@inject":e,"@inject?":e,"@state":e,"@state?":e,template:{soyState:"templ-def",variableScope:!0},literal:{},msg:{},fallbackmsg:{noEndTag:!0,reduceIndent:!0},let:{soyState:"var-def"},if:{},elseif:{noEndTag:!0,reduceIndent:!0},else:{noEndTag:!0,reduceIndent:!0},switch:{},case:{noEndTag:!0,reduceIndent:!0},default:{noEndTag:!0,reduceIndent:!0},foreach:{variableScope:!0,soyState:"var-def"},ifempty:{noEndTag:!0,reduceIndent:!0},for:{variableScope:!0,soyState:"var-def"},call:{soyState:"templ-ref"},param:{soyState:"param-ref"},print:{noEndTag:!0},deltemplate:{soyState:"templ-def",variableScope:!0},delcall:{soyState:"templ-ref"},log:{},element:{variableScope:!0}},n=Object.keys(a).filter((function(t){return!a[t].noEndTag||a[t].reduceIndent}))
t.defineMode("soy",(function(e){var r=t.getMode(e,"text/plain"),o={html:t.getMode(e,{name:"text/html",multilineTagIndentFactor:2,multilineTagIndentPastTag:!1}),attributes:r,text:r,uri:r,trusted_resource_uri:r,css:t.getMode(e,"text/css"),js:t.getMode(e,{name:"text/javascript",statementIndent:2*e.indentUnit})}
function s(t){return t[t.length-1]}function i(t,e,a){if(t.sol()){for(var n=0;n<e.indent&&t.eat(/\s/);n++);if(n)return null}var r=t.string,o=a.exec(r.substr(t.pos))
o&&(t.string=r.substr(0,t.pos+o.index))
var i=t.hideFirstChars(e.indent,(function(){var a=s(e.localStates)
return a.mode.token(t,a.state)}))
return t.string=r,i}function l(t,e){return{element:e,next:t}}function c(t){t.context&&(t.context.scope&&(t.variables=t.context.scope),t.context=t.context.previousContext)}function d(t,e,a){this.previousContext=t,this.tag=e,this.kind=null,this.scope=a}return{startState:function(){return{soyState:[],templates:null,variables:l(null,"ij"),scopes:null,indent:0,quoteKind:null,context:null,localStates:[{mode:o.html,state:t.startState(o.html)}]}},copyState:function(e){return{tag:e.tag,soyState:e.soyState.concat([]),templates:e.templates,variables:e.variables,context:e.context,indent:e.indent,quoteKind:e.quoteKind,localStates:e.localStates.map((function(e){return{mode:e.mode,state:t.copyState(e.mode,e.state)}}))}},token:function(r,m){var u,p,f
switch(s(m.soyState)){case"comment":if(r.match(/^.*?\*\//)?m.soyState.pop():r.skipToEnd(),!m.context||!m.context.scope)for(var g=/@param\??\s+(\S+)/g,h=r.current();y=g.exec(h);)m.variables=l(m.variables,y[1])
return"comment"
case"string":var y
return(y=r.match(/^.*?(["']|\\[\s\S])/))?y[1]==m.quoteKind&&(m.quoteKind=null,m.soyState.pop()):r.skipToEnd(),"string"}if(!m.soyState.length||"literal"!=s(m.soyState)){if(r.match(/^\/\*/))return m.soyState.push("comment"),"comment"
if(r.match(r.sol()?/^\s*\/\/.*/:/^\s+\/\/.*/))return"comment"}switch(s(m.soyState)){case"templ-def":return(y=r.match(/^\.?([\w]+(?!\.[\w]+)*)/))?(m.templates=l(m.templates,y[1]),m.soyState.pop(),"def"):(r.next(),null)
case"templ-ref":return(y=r.match(/(\.?[a-zA-Z_][a-zA-Z_0-9]+)+/))?(m.soyState.pop(),"."==y[0][0]?"variable-2":"variable"):(r.next(),null)
case"namespace-def":return(y=r.match(/^\.?([\w\.]+)/))?(m.soyState.pop(),"variable"):(r.next(),null)
case"param-def":return(y=r.match(/^\w+/))?(m.variables=l(m.variables,y[0]),m.soyState.pop(),m.soyState.push("param-type"),"def"):(r.next(),null)
case"param-ref":return(y=r.match(/^\w+/))?(m.soyState.pop(),"property"):(r.next(),null)
case"param-type":return"}"==r.peek()?(m.soyState.pop(),null):r.eatWhile(/^([\w]+|[?])/)?"type":(r.next(),null)
case"var-def":return(y=r.match(/^\$([\w]+)/))?(m.variables=l(m.variables,y[1]),m.soyState.pop(),"def"):(r.next(),null)
case"tag":var S=(T="/"==m.tag[0])?m.tag.substring(1):m.tag,v=a[S]
if(r.match(/^\/?}/)){var b="/}"==r.current()
return b&&!T&&c(m),"/template"==m.tag||"/deltemplate"==m.tag?(m.variables=l(null,"ij"),m.indent=0):m.indent-=e.indentUnit*(b||-1==n.indexOf(m.tag)?2:1),m.soyState.pop(),"keyword"}if(r.match(/^([\w?]+)(?==)/)){if("kind"==r.current()&&(y=r.match(/^="([^"]+)/,!1))){var x=y[1]
m.context.kind=x
var w=o[x]||o.html;(C=s(m.localStates)).mode.indent&&(m.indent+=C.mode.indent(C.state,"","")),m.localStates.push({mode:w,state:t.startState(w)})}return"attribute"}return(y=r.match(/([\w]+)(?=\()/))?"variable callee":(y=r.match(/^["']/))?(m.soyState.push("string"),m.quoteKind=y,"string"):r.match(/(null|true|false)(?!\w)/)||r.match(/0x([0-9a-fA-F]{2,})/)||r.match(/-?([0-9]*[.])?[0-9]+(e[0-9]*)?/)?"atom":r.match(/(\||[+\-*\/%]|[=!]=|\?:|[<>]=?)/)?"operator":(y=r.match(/^\$([\w]+)/))?(u=m.variables,p=y[1],function(t,e){for(;t;){if(t.element===e)return!0
t=t.next}return!1}(u,p)?"variable-2":f?"variable":"variable-2 error"):(y=r.match(/^\w+/))?/^(?:as|and|or|not|in)$/.test(y[0])?"keyword":null:(r.next(),null)
case"literal":return r.match(/^(?=\{\/literal})/)?(m.indent-=e.indentUnit,m.soyState.pop(),this.token(r,m)):i(r,m,/\{\/literal}/)}if(r.match(/^\{literal}/))return m.indent+=e.indentUnit,m.soyState.push("literal"),m.context=new d(m.context,"literal",m.variables),"keyword"
if(y=r.match(/^\{([/@\\]?\w+\??)(?=$|[\s}]|\/[/*])/)){var k=m.tag
m.tag=y[1]
var T="/"==m.tag[0],E=!!a[m.tag]
S=T?m.tag.substring(1):m.tag,v=a[S]
"/switch"!=m.tag&&(m.indent+=((T||v&&v.reduceIndent)&&"switch"!=k?1:2)*e.indentUnit),m.soyState.push("tag")
var I=!1
if(v)if(T||v.soyState&&m.soyState.push(v.soyState),v.noEndTag||!E&&T){if(T)if(m.context&&m.context.tag==S){if(m.context){var C
if(m.context.kind)m.localStates.pop(),(C=s(m.localStates)).mode.indent&&(m.indent-=C.mode.indent(C.state,"",""))
c(m)}}else I=!0}else m.context=new d(m.context,m.tag,v.variableScope?m.variables:null)
else T&&(I=!0)
return(I?"error ":"")+"keyword"}return r.eat("{")?(m.tag="print",m.indent+=2*e.indentUnit,m.soyState.push("tag"),"keyword"):i(r,m,/\{|\s+\/\/|\/\*/)},indent:function(a,n,r){var o=a.indent,i=s(a.soyState)
if("comment"==i)return t.Pass
if("literal"==i)/^\{\/literal}/.test(n)&&(o-=e.indentUnit)
else{if(/^\s*\{\/(template|deltemplate)\b/.test(n))return 0;/^\{(\/|(fallbackmsg|elseif|else|ifempty)\b)/.test(n)&&(o-=e.indentUnit),"switch"!=a.tag&&/^\{(case|default)\b/.test(n)&&(o-=e.indentUnit),/^\{\/switch\b/.test(n)&&(o-=e.indentUnit)}var l=s(a.localStates)
return o&&l.mode.indent&&(o+=l.mode.indent(l.state,n,r)),o},innerMode:function(t){return t.soyState.length&&"literal"!=s(t.soyState)?null:s(t.localStates)},electricInput:/^\s*\{(\/|\/template|\/deltemplate|\/switch|fallbackmsg|elseif|else|case|default|ifempty|\/literal\})$/,lineComment:"//",blockCommentStart:"/*",blockCommentEnd:"*/",blockCommentContinue:" * ",useInnerComments:!1,fold:"indent"}}),"htmlmixed"),t.registerHelper("wordChars","soy",/[\w$]/),t.registerHelper("hintWords","soy",Object.keys(a).concat(["css","debugger"])),t.defineMIME("text/x-soy","soy")}))