Skip to content

Commit

Permalink
Fix #4347 %!TEX/%!BIB options accept multiple bash args and handles s…
Browse files Browse the repository at this point in the history
…paces
  • Loading branch information
James-Yu committed Aug 30, 2024
1 parent 1a9b622 commit 87ea37e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/compile/recipe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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}.`)
}
}
Expand All @@ -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}.`)
}
}
Expand Down
74 changes: 73 additions & 1 deletion test/units/06_compile_recipe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -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')

Expand All @@ -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')

Expand Down

0 comments on commit 87ea37e

Please sign in to comment.