output-files.js 5.2 KB

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