11518cc1354d589ba873312fff0acffadd079b01
[redakcja.git] / project / static / js / reg_syntax.js
1         EditAreaLoader.prototype.get_regexp= function(text_array){\r
2                 //res="( |=|\\n|\\r|\\[|\\(|ยต|)(";\r
3                 res="(\\b)(";\r
4                 for(i=0; i<text_array.length; i++){\r
5                         if(i>0)\r
6                                 res+="|";\r
7                         //res+="("+ tab_text[i] +")";\r
8                         //res+=tab_text[i].replace(/(\.|\?|\*|\+|\\|\(|\)|\[|\]|\{|\})/g, "\\$1");\r
9                         res+=this.get_escaped_regexp(text_array[i]);\r
10                 }\r
11                 //res+=")( |\\.|:|\\{|\\(|\\)|\\[|\\]|\'|\"|\\r|\\n|\\t|$)";\r
12                 res+=")(\\b)";\r
13                 reg= new RegExp(res);\r
14                 \r
15                 return res;\r
16         };\r
17         \r
18         \r
19         EditAreaLoader.prototype.get_escaped_regexp= function(str){\r
20                 return str.toString().replace(/(\.|\?|\*|\+|\\|\(|\)|\[|\]|\}|\{|\$|\^|\|)/g, "\\$1");\r
21         };\r
22         \r
23         EditAreaLoader.prototype.init_syntax_regexp= function(){\r
24                 var lang_style= {};     \r
25                 for(var lang in this.load_syntax){\r
26                         if(!this.syntax[lang])  // init the regexp if not already initialized\r
27                         {\r
28                                 this.syntax[lang]= {};\r
29                                 this.syntax[lang]["keywords_reg_exp"]= {};\r
30                                 this.keywords_reg_exp_nb=0;\r
31                         \r
32                                 if(this.load_syntax[lang]['KEYWORDS']){\r
33                                         param="g";\r
34                                         if(this.load_syntax[lang]['KEYWORD_CASE_SENSITIVE']===false)\r
35                                                 param+="i";\r
36                                         for(var i in this.load_syntax[lang]['KEYWORDS']){\r
37                                                 if(typeof(this.load_syntax[lang]['KEYWORDS'][i])=="function") continue;\r
38                                                 this.syntax[lang]["keywords_reg_exp"][i]= new RegExp(this.get_regexp( this.load_syntax[lang]['KEYWORDS'][i] ), param);\r
39                                                 this.keywords_reg_exp_nb++;\r
40                                         }\r
41                                 }\r
42                                 \r
43                                 if(this.load_syntax[lang]['OPERATORS']){\r
44                                         var str="";\r
45                                         var nb=0;\r
46                                         for(var i in this.load_syntax[lang]['OPERATORS']){\r
47                                                 if(typeof(this.load_syntax[lang]['OPERATORS'][i])=="function") continue;\r
48                                                 if(nb>0)\r
49                                                         str+="|";                               \r
50                                                 str+=this.get_escaped_regexp(this.load_syntax[lang]['OPERATORS'][i]);\r
51                                                 nb++;\r
52                                         }\r
53                                         if(str.length>0)\r
54                                                 this.syntax[lang]["operators_reg_exp"]= new RegExp("("+str+")","g");\r
55                                 }\r
56                                 \r
57                                 if(this.load_syntax[lang]['DELIMITERS']){\r
58                                         var str="";\r
59                                         var nb=0;\r
60                                         for(var i in this.load_syntax[lang]['DELIMITERS']){\r
61                                                 if(typeof(this.load_syntax[lang]['DELIMITERS'][i])=="function") continue;\r
62                                                 if(nb>0)\r
63                                                         str+="|";\r
64                                                 str+=this.get_escaped_regexp(this.load_syntax[lang]['DELIMITERS'][i]);\r
65                                                 nb++;\r
66                                         }\r
67                                         if(str.length>0)\r
68                                                 this.syntax[lang]["delimiters_reg_exp"]= new RegExp("("+str+")","g");\r
69                                 }\r
70                                 \r
71                                 \r
72                 //              /(("(\\"|[^"])*"?)|('(\\'|[^'])*'?)|(//(.|\r|\t)*\n)|(/\*(.|\n|\r|\t)*\*/)|(<!--(.|\n|\r|\t)*-->))/gi\r
73                                 var syntax_trace=[];\r
74                                 \r
75                 //              /("(?:[^"\\]*(\\\\)*(\\"?)?)*("|$))/g\r
76                                 \r
77                                 this.syntax[lang]["quotes"]={};\r
78                                 var quote_tab= [];\r
79                                 if(this.load_syntax[lang]['QUOTEMARKS']){\r
80                                         for(var i in this.load_syntax[lang]['QUOTEMARKS']){     \r
81                                                 if(typeof(this.load_syntax[lang]['QUOTEMARKS'][i])=="function") continue;                       \r
82                                                 var x=this.get_escaped_regexp(this.load_syntax[lang]['QUOTEMARKS'][i]);\r
83                                                 this.syntax[lang]["quotes"][x]=x;\r
84                                                 //quote_tab[quote_tab.length]="("+x+"(?:\\\\"+x+"|[^"+x+"])*("+x+"|$))";\r
85                                                 //previous working : quote_tab[quote_tab.length]="("+x+"(?:[^"+x+"\\\\]*(\\\\\\\\)*(\\\\"+x+"?)?)*("+x+"|$))";\r
86                                                 quote_tab[quote_tab.length]="("+ x +"(\\\\.|[^"+ x +"])*(?:"+ x +"|$))";\r
87                                                 \r
88                                                 syntax_trace.push(x);                   \r
89                                         }                       \r
90                                 }\r
91                                                 \r
92                                 this.syntax[lang]["comments"]={};\r
93                                 if(this.load_syntax[lang]['COMMENT_SINGLE']){\r
94                                         for(var i in this.load_syntax[lang]['COMMENT_SINGLE']){ \r
95                                                 if(typeof(this.load_syntax[lang]['COMMENT_SINGLE'][i])=="function") continue;                                           \r
96                                                 var x=this.get_escaped_regexp(this.load_syntax[lang]['COMMENT_SINGLE'][i]);\r
97                                                 quote_tab[quote_tab.length]="("+x+"(.|\\r|\\t)*(\\n|$))";\r
98                                                 syntax_trace.push(x);\r
99                                                 this.syntax[lang]["comments"][x]="\n";\r
100                                         }                       \r
101                                 }               \r
102                                 // (/\*(.|[\r\n])*?\*/)\r
103                                 if(this.load_syntax[lang]['COMMENT_MULTI']){\r
104                                         for(var i in this.load_syntax[lang]['COMMENT_MULTI']){\r
105                                                 if(typeof(this.load_syntax[lang]['COMMENT_MULTI'][i])=="function") continue;                                                    \r
106                                                 var start=this.get_escaped_regexp(i);\r
107                                                 var end=this.get_escaped_regexp(this.load_syntax[lang]['COMMENT_MULTI'][i]);\r
108                                                 quote_tab[quote_tab.length]="("+start+"(.|\\n|\\r)*?("+end+"|$))";\r
109                                                 syntax_trace.push(start);\r
110                                                 syntax_trace.push(end);\r
111                                                 this.syntax[lang]["comments"][i]=this.load_syntax[lang]['COMMENT_MULTI'][i];\r
112                                         }                       \r
113                                 }               \r
114                                 if(quote_tab.length>0)\r
115                                         this.syntax[lang]["comment_or_quote_reg_exp"]= new RegExp("("+quote_tab.join("|")+")","gi");\r
116                                 \r
117                                 if(syntax_trace.length>0) //   /((.|\n)*?)(\\*("|'|\/\*|\*\/|\/\/|$))/g\r
118                                         this.syntax[lang]["syntax_trace_regexp"]= new RegExp("((.|\n)*?)(\\\\*("+ syntax_trace.join("|") +"|$))", "gmi");\r
119                                 \r
120                                 if(this.load_syntax[lang]['SCRIPT_DELIMITERS']){\r
121                                         this.syntax[lang]["script_delimiters"]= {};\r
122                                         for(var i in this.load_syntax[lang]['SCRIPT_DELIMITERS']){\r
123                                                 if(typeof(this.load_syntax[lang]['SCRIPT_DELIMITERS'][i])=="function") continue;                                                        \r
124                                                 this.syntax[lang]["script_delimiters"][i]= this.load_syntax[lang]['SCRIPT_DELIMITERS'];\r
125                                         }                       \r
126                                 }\r
127                                 \r
128                                 this.syntax[lang]["custom_regexp"]= {};\r
129                                 if(this.load_syntax[lang]['REGEXPS']){\r
130                                         for(var i in this.load_syntax[lang]['REGEXPS']){\r
131                                                 if(typeof(this.load_syntax[lang]['REGEXPS'][i])=="function") continue;\r
132                                                 var val= this.load_syntax[lang]['REGEXPS'][i];\r
133                                                 if(!this.syntax[lang]["custom_regexp"][val['execute']])\r
134                                                         this.syntax[lang]["custom_regexp"][val['execute']]= {};\r
135                                                 this.syntax[lang]["custom_regexp"][val['execute']][i]={'regexp' : new RegExp(val['search'], val['modifiers'])\r
136                                                                                                                                                         , 'class' : val['class']};\r
137                                         }\r
138                                 }\r
139                                 \r
140                                 if(this.load_syntax[lang]['STYLES']){                                                   \r
141                                         lang_style[lang]= {};\r
142                                         for(var i in this.load_syntax[lang]['STYLES']){\r
143                                                 if(typeof(this.load_syntax[lang]['STYLES'][i])=="function") continue;\r
144                                                 if(typeof(this.load_syntax[lang]['STYLES'][i]) != "string"){\r
145                                                         for(var j in this.load_syntax[lang]['STYLES'][i]){                                                      \r
146                                                                 lang_style[lang][j]= this.load_syntax[lang]['STYLES'][i][j];\r
147                                                         }\r
148                                                 }else{\r
149                                                         lang_style[lang][i]= this.load_syntax[lang]['STYLES'][i];\r
150                                                 }\r
151                                         }\r
152                                 }\r
153                                 // build style string\r
154                                 var style="";           \r
155                                 for(var i in lang_style[lang]){\r
156                                         if(lang_style[lang][i].length>0){\r
157                                                 style+= "."+ lang +" ."+ i.toLowerCase() +" span{"+lang_style[lang][i]+"}\n";\r
158                                                 style+= "."+ lang +" ."+ i.toLowerCase() +"{"+lang_style[lang][i]+"}\n";                                \r
159                                         }\r
160                                 }\r
161                                 this.syntax[lang]["styles"]=style;\r
162                         }\r
163                 }                               \r
164         };\r
165         \r
166         editAreaLoader.waiting_loading["reg_syntax.js"]= "loaded";\r