diff --git a/src/compile/recipe.ts b/src/compile/recipe.ts index b6f9e1a2c..d00906d43 100644 --- a/src/compile/recipe.ts +++ b/src/compile/recipe.ts @@ -180,6 +180,7 @@ async function findMagicComments(rootFile: string): Promise<{tex?: Tool, bib?: T const regexTexOptions = /^(?:%\s*!\s*T[Ee]X\s(?:TS-)?options\s*=\s*(.*)$)/m const regexBibOptions = /^(?:%\s*!\s*BIB\s(?:TS-)?options\s*=\s*(.*)$)/m const regexRecipe = /^(?:%\s*!\s*LW\srecipe\s*=\s*(.*)$)/m + const bashArgSplit = /(?:"(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|\\.|[^\s\\'"])+/g let content = '' for (const line of (await lw.file.read(rootFile))?.split('\n') || []) { if (line.startsWith('%') || line.trim().length === 0) { @@ -199,7 +200,7 @@ async function findMagicComments(rootFile: string): Promise<{tex?: Tool, bib?: T logger.log(`Found TeX program by magic comment: ${texCommand.command}.`) const res = content.match(regexTexOptions) if (res) { - texCommand.args = [res[1]] + texCommand.args = [...res[1].matchAll(bashArgSplit)].map(arg => arg[0].replace(/(\\\s)/g, ' ').replace(/(^['"]|['"]$)/g, '')) logger.log(`Found TeX options by magic comment: ${texCommand.args}.`) } } @@ -214,7 +215,7 @@ async function findMagicComments(rootFile: string): Promise<{tex?: Tool, bib?: T logger.log(`Found BIB program by magic comment: ${bibCommand.command}.`) const res = content.match(regexBibOptions) if (res) { - bibCommand.args = [res[1]] + bibCommand.args = [...res[1].matchAll(bashArgSplit)].map(arg => arg[0].replace(/(\\\s)/g, ' ').replace(/(^['"]|['"]$)/g, '')) logger.log(`Found BIB options by magic comment: ${bibCommand.args}.`) } } diff --git a/test/units/06_compile_recipe.test.ts b/test/units/06_compile_recipe.test.ts index e95d2a65e..65ba6e978 100644 --- a/test/units/06_compile_recipe.test.ts +++ b/test/units/06_compile_recipe.test.ts @@ -238,7 +238,7 @@ describe(path.basename(__filename).split('.')[0] + ':', () => { }) }) - describe('lw.compile->recipe.findMagicComments', () => { + describe.only('lw.compile->recipe.findMagicComments', () => { let readStub: sinon.SinonStub before(() => { @@ -285,6 +285,42 @@ describe(path.basename(__filename).split('.')[0] + ':', () => { }) }) + it('should detect TeX magic options with multiple arguments (#4347)', async () => { + readStub.resolves('% !TEX program = pdflatex\n% !TEX options = --shell-escape --another\n') + + const result = await recipe.findMagicComments('dummy.tex') + + assert.deepStrictEqual(result, { + tex: { name: lw.constant.TEX_MAGIC_PROGRAM_NAME, command: 'pdflatex', args: ['--shell-escape', '--another'] }, + bib: undefined, + recipe: undefined, + }) + }) + + it('should detect TeX magic options with quoted space in arguments (#4347)', async () => { + readStub.resolves('% !TEX program = pdflatex\n% !TEX options = "an argument"\n') + + const result = await recipe.findMagicComments('dummy.tex') + + assert.deepStrictEqual(result, { + tex: { name: lw.constant.TEX_MAGIC_PROGRAM_NAME, command: 'pdflatex', args: ['an argument'] }, + bib: undefined, + recipe: undefined, + }) + }) + + it('should detect TeX magic options with escaped space in arguments (#4347)', async () => { + readStub.resolves('% !TEX program = pdflatex\n% !TEX options = an\\ argument\n') + + const result = await recipe.findMagicComments('dummy.tex') + + assert.deepStrictEqual(result, { + tex: { name: lw.constant.TEX_MAGIC_PROGRAM_NAME, command: 'pdflatex', args: ['an argument'] }, + bib: undefined, + recipe: undefined, + }) + }) + it('should detect only BIB magic comment', async () => { readStub.resolves('% !BIB program = bibtex\n') @@ -309,6 +345,42 @@ describe(path.basename(__filename).split('.')[0] + ':', () => { }) }) + it('should detect BIB magic options with multiple arguments (#4347)', async () => { + readStub.resolves('% !BIB program = bibtex\n% !BIB options = --min-crossrefs=100 --another\n') + + const result = await recipe.findMagicComments('dummy.tex') + + assert.deepStrictEqual(result, { + tex: undefined, + bib: { name: lw.constant.BIB_MAGIC_PROGRAM_NAME, command: 'bibtex', args: ['--min-crossrefs=100', '--another'] }, + recipe: undefined, + }) + }) + + it('should detect BIB magic options with quoted space in arguments (#4347)', async () => { + readStub.resolves('% !BIB program = bibtex\n% !BIB options = "an argument"\n') + + const result = await recipe.findMagicComments('dummy.tex') + + assert.deepStrictEqual(result, { + tex: undefined, + bib: { name: lw.constant.BIB_MAGIC_PROGRAM_NAME, command: 'bibtex', args: ['an argument'] }, + recipe: undefined, + }) + }) + + it('should detect BIB magic options with escaped space in arguments (#4347)', async () => { + readStub.resolves('% !BIB program = bibtex\n% !BIB options = an\\ argument\n') + + const result = await recipe.findMagicComments('dummy.tex') + + assert.deepStrictEqual(result, { + tex: undefined, + bib: { name: lw.constant.BIB_MAGIC_PROGRAM_NAME, command: 'bibtex', args: ['an argument'] }, + recipe: undefined, + }) + }) + it('should detect only LW recipe comment', async () => { readStub.resolves('% !LW recipe = default\n')