UNPKG

2.24 kBJavaScriptView Raw
1'use strict';
2var eol = require('eol');
3var commentsUtil = require('../utils/comments');
4
5module.exports = function (params) {
6 params = params || {};
7 var regex = commentsUtil.getRegex(params.customTags);
8 var hamlRubyComment = new RegExp('^\\s*-#' + regex + '$', 'mig');
9 var hamlHtmlComment = new RegExp('^\\s*/' + regex + '$', 'mig');
10 var erbComment = new RegExp('<%#' + regex + '%>', 'mig');
11 var htmlComment = new RegExp('<!--' + regex + '-->', 'mig');
12
13 return function parse(contents, file) {
14 var comments = [];
15
16 eol.split(contents).forEach(function (line, index) {
17
18 var hamlRubyCommentMatch = hamlRubyComment.exec(line);
19 var hamlHtmlCommentMatch = hamlHtmlComment.exec(line);
20 var erbCommentMatch = erbComment.exec(line);
21 var htmlCommentMatch = htmlComment.exec(line);
22 var comment;
23
24 while (hamlRubyCommentMatch) {
25 comment = commentsUtil.prepareComment(hamlRubyCommentMatch, index + 1, file);
26 if (!comment) {
27 break;
28 }
29 comments.push(comment);
30 hamlRubyCommentMatch = hamlRubyComment.exec(line);
31 }
32
33 while (hamlHtmlCommentMatch) {
34 comment = commentsUtil.prepareComment(hamlHtmlCommentMatch, index + 1, file);
35 if (!comment) {
36 break;
37 }
38 comments.push(comment);
39
40 hamlHtmlCommentMatch = hamlHtmlComment.exec(line);
41 }
42
43 while (erbCommentMatch) {
44 comment = commentsUtil.prepareComment(erbCommentMatch, index + 1, file);
45 if (!comment) {
46 break;
47 }
48 comments.push(comment);
49 erbCommentMatch = erbComment.exec(line);
50 }
51
52 while (htmlCommentMatch) {
53 comment = commentsUtil.prepareComment(htmlCommentMatch, index + 1, file);
54 if (!comment) {
55 break;
56 }
57 comments.push(comment);
58 htmlCommentMatch = htmlComment.exec(line);
59 }
60 });
61 return comments;
62 };
63};