mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
0097564754
Open in Overleaf: Add support for arrays of files GitOrigin-RevId: 5ef6d1ace6b7c6c041f2846f2f433b39fb137372
56 lines
No EOL
1.9 KiB
CoffeeScript
56 lines
No EOL
1.9 KiB
CoffeeScript
ENGINE_TO_COMPILER_MAP = {
|
|
latex_dvipdf: "latex"
|
|
pdflatex: "pdflatex"
|
|
xelatex: "xelatex"
|
|
lualatex: "lualatex"
|
|
}
|
|
|
|
module.exports = ProjectHelper =
|
|
compilerFromV1Engine: (engine) ->
|
|
return ENGINE_TO_COMPILER_MAP[engine]
|
|
|
|
ensureNameIsUnique: (nameList, name, suffixes = [], maxLength, callback = (error, name, changed)->) ->
|
|
# create a set of all project names
|
|
allNames = new Set(nameList)
|
|
isUnique = (x) -> !allNames.has(x)
|
|
# check if the supplied name is already unique
|
|
if isUnique(name)
|
|
return callback(null, name, false)
|
|
# the name already exists, try adding the user-supplied suffixes to generate a unique name
|
|
for suffix in suffixes
|
|
candidateName = ProjectHelper._addSuffixToProjectName(name, suffix, maxLength)
|
|
if isUnique(candidateName)
|
|
return callback(null, candidateName, true)
|
|
# if there are no (more) suffixes, use a numeric one
|
|
uniqueName = ProjectHelper._addNumericSuffixToProjectName(name, allNames, maxLength)
|
|
if uniqueName?
|
|
callback(null, uniqueName, true)
|
|
else
|
|
callback(new Error("Failed to generate a unique name for: #{name}"))
|
|
|
|
_addSuffixToProjectName: (name, suffix = '', maxLength) ->
|
|
# append the suffix and truncate the project title if needed
|
|
truncatedLength = maxLength - suffix.length
|
|
return name.substr(0, truncatedLength) + suffix
|
|
|
|
_addNumericSuffixToProjectName: (name, allProjectNames, maxLength) ->
|
|
NUMERIC_SUFFIX_MATCH = / \((\d+)\)$/
|
|
suffixedName = (basename, number) ->
|
|
suffix = " (#{number})"
|
|
return basename.substr(0, maxLength - suffix.length) + suffix
|
|
|
|
match = name.match(NUMERIC_SUFFIX_MATCH)
|
|
basename = name
|
|
n = 1
|
|
last = allProjectNames.size + n
|
|
|
|
if match?
|
|
basename = name.replace(NUMERIC_SUFFIX_MATCH, '')
|
|
n = parseInt(match[1])
|
|
|
|
while n <= last
|
|
candidate = suffixedName(basename, n)
|
|
return candidate unless allProjectNames.has(candidate)
|
|
n += 1
|
|
|
|
return null |