Skip to content

Instantly share code, notes, and snippets.

@bronzehedwick
Created February 20, 2019 18:48
Show Gist options
  • Save bronzehedwick/c84d2bc3239f42156dca31c3db554f4c to your computer and use it in GitHub Desktop.
Save bronzehedwick/c84d2bc3239f42156dca31c3db554f4c to your computer and use it in GitHub Desktop.
Small node script to generate a markdown file with every character mentioned in a .fountain script file
#!/usr/local/bin/node
/* eslint-env node, es6 */
const fs = require('fs');
const file = process.argv[2];
const path = require('path');
const lineReader = require('readline').createInterface({
input: fs.createReadStream(file)
});
let characters = new Map();
function isUpperCase(str) {
return str === str.toUpperCase();
}
function stripExtraTags(str) {
let newStr = str.replace('(O.S.)', '');
newStr = newStr.replace('^', '');
newStr = newStr.replace('(PHONE)', '');
newStr = newStr.replace('\*', '');
return newStr.trim();
}
function isACharacter(str) {
if (!str) return false;
if (str.startsWith('INT.')) return false;
if (str.startsWith('EXT.')) return false;
if (str.startsWith('>')) return false;
if (str.startsWith('=')) return false;
if (str.indexOf('.') !== -1) return false;
if (str.indexOf('!') !== -1) return false;
if (str.indexOf('THEME') !== -1) return false;
if (str.indexOf('CREDITS') !== -1) return false;
return true;
}
lineReader.on('line', function readLine(line) {
if (isUpperCase(line) && isACharacter(line)) {
characters.set(stripExtraTags(line), stripExtraTags(line));
}
});
lineReader.on('close', function close() {
const fileDirectory = path.dirname(file);
let contents = '# Characters\n\n';
let i = 1;
characters.forEach(item => {
contents += i + '. ' + item + '\n';
i++;
});
fs.writeFile(fileDirectory + '/Characters.md', contents, function writeFile(err) {
if (err) {
return console.error(err);
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment