bib-log-parser.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. // Generated by CoffeeScript 1.10.0
  2. define(function() {
  3. var BAD_CROSS_REFERENCE_REGEX, BibLogParser, LINE_SPLITTER_REGEX, MESSAGE_LEVELS, MULTILINE_COMMAND_ERROR_REGEX, MULTILINE_ERROR_REGEX, MULTILINE_WARNING_REGEX, SINGLELINE_WARNING_REGEX, consume, errorParsers, warningParsers;
  4. LINE_SPLITTER_REGEX = /^\[(\d+)].*>\s(INFO|WARN|ERROR)\s-\s(.*)$/;
  5. MESSAGE_LEVELS = {
  6. "INFO": "info",
  7. "WARN": "warning",
  8. "ERROR": "error"
  9. };
  10. BibLogParser = function(text, options) {
  11. if (typeof text !== 'string') {
  12. throw new Error("BibLogParser Error: text parameter must be a string");
  13. }
  14. this.text = text.replace(/(\r\n)|\r/g, '\n');
  15. this.options = options || {};
  16. this.lines = text.split('\n');
  17. };
  18. consume = function(logText, regex, process) {
  19. var iterationCount, match, newEntry, re, result, text;
  20. text = logText;
  21. result = [];
  22. re = regex;
  23. iterationCount = 0;
  24. while (match = re.exec(text)) {
  25. iterationCount += 1;
  26. newEntry = process(match);
  27. // Too many log entries can cause browser crashes
  28. // Construct a too many files error from the last match
  29. const maxErrors = 100
  30. if (iterationCount >= maxErrors) {
  31. const level = newEntry.level + "s"
  32. newEntry.message = `Over ${maxErrors} ${level} returned. Download raw logs to see full list`;
  33. newEntry.line = undefined;
  34. result.unshift(newEntry)
  35. return [result, ""];
  36. }
  37. result.push(newEntry);
  38. text = (match.input.slice(0, match.index)) + (match.input.slice(match.index + match[0].length + 1, match.input.length));
  39. }
  40. return [result, text];
  41. };
  42. MULTILINE_WARNING_REGEX = /^Warning--(.+)\n--line (\d+) of file (.+)$/m;
  43. SINGLELINE_WARNING_REGEX = /^Warning--(.+)$/m;
  44. MULTILINE_ERROR_REGEX = /^(.*)---line (\d+) of file (.*)\n([^]+?)\nI'm skipping whatever remains of this entry$/m;
  45. BAD_CROSS_REFERENCE_REGEX = /^(A bad cross reference---entry ".+?"\nrefers to entry.+?, which doesn't exist)$/m;
  46. MULTILINE_COMMAND_ERROR_REGEX = /^(.*)\n?---line (\d+) of file (.*)\n([^]+?)\nI'm skipping whatever remains of this command$/m;
  47. // Errors hit in BST file have a slightly different format
  48. BST_ERROR_REGEX = /^(.*?)\nwhile executing---line (\d+) of file (.*)/m;
  49. warningParsers = [
  50. [
  51. MULTILINE_WARNING_REGEX, function(match) {
  52. var fileName, fullMatch, lineNumber, message;
  53. fullMatch = match[0], message = match[1], lineNumber = match[2], fileName = match[3];
  54. return {
  55. file: fileName,
  56. level: "warning",
  57. message: message,
  58. line: lineNumber,
  59. raw: fullMatch
  60. };
  61. }
  62. ], [
  63. SINGLELINE_WARNING_REGEX, function(match) {
  64. var fullMatch, message;
  65. fullMatch = match[0], message = match[1];
  66. return {
  67. file: '',
  68. level: "warning",
  69. message: message,
  70. line: '',
  71. raw: fullMatch
  72. };
  73. }
  74. ]
  75. ];
  76. errorParsers = [
  77. [
  78. MULTILINE_ERROR_REGEX, function(match) {
  79. var fileName, firstMessage, fullMatch, lineNumber, secondMessage;
  80. fullMatch = match[0], firstMessage = match[1], lineNumber = match[2], fileName = match[3], secondMessage = match[4];
  81. return {
  82. file: fileName,
  83. level: "error",
  84. message: firstMessage + '\n' + secondMessage,
  85. line: lineNumber,
  86. raw: fullMatch
  87. };
  88. }
  89. ], [
  90. BAD_CROSS_REFERENCE_REGEX, function(match) {
  91. var fullMatch, message;
  92. fullMatch = match[0], message = match[1];
  93. return {
  94. file: '',
  95. level: "error",
  96. message: message,
  97. line: '',
  98. raw: fullMatch
  99. };
  100. }
  101. ], [
  102. MULTILINE_COMMAND_ERROR_REGEX, function(match) {
  103. var fileName, firstMessage, fullMatch, lineNumber, secondMessage;
  104. fullMatch = match[0], firstMessage = match[1], lineNumber = match[2], fileName = match[3], secondMessage = match[4];
  105. return {
  106. file: fileName,
  107. level: "error",
  108. message: firstMessage + '\n' + secondMessage,
  109. line: lineNumber,
  110. raw: fullMatch
  111. };
  112. }
  113. ],[
  114. BST_ERROR_REGEX, function(match) {
  115. var fileName, firstMessage, fullMatch, lineNumber, secondMessage;
  116. fullMatch = match[0], firstMessage = match[1], lineNumber = match[2], fileName = match[3];
  117. return {
  118. file: fileName,
  119. level: "error",
  120. message: firstMessage,
  121. line: lineNumber,
  122. raw: fullMatch
  123. };
  124. }
  125. ]
  126. ];
  127. (function() {
  128. this.parseBibtex = function() {
  129. var allErrors, allWarnings, ref, ref1, remainingText, result;
  130. result = {
  131. all: [],
  132. errors: [],
  133. warnings: [],
  134. files: [],
  135. typesetting: []
  136. };
  137. ref = warningParsers.reduce(function(accumulator, parser) {
  138. var _remainingText, currentWarnings, process, ref, regex, text, warnings;
  139. currentWarnings = accumulator[0], text = accumulator[1];
  140. regex = parser[0], process = parser[1];
  141. ref = consume(text, regex, process), warnings = ref[0], _remainingText = ref[1];
  142. return [currentWarnings.concat(warnings), _remainingText];
  143. }, [[], this.text]), allWarnings = ref[0], remainingText = ref[1];
  144. ref1 = errorParsers.reduce(function(accumulator, parser) {
  145. var _remainingText, currentErrors, errors, process, ref1, regex, text;
  146. currentErrors = accumulator[0], text = accumulator[1];
  147. regex = parser[0], process = parser[1];
  148. ref1 = consume(text, regex, process), errors = ref1[0], _remainingText = ref1[1];
  149. return [currentErrors.concat(errors), _remainingText];
  150. }, [[], remainingText]), allErrors = ref1[0], remainingText = ref1[1];
  151. result.warnings = allWarnings;
  152. result.errors = allErrors;
  153. result.all = allWarnings.concat(allErrors);
  154. return result;
  155. };
  156. this.parseBiber = function() {
  157. var result;
  158. result = {
  159. all: [],
  160. errors: [],
  161. warnings: [],
  162. files: [],
  163. typesetting: []
  164. };
  165. this.lines.forEach(function(line) {
  166. var _, fileName, fullLine, lineMatch, lineNumber, match, message, messageType, newEntry, realMessage;
  167. match = line.match(LINE_SPLITTER_REGEX);
  168. if (match) {
  169. fullLine = match[0], lineNumber = match[1], messageType = match[2], message = match[3];
  170. newEntry = {
  171. file: '',
  172. level: MESSAGE_LEVELS[messageType] || "INFO",
  173. message: message,
  174. line: '',
  175. raw: fullLine
  176. };
  177. lineMatch = newEntry.message.match(/^BibTeX subsystem: \/.+\/(\w+\.\w+)_.+, line (\d+), (.+)$/);
  178. if (lineMatch && lineMatch.length === 4) {
  179. _ = lineMatch[0], fileName = lineMatch[1], lineNumber = lineMatch[2], realMessage = lineMatch[3];
  180. newEntry.file = fileName;
  181. newEntry.line = lineNumber;
  182. newEntry.message = realMessage;
  183. }
  184. result.all.push(newEntry);
  185. switch (newEntry.level) {
  186. case 'error':
  187. return result.errors.push(newEntry);
  188. case 'warning':
  189. return result.warnings.push(newEntry);
  190. }
  191. }
  192. });
  193. return result;
  194. };
  195. return this.parse = function() {
  196. var firstLine;
  197. firstLine = this.lines[0];
  198. if (firstLine.match(/^.*INFO - This is Biber.*$/)) {
  199. return this.parseBiber();
  200. } else if (firstLine.match(/^This is BibTeX, Version.+$/)) {
  201. return this.parseBibtex();
  202. } else {
  203. throw new Error("BibLogParser Error: cannot determine whether text is biber or bibtex output");
  204. }
  205. };
  206. }).call(BibLogParser.prototype);
  207. BibLogParser.parse = function(text, options) {
  208. return new BibLogParser(text, options).parse();
  209. };
  210. return BibLogParser;
  211. });