Skip to content

Commit 756162f

Browse files
committed
feat(ASCII Art to Code): utility to format ASCII Art to code
Handle ASCII Art to code conversion for Python, Bash, Powershell, C, C++, VB.Net, Node.js, HTML, Rust, Go, Ruby, PHP, Swift, Kotlin, SQL and Java
1 parent d37be27 commit 756162f

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

src/utils/ascii-lang-utils.ts

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

0 commit comments

Comments
 (0)