-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathextend.es6
More file actions
169 lines (127 loc) · 4.63 KB
/
Copy pathextend.es6
File metadata and controls
169 lines (127 loc) · 4.63 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import fs from 'fs';
import path from 'path';
import util from 'util';
import parseToPostHtml from 'posthtml-parser';
import Api from 'posthtml/lib/api';
const errors = {
'EXTENDS_NO_SRC': '<extends> has no "src"',
'BLOCK_NO_NAME': '<block> has no "name"',
'UNEXPECTED_BLOCK': 'Unexpected block "%s"'
};
export default (options = {}) => {
return tree => {
options.encoding = options.encoding || 'utf8';
options.root = options.root || './';
options.plugins = options.plugins || [];
tree = handleExtendsNodes(tree, options, tree.messages);
const blockNodes = getBlockNodes(tree);
let blockTree;
for (let blockName of Object.keys(blockNodes)) {
let blockNode = blockNodes[blockName];
blockNode.tag = false;
blockTree = blockNode.content = blockNode.content || [];
blockNodes[blockName] = blockNode;
trimTree(blockTree);
}
return tree;
};
};
const api = new Api();
function trimTree(tree) {
let firstNode = tree[0];
let lastNode = tree[tree.length - 1];
if (typeof firstNode === 'string') {
tree[0] = firstNode = firstNode.replace(/^\s+/, '');
// make first and last node refer to the same string if they are the same node
if (tree.length === 1) {
lastNode = firstNode;
}
}
if (typeof lastNode === 'string') {
tree[tree.length - 1] = lastNode = lastNode.replace(/\s+$/, '');
}
return tree;
}
function handleExtendsNodes(tree, options, messages) {
api.match.call(applyPluginsToTree(tree, options.plugins), {tag: 'extends'}, extendsNode => {
if (! extendsNode.attrs || ! extendsNode.attrs.src) {
throw getError(errors.EXTENDS_NO_SRC);
}
const layoutPath = path.resolve(options.root, extendsNode.attrs.src);
const layoutHtml = fs.readFileSync(layoutPath, options.encoding);
let layoutTree = handleExtendsNodes(applyPluginsToTree(parseToPostHtml(layoutHtml), options.plugins), options, messages);
trimTree(layoutTree);
extendsNode.tag = false;
extendsNode.content = mergeExtendsAndLayout(layoutTree, extendsNode);
messages.push({
type: 'dependency',
file: layoutPath,
from: options.from
});
return extendsNode;
});
return tree;
}
function applyPluginsToTree(tree, plugins) {
return plugins.reduce((tree, plugin) => tree = plugin(tree), tree);
}
function mergeExtendsAndLayout(layoutTree, extendsNode) {
const layoutBlockNodes = getBlockNodes(layoutTree);
const extendsBlockNodes = getBlockNodes(extendsNode.content);
for (let layoutBlockName of Object.keys(layoutBlockNodes)) {
let extendsBlockNode = extendsBlockNodes[layoutBlockName];
if (! extendsBlockNode) {
continue;
}
let layoutBlockNode = layoutBlockNodes[layoutBlockName];
layoutBlockNode.content = trimTree(mergeContent(
extendsBlockNode.content,
layoutBlockNode.content,
getBlockType(extendsBlockNode)
));
delete extendsBlockNodes[layoutBlockName];
}
for (let extendsBlockName of Object.keys(extendsBlockNodes)) {
throw getError(errors.UNEXPECTED_BLOCK, extendsBlockName);
}
return layoutTree;
}
function mergeContent(extendBlockContent, layoutBlockContent, extendBlockType) {
extendBlockContent = extendBlockContent || [];
layoutBlockContent = layoutBlockContent || [];
switch (extendBlockType) {
case 'replace':
layoutBlockContent = extendBlockContent;
break;
case 'prepend':
layoutBlockContent = extendBlockContent.concat(layoutBlockContent);
break;
case 'append':
layoutBlockContent = layoutBlockContent.concat(extendBlockContent);
break;
}
return layoutBlockContent;
}
function getBlockType(blockNode) {
let blockType = (blockNode.attrs && blockNode.attrs.type) || '';
blockType = blockType.toLowerCase();
if (['replace', 'prepend', 'append'].indexOf(blockType) === -1) {
blockType = 'replace';
}
return blockType;
}
function getBlockNodes(content = []) {
let blockNodes = {};
api.match.call(content, {tag: 'block'}, node => {
if (! node.attrs || ! node.attrs.name) {
throw getError(errors.BLOCK_NO_NAME);
}
blockNodes[node.attrs.name] = node;
return node;
});
return blockNodes;
}
function getError() {
const message = util.format.apply(util, arguments);
return new Error('[posthtml-extend] ' + message);
}