|
| 1 | +function escapeXml(unsafe: string) { |
| 2 | + return unsafe.replace(/[<>&'"]/g, (c: string | undefined) => { |
| 3 | + switch (c) { |
| 4 | + case '<': return '<'; |
| 5 | + case '>': return '>'; |
| 6 | + case '&': return '&'; |
| 7 | + case '\'': return '''; |
| 8 | + case '"': return '"'; |
| 9 | + } |
| 10 | + return ''; |
| 11 | + }); |
| 12 | +} |
| 13 | + |
| 14 | +const dontEscape = ''; |
| 15 | +const escapeBackslash = '\\\\'; |
| 16 | +const escapeSingleQuote = '\''; |
| 17 | +const escapeDoubleQuote = '"'; |
| 18 | +const defaultEscape = escapeBackslash + escapeSingleQuote + escapeDoubleQuote; |
| 19 | +export const languages = [ |
| 20 | + { id: 'raw', name: 'Raw Text', prefix: '', suffix: '', begin: '', end: '', escape: dontEscape }, |
| 21 | + { id: 'bash', name: 'Bash', prefix: 'echo "', suffix: '"', begin: '', end: '', escape: escapeBackslash + escapeDoubleQuote }, |
| 22 | + { id: 'pwsh', name: 'PowerShell', prefix: 'Write-Output \'', suffix: '\'', begin: '', end: '', escape: escapeBackslash + escapeSingleQuote }, |
| 23 | + { id: 'c', name: 'C', prefix: 'printf("', suffix: '\\n");', begin: '#include <stdio.h>\n', end: '', escape: defaultEscape }, |
| 24 | + { id: 'cpp', name: 'C++', prefix: 'std::cout << "', suffix: '\\n";', begin: '#include <iostream>\n', end: '', escape: defaultEscape }, |
| 25 | + { id: 'csharp', name: 'C#', prefix: 'Console.WriteLine(@"', suffix: '");', begin: 'using System;\n', end: '', escape: escapeDoubleQuote }, |
| 26 | + { id: 'vbnet', name: 'VB.Net', prefix: 'Console.WriteLine("', suffix: '")', begin: '', end: '', escape: (l: string) => l.replace('"', '""') }, |
| 27 | + { id: 'node', name: 'Node.js', prefix: 'console.log("', suffix: '");', begin: '', end: '', escape: defaultEscape }, |
| 28 | + { id: 'python', name: 'Python', prefix: 'print("', suffix: '")', begin: '', end: '', escape: escapeBackslash + escapeDoubleQuote }, |
| 29 | + { id: 'html', name: 'HTML', prefix: '', suffix: '', begin: '<pre>\n', end: '\n</pre>', escape: (l: string) => escapeXml(l) }, |
| 30 | + { id: 'rust', name: 'Rust', prefix: 'println!("', suffix: '");', begin: '', end: '', escape: defaultEscape }, |
| 31 | + { id: 'go', name: 'Go', prefix: 'fmt.Println("', suffix: '")', begin: 'import "fmt"\n', end: '', escape: defaultEscape }, |
| 32 | + { id: 'ruby', name: 'Ruby', prefix: 'puts "', suffix: '"', begin: '', end: '', escape: defaultEscape }, |
| 33 | + { id: 'php', name: 'PHP', prefix: 'echo "', suffix: '\\n";', begin: '<?php\n', end: '\n?>', escape: defaultEscape }, |
| 34 | + { id: 'swift', name: 'Swift', prefix: 'print("', suffix: '")', begin: '', end: '', escape: defaultEscape }, |
| 35 | + { id: 'kotlin', name: 'Kotlin', prefix: 'println("', suffix: '")', begin: '', end: '', escape: defaultEscape }, |
| 36 | + { id: 'sql', name: 'SQL', prefix: 'SELECT \'', suffix: '\\n\'', begin: '', end: '', escape: (l: string) => l.replace('\'', '\'\'') }, |
| 37 | + { id: 'java', name: 'Java', prefix: 'System.out.println("', suffix: '");', begin: '', end: '', escape: defaultEscape }, |
| 38 | +]; |
| 39 | +export function translateToLanguage(asciiArt: string, languageId: string) { |
| 40 | + const langConfig = languages.find(l => l.id === languageId); |
| 41 | + if (!langConfig) { |
| 42 | + return asciiArt; |
| 43 | + } |
| 44 | + |
| 45 | + const escape = typeof langConfig.escape === 'function' |
| 46 | + ? langConfig.escape |
| 47 | + : function (line: string) { |
| 48 | + return langConfig.escape |
| 49 | + ? line.replace(new RegExp(`([${langConfig.escape}])`, 'g'), '\\$1') |
| 50 | + : line; |
| 51 | + }; |
| 52 | + return langConfig.begin + asciiArt.split('\n').map((line) => { |
| 53 | + return langConfig.prefix + escape(line) + langConfig.suffix; |
| 54 | + }).join('\n') + langConfig.end; |
| 55 | +} |
0 commit comments