UNPKG

2.02 kBJavaScriptView Raw
1/**
2 @overview Extracts javascript source and comments from HTML files
3 @module plugins/parseHtml
4 @author Aleksandar Rodic <aleksandar.xyz@gmail.com>
5 */
6'use strict';
7
8exports.handlers = {
9 /**
10 * Extracts HTML and javascript comments from html files
11 * HTML comments are converted into javascript comments
12 * @param e
13 * @param e.filename
14 * @param e.source
15 */
16 beforeParse: function(e) {
17 var jsSource = '';
18
19 // matches text between /** and */ inclusive and <!-- and --> inclusive
20 var docCommentRegex = new RegExp('<!--([\\s\\S]*?)-->', 'g');
21
22 // acquire all script doc comments
23 var docComments = e.source.match(docCommentRegex) || [];
24
25 // each match represents a single block of doc comments
26 docComments.forEach(function(m) {
27 // unify line ends, remove all comment characters, split into individual lines
28 var lines = m.replace(/\r\n/g, '\n').replace(/^\s*\/\*\*|^\s*\*\/|^\s*\* ?|^\s+|^\s*\<\!-\-\s*|\s*\-\-\>/gm, '').split('\n');
29 jsSource += '/**\n';
30 for (var i = 0; i < lines.length; i++) {
31 if (lines[i] !== '') {
32 jsSource += '* ' + lines[i] + '\n';
33 }
34 }
35 jsSource += '*/\n\n';
36
37 });
38
39 // matches text between <script> and </script>
40 var scriptRegex = new RegExp('<script[\\s\\S]*?\>([\\s\\S]*?)\<\\/script\>', 'g');
41
42 // acquire all script doc comments
43 var scriptText = e.source.match(scriptRegex) || [];
44
45 scriptText.forEach(function(m) {
46 // remove script tags, split into individual lines
47 var lines = m.replace(/\r\n/g, '\n').replace(/<script[\s\S]*?>|<\/script>/gmi, '').split('\n');
48 for (var i = 0; i < lines.length; i++) {
49 if (lines[i] !== '') {
50 jsSource += lines[i] + '\n';
51 }
52 }
53 });
54
55 e.source = jsSource || e.source;
56
57 }
58};