From 684ac43eef01cc0a6597b800b24a3defea525d06 Mon Sep 17 00:00:00 2001 From: Darcy Wong Date: Sun, 10 Sep 2023 13:16:10 +0700 Subject: [PATCH] fix: Handle project name for filename regex --- src/backTranslation.ts | 6 ++++-- src/index.ts | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/backTranslation.ts b/src/backTranslation.ts index 8e168d4..c46c26b 100644 --- a/src/backTranslation.ts +++ b/src/backTranslation.ts @@ -21,11 +21,13 @@ export const VERSE_PATTERN = /(\d+)-?(\d+)?\s?(.*)/; /** * Extract a book name and chapter number from the filename * @param {string} file - Path to the Toolbox text file + * @param {string} projectName - Name of the project (expected at the start of the filenames) * @returns {fileInfoType} - Object containing the book name and chapter number */ -export function getBookAndChapter(file: string) : toolbox.fileInfoType { +export function getBookAndChapter(file: string, projectName: string) : toolbox.fileInfoType { const filename = path.parse(file).base; - const pattern = /(Lem|lem)?(\d*\D+)(\d+)\D?\.rtf/; + const patternFormatStr = `(${projectName}|${projectName.toLowerCase()})?(\\d*\\D+)(\\d+)\\D?\\.rtf`; + const pattern = new RegExp(patternFormatStr); const match = filename.match(pattern); const obj: toolbox.fileInfoType = { bookName: "Placeholder", diff --git a/src/index.ts b/src/index.ts index 4cdbab2..bc99d0b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -220,7 +220,7 @@ function processDirectory(directory: string){ * @returns {books.bookType} bookObj - modified book object */ async function processBackText(filepath: string, bookObj: books.objType): Promise { - const bookInfo = backTranslation.getBookAndChapter(filepath); + const bookInfo = backTranslation.getBookAndChapter(filepath, options.projectName); const currentChapter = bookInfo.chapterNumber; const bookType = books.getBookByName(bookInfo.bookName); if (bookInfo.bookName === "Placeholder") {