output-files.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 blgFiles = []
  81. for (const [filename, file] of outputFiles) {
  82. if (filename.endsWith('.blg')) {
  83. blgFiles.push(file)
  84. }
  85. }
  86. for (const blgFile of blgFiles) {
  87. try {
  88. const response = await fetch(buildURL(blgFile, data.pdfDownloadDomain), {
  89. signal,
  90. })
  91. const log = await response.text()
  92. try {
  93. const { errors, warnings } = new BibLogParser(log, {
  94. maxErrors: 100,
  95. }).parse()
  96. accumulateResults({ errors, warnings }, 'BibTeX:')
  97. } catch (e) {
  98. // BibLog parsing errors are ignored
  99. }
  100. } catch (e) {
  101. console.warn(e) // ignore failure to fetch/parse the log file, but log a warning
  102. }
  103. }
  104. result.logEntries.all = [
  105. ...result.logEntries.errors,
  106. ...result.logEntries.warnings,
  107. ...result.logEntries.typesetting,
  108. ]
  109. return result
  110. }
  111. export function buildLogEntryAnnotations(entries, fileTreeManager) {
  112. const rootDocDirname = fileTreeManager.getRootDocDirname()
  113. const logEntryAnnotations = {}
  114. for (const entry of entries) {
  115. if (entry.file) {
  116. entry.file = normalizeFilePath(entry.file, rootDocDirname)
  117. const entity = fileTreeManager.findEntityByPath(entry.file)
  118. if (entity) {
  119. if (!(entity.id in logEntryAnnotations)) {
  120. logEntryAnnotations[entity.id] = []
  121. }
  122. logEntryAnnotations[entity.id].push({
  123. row: entry.line - 1,
  124. type: entry.level === 'error' ? 'error' : 'warning',
  125. text: entry.message,
  126. source: 'compile', // NOTE: this is used in Ace for filtering the annotations
  127. })
  128. }
  129. }
  130. }
  131. return logEntryAnnotations
  132. }
  133. function buildURL(file, pdfDownloadDomain) {
  134. if (file.build && pdfDownloadDomain) {
  135. // Downloads from the compiles domain must include a build id.
  136. // The build id is used implicitly for access control.
  137. return `${pdfDownloadDomain}${file.url}`
  138. }
  139. // Go through web instead, which uses mongo for checking project access.
  140. return `${window.origin}${file.url}`
  141. }
  142. function normalizeFilePath(path, rootDocDirname) {
  143. path = path.replace(/\/\//g, '/')
  144. path = path.replace(
  145. /^.*\/compiles\/[0-9a-f]{24}(-[0-9a-f]{24})?\/(\.\/)?/,
  146. ''
  147. )
  148. path = path.replace(/^\/compile\//, '')
  149. if (rootDocDirname) {
  150. path = path.replace(/^\.\//, rootDocDirname + '/')
  151. }
  152. return path
  153. }
  154. function isTransientWarning(warning) {
  155. return TRANSIENT_WARNING_REGEX.test(warning.message)
  156. }