123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272 |
- /**
- * @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];
- };
- 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 = 'str(' + 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 = 'str(' + element0 + ') + str(' + 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 + ') + str(' + value + ')\n';
- };
- Blockly.Python['text_length'] = function(block) {
- // Is the string null or array empty?
- var value = Blockly.Python.valueToCode(block, 'VALUE',
- Blockly.Python.ORDER_NONE) || '___';
- return ['len(' + value + ')', Blockly.Python.ORDER_FUNCTION_CALL];
- };
- Blockly.Python['text_isEmpty'] = function(block) {
- // Is the string null or array empty?
- var value = Blockly.Python.valueToCode(block, 'VALUE',
- Blockly.Python.ORDER_NONE) || '___';
- var code = 'not len(' + value + ')';
- 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 + ') + 1';
- return [code, Blockly.Python.ORDER_ADDITIVE];
- };
- 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 'Unhandled option (text_charAt).';
- };
- Blockly.Python['text_getSubstring'] = function(block) {
- // Get substring.
- var text = Blockly.Python.valueToCode(block, 'STRING',
- Blockly.Python.ORDER_MEMBER) || '___';
- var where1 = block.getFieldValue('WHERE1');
- var where2 = block.getFieldValue('WHERE2');
- 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 '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 '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_MEMBER];
- };
- 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_MEMBER];
- };
- 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_print_multiple'] = function(block) {
- // Create a list with any number of elements of any type.
- var inner_prints = new Array(block.itemCount_);
- for (var n = 0; n < block.itemCount_; n++) {
- inner_prints[n] = Blockly.Python.valueToCode(block, 'PRINT' + n,
- Blockly.Python.ORDER_NONE) || '___';
- }
- var code;
- if (block.itemCount_ == 1) {
- code = 'print(' + inner_prints.join(', ') + ')\n';
- } else {
- code = 'print(' + inner_prints.join(', ') + ')\n';
- }
- return code;
- };
- Blockly.Python['text_prompt'] = Blockly.Python['text_prompt_ext'];
- Blockly.Python['text_lower'] = function(block) {
- var text_input = Blockly.Python.valueToCode(block,"TEXT_INPUT",Blockly.Python.ORDER_ATOMIC) || '';
- var code = text_input + ".lower()"
- return [code, Blockly.Python.ORDER_ATOMIC];
- }
|