FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node-htmlprocessor/lib/parser.js at master · dciccale/node-htmlprocessor · GitHub
dciccale
/
node-htmlprocessor
Public
Notifications
You must be signed in to change notification settings
Fork
20
Star
47
Code
Issues
0
Pull requests
1
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
node-htmlprocessor
/
lib
/
parser.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
113 lines (94 loc) · 2.87 KB
Breadcrumbs
node-htmlprocessor
/
lib
/
parser.js
Copy path
File metadata and controls
113 lines (94 loc) · 2.87 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* node-htmlprocessor
* https://github.com/dciccale/node-htmlprocessor
*
* Copyright (c) 2013-2023 Denis Ciccale (@dciccale)
* Licensed under the MIT license.
* https://github.com/dciccale/node-htmlprocessor/blob/master/LICENSE-MIT
*/
'use strict'
;
var
fs
=
require
(
'fs'
)
;
module
.
exports
=
Parser
;
function
Parser
(
options
)
{
this
.
options
=
options
||
{
}
;
this
.
listFile
=
null
;
if
(
this
.
options
&&
this
.
options
.
list
)
{
// Set our log file as a writestream variable with the 'a' flag
this
.
listFile
=
fs
.
createWriteStream
(
this
.
options
.
list
,
{
flags
:
'a'
,
encoding
:
'utf8'
,
mode
:
parseInt
(
'744'
,
8
)
}
)
;
}
// Set up regular expressions
/*
* <!-- build:<type>[:target] [value] -->
* - type (required) js, css, attr, remove, template, include
* - target|attribute (optional) i.e. dev, prod, release or attributes like [href] [src]
* - value (optional) i.e. script.min.js
*/
this
.
regStart
=
new
RegExp
(
'<!--\\s*'
+
this
.
options
.
commentMarker
+
':(\\[?[\\w-]+\\]?)(?::([\\w,]+))?(?:\\s*(inline)\\s)?(?:\\s*(scoped)\\s)?(?:\\s*([^\\s]+)\\s*-->)*'
)
;
// <!-- /build -->
this
.
regEnd
=
new
RegExp
(
'(?:<!--\\s*)*\\/'
+
this
.
options
.
commentMarker
+
'\\s*-->'
)
;
// <link rel="stylesheet" href="js/bower_components/bootstrap/dist/css/bootstrap.css" />
this
.
cssHref
=
/
^
\s
*
<
\s
*
l
i
n
k
.
*
h
r
e
f
=
[
"
\'
]
(
[
^
"
'
]
*
)
[
"
\'
]
.
*
$
/
i
;
// <script src="js/bower_components/xdate/src/xdate.js"></script>
this
.
jsSrc
=
/
^
\s
*
<
\s
*
s
c
r
i
p
t
.
*
s
r
c
=
[
"
\'
]
(
[
^
"
'
]
*
)
[
"
\'
]
.
*
$
/
i
;
}
Parser
.
prototype
.
getBlocks
=
function
(
content
,
filePath
)
{
// Normalize line endings and split in lines
var
lines
=
content
.
replace
(
/
\r
\n
/
g
,
'\n'
)
.
split
(
/
\n
/
)
;
var
inside
=
false
;
var
sections
=
[
]
;
var
block
;
lines
.
forEach
(
function
(
line
)
{
var
build
=
line
.
match
(
this
.
regStart
)
;
var
endbuild
=
this
.
regEnd
.
test
(
line
)
;
var
attr
;
if
(
build
)
{
inside
=
true
;
attr
=
build
[
1
]
.
match
(
/
(?:
\[
(
[
\w
\-
]
+
)
\]
)
*
/
)
[
1
]
;
block
=
{
type
:
attr
?
'attr'
:
build
[
1
]
,
attr
:
attr
,
targets
:
!
!
build
[
2
]
?
build
[
2
]
.
split
(
','
)
:
null
,
inline
:
!
!
build
[
3
]
,
scoped
:
!
!
build
[
4
]
,
asset
:
build
[
5
]
,
indent
:
/
^
\s
*
/
.
exec
(
line
)
[
0
]
,
raw
:
[
]
}
;
}
if
(
inside
&&
block
)
{
block
.
raw
.
push
(
line
)
;
if
(
this
.
listFile
)
{
this
.
_writeToList
(
line
,
filePath
)
;
}
}
if
(
inside
&&
endbuild
)
{
inside
=
false
;
sections
.
push
(
block
)
;
}
}
,
this
)
;
if
(
this
.
listFile
)
{
this
.
listFile
.
end
(
)
;
}
return
sections
;
}
;
Parser
.
prototype
.
_writeToList
=
function
(
line
,
filePath
)
{
var
match
=
this
.
_getMatch
(
line
)
;
if
(
match
)
{
this
.
listFile
.
write
(
filePath
+
':'
+
match
[
1
]
+
'\n'
)
;
}
}
;
Parser
.
prototype
.
_getMatch
=
function
(
line
)
{
var
matchCss
=
line
.
match
(
this
.
cssHref
)
;
var
matchJs
=
line
.
match
(
this
.
jsSrc
)
;
return
matchCss
||
matchJs
||
null
;
}
;
Back
|
FazBrowse Home
|
New Git URL