output-files.js 5.3 KB

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