aw-parser
Version:
Fountain parser
132 lines (100 loc) • 3.56 kB
HTML
<html>
<script type="text/javascript" src="dist/aw-parser.amd.js"></script>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<textarea id="config" class="config"></textarea>
<textarea id="editor" class="editor">
Title: Test Script
Author: John Doe
INT. TEST - DAY
Action test. Action test. Action test. Action test. Action test. Action test. Action test. Action test. Action test.
HERO 1
Hello --
Hello!
HERO 2
Hello
</textarea>
<p id="results" class="results"></p>
<script type="text/javascript">
function getEditor() {
return document.getElementById('editor');
}
function getConfig() {
return document.getElementById('config');
}
function getResults() {
return document.getElementById('results');
}
function tokenToString(token) {
var text;
text = token.text.length > 20 ? (token.text.substr(0, 17) + '...') : token.text;
return token.type + ' [' + token.start + ':' + token.end + '] ' + text;
}
function parserResultToHTML(parserResult) {
var titleHTML, pagesHTML;
titleHTML = parserResult.title_page.map(tokenToString).join('<br />');
pagesHTML = parserResult.tokens.map(tokenToString).join('<br />');
return '<b>Title:</b><br/><br/>' + titleHTML + '<br/><br/><b>Pages:</b><br/><br/>' + pagesHTML;
}
window.onload = function() {
presenter.loadDefaults();
presenter.init();
};
var presenter = {
getConfig: function() {
try {
var defaultConfig = window.localStorage.getItem('config');
defaultConfig = JSON.parse(defaultConfig);
if (!defaultConfig) {
throw new Error();
}
} catch (e) {
defaultConfig = {
print_headers: true,
print_actions: true,
print_dialogues: true,
print_notes: true,
print_sections: true,
print_synopsis: true,
each_scene_on_new_page: true,
double_space_between_scenes: true,
use_dual_dialogue: true,
merge_multiple_empty_lines: true
};
}
return defaultConfig;
},
getLastEditor: function() {
return window.localStorage.getItem('text');
},
loadDefaults: function() {
getConfig().value = JSON.stringify(this.getConfig(), null, 2);
var defaultText = this.getLastEditor();
if (defaultText) {
getEditor().value = defaultText;
}
},
init: function() {
getEditor().onkeyup = this.parse.bind(this);
getConfig().onkeyup = this.parse.bind(this);
this.parse();
},
parse: function() {
var script, result;
script = getEditor().value;
try {
config = JSON.parse(getConfig().value);
result = awParser.parser.parse(script, config);
getResults().innerHTML = parserResultToHTML(result);
getConfig().classList.remove('invalid');
window.localStorage.setItem('text', getEditor().value);
window.localStorage.setItem('config', getConfig().value);
} catch (e) {
getConfig().classList.add('invalid');
}
}
}
</script>
</body>
</html>