-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathToScript.cfc
200 lines (186 loc) · 5.13 KB
/
ToScript.cfc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
component {
variables.indentLevel = 0;
variables.options = {indentChars=Chr(9)};
public struct function toScript(filePath="", options={}, fileContent="") {
var codeFile = new cfmlparser.File(filePath=filePath,fileContent=fileContent);
var statements = codeFile.getStatements();
var sb = createObject("java", "java.lang.StringBuilder").init(codeFile.getFileLength());
var outputBuffer = createObject("java", "java.lang.StringBuilder");
var content = codeFile.getFileContent();
var s = "";
var converter = "";
var i = 0;
var code = "";
var handledChar = false;
var result = {"code":"","errors":[], "converted":false};
structAppend(variables.options, arguments.options, true);
if (codeFile.isScript()) {
//already CFML script
result.code = codeFile.getFileContent();
result.converted = false;
return result;
}
for (i=1;i<=codeFile.getFileLength();i++) {
handledChar = false;
for (s in statements) {
if (s.getStartPosition() == i) {
handledChar = true;
handleOutput(outputBuffer, sb);
//start of a tag or comment
if (s.isComment()) {
indent(sb);
if (find(Chr(10), s.getComment())) {
sb.append("/* ");
sb.append(s.getComment());
sb.append("*/");
} else {
sb.append("// ");
sb.append(s.getComment());
}
lineBreak(sb);
//jump to end of comment
i = s.getEndPosition();
break;
} else if (s.isTag()) {
try {
if (s.getName() == "cfscript") {
//cfscript does not need a converter so just insert it
lineBreak(sb);
indent(sb);
sb.append(trim(s.getInnerContent()));
lineBreak(sb);
i = s.getEndPosition();
break;
} else {
converter = getTagConverter(s.getName());
code = converter.toScript(s);
if (s.getName() == "cffunction") {
// add extra new line to space out functions
lineBreak(sb);
} else if (listFindNoCase("cfelse,cfelseif,cfcatch,cffinally", s.getName())) {
decreaseIndent();
}
if (len(code)) {
indent(sb);
sb.append(code);
lineBreak(sb);
}
}
} catch(any e) {
arrayAppend(result.errors, {"tag":"|"&s.getName()&"|", "error":e});
lineBreak(sb);
indent(sb);
sb.append("/*");
sb.append(" toScript ERROR: ");
sb.append(e.message);
lineBreak(sb);
lineBreak(sb);
indent(sb);
indent(sb);
code = s.getText();
code = replace(code, "*/", "* /");
sb.append(code);
lineBreak(sb);
lineBreak(sb);
indent(sb);
sb.append("*/");
lineBreak(sb);
lineBreak(sb);
//jump to end of tag end
i = s.getEndPosition();
continue;
}
if (converter.indentBody(s)) {
increaseIndent();
}
i = s.getStartTagEndPosition();
}
} else if (s.getStartPosition() > i) {
//past current marker
break;
} else if (s.isTag() && s.getEndTagStartPosition() == i) {
handleOutput(outputBuffer, sb);
handledChar = true;
converter = getTagConverter(s.getName());
code = converter.toScriptEndTag(s);
//end tag
if (code == "}") {
decreaseIndent();
if (s.getName() == "cfcomponent") {
//space out cfcomponent
lineBreak(sb);
}
indent(sb);
sb.append("}");
lineBreak(sb);
} else if (s.getName() == "cfcase" || s.getName() == "cfdefaultcase") {
indent(sb);
sb.append("break;");
lineBreak(sb);
decreaseIndent();
}
i = s.getEndPosition();
}
}
if (!handledChar) {
local.outputChar = mid(content, i, 1);
if (local.outputChar == """") {
outputBuffer.append("""""");
} else {
outputBuffer.append(local.outputChar);
}
}
}
result.converted = true;
result.code = sb.toString();
return result;
}
public function getTagConverter(tagName) {
var converter = "";
try {
converter = createObject("component", "converters." & trim(lCase(tagName))).init(options);
} catch(any e) {
if (e.type == "Template") {
//due to compiler error of the CFC
rethrow;
}
converter = createObject("component", "converters.BaseConverter").init(options);
}
return converter;
}
private function lineBreak(sb) {
sb.append(Chr(13));
sb.append(Chr(10));
}
private function indent(sb) {
if (variables.indentLevel > 0) {
for (var i=0;i<variables.indentLevel;i++) {
sb.append(variables.options.indentChars);
}
}
}
private function increaseIndent() {
variables.indentLevel = variables.indentLevel+1;
}
private function decreaseIndent() {
variables.indentLevel = variables.indentLevel-1;
if (variables.indentLevel < 0) {
variables.indentLevel = 0;
}
}
private function handleOutput(outputBuffer, sb) {
if (outputBuffer.length() == 0) {
return;
}
local.outputString = trim(outputBuffer.toString());
if (len(local.outputString) > 0) {
lineBreak(sb);
indent(sb);
sb.append("writeOutput(""");
sb.append(outputString);
sb.append(""");");
lineBreak(sb);
}
outputBuffer.setLength(0);
}
}