mirror of
https://github.com/overleaf/overleaf.git
synced 2025-04-09 03:50:49 +00:00
support a mainFile parameter for templates
This commit is contained in:
parent
ce6405d5b1
commit
2692090f3f
4 changed files with 112 additions and 10 deletions
|
@ -31,3 +31,23 @@ module.exports = ProjectRootDocManager =
|
|||
ProjectEntityUpdateHandler.setRootDoc project_id, root_doc_id, callback
|
||||
else
|
||||
callback()
|
||||
|
||||
setRootDocFromName: (project_id, rootDocName, callback = (error) ->) ->
|
||||
ProjectEntityHandler.getAllDocPathsFromProject project_id, (error, docPaths) ->
|
||||
return callback(error) if error?
|
||||
# find the root doc from the filename
|
||||
root_doc_id = null
|
||||
for doc_id, path of docPaths
|
||||
# docpaths have a leading / so allow matching "folder/filename" and "/folder/filename"
|
||||
if path == rootDocName or path == "/#{rootDocName}"
|
||||
root_doc_id = doc_id
|
||||
# try a basename match if there was no match
|
||||
if !root_doc_id
|
||||
for doc_id, path of docPaths
|
||||
if Path.basename(path) == Path.basename(rootDocName)
|
||||
root_doc_id = doc_id
|
||||
# set the root doc id if we found a match
|
||||
if root_doc_id?
|
||||
ProjectEntityUpdateHandler.setRootDoc project_id, root_doc_id, callback
|
||||
else
|
||||
callback()
|
|
@ -24,6 +24,7 @@ module.exports = TemplatesController =
|
|||
data.templateId = templateId
|
||||
data.name = req.query.templateName
|
||||
data.compiler = req.query.latexEngine
|
||||
data.mainFile = req.query.mainFile
|
||||
res.render path.resolve(__dirname, "../../../views/project/editor/new_from_template"), data
|
||||
|
||||
createProjectFromV1Template: (req, res)->
|
||||
|
@ -43,6 +44,7 @@ module.exports = TemplatesController =
|
|||
currentUserId: currentUserId,
|
||||
compiler: req.body.compiler
|
||||
docId: req.body.docId
|
||||
mainFile: req.body.mainFile
|
||||
templateId: req.body.templateId
|
||||
templateVersionId: req.body.templateVersionId
|
||||
},
|
||||
|
@ -62,19 +64,27 @@ module.exports = TemplatesController =
|
|||
if err?
|
||||
logger.err err:err, zipReq:zipReq, "problem building project from zip"
|
||||
return res.sendStatus 500
|
||||
setCompiler project._id, options.compiler, ->
|
||||
fs.unlink dumpPath, ->
|
||||
delete req.session.templateData
|
||||
conditions = {_id:project._id}
|
||||
update = {
|
||||
fromV1TemplateId:options.templateId,
|
||||
fromV1TemplateVersionId:options.templateVersionId
|
||||
}
|
||||
Project.update conditions, update, {}, (err)->
|
||||
res.redirect "/project/#{project._id}"
|
||||
setMainFile project._id, options.mainFile, ->
|
||||
# ignore any errors setting main file
|
||||
setCompiler project._id, options.compiler, ->
|
||||
fs.unlink dumpPath, ->
|
||||
delete req.session.templateData
|
||||
conditions = {_id:project._id}
|
||||
update = {
|
||||
fromV1TemplateId:options.templateId,
|
||||
fromV1TemplateVersionId:options.templateVersionId
|
||||
}
|
||||
Project.update conditions, update, {}, (err)->
|
||||
res.redirect "/project/#{project._id}"
|
||||
|
||||
setCompiler = (project_id, compiler, callback)->
|
||||
if compiler?
|
||||
ProjectOptionsHandler.setCompiler project_id, compiler, callback
|
||||
else
|
||||
callback()
|
||||
|
||||
setMainFile = (project_id, mainFile, callback) ->
|
||||
if mainFile?
|
||||
ProjectRootDocManager.setRootDocFromName project_id, mainFile, callback
|
||||
else
|
||||
callback()
|
|
@ -24,3 +24,4 @@ block content
|
|||
input(type="hidden" name="templateVersionId" value=templateVersionId)
|
||||
input(type="hidden" name="templateName" value=name)
|
||||
input(type="hidden" name="compiler" value=compiler)
|
||||
input(type="hidden" name="mainFile" value=mainFile)
|
||||
|
|
|
@ -75,3 +75,74 @@ describe 'ProjectRootDocManager', ->
|
|||
it "should not set the root doc to the doc containing a documentclass", ->
|
||||
@ProjectEntityUpdateHandler.setRootDoc.called.should.equal false
|
||||
|
||||
describe "setRootDocFromName", ->
|
||||
describe "when there is a suitable root doc", ->
|
||||
beforeEach (done)->
|
||||
@docPaths =
|
||||
"doc-id-1": "/chapter1.tex"
|
||||
"doc-id-2": "/main.tex"
|
||||
"doc-id-3": "/nested/chapter1a.tex"
|
||||
"doc-id-4": "/nested/chapter1b.tex"
|
||||
@ProjectEntityHandler.getAllDocPathsFromProject = sinon.stub().callsArgWith(1, null, @docPaths)
|
||||
@ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
|
||||
@ProjectRootDocManager.setRootDocFromName @project_id, '/main.tex', done
|
||||
|
||||
it "should check the docs of the project", ->
|
||||
@ProjectEntityHandler.getAllDocPathsFromProject.calledWith(@project_id)
|
||||
.should.equal true
|
||||
|
||||
it "should set the root doc to main.tex", ->
|
||||
@ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
|
||||
.should.equal true
|
||||
|
||||
describe "when there is a suitable root doc but the leading slash is missing", ->
|
||||
beforeEach (done)->
|
||||
@docPaths =
|
||||
"doc-id-1": "/chapter1.tex"
|
||||
"doc-id-2": "/main.tex"
|
||||
"doc-id-3": "/nested/chapter1a.tex"
|
||||
"doc-id-4": "/nested/chapter1b.tex"
|
||||
@ProjectEntityHandler.getAllDocPathsFromProject = sinon.stub().callsArgWith(1, null, @docPaths)
|
||||
@ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
|
||||
@ProjectRootDocManager.setRootDocFromName @project_id, 'main.tex', done
|
||||
|
||||
it "should check the docs of the project", ->
|
||||
@ProjectEntityHandler.getAllDocPathsFromProject.calledWith(@project_id)
|
||||
.should.equal true
|
||||
|
||||
it "should set the root doc to main.tex", ->
|
||||
@ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-2")
|
||||
.should.equal true
|
||||
|
||||
describe "when there is a suitable root doc with a basename match", ->
|
||||
beforeEach (done)->
|
||||
@docPaths =
|
||||
"doc-id-1": "/chapter1.tex"
|
||||
"doc-id-2": "/main.tex"
|
||||
"doc-id-3": "/nested/chapter1a.tex"
|
||||
"doc-id-4": "/nested/chapter1b.tex"
|
||||
@ProjectEntityHandler.getAllDocPathsFromProject = sinon.stub().callsArgWith(1, null, @docPaths)
|
||||
@ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
|
||||
@ProjectRootDocManager.setRootDocFromName @project_id, 'chapter1a.tex', done
|
||||
|
||||
it "should check the docs of the project", ->
|
||||
@ProjectEntityHandler.getAllDocPathsFromProject.calledWith(@project_id)
|
||||
.should.equal true
|
||||
|
||||
it "should set the root doc using the basename", ->
|
||||
@ProjectEntityUpdateHandler.setRootDoc.calledWith(@project_id, "doc-id-3")
|
||||
.should.equal true
|
||||
|
||||
describe "when there is no suitable root doc", ->
|
||||
beforeEach (done)->
|
||||
@docPaths =
|
||||
"doc-id-1": "/chapter1.tex"
|
||||
"doc-id-2": "/main.tex"
|
||||
"doc-id-3": "/nested/chapter1a.tex"
|
||||
"doc-id-4": "/nested/chapter1b.tex"
|
||||
@ProjectEntityHandler.getAllDocPathsFromProject = sinon.stub().callsArgWith(1, null, @docPaths)
|
||||
@ProjectEntityUpdateHandler.setRootDoc = sinon.stub().callsArgWith(2)
|
||||
@ProjectRootDocManager.setRootDocFromName @project_id, "other.tex", done
|
||||
|
||||
it "should not set the root doc", ->
|
||||
@ProjectEntityUpdateHandler.setRootDoc.called.should.equal false
|
||||
|
|
Loading…
Add table
Reference in a new issue