latex-log-parser.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. define(function() {
  2. var logWrapLimit = 79;
  3. var LogText = function(text) {
  4. this.text = text.replace(/(\r\n)|\r/g, "\n");
  5. // Join any lines which look like they have wrapped.
  6. var wrappedLines = this.text.split("\n");
  7. this.lines = [wrappedLines[0]];
  8. for (var i = 1; i < wrappedLines.length; i++) {
  9. // If the previous line is as long as the wrap limit then
  10. // append this line to it.
  11. // Some lines end with ... when LaTeX knows it's hit the limit
  12. // These shouldn't be wrapped.
  13. if (wrappedLines[i-1].length == logWrapLimit && wrappedLines[i-1].slice(-3) != "...") {
  14. this.lines[this.lines.length - 1] += wrappedLines[i];
  15. } else {
  16. this.lines.push(wrappedLines[i]);
  17. }
  18. };
  19. this.row = 0;
  20. };
  21. (function() {
  22. this.nextLine = function() {
  23. this.row++;
  24. if (this.row >= this.lines.length) {
  25. return false;
  26. } else {
  27. return this.lines[this.row];
  28. }
  29. };
  30. this.rewindLine = function() {
  31. this.row--;
  32. };
  33. this.linesUpToNextWhitespaceLine = function() {
  34. return this.linesUpToNextMatchingLine(/^ *$/);
  35. };
  36. this.linesUpToNextMatchingLine = function(match) {
  37. var lines = [];
  38. var nextLine = this.nextLine();
  39. if (nextLine !== false) {
  40. lines.push(nextLine);
  41. }
  42. while (nextLine !== false && !nextLine.match(match) && nextLine !== false) {
  43. nextLine = this.nextLine();
  44. if (nextLine !== false) {
  45. lines.push(nextLine);
  46. }
  47. }
  48. return lines;
  49. }
  50. }).call(LogText.prototype);
  51. var state = {
  52. NORMAL : 0,
  53. ERROR : 1
  54. }
  55. var LatexParser = function(text, options) {
  56. this.log = new LogText(text);
  57. this.state = state.NORMAL;
  58. options = options || {};
  59. this.fileBaseNames = options.fileBaseNames || [/compiles/, /\/usr\/local/];
  60. this.ignoreDuplicates = options.ignoreDuplicates;
  61. this.data = [];
  62. this.fileStack = [];
  63. this.currentFileList = this.rootFileList = [];
  64. this.openParens = 0;
  65. };
  66. (function() {
  67. this.parse = function() {
  68. while ((this.currentLine = this.log.nextLine()) !== false) {
  69. if (this.state == state.NORMAL) {
  70. if (this.currentLineIsError()) {
  71. this.state = state.ERROR;
  72. this.currentError = {
  73. line : null,
  74. file : this.currentFilePath,
  75. level : "error",
  76. message : this.currentLine.slice(2),
  77. content : "",
  78. raw : this.currentLine + "\n"
  79. }
  80. } else if (this.currentLineIsWarning()) {
  81. this.parseWarningLine();
  82. } else if (this.currentLineIsHboxWarning()){
  83. this.parseHboxLine();
  84. } else {
  85. this.parseParensForFilenames();
  86. }
  87. }
  88. if (this.state == state.ERROR) {
  89. this.currentError.content += this.log.linesUpToNextMatchingLine(/^l\.[0-9]+/).join("\n");
  90. this.currentError.content += "\n";
  91. this.currentError.content += this.log.linesUpToNextWhitespaceLine().join("\n");
  92. this.currentError.content += "\n";
  93. this.currentError.content += this.log.linesUpToNextWhitespaceLine().join("\n");
  94. this.currentError.raw += this.currentError.content;
  95. var lineNo = this.currentError.raw.match(/l\.([0-9]+)/);
  96. if (lineNo) {
  97. this.currentError.line = parseInt(lineNo[1], 10);
  98. }
  99. this.data.push(this.currentError);
  100. this.state = state.NORMAL;
  101. }
  102. }
  103. return this.postProcess(this.data);
  104. };
  105. this.currentLineIsError = function() {
  106. return this.currentLine[0] == "!";
  107. };
  108. this.currentLineIsWarning = function() {
  109. return !!(this.currentLine.match(/^LaTeX Warning: /));
  110. };
  111. this.currentLineIsHboxWarning = function() {
  112. return !!(this.currentLine.match(/^(Over|Under)full \\(v|h)box/));
  113. };
  114. this.parseWarningLine = function() {
  115. var warningMatch = this.currentLine.match(/^LaTeX Warning: (.*)$/);
  116. if (!warningMatch) return;
  117. var warning = warningMatch[1];
  118. var lineMatch = warning.match(/line ([0-9]+)/);
  119. var line = lineMatch ? parseInt(lineMatch[1], 10) : null;
  120. this.data.push({
  121. line : line,
  122. file : this.currentFilePath,
  123. level : "warning",
  124. message : warning,
  125. raw : warning
  126. });
  127. };
  128. this.parseHboxLine = function() {
  129. var lineMatch = this.currentLine.match(/lines? ([0-9]+)/);
  130. var line = lineMatch ? parseInt(lineMatch[1], 10) : null;
  131. this.data.push({
  132. line : line,
  133. file : this.currentFilePath,
  134. level : "typesetting",
  135. message : this.currentLine,
  136. raw : this.currentLine
  137. });
  138. };
  139. // Check if we're entering or leaving a new file in this line
  140. this.parseParensForFilenames = function() {
  141. var pos = this.currentLine.search(/\(|\)/);
  142. if (pos != -1) {
  143. var token = this.currentLine[pos]
  144. this.currentLine = this.currentLine.slice(pos + 1);
  145. if (token == "(") {
  146. var filePath = this.consumeFilePath();
  147. if (filePath) {
  148. this.currentFilePath = filePath;
  149. var newFile = {
  150. path : filePath,
  151. files : []
  152. };
  153. this.fileStack.push(newFile);
  154. this.currentFileList.push(newFile);
  155. this.currentFileList = newFile.files;
  156. } else {
  157. this.openParens++;
  158. }
  159. } else if (token == ")") {
  160. if (this.openParens > 0) {
  161. this.openParens--;
  162. } else {
  163. if (this.fileStack.length > 1) {
  164. this.fileStack.pop();
  165. var previousFile = this.fileStack[this.fileStack.length - 1];
  166. this.currentFilePath = previousFile.path;
  167. this.currentFileList = previousFile.files;
  168. }
  169. // else {
  170. // Something has gone wrong but all we can do now is ignore it :(
  171. // }
  172. }
  173. }
  174. // Process the rest of the line
  175. this.parseParensForFilenames();
  176. }
  177. };
  178. this.consumeFilePath = function() {
  179. // Our heuristic for detecting file names are rather crude
  180. // A file may not contain a space, or ) in it
  181. // To be a file path it must have at least one /
  182. if (!this.currentLine.match(/^\/?([^ \)]+\/)+/)) {
  183. return false;
  184. }
  185. var endOfFilePath = this.currentLine.search(/ |\)/);
  186. var path;
  187. if (endOfFilePath == -1) {
  188. path = this.currentLine;
  189. this.currentLine = "";
  190. } else {
  191. path = this.currentLine.slice(0, endOfFilePath);
  192. this.currentLine = this.currentLine.slice(endOfFilePath);
  193. }
  194. return path;
  195. };
  196. this.postProcess = function(data) {
  197. var all = []
  198. var errors = [];
  199. var warnings = [];
  200. var typesetting = [];
  201. var hashes = [];
  202. function hashEntry(entry) {
  203. return entry.raw;
  204. }
  205. for (var i = 0; i < data.length; i++) {
  206. if (this.ignoreDuplicates && hashes.indexOf(hashEntry(data[i])) > -1) {
  207. continue;
  208. }
  209. if (data[i].level == "error") {
  210. errors.push(data[i]);
  211. } else if (data[i].level == "typesetting") {
  212. typesetting.push(data[i]);
  213. } else if (data[i].level == "warning") {
  214. warnings.push(data[i]);
  215. }
  216. all.push(data[i]);
  217. hashes.push(hashEntry(data[i]));
  218. }
  219. return {
  220. errors : errors,
  221. warnings : warnings,
  222. typesetting : typesetting,
  223. all : all,
  224. files : this.rootFileList
  225. }
  226. }
  227. }).call(LatexParser.prototype);
  228. LatexParser.parse = function(text, options) {
  229. return (new LatexParser(text, options)).parse()
  230. }
  231. return LatexParser;
  232. })