diff options
| author | HO-COOH <42881734+HO-COOH@users.noreply.github.com> | 2021-05-15 00:14:36 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-05-14 18:14:36 +0200 |
| commit | af70cf95d9d010f27da1c1c8987c807ee3255a10 (patch) | |
| tree | 9d6de559ea072c684f690d32816f7fe3ae1a88d3 /src/templatedString.ts | |
| parent | be07386c572fe8258a3b4c101b85dd900867bb13 (diff) | |
| download | doxdocgen-af70cf95d9d010f27da1c1c8987c807ee3255a10.tar.gz | |
Refactor, add {file} template (#221)
* Refactor, add {file} template
* Add test for {file} expansion
Diffstat (limited to 'src/templatedString.ts')
| -rw-r--r-- | src/templatedString.ts | 84 |
1 files changed, 84 insertions, 0 deletions
diff --git a/src/templatedString.ts b/src/templatedString.ts new file mode 100644 index 0000000..c397ad9 --- /dev/null +++ b/src/templatedString.ts @@ -0,0 +1,84 @@ +import { getEnvVars } from "./util"; + +/** + * Represent a templated variable in string + */ +export interface ITemplate { + toReplace: string; // The template to be replaced in string + with: string; // The value to replace with +} + +export function getIndentedTemplate(replace: string): string { + if (replace === "") { + return ""; + } + const snippets = replace.split(/({indent:\d+})/); + + let indentedString: string = ""; + let indentWidth: number = 0; + + // tslint:disable-next-line:prefer-for-of + snippets.forEach((element) => { + if (element.match(/{indent:\d+}/)) { + const indents = parseInt(element.match(/{indent:(\d+)}/)[1], 10); + indentWidth = indents; + const numSpaces = Math.max(indentWidth - indentedString.length, 0); + indentedString += " ".repeat(numSpaces); + } else { + // just some text + indentedString += element; + } + }); + + return indentedString; +} + +/** + * Expand variable template in the string + * @param original the original string + * @param template variable template to be expanded + * @returns new string with expanded template + */ +export function getTemplatedString(original: string, template: ITemplate): string { + const replacedTemplate = original.replace(template.toReplace, template.with); + const replacedWithEnv = getEnvVars(replacedTemplate); + return getIndentedTemplate(replacedWithEnv); +} + +/** + * Generate lines of doxygen comments from template + * @param lines Arrays to store lines of comments + * @param replace Variable template to be expanded + * @param template Original string that contains variable templates + * @param templateWith Arrays of values to replace in the original template string + */ +export function generateFromTemplate( + lines: string[], + replace: string, + template: string, + templateWith: string[], +) { + templateWith.forEach((element: string) => { + // Ignore null values + if (element !== null) { + lines.push(...getTemplatedString(template, { toReplace: replace, with: element }).split("\n")); + } + }); +} + +/** + * Expand multiple variable templates in the string + * @param original string containing multiple variables to be expanded + * @param templates variable templates to be expanded + * @returns new string with expanded templates + */ +export function getMultiTemplatedString( + original: string, + templates: ITemplate[], +): string { + // For each replace entry, attempt to replace it with the corresponding param in the template + for (const template of templates) { + original = original.replace(template.toReplace, template.with); + } + return getEnvVars(original); +} |