2014-06-28 08:25:06 -04:00
|
|
|
define [
|
|
|
|
"base"
|
2014-06-29 09:22:08 -04:00
|
|
|
"libs/latex-log-parser"
|
|
|
|
], (App, LogParser) ->
|
2014-06-30 06:35:32 -04:00
|
|
|
App.controller "PdfController", ["$scope", "$http", "ide", ($scope, $http, ide) ->
|
2014-06-28 08:25:06 -04:00
|
|
|
$scope.pdf =
|
2014-06-29 09:22:08 -04:00
|
|
|
url: null # Pdf Url
|
|
|
|
error: false # Server error
|
|
|
|
timeout: false # Server timed out
|
|
|
|
failure: false # PDF failed to compile
|
|
|
|
compiling: false
|
2014-06-30 06:35:32 -04:00
|
|
|
uncompiled: true
|
2014-06-29 09:22:08 -04:00
|
|
|
logEntries: []
|
2014-06-30 07:05:43 -04:00
|
|
|
rawLog: ""
|
|
|
|
view: null # 'pdf' 'logs'
|
|
|
|
showRawLog: false
|
2014-06-29 09:22:08 -04:00
|
|
|
|
2014-06-30 06:35:32 -04:00
|
|
|
autoCompile = true
|
|
|
|
$scope.$on "doc:opened", () ->
|
|
|
|
return if !autoCompile
|
|
|
|
autoCompile = false
|
|
|
|
$scope.recompile(isAutoCompile: true)
|
|
|
|
|
2014-06-29 09:22:08 -04:00
|
|
|
sendCompileRequest = (options = {}) ->
|
|
|
|
url = "/project/#{$scope.project_id}/compile"
|
|
|
|
if options.isAutoCompile
|
|
|
|
url += "?auto_compile=true"
|
|
|
|
return $http.post url, {
|
2014-06-30 06:35:32 -04:00
|
|
|
settingsOverride:
|
|
|
|
rootDoc_id: options.rootDocOverride_id or null
|
2014-06-29 09:22:08 -04:00
|
|
|
_csrf: window.csrfToken
|
|
|
|
}
|
|
|
|
|
|
|
|
parseCompileResponse = (response) ->
|
|
|
|
# Reset everything
|
2014-06-30 06:35:32 -04:00
|
|
|
$scope.pdf.error = false
|
|
|
|
$scope.pdf.timedout = false
|
|
|
|
$scope.pdf.failure = false
|
|
|
|
$scope.pdf.uncompiled = false
|
|
|
|
$scope.pdf.url = null
|
2014-06-29 09:22:08 -04:00
|
|
|
|
|
|
|
if response.status == "timedout"
|
|
|
|
$scope.pdf.timedout = true
|
|
|
|
else if response.status == "autocompile-backoff"
|
2014-06-30 06:35:32 -04:00
|
|
|
$scope.pdf.uncompiled = true
|
2014-06-29 09:22:08 -04:00
|
|
|
else if response.status == "failure"
|
|
|
|
$scope.pdf.failure = true
|
|
|
|
fetchLogs()
|
|
|
|
else if response.status == "success"
|
|
|
|
$scope.pdf.url = "/project/#{$scope.project_id}/output/output.pdf?cache_bust=#{Date.now()}"
|
|
|
|
fetchLogs()
|
|
|
|
|
2014-06-30 07:05:43 -04:00
|
|
|
IGNORE_FILES = ["output.fls", "output.fdb_latexmk"]
|
|
|
|
$scope.pdf.outputFiles = []
|
|
|
|
for file in response.outputFiles
|
|
|
|
if IGNORE_FILES.indexOf(file.path) == -1
|
|
|
|
# Turn 'output.blg' into 'blg file'.
|
|
|
|
if file.path.match(/^output\./)
|
|
|
|
file.name = "#{file.path.replace(/^output\./, "")} file"
|
|
|
|
else
|
|
|
|
file.name = file.path
|
|
|
|
$scope.pdf.outputFiles.push file
|
|
|
|
|
2014-06-29 09:22:08 -04:00
|
|
|
fetchLogs = () ->
|
|
|
|
$http.get "/project/#{$scope.project_id}/output/output.log"
|
|
|
|
.success (log) ->
|
2014-06-30 07:05:43 -04:00
|
|
|
$scope.pdf.rawLog = log
|
2014-06-29 09:22:08 -04:00
|
|
|
logEntries = LogParser.parse(log, ignoreDuplicates: true)
|
|
|
|
$scope.pdf.logEntries = logEntries
|
|
|
|
$scope.pdf.logEntries.all = logEntries.errors.concat(logEntries.warnings).concat(logEntries.typesetting)
|
|
|
|
for entry in logEntries.all
|
|
|
|
entry.file = entry.file.replace(/^(.*)\/compiles\/[0-9a-f]{24}\/(\.\/)?/, "")
|
|
|
|
entry.file = entry.file.replace(/^\/compile\//, "")
|
2014-06-30 06:35:32 -04:00
|
|
|
.error () ->
|
|
|
|
$scope.pdf.logEntries = []
|
2014-06-30 07:05:43 -04:00
|
|
|
$scope.pdf.rawLog = ""
|
2014-06-30 06:35:32 -04:00
|
|
|
|
|
|
|
getRootDocOverride_id = () ->
|
|
|
|
doc = ide.editorManager.getCurrentDocValue()
|
|
|
|
return null if !doc?
|
|
|
|
for line in doc.split("\n")
|
|
|
|
match = line.match /(.*)\\documentclass/
|
|
|
|
if match and !match[1].match /%/
|
|
|
|
return ide.editorManager.getCurrentDocId()
|
|
|
|
return null
|
2014-06-29 09:22:08 -04:00
|
|
|
|
2014-06-30 06:35:32 -04:00
|
|
|
$scope.recompile = (options = {}) ->
|
|
|
|
console.log "Recompiling", options
|
2014-06-29 09:22:08 -04:00
|
|
|
return if $scope.pdf.compiling
|
|
|
|
$scope.pdf.compiling = true
|
2014-06-30 06:35:32 -04:00
|
|
|
|
|
|
|
options.rootDocOverride_id = getRootDocOverride_id()
|
|
|
|
|
|
|
|
sendCompileRequest(options)
|
2014-06-29 09:22:08 -04:00
|
|
|
.success (data) ->
|
2014-06-30 06:35:32 -04:00
|
|
|
$scope.pdf.view = "pdf"
|
2014-06-29 09:22:08 -04:00
|
|
|
$scope.pdf.compiling = false
|
|
|
|
parseCompileResponse(data)
|
|
|
|
.error () ->
|
|
|
|
$scope.pdf.compiling = false
|
|
|
|
$scope.pdf.error = true
|
|
|
|
|
|
|
|
$scope.toggleLogs = () ->
|
2014-06-30 06:35:32 -04:00
|
|
|
if !$scope.pdf.view? or $scope.pdf.view == "pdf"
|
2014-06-29 09:22:08 -04:00
|
|
|
$scope.pdf.view = "logs"
|
|
|
|
else
|
|
|
|
$scope.pdf.view = "pdf"
|
|
|
|
|
|
|
|
$scope.showPdf = () ->
|
|
|
|
$scope.pdf.view = "pdf"
|
2014-06-30 07:05:43 -04:00
|
|
|
|
|
|
|
$scope.toggleRawLog = () ->
|
|
|
|
$scope.pdf.showRawLog = !$scope.pdf.showRawLog
|
|
|
|
|
|
|
|
$scope.openOutputFile = (file) ->
|
|
|
|
window.open("/project/#{$scope.project_id}/output/#{file.path}")
|
2014-06-28 08:25:06 -04:00
|
|
|
]
|