-
Notifications
You must be signed in to change notification settings - Fork 55
/
fountain.js
284 lines (224 loc) · 10 KB
/
fountain.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// fountain-js 0.1.10
// http://www.opensource.org/licenses/mit-license.php
// Copyright (c) 2012 Matt Daly
;(function() {
'use strict';
var regex = {
title_page: /^((?:title|credit|author[s]?|source|notes|draft date|date|contact|copyright)\:)/gim,
scene_heading: /^((?:\*{0,3}_?)?(?:(?:int|ext|est|i\/e)[. ]).+)|^(?:\.(?!\.+))(.+)/i,
scene_number: /( *#(.+)# *)/,
transition: /^((?:FADE (?:TO BLACK|OUT)|CUT TO BLACK)\.|.+ TO\:)|^(?:> *)(.+)/,
dialogue: /^([A-Z*_]+[0-9A-Z (._\-')]*)(\^?)?(?:\n(?!\n+))([\s\S]+)/,
parenthetical: /^(\(.+\))$/,
action: /^(.+)/g,
centered: /^(?:> *)(.+)(?: *<)(\n.+)*/g,
section: /^(#+)(?: *)(.*)/,
synopsis: /^(?:\=(?!\=+) *)(.*)/,
note: /^(?:\[{2}(?!\[+))(.+)(?:\]{2}(?!\[+))$/,
note_inline: /(?:\[{2}(?!\[+))([\s\S]+?)(?:\]{2}(?!\[+))/g,
boneyard: /(^\/\*|^\*\/)$/g,
page_break: /^\={3,}$/,
line_break: /^ {2}$/,
emphasis: /(_|\*{1,3}|_\*{1,3}|\*{1,3}_)(.+)(_|\*{1,3}|_\*{1,3}|\*{1,3}_)/g,
bold_italic_underline: /(_{1}\*{3}(?=.+\*{3}_{1})|\*{3}_{1}(?=.+_{1}\*{3}))(.+?)(\*{3}_{1}|_{1}\*{3})/g,
bold_underline: /(_{1}\*{2}(?=.+\*{2}_{1})|\*{2}_{1}(?=.+_{1}\*{2}))(.+?)(\*{2}_{1}|_{1}\*{2})/g,
italic_underline: /(?:_{1}\*{1}(?=.+\*{1}_{1})|\*{1}_{1}(?=.+_{1}\*{1}))(.+?)(\*{1}_{1}|_{1}\*{1})/g,
bold_italic: /(\*{3}(?=.+\*{3}))(.+?)(\*{3})/g,
bold: /(\*{2}(?=.+\*{2}))(.+?)(\*{2})/g,
italic: /(\*{1}(?=.+\*{1}))(.+?)(\*{1})/g,
underline: /(_{1}(?=.+_{1}))(.+?)(_{1})/g,
splitter: /\n{2,}/g,
cleaner: /^\n+|\n+$/,
standardizer: /\r\n|\r/g,
whitespacer: /^\t+|^ {3,}/gm
};
var lexer = function (script) {
return script.replace(regex.boneyard, '\n$1\n')
.replace(regex.standardizer, '\n')
.replace(regex.cleaner, '')
.replace(regex.whitespacer, '');
};
var tokenize = function (script) {
var src = lexer(script).split(regex.splitter)
, i = src.length, line, match, parts, text, meta, x, xlen, dual
, tokens = [];
while (i--) {
line = src[i];
// title page
if (regex.title_page.test(line)) {
match = line.replace(regex.title_page, '\n$1').split(regex.splitter).reverse();
for (x = 0, xlen = match.length; x < xlen; x++) {
parts = match[x].replace(regex.cleaner, '').split(/\:\n*/);
tokens.push({ type: parts[0].trim().toLowerCase().replace(' ', '_'), text: parts[1].trim() });
}
continue;
}
// scene headings
if (match = line.match(regex.scene_heading)) {
text = match[1] || match[2];
if (text.indexOf(' ') !== text.length - 2) {
if (meta = text.match(regex.scene_number)) {
meta = meta[2];
text = text.replace(regex.scene_number, '');
}
tokens.push({ type: 'scene_heading', text: text, scene_number: meta || undefined });
}
continue;
}
// centered
if (match = line.match(regex.centered)) {
tokens.push({ type: 'centered', text: match[0].replace(/>|</g, '') });
continue;
}
// transitions
if (match = line.match(regex.transition)) {
tokens.push({ type: 'transition', text: match[1] || match[2] });
continue;
}
// dialogue blocks - characters, parentheticals and dialogue
if (match = line.match(regex.dialogue)) {
if (match[1].indexOf(' ') !== match[1].length - 2) {
// we're iterating from the bottom up, so we need to push these backwards
if (match[2]) {
tokens.push({ type: 'dual_dialogue_end' });
}
tokens.push({ type: 'dialogue_end' });
parts = match[3].split(/(\(.+\))(?:\n+)/).reverse();
for (x = 0, xlen = parts.length; x < xlen; x++) {
text = parts[x];
if (text.length > 0) {
tokens.push({ type: regex.parenthetical.test(text) ? 'parenthetical' : 'dialogue', text: text });
}
}
tokens.push({ type: 'character', text: match[1].trim() });
tokens.push({ type: 'dialogue_begin', dual: match[2] ? 'right' : dual ? 'left' : undefined });
if (dual) {
tokens.push({ type: 'dual_dialogue_begin' });
}
dual = match[2] ? true : false;
continue;
}
}
// section
if (match = line.match(regex.section)) {
tokens.push({ type: 'section', text: match[2], depth: match[1].length });
continue;
}
// synopsis
if (match = line.match(regex.synopsis)) {
tokens.push({ type: 'synopsis', text: match[1] });
continue;
}
// notes
if (match = line.match(regex.note)) {
tokens.push({ type: 'note', text: match[1]});
continue;
}
// boneyard
if (match = line.match(regex.boneyard)) {
tokens.push({ type: match[0][0] === '/' ? 'boneyard_begin' : 'boneyard_end' });
continue;
}
// page breaks
if (regex.page_break.test(line)) {
tokens.push({ type: 'page_break' });
continue;
}
// line breaks
if (regex.line_break.test(line)) {
tokens.push({ type: 'line_break' });
continue;
}
tokens.push({ type: 'action', text: line });
}
return tokens;
};
var inline = {
note: '<!-- $1 -->',
line_break: '<br />',
bold_italic_underline: '<span class=\"bold italic underline\">$2</span>',
bold_underline: '<span class=\"bold underline\">$2</span>',
italic_underline: '<span class=\"italic underline\">$2</span>',
bold_italic: '<span class=\"bold italic\">$2</span>',
bold: '<span class=\"bold\">$2</span>',
italic: '<span class=\"italic\">$2</span>',
underline: '<span class=\"underline\">$2</span>'
};
inline.lexer = function (s) {
if (!s) {
return;
}
var styles = [ 'underline', 'italic', 'bold', 'bold_italic', 'italic_underline', 'bold_underline', 'bold_italic_underline' ]
, i = styles.length, style, match;
s = s.replace(regex.note_inline, inline.note).replace(/\\\*/g, '[star]').replace(/\\_/g, '[underline]').replace(/\n/g, inline.line_break);
// if (regex.emphasis.test(s)) { // this was causing only every other occurence of an emphasis syntax to be parsed
while (i--) {
style = styles[i];
match = regex[style];
if (match.test(s)) {
s = s.replace(match, inline[style]);
}
}
// }
return s.replace(/\[star\]/g, '*').replace(/\[underline\]/g, '_').trim();
};
var parse = function (script, toks, callback) {
if (callback === undefined && typeof toks === 'function') {
callback = toks;
toks = undefined;
}
var tokens = tokenize(script)
, i = tokens.length, token
, title, title_page = [], html = [], output;
while (i--) {
token = tokens[i];
token.text = inline.lexer(token.text);
switch (token.type) {
case 'title': title_page.push('<h1>' + token.text + '</h1>'); title = token.text.replace('<br />', ' ').replace(/<(?:.|\n)*?>/g, ''); break;
case 'credit': title_page.push('<p class=\"credit\">' + token.text + '</p>'); break;
case 'author': title_page.push('<p class=\"authors\">' + token.text + '</p>'); break;
case 'authors': title_page.push('<p class=\"authors\">' + token.text + '</p>'); break;
case 'source': title_page.push('<p class=\"source\">' + token.text + '</p>'); break;
case 'notes': title_page.push('<p class=\"notes\">' + token.text + '</p>'); break;
case 'draft_date': title_page.push('<p class=\"draft-date\">' + token.text + '</p>'); break;
case 'date': title_page.push('<p class=\"date\">' + token.text + '</p>'); break;
case 'contact': title_page.push('<p class=\"contact\">' + token.text + '</p>'); break;
case 'copyright': title_page.push('<p class=\"copyright\">' + token.text + '</p>'); break;
case 'scene_heading': html.push('<h3' + (token.scene_number ? ' id=\"' + token.scene_number + '\">' : '>') + token.text + '</h3>'); break;
case 'transition': html.push('<h2>' + token.text + '</h2>'); break;
case 'dual_dialogue_begin': html.push('<div class=\"dual-dialogue\">'); break;
case 'dialogue_begin': html.push('<div class=\"dialogue' + (token.dual ? ' ' + token.dual : '') + '\">'); break;
case 'character': html.push('<h4>' + token.text + '</h4>'); break;
case 'parenthetical': html.push('<p class=\"parenthetical\">' + token.text + '</p>'); break;
case 'dialogue': html.push('<p>' + token.text + '</p>'); break;
case 'dialogue_end': html.push('</div> '); break;
case 'dual_dialogue_end': html.push('</div> '); break;
case 'section': html.push('<p class=\"section\" data-depth=\"' + token.depth + '\">' + token.text + '</p>'); break;
case 'synopsis': html.push('<p class=\"synopsis\">' + token.text + '</p>'); break;
case 'note': html.push('<!-- ' + token.text + '-->'); break;
case 'boneyard_begin': html.push('<!-- '); break;
case 'boneyard_end': html.push(' -->'); break;
case 'action': html.push('<p>' + token.text + '</p>'); break;
case 'centered': html.push('<p class=\"centered\">' + token.text + '</p>'); break;
case 'page_break': html.push('<hr />'); break;
case 'line_break': html.push('<br />'); break;
}
}
output = { title: title, html: { title_page: title_page.join(''), script: html.join('') }, tokens: toks ? tokens.reverse() : undefined };
if (typeof callback === 'function') {
return callback(output);
}
return output;
};
var fountain = function (script, callback) {
return fountain.parse(script, callback);
};
fountain.parse = function (script, tokens, callback) {
return parse(script, tokens, callback);
};
if (typeof module !== 'undefined') {
module.exports = fountain;
} else {
this.fountain = fountain;
}
}).call(this);