output-files.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 { enablePdfCaching } from './pdf-caching-flags'
  5. import { debugConsole } from '@/utils/debugging'
  6. import { dirname, findEntityByPath } from '@/features/file-tree/util/path'
  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. let nextEntryId = 1
  33. function generateEntryKey() {
  34. return '' + nextEntryId++
  35. }
  36. export const handleLogFiles = async (outputFiles, data, signal) => {
  37. const result = {
  38. log: null,
  39. logEntries: {
  40. errors: [],
  41. warnings: [],
  42. typesetting: [],
  43. },
  44. }
  45. function accumulateResults(newEntries, type) {
  46. for (const key in result.logEntries) {
  47. if (newEntries[key]) {
  48. for (const entry of newEntries[key]) {
  49. if (type) {
  50. entry.type = newEntries.type
  51. }
  52. if (entry.file) {
  53. entry.file = normalizeFilePath(entry.file)
  54. }
  55. entry.key = generateEntryKey()
  56. }
  57. result.logEntries[key].push(...newEntries[key])
  58. }
  59. }
  60. }
  61. const logFile = outputFiles.get('output.log')
  62. if (logFile) {
  63. try {
  64. const response = await fetch(buildURL(logFile, data.pdfDownloadDomain), {
  65. signal,
  66. })
  67. result.log = await response.text()
  68. let { errors, warnings, typesetting } = HumanReadableLogs.parse(
  69. result.log,
  70. {
  71. ignoreDuplicates: true,
  72. oldRegexes:
  73. getMeta('ol-splitTestVariants')?.['latex-log-parser'] !== 'new',
  74. }
  75. )
  76. if (data.status === 'stopped-on-first-error') {
  77. // Hide warnings that could disappear after a second pass
  78. warnings = warnings.filter(warning => !isTransientWarning(warning))
  79. }
  80. accumulateResults({ errors, warnings, typesetting })
  81. } catch (e) {
  82. debugConsole.warn(e) // ignore failure to fetch/parse the log file, but log a warning
  83. }
  84. }
  85. const blgFiles = []
  86. for (const [filename, file] of outputFiles) {
  87. if (filename.endsWith('.blg')) {
  88. blgFiles.push(file)
  89. }
  90. }
  91. for (const blgFile of blgFiles) {
  92. try {
  93. const response = await fetch(buildURL(blgFile, data.pdfDownloadDomain), {
  94. signal,
  95. })
  96. const log = await response.text()
  97. try {
  98. const { errors, warnings } = new BibLogParser(log, {
  99. maxErrors: 100,
  100. }).parse()
  101. accumulateResults({ errors, warnings }, 'BibTeX:')
  102. } catch (e) {
  103. // BibLog parsing errors are ignored
  104. }
  105. } catch (e) {
  106. debugConsole.warn(e) // ignore failure to fetch/parse the log file, but log a warning
  107. }
  108. }
  109. result.logEntries.all = [
  110. ...result.logEntries.errors,
  111. ...result.logEntries.warnings,
  112. ...result.logEntries.typesetting,
  113. ]
  114. return result
  115. }
  116. export function buildLogEntryAnnotations(entries, fileTreeData, rootDocId) {
  117. const rootDocDirname = dirname(fileTreeData, rootDocId)
  118. const logEntryAnnotations = {}
  119. for (const entry of entries) {
  120. if (entry.file) {
  121. entry.file = normalizeFilePath(entry.file, rootDocDirname)
  122. const entity = findEntityByPath(fileTreeData, entry.file)?.entity
  123. if (entity) {
  124. if (!(entity._id in logEntryAnnotations)) {
  125. logEntryAnnotations[entity._id] = []
  126. }
  127. logEntryAnnotations[entity._id].push({
  128. row: entry.line - 1,
  129. type: entry.level === 'error' ? 'error' : 'warning',
  130. text: entry.message,
  131. source: 'compile', // NOTE: this is used in Ace for filtering the annotations
  132. ruleId: entry.ruleId,
  133. })
  134. }
  135. }
  136. }
  137. return logEntryAnnotations
  138. }
  139. export const buildRuleCounts = (entries = []) => {
  140. const counts = {}
  141. for (const entry of entries) {
  142. const key = `${entry.level}_${entry.ruleId}`
  143. counts[key] = counts[key] ? counts[key] + 1 : 1
  144. }
  145. return counts
  146. }
  147. function buildURL(file, pdfDownloadDomain) {
  148. if (file.build && pdfDownloadDomain) {
  149. // Downloads from the compiles domain must include a build id.
  150. // The build id is used implicitly for access control.
  151. return `${pdfDownloadDomain}${file.url}`
  152. }
  153. // Go through web instead, which uses mongo for checking project access.
  154. return `${window.origin}${file.url}`
  155. }
  156. function normalizeFilePath(path, rootDocDirname) {
  157. path = path.replace(/\/\//g, '/')
  158. path = path.replace(
  159. /^.*\/compiles\/[0-9a-f]{24}(-[0-9a-f]{24})?\/(\.\/)?/,
  160. ''
  161. )
  162. path = path.replace(/^\/compile\//, '')
  163. if (rootDocDirname) {
  164. path = path.replace(/^\.\//, rootDocDirname + '/')
  165. }
  166. return path
  167. }
  168. function isTransientWarning(warning) {
  169. return TRANSIENT_WARNING_REGEX.test(warning.message)
  170. }