latex-log-parser.js 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // Generated by CoffeeScript 1.12.7
  2. define(function() {
  3. var HBOX_WARNING_REGEX, LATEX_WARNING_REGEX, LINES_REGEX, LOG_WRAP_LIMIT, LatexParser, LogText, PACKAGE_REGEX, PACKAGE_WARNING_REGEX, state;
  4. LOG_WRAP_LIMIT = 79;
  5. LATEX_WARNING_REGEX = /^LaTeX Warning: (.*)$/;
  6. HBOX_WARNING_REGEX = /^(Over|Under)full \\(v|h)box/;
  7. PACKAGE_WARNING_REGEX = /^(Package \b.+\b Warning:.*)$/;
  8. LINES_REGEX = /lines? ([0-9]+)/;
  9. PACKAGE_REGEX = /^Package (\b.+\b) Warning/;
  10. LogText = function(text) {
  11. var i, wrappedLines;
  12. this.text = text.replace(/(\r\n)|\r/g, '\n');
  13. wrappedLines = this.text.split('\n');
  14. this.lines = [wrappedLines[0]];
  15. i = 1;
  16. while (i < wrappedLines.length) {
  17. if (wrappedLines[i - 1].length === LOG_WRAP_LIMIT && wrappedLines[i - 1].slice(-3) !== '...') {
  18. this.lines[this.lines.length - 1] += wrappedLines[i];
  19. } else {
  20. this.lines.push(wrappedLines[i]);
  21. }
  22. i++;
  23. }
  24. this.row = 0;
  25. };
  26. (function() {
  27. this.nextLine = function() {
  28. this.row++;
  29. if (this.row >= this.lines.length) {
  30. return false;
  31. } else {
  32. return this.lines[this.row];
  33. }
  34. };
  35. this.rewindLine = function() {
  36. this.row--;
  37. };
  38. this.linesUpToNextWhitespaceLine = function() {
  39. return this.linesUpToNextMatchingLine(/^ *$/);
  40. };
  41. this.linesUpToNextMatchingLine = function(match) {
  42. var lines, nextLine;
  43. lines = [];
  44. nextLine = this.nextLine();
  45. if (nextLine !== false) {
  46. lines.push(nextLine);
  47. }
  48. while (nextLine !== false && !nextLine.match(match) && nextLine !== false) {
  49. nextLine = this.nextLine();
  50. if (nextLine !== false) {
  51. lines.push(nextLine);
  52. }
  53. }
  54. return lines;
  55. };
  56. }).call(LogText.prototype);
  57. state = {
  58. NORMAL: 0,
  59. ERROR: 1
  60. };
  61. LatexParser = function(text, options) {
  62. this.log = new LogText(text);
  63. this.state = state.NORMAL;
  64. options = options || {};
  65. this.fileBaseNames = options.fileBaseNames || [/compiles/, /\/usr\/local/];
  66. this.ignoreDuplicates = options.ignoreDuplicates;
  67. this.data = [];
  68. this.fileStack = [];
  69. this.currentFileList = this.rootFileList = [];
  70. this.openParens = 0;
  71. };
  72. (function() {
  73. this.parse = function() {
  74. var lineNo;
  75. while ((this.currentLine = this.log.nextLine()) !== false) {
  76. if (this.state === state.NORMAL) {
  77. if (this.currentLineIsError()) {
  78. this.state = state.ERROR;
  79. this.currentError = {
  80. line: null,
  81. file: this.currentFilePath,
  82. level: 'error',
  83. message: this.currentLine.slice(2),
  84. content: '',
  85. raw: this.currentLine + '\n'
  86. };
  87. } else if (this.currentLineIsRunawayArgument()) {
  88. this.parseRunawayArgumentError();
  89. } else if (this.currentLineIsWarning()) {
  90. this.parseSingleWarningLine(LATEX_WARNING_REGEX);
  91. } else if (this.currentLineIsHboxWarning()) {
  92. this.parseHboxLine();
  93. } else if (this.currentLineIsPackageWarning()) {
  94. this.parseMultipleWarningLine();
  95. } else {
  96. this.parseParensForFilenames();
  97. }
  98. }
  99. if (this.state === state.ERROR) {
  100. this.currentError.content += this.log.linesUpToNextMatchingLine(/^l\.[0-9]+/).join('\n');
  101. this.currentError.content += '\n';
  102. this.currentError.content += this.log.linesUpToNextWhitespaceLine().join('\n');
  103. this.currentError.content += '\n';
  104. this.currentError.content += this.log.linesUpToNextWhitespaceLine().join('\n');
  105. this.currentError.raw += this.currentError.content;
  106. lineNo = this.currentError.raw.match(/l\.([0-9]+)/);
  107. if (lineNo) {
  108. this.currentError.line = parseInt(lineNo[1], 10);
  109. }
  110. this.data.push(this.currentError);
  111. this.state = state.NORMAL;
  112. }
  113. }
  114. return this.postProcess(this.data);
  115. };
  116. this.currentLineIsError = function() {
  117. return this.currentLine[0] === '!';
  118. };
  119. this.currentLineIsRunawayArgument = function() {
  120. return this.currentLine.match(/^Runaway argument/);
  121. };
  122. this.currentLineIsWarning = function() {
  123. return !!this.currentLine.match(LATEX_WARNING_REGEX);
  124. };
  125. this.currentLineIsPackageWarning = function() {
  126. return !!this.currentLine.match(PACKAGE_WARNING_REGEX);
  127. };
  128. this.currentLineIsHboxWarning = function() {
  129. return !!this.currentLine.match(HBOX_WARNING_REGEX);
  130. };
  131. this.parseRunawayArgumentError = function() {
  132. var lineNo;
  133. this.currentError = {
  134. line: null,
  135. file: this.currentFilePath,
  136. level: 'error',
  137. message: this.currentLine,
  138. content: '',
  139. raw: this.currentLine + '\n'
  140. };
  141. this.currentError.content += this.log.linesUpToNextWhitespaceLine().join('\n');
  142. this.currentError.content += '\n';
  143. this.currentError.content += this.log.linesUpToNextWhitespaceLine().join('\n');
  144. this.currentError.raw += this.currentError.content;
  145. lineNo = this.currentError.raw.match(/l\.([0-9]+)/);
  146. if (lineNo) {
  147. this.currentError.line = parseInt(lineNo[1], 10);
  148. }
  149. return this.data.push(this.currentError);
  150. };
  151. this.parseSingleWarningLine = function(prefix_regex) {
  152. var line, lineMatch, warning, warningMatch;
  153. warningMatch = this.currentLine.match(prefix_regex);
  154. if (!warningMatch) {
  155. return;
  156. }
  157. warning = warningMatch[1];
  158. lineMatch = warning.match(LINES_REGEX);
  159. line = lineMatch ? parseInt(lineMatch[1], 10) : null;
  160. this.data.push({
  161. line: line,
  162. file: this.currentFilePath,
  163. level: 'warning',
  164. message: warning,
  165. raw: warning
  166. });
  167. };
  168. this.parseMultipleWarningLine = function() {
  169. var line, lineMatch, packageMatch, packageName, prefixRegex, raw_message, warningMatch, warning_lines;
  170. warningMatch = this.currentLine.match(PACKAGE_WARNING_REGEX);
  171. if (!warningMatch) {
  172. return;
  173. }
  174. warning_lines = [warningMatch[1]];
  175. lineMatch = this.currentLine.match(LINES_REGEX);
  176. line = lineMatch ? parseInt(lineMatch[1], 10) : null;
  177. packageMatch = this.currentLine.match(PACKAGE_REGEX);
  178. packageName = packageMatch[1];
  179. prefixRegex = new RegExp('(?:\\(' + packageName + '\\))*[\\s]*(.*)', 'i');
  180. while (!!(this.currentLine = this.log.nextLine())) {
  181. lineMatch = this.currentLine.match(LINES_REGEX);
  182. line = lineMatch ? parseInt(lineMatch[1], 10) : line;
  183. warningMatch = this.currentLine.match(prefixRegex);
  184. warning_lines.push(warningMatch[1]);
  185. }
  186. raw_message = warning_lines.join(' ');
  187. this.data.push({
  188. line: line,
  189. file: this.currentFilePath,
  190. level: 'warning',
  191. message: raw_message,
  192. raw: raw_message
  193. });
  194. };
  195. this.parseHboxLine = function() {
  196. var line, lineMatch;
  197. lineMatch = this.currentLine.match(LINES_REGEX);
  198. line = lineMatch ? parseInt(lineMatch[1], 10) : null;
  199. this.data.push({
  200. line: line,
  201. file: this.currentFilePath,
  202. level: 'typesetting',
  203. message: this.currentLine,
  204. raw: this.currentLine
  205. });
  206. };
  207. this.parseParensForFilenames = function() {
  208. var filePath, newFile, pos, previousFile, token;
  209. pos = this.currentLine.search(/\(|\)/);
  210. if (pos !== -1) {
  211. token = this.currentLine[pos];
  212. this.currentLine = this.currentLine.slice(pos + 1);
  213. if (token === '(') {
  214. filePath = this.consumeFilePath();
  215. if (filePath) {
  216. this.currentFilePath = filePath;
  217. newFile = {
  218. path: filePath,
  219. files: []
  220. };
  221. this.fileStack.push(newFile);
  222. this.currentFileList.push(newFile);
  223. this.currentFileList = newFile.files;
  224. } else {
  225. this.openParens++;
  226. }
  227. } else if (token === ')') {
  228. if (this.openParens > 0) {
  229. this.openParens--;
  230. } else {
  231. if (this.fileStack.length > 1) {
  232. this.fileStack.pop();
  233. previousFile = this.fileStack[this.fileStack.length - 1];
  234. this.currentFilePath = previousFile.path;
  235. this.currentFileList = previousFile.files;
  236. }
  237. }
  238. }
  239. this.parseParensForFilenames();
  240. }
  241. };
  242. this.consumeFilePath = function() {
  243. var endOfFilePath, path;
  244. if (!this.currentLine.match(/^\/?([^ \)]+\/)+/)) {
  245. return false;
  246. }
  247. endOfFilePath = this.currentLine.search(RegExp(' |\\)'));
  248. path = void 0;
  249. if (endOfFilePath === -1) {
  250. path = this.currentLine;
  251. this.currentLine = '';
  252. } else {
  253. path = this.currentLine.slice(0, endOfFilePath);
  254. this.currentLine = this.currentLine.slice(endOfFilePath);
  255. }
  256. return path;
  257. };
  258. return this.postProcess = function(data) {
  259. var all, errors, hashEntry, hashes, i, typesetting, warnings;
  260. all = [];
  261. errors = [];
  262. warnings = [];
  263. typesetting = [];
  264. hashes = [];
  265. hashEntry = function(entry) {
  266. return entry.raw;
  267. };
  268. i = 0;
  269. while (i < data.length) {
  270. if (this.ignoreDuplicates && hashes.indexOf(hashEntry(data[i])) > -1) {
  271. i++;
  272. continue;
  273. }
  274. if (data[i].level === 'error') {
  275. errors.push(data[i]);
  276. } else if (data[i].level === 'typesetting') {
  277. typesetting.push(data[i]);
  278. } else if (data[i].level === 'warning') {
  279. warnings.push(data[i]);
  280. }
  281. all.push(data[i]);
  282. hashes.push(hashEntry(data[i]));
  283. i++;
  284. }
  285. return {
  286. errors: errors,
  287. warnings: warnings,
  288. typesetting: typesetting,
  289. all: all,
  290. files: this.rootFileList
  291. };
  292. };
  293. }).call(LatexParser.prototype);
  294. LatexParser.parse = function(text, options) {
  295. return new LatexParser(text, options).parse();
  296. };
  297. return LatexParser;
  298. });