From 2850dbd0016cd986f052a674372989c423db231a Mon Sep 17 00:00:00 2001 From: sharevb Date: Tue, 2 Apr 2024 09:00:01 +0200 Subject: [PATCH 1/3] 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 --- src/utils/ascii-lang-utils.ts | 54 +++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/utils/ascii-lang-utils.ts diff --git a/src/utils/ascii-lang-utils.ts b/src/utils/ascii-lang-utils.ts new file mode 100644 index 00000000..83af3423 --- /dev/null +++ b/src/utils/ascii-lang-utils.ts @@ -0,0 +1,54 @@ +function escapeXml(unsafe: string) { + return unsafe.replace(/[<>&'"]/g, (c: string | undefined) => { + switch (c) { + case '<': return '<'; + case '>': return '>'; + case '&': return '&'; + case '\'': return '''; + case '"': return '"'; + } + }); +} + +const dontEscape = ''; +const escapeBackslash = '\\\\'; +const escapeSingleQuote = '\''; +const escapeDoubleQuote = '"'; +const defaultEscape = escapeBackslash + escapeSingleQuote + escapeDoubleQuote; +export const languages = [ + { id: 'raw', name: 'Raw Text', prefix: '', suffix: '', begin: '', end: '', escape: dontEscape }, + { id: 'bash', name: 'Bash', prefix: 'echo "', suffix: '"', begin: '', end: '', escape: escapeBackslash + escapeDoubleQuote }, + { id: 'pwsh', name: 'PowerShell', prefix: 'Write-Output \'', suffix: '\'', begin: '', end: '', escape: escapeBackslash + escapeSingleQuote }, + { id: 'c', name: 'C', prefix: 'printf("', suffix: '\\n");', begin: '#include \n', end: '', escape: defaultEscape }, + { id: 'cpp', name: 'C++', prefix: 'std::cout << "', suffix: '\\n";', begin: '#include \n', end: '', escape: defaultEscape }, + { id: 'csharp', name: 'C#', prefix: 'Console.WriteLine(@"', suffix: '");', begin: 'using System;\n', end: '', escape: escapeDoubleQuote }, + { id: 'vbnet', name: 'VB.Net', prefix: 'Console.WriteLine("', suffix: '")', begin: '', end: '', escape: (l: string) => l.replace('"', '""') }, + { id: 'node', name: 'Node.js', prefix: 'console.log("', suffix: '");', begin: '', end: '', escape: defaultEscape }, + { id: 'python', name: 'Python', prefix: 'print("', suffix: '")', begin: '', end: '', escape: escapeBackslash + escapeDoubleQuote }, + { id: 'html', name: 'HTML', prefix: '', suffix: '', begin: '
\n', end: '\n
', escape: (l: string) => escapeXml(l) }, + { id: 'rust', name: 'Rust', prefix: 'println!("', suffix: '");', begin: '', end: '', escape: defaultEscape }, + { id: 'go', name: 'Go', prefix: 'fmt.Println("', suffix: '")', begin: 'import "fmt"\n', end: '', escape: defaultEscape }, + { id: 'ruby', name: 'Ruby', prefix: 'puts "', suffix: '"', begin: '', end: '', escape: defaultEscape }, + { id: 'php', name: 'PHP', prefix: 'echo "', suffix: '\\n";', begin: '', escape: defaultEscape }, + { id: 'swift', name: 'Swift', prefix: 'print("', suffix: '")', begin: '', end: '', escape: defaultEscape }, + { id: 'kotlin', name: 'Kotlin', prefix: 'println("', suffix: '")', begin: '', end: '', escape: defaultEscape }, + { id: 'sql', name: 'SQL', prefix: 'SELECT \'', suffix: '\\n\'', begin: '', end: '', escape: (l: string) => l.replace('\'', '\'\'') }, + { id: 'java', name: 'Java', prefix: 'System.out.println("', suffix: '");', begin: '', end: '', escape: defaultEscape }, +]; +export function translateToLanguage(asciiArt: string, languageId: string) { + const langConfig = languages.find(l => l.id === languageId); + if (!langConfig) { + return asciiArt; + } + + const escape = typeof langConfig.escape === 'function' + ? langConfig.escape + : function (line: string) { + return langConfig.escape + ? line.replace(new RegExp(`([${langConfig.escape}])`, 'g'), '\\$1') + : line; + }; + return langConfig.begin + asciiArt.split('\n').map((line) => { + return langConfig.prefix + escape(line) + langConfig.suffix; + }).join('\n') + langConfig.end; +} From 093daa8fd24d2ee296c5ba0967569872e9333555 Mon Sep 17 00:00:00 2001 From: sharevb Date: Tue, 2 Apr 2024 09:02:56 +0200 Subject: [PATCH 2/3] feat(ASCII Art Drawer): add coding languages support Add support for outputing in many coding languages (Python , bash....) FIx other part of #934 --- components.d.ts | 1 + .../ascii-text-drawer/ascii-text-drawer.vue | 21 +++++++++++++++---- src/utils/ascii-lang-utils.ts | 1 + 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/components.d.ts b/components.d.ts index e31119b3..f2c3146f 100644 --- a/components.d.ts +++ b/components.d.ts @@ -159,6 +159,7 @@ declare module '@vue/runtime-core' { RouterLink: typeof import('vue-router')['RouterLink'] RouterView: typeof import('vue-router')['RouterView'] RsaKeyPairGenerator: typeof import('./src/tools/rsa-key-pair-generator/rsa-key-pair-generator.vue')['default'] + SafelinkDecoder: typeof import('./src/tools/safelink-decoder/safelink-decoder.vue')['default'] SlugifyString: typeof import('./src/tools/slugify-string/slugify-string.vue')['default'] SpanCopyable: typeof import('./src/components/SpanCopyable.vue')['default'] SqlPrettify: typeof import('./src/tools/sql-prettify/sql-prettify.vue')['default'] diff --git a/src/tools/ascii-text-drawer/ascii-text-drawer.vue b/src/tools/ascii-text-drawer/ascii-text-drawer.vue index 9a6520a4..7cd163a3 100644 --- a/src/tools/ascii-text-drawer/ascii-text-drawer.vue +++ b/src/tools/ascii-text-drawer/ascii-text-drawer.vue @@ -1,8 +1,10 @@