forked from RaspberryPiFoundation/blockly
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtext.js
More file actions
301 lines (279 loc) · 10.2 KB
/
text.js
File metadata and controls
301 lines (279 loc) · 10.2 KB
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/**
* @license
* Visual Blocks Language
*
* Copyright 2012 Google Inc.
* https://developers.google.com/blockly/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @fileoverview Generating Python for text blocks.
* @author q.neutron@gmail.com (Quynh Neutron)
*/
'use strict';
goog.provide('Blockly.Python.texts');
goog.require('Blockly.Python');
Blockly.Python['text'] = function(block) {
// Text value.
var code = Blockly.Python.quote_(block.getFieldValue('TEXT'));
return [code, Blockly.Python.ORDER_ATOMIC];
};
/**
* Enclose the provided value in 'str(...)' function.
* Leave string literals alone.
* @param {string} value Code evaluating to a value.
* @return {string} Code evaluating to a string.
* @private
*/
Blockly.Python.text.forceString_ = function(value) {
if (Blockly.Python.text.forceString_.strRegExp.test(value)) {
return value;
}
return 'str(' + value + ')';
};
/**
* Regular expression to detect a single-quoted string literal.
*/
Blockly.Python.text.forceString_.strRegExp = /^\s*'([^']|\\')*'\s*$/;
Blockly.Python['text_join'] = function(block) {
// Create a string made up of any number of elements of any type.
//Should we allow joining by '-' or ',' or any other characters?
switch (block.itemCount_) {
case 0:
return ['\'\'', Blockly.Python.ORDER_ATOMIC];
break;
case 1:
var element = Blockly.Python.valueToCode(block, 'ADD0',
Blockly.Python.ORDER_NONE) || '\'\'';
var code = Blockly.Python.text.forceString_(element);
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
break;
case 2:
var element0 = Blockly.Python.valueToCode(block, 'ADD0',
Blockly.Python.ORDER_NONE) || '\'\'';
var element1 = Blockly.Python.valueToCode(block, 'ADD1',
Blockly.Python.ORDER_NONE) || '\'\'';
var code = Blockly.Python.text.forceString_(element0) + ' + ' +
Blockly.Python.text.forceString_(element1);
return [code, Blockly.Python.ORDER_ADDITIVE];
break;
default:
var elements = [];
for (var i = 0; i < block.itemCount_; i++) {
elements[i] = Blockly.Python.valueToCode(block, 'ADD' + i,
Blockly.Python.ORDER_NONE) || '\'\'';
}
var tempVar = Blockly.Python.variableDB_.getDistinctName('x',
Blockly.Variables.NAME_TYPE);
var code = '\'\'.join([str(' + tempVar + ') for ' + tempVar + ' in [' +
elements.join(', ') + ']])';
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
}
};
Blockly.Python['text_append'] = function(block) {
// Append to a variable in place.
var varName = Blockly.Python.variableDB_.getName(block.getFieldValue('VAR'),
Blockly.Variables.NAME_TYPE);
var value = Blockly.Python.valueToCode(block, 'TEXT',
Blockly.Python.ORDER_NONE) || '\'\'';
return varName + ' = str(' + varName + ') + ' +
Blockly.Python.text.forceString_(value) + '\n';
};
Blockly.Python['text_length'] = function(block) {
// Is the string null or array empty?
var text = Blockly.Python.valueToCode(block, 'VALUE',
Blockly.Python.ORDER_NONE) || '\'\'';
return ['len(' + text + ')', Blockly.Python.ORDER_FUNCTION_CALL];
};
Blockly.Python['text_isEmpty'] = function(block) {
// Is the string null or array empty?
var text = Blockly.Python.valueToCode(block, 'VALUE',
Blockly.Python.ORDER_NONE) || '\'\'';
var code = 'not len(' + text + ')';
return [code, Blockly.Python.ORDER_LOGICAL_NOT];
};
Blockly.Python['text_indexOf'] = function(block) {
// Search the text for a substring.
// Should we allow for non-case sensitive???
var operator = block.getFieldValue('END') == 'FIRST' ? 'find' : 'rfind';
var substring = Blockly.Python.valueToCode(block, 'FIND',
Blockly.Python.ORDER_NONE) || '\'\'';
var text = Blockly.Python.valueToCode(block, 'VALUE',
Blockly.Python.ORDER_MEMBER) || '\'\'';
var code = text + '.' + operator + '(' + substring + ')';
if (block.workspace.options.oneBasedIndex) {
return [code + ' + 1', Blockly.Python.ORDER_ADDITIVE];
}
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
};
Blockly.Python['text_charAt'] = function(block) {
// Get letter at index.
// Note: Until January 2013 this block did not have the WHERE input.
var where = block.getFieldValue('WHERE') || 'FROM_START';
var text = Blockly.Python.valueToCode(block, 'VALUE',
Blockly.Python.ORDER_MEMBER) || '\'\'';
switch (where) {
case 'FIRST':
var code = text + '[0]';
return [code, Blockly.Python.ORDER_MEMBER];
case 'LAST':
var code = text + '[-1]';
return [code, Blockly.Python.ORDER_MEMBER];
case 'FROM_START':
var at = Blockly.Python.getAdjustedInt(block, 'AT');
var code = text + '[' + at + ']';
return [code, Blockly.Python.ORDER_MEMBER];
case 'FROM_END':
var at = Blockly.Python.getAdjustedInt(block, 'AT', 1, true);
var code = text + '[' + at + ']';
return [code, Blockly.Python.ORDER_MEMBER];
case 'RANDOM':
Blockly.Python.definitions_['import_random'] = 'import random';
var functionName = Blockly.Python.provideFunction_(
'text_random_letter',
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(text):',
' x = int(random.random() * len(text))',
' return text[x];']);
code = functionName + '(' + text + ')';
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
}
throw Error('Unhandled option (text_charAt).');
};
Blockly.Python['text_getSubstring'] = function(block) {
// Get substring.
var where1 = block.getFieldValue('WHERE1');
var where2 = block.getFieldValue('WHERE2');
var text = Blockly.Python.valueToCode(block, 'STRING',
Blockly.Python.ORDER_MEMBER) || '\'\'';
switch (where1) {
case 'FROM_START':
var at1 = Blockly.Python.getAdjustedInt(block, 'AT1');
if (at1 == '0') {
at1 = '';
}
break;
case 'FROM_END':
var at1 = Blockly.Python.getAdjustedInt(block, 'AT1', 1, true);
break;
case 'FIRST':
var at1 = '';
break;
default:
throw Error('Unhandled option (text_getSubstring)');
}
switch (where2) {
case 'FROM_START':
var at2 = Blockly.Python.getAdjustedInt(block, 'AT2', 1);
break;
case 'FROM_END':
var at2 = Blockly.Python.getAdjustedInt(block, 'AT2', 0, true);
// Ensure that if the result calculated is 0 that sub-sequence will
// include all elements as expected.
if (!Blockly.isNumber(String(at2))) {
Blockly.Python.definitions_['import_sys'] = 'import sys';
at2 += ' or sys.maxsize';
} else if (at2 == '0') {
at2 = '';
}
break;
case 'LAST':
var at2 = '';
break;
default:
throw Error('Unhandled option (text_getSubstring)');
}
var code = text + '[' + at1 + ' : ' + at2 + ']';
return [code, Blockly.Python.ORDER_MEMBER];
};
Blockly.Python['text_changeCase'] = function(block) {
// Change capitalization.
var OPERATORS = {
'UPPERCASE': '.upper()',
'LOWERCASE': '.lower()',
'TITLECASE': '.title()'
};
var operator = OPERATORS[block.getFieldValue('CASE')];
var text = Blockly.Python.valueToCode(block, 'TEXT',
Blockly.Python.ORDER_MEMBER) || '\'\'';
var code = text + operator;
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
};
Blockly.Python['text_trim'] = function(block) {
// Trim spaces.
var OPERATORS = {
'LEFT': '.lstrip()',
'RIGHT': '.rstrip()',
'BOTH': '.strip()'
};
var operator = OPERATORS[block.getFieldValue('MODE')];
var text = Blockly.Python.valueToCode(block, 'TEXT',
Blockly.Python.ORDER_MEMBER) || '\'\'';
var code = text + operator;
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
};
Blockly.Python['text_print'] = function(block) {
// Print statement.
var msg = Blockly.Python.valueToCode(block, 'TEXT',
Blockly.Python.ORDER_NONE) || '\'\'';
return 'print(' + msg + ')\n';
};
Blockly.Python['text_prompt_ext'] = function(block) {
// Prompt function.
var functionName = Blockly.Python.provideFunction_(
'text_prompt',
['def ' + Blockly.Python.FUNCTION_NAME_PLACEHOLDER_ + '(msg):',
' try:',
' return raw_input(msg)',
' except NameError:',
' return input(msg)']);
if (block.getField('TEXT')) {
// Internal message.
var msg = Blockly.Python.quote_(block.getFieldValue('TEXT'));
} else {
// External message.
var msg = Blockly.Python.valueToCode(block, 'TEXT',
Blockly.Python.ORDER_NONE) || '\'\'';
}
var code = functionName + '(' + msg + ')';
var toNumber = block.getFieldValue('TYPE') == 'NUMBER';
if (toNumber) {
code = 'float(' + code + ')';
}
return [code, Blockly.Python.ORDER_FUNCTION_CALL];
};
Blockly.Python['text_prompt'] = Blockly.Python['text_prompt_ext'];
Blockly.Python['text_count'] = function(block) {
var text = Blockly.Python.valueToCode(block, 'TEXT',
Blockly.Python.ORDER_MEMBER) || '\'\'';
var sub = Blockly.Python.valueToCode(block, 'SUB',
Blockly.Python.ORDER_NONE) || '\'\'';
var code = text + '.count(' + sub + ')';
return [code, Blockly.Python.ORDER_MEMBER];
};
Blockly.Python['text_replace'] = function(block) {
var text = Blockly.Python.valueToCode(block, 'TEXT',
Blockly.Python.ORDER_MEMBER) || '\'\'';
var from = Blockly.Python.valueToCode(block, 'FROM',
Blockly.Python.ORDER_NONE) || '\'\'';
var to = Blockly.Python.valueToCode(block, 'TO',
Blockly.Python.ORDER_NONE) || '\'\'';
var code = text + '.replace(' + from + ', ' + to + ')';
return [code, Blockly.Python.ORDER_MEMBER];
};
Blockly.Python['text_reverse'] = function(block) {
var text = Blockly.Python.valueToCode(block, 'TEXT',
Blockly.Python.ORDER_MEMBER) || '\'\'';
var code = text + '[::-1]';
return [code, Blockly.Python.ORDER_MEMBER];
};