output-files.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. const searchParams = new URLSearchParams(window.location.search)
  6. export const handleOutputFiles = async (outputFiles, projectId, data) => {
  7. const result = {}
  8. const pdfDownloadDomain = data.pdfDownloadDomain ?? ''
  9. const outputFile = outputFiles.get('output.pdf')
  10. if (outputFile) {
  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 (searchParams.get('verify_chunks') === 'true') {
  19. // Instruct the serviceWorker to verify composed ranges.
  20. params.set('verify_chunks', 'true')
  21. }
  22. if (getMeta('ol-enablePdfCaching')) {
  23. // Tag traffic that uses the pdf caching logic.
  24. params.set('enable_pdf_caching', 'true')
  25. }
  26. result.pdfUrl = `${pdfDownloadDomain}${outputFile.url}?${params}`
  27. // build the URL for downloading the PDF
  28. params.set('popupDownload', 'true') // save PDF download as file
  29. result.pdfDownloadUrl = `/download/project/${projectId}/build/${outputFile.build}/output/output.pdf?${params}`
  30. }
  31. return result
  32. }
  33. export const handleLogFiles = async (outputFiles, data, signal) => {
  34. const pdfDownloadDomain = data.pdfDownloadDomain ?? ''
  35. const result = {
  36. log: null,
  37. logEntries: {
  38. errors: [],
  39. warnings: [],
  40. typesetting: [],
  41. },
  42. }
  43. function accumulateResults(newEntries, type) {
  44. for (const key in result.logEntries) {
  45. if (newEntries[key]) {
  46. for (const entry of newEntries[key]) {
  47. if (type) {
  48. entry.type = newEntries.type
  49. }
  50. if (entry.file) {
  51. entry.file = normalizeFilePath(entry.file)
  52. }
  53. entry.key = uuid()
  54. }
  55. result.logEntries[key].push(...newEntries[key])
  56. }
  57. }
  58. }
  59. const logFile = outputFiles.get('output.log')
  60. if (logFile) {
  61. try {
  62. const response = await fetch(`${pdfDownloadDomain}${logFile.url}`, {
  63. signal,
  64. })
  65. result.log = await response.text()
  66. const { errors, warnings, typesetting } = HumanReadableLogs.parse(
  67. result.log,
  68. {
  69. ignoreDuplicates: true,
  70. }
  71. )
  72. accumulateResults({ errors, warnings, typesetting })
  73. } catch (e) {
  74. console.warn(e) // ignore failure to fetch/parse the log file, but log a warning
  75. }
  76. }
  77. const blgFile = outputFiles.get('output.blg')
  78. if (blgFile) {
  79. try {
  80. const response = await fetch(`${pdfDownloadDomain}${blgFile.url}`, {
  81. signal,
  82. })
  83. const log = await response.text()
  84. try {
  85. const { errors, warnings } = new BibLogParser(log, {
  86. maxErrors: 100,
  87. }).parse()
  88. accumulateResults({ errors, warnings }, 'BibTeX:')
  89. } catch (e) {
  90. // BibLog parsing errors are ignored
  91. }
  92. } catch (e) {
  93. console.warn(e) // ignore failure to fetch/parse the log file, but log a warning
  94. }
  95. }
  96. result.logEntries.all = [
  97. ...result.logEntries.errors,
  98. ...result.logEntries.warnings,
  99. ...result.logEntries.typesetting,
  100. ]
  101. return result
  102. }
  103. export function buildLogEntryAnnotations(entries, fileTreeManager) {
  104. const rootDocDirname = fileTreeManager.getRootDocDirname()
  105. const logEntryAnnotations = {}
  106. for (const entry of entries) {
  107. if (entry.file) {
  108. entry.file = normalizeFilePath(entry.file, rootDocDirname)
  109. const entity = fileTreeManager.findEntityByPath(entry.file)
  110. if (entity) {
  111. if (!(entity.id in logEntryAnnotations)) {
  112. logEntryAnnotations[entity.id] = []
  113. }
  114. logEntryAnnotations[entity.id].push({
  115. row: entry.line - 1,
  116. type: entry.level === 'error' ? 'error' : 'warning',
  117. text: entry.message,
  118. source: 'compile',
  119. })
  120. }
  121. }
  122. }
  123. return logEntryAnnotations
  124. }
  125. function normalizeFilePath(path, rootDocDirname) {
  126. path = path.replace(
  127. /^.*\/compiles\/[0-9a-f]{24}(-[0-9a-f]{24})?\/(\.\/)?/,
  128. ''
  129. )
  130. path = path.replace(/^\/compile\//, '')
  131. if (rootDocDirname) {
  132. path = path.replace(/^\.\//, rootDocDirname + '/')
  133. }
  134. return path
  135. }