output-files.js 5.1 KB

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