Skip to content

Commit 1a28903

Browse files
feat: custom Triton placeholders as output type (#35)
1 parent 6b156e1 commit 1a28903

File tree

5 files changed

+91
-6
lines changed

5 files changed

+91
-6
lines changed

src/components/form/InputForm.jsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ const outputTypeOptions = [
1919
name: 'Triton Placeholders ([lang] tags)',
2020
value: 'triton_placeholders',
2121
},
22+
{
23+
name: 'Triton Placeholders (Custom Tags)',
24+
value: 'triton_placeholders_custom',
25+
},
2226
{
2327
name: 'Placeholder API',
2428
value: 'papi',
@@ -31,9 +35,14 @@ const InputForm = () => {
3135
const [variableRegex, setVariableRegex] = useState('');
3236
const [outputType, setOutputType] = useState('triton_placeholders');
3337
const [ignoredKeys, setIgnoredKeys] = useState('');
38+
const [langSyntax, setLangSyntax] = useState('lang');
39+
const [argSyntax, setArgSyntax] = useState('arg');
40+
const [argsSyntax, setArgsSyntax] = useState('args');
3441
const [levelDelimiter, setLevelDelimiter] = useState('.');
3542
const [files, setFiles] = useState([]);
3643

44+
const isCustomPlaceholders = outputType === 'triton_placeholders_custom';
45+
3746
return (
3847
<form className={classes.root} noValidate autoComplete='off'>
3948
<TextField
@@ -61,6 +70,40 @@ const InputForm = () => {
6170
value={outputType}
6271
onChange={handleFieldChange(setOutputType)}
6372
/>
73+
{isCustomPlaceholders && (
74+
<>
75+
<TextField
76+
label='Lang Syntax'
77+
value={langSyntax}
78+
onChange={handleFieldChange(setLangSyntax)}
79+
placeholder='lang'
80+
helperText={`Custom lang tag you want to use. Start tag [${langSyntax}] and end tags [/${langSyntax}] are added automatically.`}
81+
margin='normal'
82+
variant='outlined'
83+
fullWidth
84+
/>
85+
<TextField
86+
label='Args Syntax'
87+
value={argsSyntax}
88+
onChange={handleFieldChange(setArgsSyntax)}
89+
placeholder='args'
90+
helperText={`Custom args tag you want to use. Start tag [${argsSyntax}] and end tags [/${argsSyntax}] are added automatically.`}
91+
margin='normal'
92+
variant='outlined'
93+
fullWidth
94+
/>
95+
<TextField
96+
label='Arg Syntax'
97+
value={argSyntax}
98+
onChange={handleFieldChange(setArgSyntax)}
99+
placeholder='arg'
100+
helperText={`Custom arg tag you want to use. Start tag [${argSyntax}] and end tags [/${argSyntax}] are added automatically.`}
101+
margin='normal'
102+
variant='outlined'
103+
fullWidth
104+
/>
105+
</>
106+
)}
64107
<TextField
65108
label='Level Delimiter'
66109
value={levelDelimiter}
@@ -85,6 +128,9 @@ const InputForm = () => {
85128
prefix={prefix}
86129
variableRegex={variableRegex}
87130
outputType={outputType}
131+
langSyntax={langSyntax}
132+
argsSyntax={argsSyntax}
133+
argSyntax={argsSyntax}
88134
ignoredKeys={ignoredKeys}
89135
levelDelimiter={levelDelimiter}
90136
files={files}

src/components/form/SubmitButton.jsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const SubmitButton = ({
99
prefix,
1010
variableRegex,
1111
outputType,
12+
langSyntax,
13+
argsSyntax,
14+
argSyntax,
1215
ignoredKeys,
1316
levelDelimiter,
1417
files,
@@ -20,6 +23,9 @@ const SubmitButton = ({
2023
prefix,
2124
variableRegex,
2225
outputType,
26+
langSyntax,
27+
argsSyntax,
28+
argSyntax,
2329
ignoredKeys,
2430
levelDelimiter,
2531
files: fileContents,
@@ -44,6 +50,9 @@ SubmitButton.propTypes = {
4450
prefix: PropTypes.string,
4551
variableRegex: PropTypes.string,
4652
outputType: PropTypes.string,
53+
langSyntax: PropTypes.string,
54+
argsSyntax: PropTypes.string,
55+
argSyntax: PropTypes.string,
4756
ignoredKeys: PropTypes.string,
4857
files: PropTypes.arrayOf(PropTypes.any),
4958
};
@@ -52,6 +61,9 @@ SubmitButton.defaultProps = {
5261
prefix: '',
5362
variableRegex: '',
5463
outputType: '',
64+
langSyntax: 'lang',
65+
argsSyntax: 'args',
66+
argSyntax: 'arg',
5567
ignoredKeys: '',
5668
files: [],
5769
};

src/lib/converter.js

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,23 @@ import { getFormatManager, json } from './formats';
22
import { getOutputTypeManager } from './outputType';
33

44
class Converter {
5-
constructor({ prefix, variableRegex, outputType, ignoredKeys, levelDelimiter, files }) {
5+
constructor({
6+
prefix,
7+
variableRegex,
8+
outputType,
9+
langSyntax,
10+
argSyntax,
11+
argsSyntax,
12+
ignoredKeys,
13+
levelDelimiter,
14+
files,
15+
}) {
616
this.prefix = prefix;
717
this.variableRegex = variableRegex;
818
this.outputType = outputType;
19+
this.langSyntax = langSyntax;
20+
this.argSyntax = argSyntax;
21+
this.argsSyntax = argsSyntax;
922
this.ignoredKeys = ignoredKeys;
1023
this.levelDelimiter = levelDelimiter;
1124
this.files = files;
@@ -39,8 +52,15 @@ class Converter {
3952
variables,
4053
});
4154
translations.forEach((translation) => this.addTranslation(language, translation));
42-
if (target[key] === undefined)
43-
target[key] = outputTypeManager.convertOriginalMessage(fullKey, variables);
55+
if (target[key] === undefined) {
56+
target[key] = outputTypeManager.convertOriginalMessage(
57+
fullKey,
58+
variables,
59+
this.langSyntax,
60+
this.argsSyntax,
61+
this.argSyntax
62+
);
63+
}
4464
});
4565
}
4666

@@ -88,6 +108,9 @@ export const handleConversion = ({
88108
prefix = '',
89109
variableRegex,
90110
outputType = 'triton_placeholders',
111+
langSyntax = 'lang',
112+
argsSyntax = 'args',
113+
argSyntax = 'arg',
91114
ignoredKeys = '',
92115
levelDelimiter = '.',
93116
files = {},
@@ -96,6 +119,9 @@ export const handleConversion = ({
96119
prefix,
97120
variableRegex: variableRegex ? new RegExp(variableRegex, 'g') : /.^/g,
98121
outputType,
122+
langSyntax,
123+
argsSyntax,
124+
argSyntax,
99125
ignoredKeys: ignoredKeys
100126
.split('\n')
101127
.filter((value) => !!value)

src/lib/outputType/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as tritonPlaceholders from './tritonPlaceholders';
33

44
export const getOutputTypeManager = (outputType) => {
55
if (outputType === 'triton_placeholders') return tritonPlaceholders;
6+
if (outputType === 'triton_placeholders_custom') return tritonPlaceholders;
67
if (outputType === 'papi') return placeholderApi;
78
throw new Error(`Output type manager not available for ${outputType}`);
89
};

src/lib/outputType/tritonPlaceholders.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export const convertOriginalMessage = (key, variables) => {
2-
if (!variables) return `[lang]${key}[/lang]`;
3-
return `[lang]${key}[args]${variables.map((v) => `[arg]${v}[/arg]`).join('')}[/args][/lang]`;
1+
export const convertOriginalMessage = (key, variables, lang, args, arg) => {
2+
if (!variables) return `[${lang}]${key}[/${lang}]`;
3+
return `[${lang}]${key}[${args}]${variables.map((v) => `[${arg}]${v}[/${arg}]`).join('')}[/${args}][/${lang}]`;
44
};
55

66
export const getTranslations = ({ text, key, variables }) => {

0 commit comments

Comments
 (0)