| 820 | } |
| 821 | |
| 822 | function guardUnicode ({ code, codeLanguage }) { |
| 823 | if (!code) { return code } |
| 824 | const commentStart = { javascript: '//', python: '#', lua: '--' }[codeLanguage] || '//' |
| 825 | const singleLineCommentWithNonASCIIRegex = new RegExp(`[ \t]*${commentStart}.*?[^ -~]`) |
| 826 | const singleLineCommentPartsRegex = new RegExp(`([ \t]*${commentStart})(.+)`) |
| 827 | // For some reason, Blockly garbles text encoding if you do a comment line like this: |
| 828 | // # 然后,使用while True循环攻击"Cupboard"(橱柜)。 |
| 829 | // or this |
| 830 | // // Puis, attaquez le "Cupboard" à l'aide d'une boucle while-true. |
| 831 | // It seems to be the quotes. So if we have non-ASCII text in a comment string, let's get rid of quotes. |
| 832 | const lines = code.split('\n') |
| 833 | for (let i = 0; i < lines.length; ++i) { |
| 834 | const line = lines[i] |
| 835 | const hasNonASCII = singleLineCommentWithNonASCIIRegex.test(line) |
| 836 | if (hasNonASCII) { |
| 837 | lines[i] = line.replace(singleLineCommentPartsRegex, (match, commentStart, commentContent) => { |
| 838 | return commentStart + commentContent.replace(/"/g, "'") |
| 839 | }) |
| 840 | } |
| 841 | } |
| 842 | return lines.join('\n') |
| 843 | } |
| 844 | |
| 845 | function addStartBlock (state) { |
| 846 | const startBlock = { type: 'start' } |