bib-log-parser.js 7.4 KB

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