-
Notifications
You must be signed in to change notification settings - Fork 3
/
template.html
256 lines (227 loc) · 6.94 KB
/
template.html
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Shift Template</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="favicon.ico">
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="css/ribbon.css" rel="stylesheet">
<link href="css/css.css" rel="stylesheet">
<link href="css/demo.css" rel="stylesheet">
<link href="//fonts.googleapis.com/css?family=Source+Code+Pro:400,700" rel="stylesheet" type="text/css"/>
<!-- highlight.js -->
<link href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.7/styles/magula.min.css" rel="stylesheet"/>
<script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/8.7/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<!-- ace editor -->
<script src="vendor/ace-builds/src-min/ace.js"></script>
<script src="vendor/ace-builds/src-min/mode-javascript.js"></script>
<script src="vendor/ace-builds/src-min/theme-monokai.js"></script>
<script src="js/shift-ast.js"></script>
<script src="js/shift-parser.js"></script>
<script src="js/shift-template.js"></script>
<script src="js/shift-codegen.js"></script>
<style>
#demo0, #demo1 {
resize: vertical;
}
#demo0 {
height: 250px;
}
#template-container {
width: 50%;
}
#modifier-container {
width: 100%;
}
#template {
width: 100%;
}
#modifier {
width: 100%;
}
#output {
width: 100%;
background-color: #F8F8F8;
}
#demo1 .output-container {
width: 50%;
}
.program-radio-label {
font-size: medium;
}
#program-radio {
margin-bottom: 0px;
}
#template-radio {
margin-bottom: -10px;
}
.error-message {
padding-left: 4.3em;
position: relative;
float: top;
}
.editor.error {
position: relative;
}
.editor.error .editor {
position: absolute;
bottom: 0;
height: 93%;
}
.editor.error .error-message {
display: block;
}
#save-button {
display: block;
margin-left: auto;
margin-right: auto;
}
</style>
</head>
<body>
<div class="ribbon-container right">
<div class="ribbon">
<a href="https://github.com/shapesecurity/shift-template-js" target="_blank">Fork me on GitHub</a>
</div>
</div>
<div class="container">
<div class="content">
<div class="masthead">
<h3><a href="./index.html" class="muted hub-link">part of the Shift suite</a></h3>
</div>
<div class="jumbotron">
<h1><span class="shift">Shift</span> Template</h1>
<p class="lead">
generates a Shift AST from Shift templating markup
</p>
</div>
<hr />
<section id="demo">
<h3>Demo</h3>
<p>
This demo shows a rendering of the Template output on the right. You may
edit the template on the left, and the template parameters in the center.
The template parameters here are passed directly to the second argument of
applyStructuredTemplate.
</p>
<p id="program-radio" class="text-center">
<label class="radio inline program-radio-label">
<input type="radio" name="program-radio" id="script-radio" value="script"> Script
</label>
<label class="radio inline program-radio-label">
<input type="radio" name="program-radio" id="module-radio" value="module" checked> Module
</label>
</p>
<div class="demo" id="demo0">
<div id="modifier-container" class="editor">
<div spellcheck="false" class="editor" id="modifier">{
foo: node => new Shift.IdentifierExpression({name: node.name + '_'}),
bar: node => new Shift.LiteralNumericExpression({value: node.value + 1}),
}
</div>
<div class="error-message"></div>
</div>
</div>
<div class="demo" id="demo1">
<div id="template-container" class="editor">
<div spellcheck="false" class="editor" id="template">a + /*# foo # IdentifierExpression #*/ b;
0 + /*# bar #*/ 1;
</div>
<div class="error-message"></div>
</div>
<div class="output-container">
<div class="editor" spellcheck="false" id="output"></div>
</div>
<script src="js/template/main.js"></script>
</div>
</section>
<section id="installation">
<h3>Installation</h3>
<pre><code class="nohighlight">npm install shift-template</code></pre>
</section>
<section id="usage">
<h3>Usage</h3>
<pre><code class="javascript">import Shift from "shift-ast";
import { applyTemplate } from "shift-template";
let src = `
a + /*# foo # IdentifierExpression #*/ b;
0 + /*# bar #*/ 1;
`;
let replaced = applyTemplate(src, {
foo: node => new Shift.IdentifierExpression({ name: node.name + '_' }),
bar: node => new Shift.LiteralNumericExpression({ value: node.value + 1 }),
});
/* replaced is an AST representing:
a + b_;
0 + 2;
*/
</code></pre>
<pre><code class="javascript">import Shift from "shift-ast";
import { applyStructuredTemplate } from "shift-template";
let src = `
[
/*# if markerAtStart #*/
{ prop: 'marker' },
/*# for each x of xs #*/
{ prop: /*# x::prop #*/ null },
/*# unless markerAtStart #*/
{ prop: 'marker' },
]
`;
let templateValues = {
markerAtStart: false,
xs: [
{ prop: () => new Shift.LiteralNumericExpression({ value: 1 }) },
{ prop: () => new Shift.LiteralNumericExpression({ value: 2 }) },
{ prop: () => new Shift.LiteralNumericExpression({ value: 3 }) },
]
};
let replaced = applyStructuredTemplate(script, templateValues);
/* replaced is an AST representing:
[
{ prop: 1 },
{ prop: 2 },
{ prop: 3 },
{ prop: 'marker' },
]
*/
</code></pre>
<pre><code class="javascript">import { parseScriptWithLocation } from "shift-parser";
import { findNodes } from "shift-template";
let { tree, location, comments } = parseScriptWithLocation("/* ECMAScript program template markup */");
let names = findNodes({ tree, locations, comments });
/* names =
[
{
name: "foo",
node: {
type: "IdentifierExpression",
name: "b",
},
comment: { text, type, start, end },
},
{
name: "bar",
node: {
type: "LiteralNumericExpression",
value: 1,
},
comment: { text, type, start, end },
},
]
*/
</code></pre>
</section>
</div>
<hr />
<div class="footer">
<p>© Shape Security, Inc.</p>
</div>
</div>
</body>
</html>