Skip to content

Instantly share code, notes, and snippets.

@sequitur
Last active November 5, 2015 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sequitur/1e09f116860dd4e1343b to your computer and use it in GitHub Desktop.
Save sequitur/1e09f116860dd4e1343b to your computer and use it in GitHub Desktop.
Extracts all quoted text from a file, and generates a word count. Meant to be run by Node.
/*
* Extracts all text in double quotes from a given text file, as well as
* coffeescript-style fenced quote blocks ("""). Outputs an approximate word
* count for all of the content in quotations.
*
* Runs on Node.js, but doesn't require any npm modules.
*/
var START_QUOTE = /\ \"(.*)$/;
var END_QUOTE = /[^\\]\"(.*)$/;
var START_FENCED_QUOTE = /\"\"\"(.*)$/;
var END_FENCED_QUOTE = /^(.*)[^\\]\"\"\"(.*)$/;
var parseLine = function (line) {
var matches;
if (line === '') return [null, '', parseLine];
matches = START_FENCED_QUOTE.exec(line);
if (matches) return [null, matches[1], parseFencedQuote];
matches = START_QUOTE.exec(line);
if (matches) return [null, matches[1], parseQuote];
return [null, '', parseLine];
};
var parseQuote = function (line) {
var matches;
if (line === '') return [null, '', parseQuote];
matches = END_QUOTE.exec(line);
if (matches) return [matches[1], matches[2], parseLine];
return [line, '', parseQuote];
};
var parseFencedQuote = function (line) {
var matches;
if (line === '') return [null, '', parseFencedQuote];
matches = END_FENCED_QUOTE.exec(line);
if (matches) return [matches[1], matches[2], parseLine];
return [line, '', parseFencedQuote];
};
var extract = function (lines) {
var currentState = parseLine;
var stack = [];
var remainder, output, values;
lines.forEach(function (line) {
values = currentState(line);
output = values[0];
remainder = values[1];
currentState = values[2];
if (output) stack.push(output);
while (remainder) {
values = currentState(line);
output = values[0];
remainder = values[1];
currentState = values[2];
if (output) stack.push(output);
}
});
return stack;
};
fs = require('fs');
/* Edit this; relative path to the game file. */
var file = fs.readFileSync('game/main.coffee', 'utf8');
console.log(
extract(file.split("\n"))
.join(' ')
.split(/\s/)
.filter(function (item) {return item !== ''})
.length);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment