1 |
|
2 | 'use strict'
|
3 |
|
4 | const Context = require('./context')
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | class Extractor {
|
14 | constructor (options) {
|
15 | this.output = []
|
16 | this.blocks = []
|
17 | this.options = options || {}
|
18 | }
|
19 |
|
20 | push (fname, src) {
|
21 | const self = this
|
22 | const c = new Context(fname, src)
|
23 | c.process()
|
24 | c.blocks.forEach(function (block) {
|
25 | if (self.isBlockAllowed(block)) {
|
26 | self.blocks.push(block)
|
27 | }
|
28 | })
|
29 | }
|
30 |
|
31 | isBlockAllowed (block) {
|
32 | if (this.options.excludeTags &&
|
33 | hasIntersection(this.options.excludeTags, block.tags)) {
|
34 | return false
|
35 | }
|
36 |
|
37 | return true
|
38 | }
|
39 |
|
40 | toJson () {
|
41 | return { blocks: this.blocks }
|
42 | }
|
43 | }
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 | function hasIntersection (list, other) {
|
54 | if (!Array.isArray(list) || !Array.isArray(other)) return
|
55 | for (let i = 0, len = list.length; i < len; i++) {
|
56 | let item = list[i]
|
57 | if (~other.indexOf(item)) return true
|
58 | }
|
59 | return false
|
60 | }
|
61 |
|
62 | module.exports = Extractor
|