feat(new tool): String Escaper

Fix #523
This commit is contained in:
ShareVB committed 2025-05-10 10:19:32 +02:00
1 parent 91f565f839
commit 58e2fb39b7
5 files changed
+106 -27

No files matched your search

@@ -1,7 +1,7 @@
<script setup lang="ts">
import figlet from 'figlet';
import TextareaCopyable from '@/components/TextareaCopyable.vue';
import { languages, translateToLanguage } from '@/utils/ascii-lang-utils';
import { languages, printToLanguage } from '@/utils/ascii-lang-utils';
const input = ref('Ascii ART');
const language = useStorage('ascii-text-drawer:language', 'raw');
@@ -40,7 +40,7 @@ watchEffect(async () => {
resolve(text ?? '');
})));
output.value = translateToLanguage(rawOutput, languageValue);
output.value = printToLanguage(rawOutput, languageValue);
errored.value = false;
}
catch (e: any) {
@@ -1,7 +1,7 @@
<script setup lang="ts">
import { ImageToAsciiArt } from 'image-to-ascii-art';
import TextareaCopyable from '@/components/TextareaCopyable.vue';
import { languages, translateToLanguage } from '@/utils/ascii-lang-utils';
import { languages, printToLanguage } from '@/utils/ascii-lang-utils';
const inputBase64 = ref('');
const language = useStorage('image-to-ascii-art:language', 'raw');
@@ -39,7 +39,7 @@ const output = computedAsync(async () => {
drawHeight: scaleValue * 0.4,
},
});
outputValue = translateToLanguage(await imageToAsciiArt.convert(inputBase64Value), languageValue);
outputValue = printToLanguage(await imageToAsciiArt.convert(inputBase64Value), languageValue);
imageToAsciiArt.destroy();
}
catch (e) {
+13
View File
@@ -0,0 +1,13 @@
import { Code } from '@vicons/tabler';
import { defineTool } from '../tool';
export const tool = defineTool({
name: 'String Escaper',
path: '/string-escaper',
description: 'Escape string to code language version',
keywords: ['string', 'code', 'escaper'],
component: () => import('./string-escaper.vue'),
icon: Code,
createdAt: new Date('2024-08-15'),
category: 'Development',
});
@@ -0,0 +1,39 @@
<script setup lang="ts">
import TextareaCopyable from '@/components/TextareaCopyable.vue';
import { escapeForLanguage, languages } from '@/utils/ascii-lang-utils';
import { useQueryParamOrStorage } from '@/composable/queryParams';
const rawInput = ref('Hello "every"\n A\'');
const language = useQueryParamOrStorage({ name: 'lang', storageName: 'string-escaper:l', defaultValue: 'raw' });
const single_linize = useQueryParamOrStorage({ name: 'singleline', storageName: 'string-escaper:sl', defaultValue: false });
const languagesOptions = languages.map(lang => ({ value: lang.id, label: lang.name }));
const stringOutput = computed(() => escapeForLanguage(
rawInput.value, language.value,
{ single_linize: single_linize.value }));
</script>
<template>
<c-card title="String Escaper">
<c-input-text
v-model:value="rawInput"
label="Your text:"
placeholder="Your raw text to escape"
raw-text
multiline
rows="4"
mb-2
/>
<c-select v-model:value="language" label="Target language:" :options="languagesOptions" searchable mb-2 />
<n-space justify="center">
<n-checkbox v-model:checked="single_linize">
Single linize
</n-checkbox>
</n-space>
</c-card>
<c-card title="Your escaped string in target language">
<TextareaCopyable
:value="stringOutput"
/>
</c-card>
</template>
+50 -23
View File
@@ -17,29 +17,49 @@ 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 <stdio.h>\n', end: '', escape: defaultEscape },
{ id: 'cpp', name: 'C++', prefix: 'std::cout << "', suffix: '\\n";', begin: '#include <iostream>\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(/"/g, '""') },
{ 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: '<pre>\n', end: '\n</pre>', 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: '<?php\n', end: '\n?>', 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(/'/g, '\'\'') },
{ id: 'java', name: 'Java', prefix: 'System.out.println("', suffix: '");', begin: '', end: '', escape: defaultEscape },
{ id: 'raw', name: 'Raw Text', cr: '\n', start_quote: '', prefix: '', suffix: '', begin: '', end: '', escape: dontEscape },
{ id: 'bash', name: 'Bash', crlf: '\\n', start_quote: '"', prefix: 'echo ', suffix: '', begin: '', end: '', escape: escapeBackslash + escapeDoubleQuote },
{ id: 'pwsh', name: 'PowerShell', crlf: '\\n', start_quote: '"', prefix: 'Write-Output ', suffix: '', begin: '', end: '', escape: escapeBackslash + escapeSingleQuote },
{ id: 'c', name: 'C', crlf: '\\n', start_quote: '"', prefix: 'printf(', suffix: ');', suffix_eol: '\\n', begin: '#include <stdio.h>\n', end: '', escape: defaultEscape },
{ id: 'cpp', name: 'C++', crlf: '\\n', start_quote: '"', prefix: 'std::cout << ', suffix: ';', suffix_eol: '\\n', begin: '#include <iostream>\n', end: '', escape: defaultEscape },
{ id: 'csharp', name: 'C#', crlf: '\\n', start_quote: '"', prefix: 'Console.WriteLine(', suffix: ');', begin: 'using System;\n', end: '', escape: (l: string) => l.replace(/[\\'"]/g, (m: string) => `\\${m}`).replace(/\t/g, '\\t') },
{ id: 'csharp_verb', name: 'C# (Verbatim)', crlf: '" + "\\n" + @"', start_quote: '@"', end_quote: '"', prefix: 'Console.WriteLine(', suffix: ');', begin: 'using System;\n', end: '', escape: (l: string) => l.replace(/["]/g, '""').replace(/\t/g, '\\t') },
{ id: 'csharp_interf', name: 'C# (Interpolated)', crlf: '\\n', start_quote: '$"', end_quote: '"', prefix: 'Console.WriteLine(', suffix: ');', begin: 'using System;\n', end: '', escape: (l: string) => l.replace(/["{}]/g, (m: string) => m + m).replace(/\t/g, '\\t') },
{ id: 'csharp_raw', name: 'C# (Raw)', crlf: '\n', start_quote: '"""', prefix: 'Console.WriteLine(', suffix: ');', begin: 'using System;\n', end: '', escape: dontEscape },
{ id: 'vbnet', name: 'VB.Net', crlf: '" & vbCrLf & "', start_quote: '"', prefix: 'Console.WriteLine(', suffix: ')', begin: '', end: '', escape: (l: string) => l.replace(/"/g, '""') },
{ id: 'node', name: 'Node.js', crlf: '\\n', start_quote: '"', prefix: 'console.log(', suffix: ');', begin: '', end: '', escape: defaultEscape },
{ id: 'python', name: 'Python', crlf: '\\n', start_quote: '"', prefix: 'print(', suffix: ')', begin: '', end: '', escape: escapeBackslash + escapeDoubleQuote },
{ id: 'html', name: 'HTML', crlf: '<br />', start_quote: '', prefix: '', suffix: '', begin: '<pre>\n', end: '\n</pre>', escape: (l: string) => escapeXml(l) },
{ id: 'rust', name: 'Rust', crlf: '\\n', start_quote: '"', prefix: 'println!(', suffix: ');', begin: '', end: '', escape: defaultEscape },
{ id: 'go', name: 'Go', crlf: '\\n', start_quote: '"', prefix: 'fmt.Println(', suffix: ')', begin: 'import "fmt"\n', end: '', escape: defaultEscape },
{ id: 'ruby', name: 'Ruby', crlf: '\\n', start_quote: '"', prefix: 'puts ', suffix: '', begin: '', end: '', escape: defaultEscape },
{ id: 'php', name: 'PHP', crlf: '\\n', start_quote: '"', prefix: 'echo ', suffix: ';', suffix_eol: '\\n', begin: '<?php\n', end: '\n?>', escape: defaultEscape },
{ id: 'swift', name: 'Swift', crlf: '\\n', start_quote: '"', prefix: 'print(', suffix: ')', begin: '', end: '', escape: defaultEscape },
{ id: 'kotlin', name: 'Kotlin', crlf: '\\n', start_quote: '"', prefix: 'println(', suffix: ')', begin: '', end: '', escape: defaultEscape },
{ id: 'sql', name: 'SQL', crlf: '\' + CHAR(10 + \'', start_quote: '\'', prefix: 'SELECT ', suffix: '', suffix_eol: '\\n', begin: '', end: '', escape: (l: string) => l.replace(/'/g, '\'\'') },
{ id: 'java', name: 'Java', crlf: '\\n', start_quote: '"', prefix: 'System.out.println(', suffix: ');', begin: '', end: '', escape: defaultEscape },
];
export function translateToLanguage(asciiArt: string, languageId: string) {
export function printToLanguage(s: string, languageId: string) {
const langConfig = languages.find(l => l.id === languageId);
if (!langConfig) {
return asciiArt;
return s;
}
return langConfig.begin + s.split('\n').map((line) => {
return langConfig.prefix + escapeForLanguage(line, languageId,
{ suffix_eol: langConfig.suffix_eol || '' },
) + langConfig.suffix;
}).join('\n') + langConfig.end;
}
export function escapeForLanguage(s: string, languageId: string,
options: {
suffix_eol?: string
single_linize?: boolean
}) {
const langConfig = languages.find(l => l.id === languageId);
if (!langConfig) {
return s;
}
const escape = typeof langConfig.escape === 'function'
@@ -49,7 +69,14 @@ export function translateToLanguage(asciiArt: string, languageId: string) {
? 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;
const escapeMultiLine = function (s: string) {
const result = escape(s);
if (!options.single_linize) {
return result;
}
return result.replace(/\n/g, langConfig.crlf || '\\n');
};
return (options.single_linize ? [s] : s.split('\n')).map((line) => {
return langConfig.start_quote + escapeMultiLine(line) + (options.suffix_eol || '') + (langConfig.end_quote || langConfig.start_quote);
}).join('\n');
}