output-files.js 4.2 KB

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