This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
124 lines (107 loc) · 3.18 KB
/
main.js
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
114
115
116
117
118
119
120
121
122
123
124
/**
* Module for handling filters
*/
if (typeof module === 'object' && typeof define !== 'function') {
var define = function (factory) {
module.exports = factory(require, exports, module);
};
}
define(function(require, exports, module) {
var utils = require('../utils/common');
var profile = require('../assets/profile');
var resources = require('../assets/resources');
/** List of registered filters */
var registeredFilters = {
html: require('./html'),
haml: require('./haml'),
jade: require('./jade'),
jsx: require('./jsx'),
slim: require('./slim'),
xsl: require('./xsl'),
css: require('./css'),
bem: require('./bem'),
c: require('./comment'),
e: require('./escape'),
s: require('./singleLine'),
t: require('./trim')
};
/** Filters that will be applied for unknown syntax */
var basicFilters = 'html';
function list(filters) {
if (!filters)
return [];
if (typeof filters === 'string') {
return filters.split(/[\|,]/g);
}
return filters;
}
return {
/**
* Register new filter
* @param {String} name Filter name
* @param {Function} fn Filter function
*/
add: function(name, fn) {
registeredFilters[name] = fn;
},
/**
* Apply filters for final output tree
* @param {AbbreviationNode} tree Output tree
* @param {Array} filters List of filters to apply. Might be a
* <code>String</code>
* @param {Object} profile Output profile, defined in <i>profile</i>
* module. Filters defined it profile are not used, <code>profile</code>
* is passed to filter function
* @memberOf emmet.filters
* @returns {AbbreviationNode}
*/
apply: function(tree, filters, profileName) {
profileName = profile.get(profileName);
list(filters).forEach(function(filter) {
var name = utils.trim(filter.toLowerCase());
if (name && name in registeredFilters) {
tree = registeredFilters[name](tree, profileName);
}
});
return tree;
},
/**
* Composes list of filters that should be applied to a tree, based on
* passed data
* @param {String} syntax Syntax name ('html', 'css', etc.)
* @param {Object} profile Output profile
* @param {String} additionalFilters List or pipe-separated
* string of additional filters to apply
* @returns {Array}
*/
composeList: function(syntax, profileName, additionalFilters) {
profileName = profile.get(profileName);
var filters = list(profileName.filters || resources.findItem(syntax, 'filters') || basicFilters);
if (profileName.extraFilters) {
filters = filters.concat(list(profileName.extraFilters));
}
if (additionalFilters) {
filters = filters.concat(list(additionalFilters));
}
if (!filters || !filters.length) {
// looks like unknown syntax, apply basic filters
filters = list(basicFilters);
}
return filters;
},
/**
* Extracts filter list from abbreviation
* @param {String} abbr
* @returns {Array} Array with cleaned abbreviation and list of
* extracted filters
*/
extract: function(abbr) {
var filters = '';
abbr = abbr.replace(/\|([\w\|\-]+)$/, function(str, p1){
filters = p1;
return '';
});
return [abbr, list(filters)];
}
};
});