output-files.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import getMeta from '../../../utils/meta'
  2. import HumanReadableLogs from '../../../ide/human-readable-logs/HumanReadableLogs'
  3. import BibLogParser from '../../../ide/log-parser/bib-log-parser'
  4. import { v4 as uuid } from 'uuid'
  5. import { enablePdfCaching } from './pdf-caching-flags'
  6. // Warnings that may disappear after a second LaTeX pass
  7. const TRANSIENT_WARNING_REGEX = /^(Reference|Citation).+undefined on input line/
  8. export function handleOutputFiles(outputFiles, projectId, data) {
  9. const outputFile = outputFiles.get('output.pdf')
  10. if (!outputFile) return null
  11. // build the URL for viewing the PDF in the preview UI
  12. const params = new URLSearchParams({
  13. compileGroup: data.compileGroup,
  14. })
  15. if (data.clsiServerId) {
  16. params.set('clsiserverid', data.clsiServerId)
  17. }
  18. if (enablePdfCaching) {
  19. // Tag traffic that uses the pdf caching logic.
  20. params.set('enable_pdf_caching', 'true')
  21. }
  22. outputFile.pdfUrl = `${buildURL(
  23. outputFile,
  24. data.pdfDownloadDomain
  25. )}?${params}`
  26. // build the URL for downloading the PDF
  27. params.set('popupDownload', 'true') // save PDF download as file
  28. outputFile.pdfDownloadUrl = `/download/project/${projectId}/build/${outputFile.build}/output/output.pdf?${params}`
  29. return outputFile
  30. }
  31. export const handleLogFiles = async (outputFiles, data, signal) => {
  32. const result = {
  33. log: null,
  34. logEntries: {
  35. errors: [],
  36. warnings: [],
  37. typesetting: [],
  38. },
  39. }
  40. function accumulateResults(newEntries, type) {
  41. for (const key in result.logEntries) {
  42. if (newEntries[key]) {
  43. for (const entry of newEntries[key]) {
  44. if (type) {
  45. entry.type = newEntries.type
  46. }
  47. if (entry.file) {
  48. entry.file = normalizeFilePath(entry.file)
  49. }
  50. entry.key = uuid()
  51. }
  52. result.logEntries[key].push(...newEntries[key])
  53. }
  54. }
  55. }
  56. const logFile = outputFiles.get('output.log')
  57. if (logFile) {
  58. try {
  59. const response = await fetch(buildURL(logFile, data.pdfDownloadDomain), {
  60. signal,
  61. })
  62. result.log = await response.text()
  63. let { errors, warnings, typesetting } = HumanReadableLogs.parse(
  64. result.log,
  65. {
  66. ignoreDuplicates: true,
  67. oldRegexes:
  68. getMeta('ol-splitTestVariants')?.['latex-log-parser'] !== 'new',
  69. }
  70. )
  71. if (data.status === 'stopped-on-first-error') {
  72. // Hide warnings that could disappear after a second pass
  73. warnings = warnings.filter(warning => !isTransientWarning(warning))
  74. }
  75. accumulateResults({ errors, warnings, typesetting })
  76. } catch (e) {
  77. console.warn(e) // ignore failure to fetch/parse the log file, but log a warning
  78. }
  79. }
  80. const blgFile = outputFiles.get('output.blg')
  81. if (blgFile) {
  82. try {
  83. const response = await fetch(buildURL(blgFile, data.pdfDownloadDomain), {
  84. signal,
  85. })
  86. const log = await response.text()
  87. try {
  88. const { errors, warnings } = new BibLogParser(log, {
  89. maxErrors: 100,
  90. }).parse()
  91. accumulateResults({ errors, warnings }, 'BibTeX:')
  92. } catch (e) {
  93. // BibLog parsing errors are ignored
  94. }
  95. } catch (e) {
  96. console.warn(e) // ignore failure to fetch/parse the log file, but log a warning
  97. }
  98. }
  99. result.logEntries.all = [
  100. ...result.logEntries.errors,
  101. ...result.logEntries.warnings,
  102. ...result.logEntries.typesetting,
  103. ]
  104. return result
  105. }
  106. export function buildLogEntryAnnotations(entries, fileTreeManager) {
  107. const rootDocDirname = fileTreeManager.getRootDocDirname()
  108. const logEntryAnnotations = {}
  109. for (const entry of entries) {
  110. if (entry.file) {
  111. entry.file = normalizeFilePath(entry.file, rootDocDirname)
  112. const entity = fileTreeManager.findEntityByPath(entry.file)
  113. if (entity) {
  114. if (!(entity.id in logEntryAnnotations)) {
  115. logEntryAnnotations[entity.id] = []
  116. }
  117. logEntryAnnotations[entity.id].push({
  118. row: entry.line - 1,
  119. type: entry.level === 'error' ? 'error' : 'warning',
  120. text: entry.message,
  121. source: 'compile', // NOTE: this is used in Ace for filtering the annotations
  122. })
  123. }
  124. }
  125. }
  126. return logEntryAnnotations
  127. }
  128. function buildURL(file, pdfDownloadDomain) {
  129. if (file.build && pdfDownloadDomain) {
  130. // Downloads from the compiles domain must include a build id.
  131. // The build id is used implicitly for access control.
  132. return `${pdfDownloadDomain}${file.url}`
  133. }
  134. // Go through web instead, which uses mongo for checking project access.
  135. return file.url
  136. }
  137. function normalizeFilePath(path, rootDocDirname) {
  138. path = path.replace(
  139. /^.*\/compiles\/[0-9a-f]{24}(-[0-9a-f]{24})?\/(\.\/)?/,
  140. ''
  141. )
  142. path = path.replace(/^\/compile\//, '')
  143. if (rootDocDirname) {
  144. path = path.replace(/^\.\//, rootDocDirname + '/')
  145. }
  146. return path
  147. }
  148. function isTransientWarning(warning) {
  149. return TRANSIENT_WARNING_REGEX.test(warning.message)
  150. }