2021-09-21 04:56:44 -04:00
|
|
|
define(function () {
|
|
|
|
// Define some constants
|
|
|
|
const LOG_WRAP_LIMIT = 79
|
|
|
|
const LATEX_WARNING_REGEX = /^LaTeX Warning: (.*)$/
|
|
|
|
const HBOX_WARNING_REGEX = /^(Over|Under)full \\(v|h)box/
|
|
|
|
const PACKAGE_WARNING_REGEX = /^(Package \b.+\b Warning:.*)$/
|
|
|
|
// This is used to parse the line number from common latex warnings
|
|
|
|
const LINES_REGEX = /lines? ([0-9]+)/
|
|
|
|
// This is used to parse the package name from the package warnings
|
|
|
|
const PACKAGE_REGEX = /^Package (\b.+\b) Warning/
|
|
|
|
|
|
|
|
const LogText = function (text) {
|
|
|
|
this.text = text.replace(/(\r\n)|\r/g, '\n')
|
|
|
|
// Join any lines which look like they have wrapped.
|
|
|
|
const wrappedLines = this.text.split('\n')
|
|
|
|
this.lines = [wrappedLines[0]]
|
|
|
|
let i = 1
|
2016-09-22 09:47:48 -04:00
|
|
|
while (i < wrappedLines.length) {
|
2021-09-21 04:56:44 -04:00
|
|
|
// If the previous line is as long as the wrap limit then
|
|
|
|
// append this line to it.
|
|
|
|
// Some lines end with ... when LaTeX knows it's hit the limit
|
|
|
|
// These shouldn't be wrapped.
|
|
|
|
if (
|
|
|
|
wrappedLines[i - 1].length === LOG_WRAP_LIMIT &&
|
|
|
|
wrappedLines[i - 1].slice(-3) !== '...'
|
|
|
|
) {
|
|
|
|
this.lines[this.lines.length - 1] += wrappedLines[i]
|
2016-09-22 09:47:48 -04:00
|
|
|
} else {
|
2021-09-21 04:56:44 -04:00
|
|
|
this.lines.push(wrappedLines[i])
|
2016-09-22 09:47:48 -04:00
|
|
|
}
|
2021-09-21 04:56:44 -04:00
|
|
|
i++
|
2016-09-22 09:47:48 -04:00
|
|
|
}
|
2021-09-21 04:56:44 -04:00
|
|
|
this.row = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
;(function () {
|
|
|
|
this.nextLine = function () {
|
|
|
|
this.row++
|
2016-09-22 09:47:48 -04:00
|
|
|
if (this.row >= this.lines.length) {
|
2021-09-21 04:56:44 -04:00
|
|
|
return false
|
2016-09-22 09:47:48 -04:00
|
|
|
} else {
|
2021-09-21 04:56:44 -04:00
|
|
|
return this.lines[this.row]
|
2016-09-22 09:47:48 -04:00
|
|
|
}
|
2021-09-21 04:56:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.rewindLine = function () {
|
|
|
|
this.row--
|
|
|
|
}
|
|
|
|
|
|
|
|
this.linesUpToNextWhitespaceLine = function () {
|
|
|
|
return this.linesUpToNextMatchingLine(/^ *$/)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.linesUpToNextMatchingLine = function (match) {
|
|
|
|
const lines = []
|
|
|
|
let nextLine = this.nextLine()
|
2016-09-22 09:47:48 -04:00
|
|
|
if (nextLine !== false) {
|
2021-09-21 04:56:44 -04:00
|
|
|
lines.push(nextLine)
|
2016-09-22 09:47:48 -04:00
|
|
|
}
|
2021-09-21 04:56:44 -04:00
|
|
|
while (
|
|
|
|
nextLine !== false &&
|
|
|
|
!nextLine.match(match) &&
|
|
|
|
nextLine !== false
|
|
|
|
) {
|
|
|
|
nextLine = this.nextLine()
|
2016-09-22 09:47:48 -04:00
|
|
|
if (nextLine !== false) {
|
2021-09-21 04:56:44 -04:00
|
|
|
lines.push(nextLine)
|
2014-06-02 06:44:41 -04:00
|
|
|
}
|
2016-09-22 09:47:48 -04:00
|
|
|
}
|
2021-09-21 04:56:44 -04:00
|
|
|
return lines
|
|
|
|
}
|
|
|
|
}.call(LogText.prototype))
|
|
|
|
|
|
|
|
const state = {
|
2016-09-22 09:47:48 -04:00
|
|
|
NORMAL: 0,
|
2021-09-21 04:56:44 -04:00
|
|
|
ERROR: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
const LatexParser = function (text, options) {
|
|
|
|
this.log = new LogText(text)
|
|
|
|
this.state = state.NORMAL
|
|
|
|
options = options || {}
|
|
|
|
this.fileBaseNames = options.fileBaseNames || [/compiles/, /\/usr\/local/]
|
|
|
|
this.ignoreDuplicates = options.ignoreDuplicates
|
|
|
|
this.data = []
|
|
|
|
this.fileStack = []
|
|
|
|
this.currentFileList = this.rootFileList = []
|
|
|
|
this.openParens = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
;(function () {
|
|
|
|
this.parse = function () {
|
2016-09-22 09:47:48 -04:00
|
|
|
while ((this.currentLine = this.log.nextLine()) !== false) {
|
|
|
|
if (this.state === state.NORMAL) {
|
|
|
|
if (this.currentLineIsError()) {
|
2021-09-21 04:56:44 -04:00
|
|
|
this.state = state.ERROR
|
2016-09-22 09:47:48 -04:00
|
|
|
this.currentError = {
|
|
|
|
line: null,
|
|
|
|
file: this.currentFilePath,
|
|
|
|
level: 'error',
|
|
|
|
message: this.currentLine.slice(2),
|
|
|
|
content: '',
|
2021-09-21 04:56:44 -04:00
|
|
|
raw: this.currentLine + '\n',
|
|
|
|
}
|
|
|
|
} else if (this.currentLineIsFileLineError()) {
|
|
|
|
this.state = state.ERROR
|
|
|
|
this.parseFileLineError()
|
2016-09-22 09:47:48 -04:00
|
|
|
} else if (this.currentLineIsRunawayArgument()) {
|
2021-09-21 04:56:44 -04:00
|
|
|
this.parseRunawayArgumentError()
|
2016-09-22 09:47:48 -04:00
|
|
|
} else if (this.currentLineIsWarning()) {
|
2021-09-21 04:56:44 -04:00
|
|
|
this.parseSingleWarningLine(LATEX_WARNING_REGEX)
|
2016-09-22 09:47:48 -04:00
|
|
|
} else if (this.currentLineIsHboxWarning()) {
|
2021-09-21 04:56:44 -04:00
|
|
|
this.parseHboxLine()
|
2016-09-22 09:47:48 -04:00
|
|
|
} else if (this.currentLineIsPackageWarning()) {
|
2021-09-21 04:56:44 -04:00
|
|
|
this.parseMultipleWarningLine()
|
2016-09-22 09:47:48 -04:00
|
|
|
} else {
|
2021-09-21 04:56:44 -04:00
|
|
|
this.parseParensForFilenames()
|
2016-09-22 09:47:48 -04:00
|
|
|
}
|
2014-02-12 05:23:40 -05:00
|
|
|
}
|
2016-09-22 09:47:48 -04:00
|
|
|
if (this.state === state.ERROR) {
|
2021-09-21 04:56:44 -04:00
|
|
|
this.currentError.content += this.log
|
|
|
|
.linesUpToNextMatchingLine(/^l\.[0-9]+/)
|
|
|
|
.join('\n')
|
|
|
|
this.currentError.content += '\n'
|
|
|
|
this.currentError.content += this.log
|
|
|
|
.linesUpToNextWhitespaceLine()
|
|
|
|
.join('\n')
|
|
|
|
this.currentError.content += '\n'
|
|
|
|
this.currentError.content += this.log
|
|
|
|
.linesUpToNextWhitespaceLine()
|
|
|
|
.join('\n')
|
|
|
|
this.currentError.raw += this.currentError.content
|
|
|
|
const lineNo = this.currentError.raw.match(/l\.([0-9]+)/)
|
|
|
|
if (lineNo && this.currentError.line === null) {
|
|
|
|
this.currentError.line = parseInt(lineNo[1], 10)
|
2016-09-22 09:47:48 -04:00
|
|
|
}
|
2021-09-21 04:56:44 -04:00
|
|
|
this.data.push(this.currentError)
|
|
|
|
this.state = state.NORMAL
|
2016-09-22 09:47:48 -04:00
|
|
|
}
|
|
|
|
}
|
2021-09-21 04:56:44 -04:00
|
|
|
return this.postProcess(this.data)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.currentLineIsError = function () {
|
|
|
|
return this.currentLine[0] === '!'
|
|
|
|
}
|
|
|
|
|
|
|
|
this.currentLineIsFileLineError = function () {
|
|
|
|
return /^\/.*:\d+: .*/.test(this.currentLine)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.currentLineIsRunawayArgument = function () {
|
|
|
|
return this.currentLine.match(/^Runaway argument/)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.currentLineIsWarning = function () {
|
|
|
|
return !!this.currentLine.match(LATEX_WARNING_REGEX)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.currentLineIsPackageWarning = function () {
|
|
|
|
return !!this.currentLine.match(PACKAGE_WARNING_REGEX)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.currentLineIsHboxWarning = function () {
|
|
|
|
return !!this.currentLine.match(HBOX_WARNING_REGEX)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.parseFileLineError = function () {
|
|
|
|
const result = this.currentLine.match(/^(\/.*):(\d+): (.*)/)
|
|
|
|
this.currentError = {
|
|
|
|
line: result[2],
|
|
|
|
file: result[1],
|
|
|
|
level: 'error',
|
|
|
|
message: result[3],
|
|
|
|
content: '',
|
|
|
|
raw: this.currentLine + '\n',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.parseRunawayArgumentError = function () {
|
2016-09-22 09:47:48 -04:00
|
|
|
this.currentError = {
|
|
|
|
line: null,
|
|
|
|
file: this.currentFilePath,
|
|
|
|
level: 'error',
|
|
|
|
message: this.currentLine,
|
|
|
|
content: '',
|
2021-09-21 04:56:44 -04:00
|
|
|
raw: this.currentLine + '\n',
|
|
|
|
}
|
|
|
|
this.currentError.content += this.log
|
|
|
|
.linesUpToNextWhitespaceLine()
|
|
|
|
.join('\n')
|
|
|
|
this.currentError.content += '\n'
|
|
|
|
this.currentError.content += this.log
|
|
|
|
.linesUpToNextWhitespaceLine()
|
|
|
|
.join('\n')
|
|
|
|
this.currentError.raw += this.currentError.content
|
|
|
|
const lineNo = this.currentError.raw.match(/l\.([0-9]+)/)
|
2016-09-22 09:47:48 -04:00
|
|
|
if (lineNo) {
|
2021-09-21 04:56:44 -04:00
|
|
|
this.currentError.line = parseInt(lineNo[1], 10)
|
2016-09-22 09:47:48 -04:00
|
|
|
}
|
2021-09-21 04:56:44 -04:00
|
|
|
return this.data.push(this.currentError)
|
|
|
|
}
|
|
|
|
|
|
|
|
this.parseSingleWarningLine = function (prefix_regex) {
|
|
|
|
const warningMatch = this.currentLine.match(prefix_regex)
|
2016-09-22 09:47:48 -04:00
|
|
|
if (!warningMatch) {
|
2021-09-21 04:56:44 -04:00
|
|
|
return
|
2016-09-22 09:47:48 -04:00
|
|
|
}
|
2021-09-21 04:56:44 -04:00
|
|
|
const warning = warningMatch[1]
|
|
|
|
const lineMatch = warning.match(LINES_REGEX)
|
|
|
|
const line = lineMatch ? parseInt(lineMatch[1], 10) : null
|
2016-09-22 09:47:48 -04:00
|
|
|
this.data.push({
|
2021-09-21 04:56:44 -04:00
|
|
|
line,
|
2016-09-22 09:47:48 -04:00
|
|
|
file: this.currentFilePath,
|
|
|
|
level: 'warning',
|
|
|
|
message: warning,
|
2021-09-21 04:56:44 -04:00
|
|
|
raw: warning,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
this.parseMultipleWarningLine = function () {
|
|
|
|
// Some package warnings are multiple lines, let's parse the first line
|
|
|
|
let warningMatch = this.currentLine.match(PACKAGE_WARNING_REGEX)
|
2016-09-22 09:47:48 -04:00
|
|
|
if (!warningMatch) {
|
2021-09-21 04:56:44 -04:00
|
|
|
return
|
2016-09-22 09:47:48 -04:00
|
|
|
}
|
2021-09-21 04:56:44 -04:00
|
|
|
// Something strange happened, return early
|
|
|
|
const warning_lines = [warningMatch[1]]
|
|
|
|
let lineMatch = this.currentLine.match(LINES_REGEX)
|
|
|
|
let line = lineMatch ? parseInt(lineMatch[1], 10) : null
|
|
|
|
const packageMatch = this.currentLine.match(PACKAGE_REGEX)
|
|
|
|
const packageName = packageMatch[1]
|
|
|
|
// Regex to get rid of the unnecesary (packagename) prefix in most multi-line warnings
|
|
|
|
const prefixRegex = new RegExp(
|
|
|
|
'(?:\\(' + packageName + '\\))*[\\s]*(.*)',
|
|
|
|
'i'
|
|
|
|
)
|
|
|
|
// After every warning message there's a blank line, let's use it
|
2016-09-22 09:47:48 -04:00
|
|
|
while (!!(this.currentLine = this.log.nextLine())) {
|
2021-09-21 04:56:44 -04:00
|
|
|
lineMatch = this.currentLine.match(LINES_REGEX)
|
|
|
|
line = lineMatch ? parseInt(lineMatch[1], 10) : line
|
|
|
|
warningMatch = this.currentLine.match(prefixRegex)
|
|
|
|
warning_lines.push(warningMatch[1])
|
2016-09-22 09:47:48 -04:00
|
|
|
}
|
2021-09-21 04:56:44 -04:00
|
|
|
const raw_message = warning_lines.join(' ')
|
2016-09-22 09:47:48 -04:00
|
|
|
this.data.push({
|
2021-09-21 04:56:44 -04:00
|
|
|
line,
|
2016-09-22 09:47:48 -04:00
|
|
|
file: this.currentFilePath,
|
|
|
|
level: 'warning',
|
|
|
|
message: raw_message,
|
2021-09-21 04:56:44 -04:00
|
|
|
raw: raw_message,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
this.parseHboxLine = function () {
|
|
|
|
const lineMatch = this.currentLine.match(LINES_REGEX)
|
|
|
|
const line = lineMatch ? parseInt(lineMatch[1], 10) : null
|
2016-09-22 09:47:48 -04:00
|
|
|
this.data.push({
|
2021-09-21 04:56:44 -04:00
|
|
|
line,
|
2016-09-22 09:47:48 -04:00
|
|
|
file: this.currentFilePath,
|
|
|
|
level: 'typesetting',
|
|
|
|
message: this.currentLine,
|
2021-09-21 04:56:44 -04:00
|
|
|
raw: this.currentLine,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if we're entering or leaving a new file in this line
|
|
|
|
|
|
|
|
this.parseParensForFilenames = function () {
|
|
|
|
const pos = this.currentLine.search(/\(|\)/)
|
2016-09-22 09:47:48 -04:00
|
|
|
if (pos !== -1) {
|
2021-09-21 04:56:44 -04:00
|
|
|
const token = this.currentLine[pos]
|
|
|
|
this.currentLine = this.currentLine.slice(pos + 1)
|
2016-09-22 09:47:48 -04:00
|
|
|
if (token === '(') {
|
2021-09-21 04:56:44 -04:00
|
|
|
const filePath = this.consumeFilePath()
|
2016-09-22 09:47:48 -04:00
|
|
|
if (filePath) {
|
2021-09-21 04:56:44 -04:00
|
|
|
this.currentFilePath = filePath
|
|
|
|
const newFile = {
|
2016-09-22 09:47:48 -04:00
|
|
|
path: filePath,
|
2021-09-21 04:56:44 -04:00
|
|
|
files: [],
|
|
|
|
}
|
|
|
|
this.fileStack.push(newFile)
|
|
|
|
this.currentFileList.push(newFile)
|
|
|
|
this.currentFileList = newFile.files
|
2016-09-22 09:47:48 -04:00
|
|
|
} else {
|
2021-09-21 04:56:44 -04:00
|
|
|
this.openParens++
|
2016-09-22 09:47:48 -04:00
|
|
|
}
|
|
|
|
} else if (token === ')') {
|
|
|
|
if (this.openParens > 0) {
|
2021-09-21 04:56:44 -04:00
|
|
|
this.openParens--
|
2016-09-22 09:47:48 -04:00
|
|
|
} else {
|
|
|
|
if (this.fileStack.length > 1) {
|
2021-09-21 04:56:44 -04:00
|
|
|
this.fileStack.pop()
|
|
|
|
const previousFile = this.fileStack[this.fileStack.length - 1]
|
|
|
|
this.currentFilePath = previousFile.path
|
|
|
|
this.currentFileList = previousFile.files
|
2014-02-12 05:23:40 -05:00
|
|
|
}
|
2016-09-22 09:47:48 -04:00
|
|
|
}
|
|
|
|
}
|
2021-09-21 04:56:44 -04:00
|
|
|
// else {
|
|
|
|
// Something has gone wrong but all we can do now is ignore it :(
|
|
|
|
// }
|
|
|
|
// Process the rest of the line
|
|
|
|
this.parseParensForFilenames()
|
2016-09-22 09:47:48 -04:00
|
|
|
}
|
2021-09-21 04:56:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
this.consumeFilePath = function () {
|
|
|
|
// Our heuristic for detecting file names are rather crude
|
|
|
|
// A file may not contain a space, or ) in it
|
|
|
|
// To be a file path it must have at least one /
|
2016-09-22 09:47:48 -04:00
|
|
|
if (!this.currentLine.match(/^\/?([^ \)]+\/)+/)) {
|
2021-09-21 04:56:44 -04:00
|
|
|
return false
|
2016-09-22 09:47:48 -04:00
|
|
|
}
|
2021-09-21 04:56:44 -04:00
|
|
|
const endOfFilePath = this.currentLine.search(RegExp(' |\\)'))
|
|
|
|
let path = undefined
|
2016-09-22 09:47:48 -04:00
|
|
|
if (endOfFilePath === -1) {
|
2021-09-21 04:56:44 -04:00
|
|
|
path = this.currentLine
|
|
|
|
this.currentLine = ''
|
2016-09-22 09:47:48 -04:00
|
|
|
} else {
|
2021-09-21 04:56:44 -04:00
|
|
|
path = this.currentLine.slice(0, endOfFilePath)
|
|
|
|
this.currentLine = this.currentLine.slice(endOfFilePath)
|
2016-09-22 09:47:48 -04:00
|
|
|
}
|
2021-09-21 04:56:44 -04:00
|
|
|
return path
|
|
|
|
}
|
|
|
|
|
|
|
|
this.postProcess = function (data) {
|
|
|
|
const all = []
|
|
|
|
const errors = []
|
|
|
|
const warnings = []
|
|
|
|
const typesetting = []
|
|
|
|
const hashes = []
|
|
|
|
|
|
|
|
const hashEntry = entry => entry.raw
|
|
|
|
|
|
|
|
let i = 0
|
2016-09-22 09:47:48 -04:00
|
|
|
while (i < data.length) {
|
|
|
|
if (this.ignoreDuplicates && hashes.indexOf(hashEntry(data[i])) > -1) {
|
2021-09-21 04:56:44 -04:00
|
|
|
i++
|
|
|
|
continue
|
2016-09-22 09:47:48 -04:00
|
|
|
}
|
|
|
|
if (data[i].level === 'error') {
|
2021-09-21 04:56:44 -04:00
|
|
|
errors.push(data[i])
|
2016-09-22 09:47:48 -04:00
|
|
|
} else if (data[i].level === 'typesetting') {
|
2021-09-21 04:56:44 -04:00
|
|
|
typesetting.push(data[i])
|
2016-09-22 09:47:48 -04:00
|
|
|
} else if (data[i].level === 'warning') {
|
2021-09-21 04:56:44 -04:00
|
|
|
warnings.push(data[i])
|
2014-02-12 05:23:40 -05:00
|
|
|
}
|
2021-09-21 04:56:44 -04:00
|
|
|
all.push(data[i])
|
|
|
|
hashes.push(hashEntry(data[i]))
|
|
|
|
i++
|
2016-09-22 09:47:48 -04:00
|
|
|
}
|
|
|
|
return {
|
2021-09-21 04:56:44 -04:00
|
|
|
errors,
|
|
|
|
warnings,
|
|
|
|
typesetting,
|
|
|
|
all,
|
|
|
|
files: this.rootFileList,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}.call(LatexParser.prototype))
|
|
|
|
|
|
|
|
LatexParser.parse = (text, options) => new LatexParser(text, options).parse()
|
|
|
|
|
|
|
|
return LatexParser
|
|
|
|
})
|