Skip to content

Instantly share code, notes, and snippets.

@MayerDaniel
Last active May 14, 2022 19:33
Show Gist options
  • Save MayerDaniel/c2a593e974613f90f1bfff0c1a1e78a6 to your computer and use it in GitHub Desktop.
Save MayerDaniel/c2a593e974613f90f1bfff0c1a1e78a6 to your computer and use it in GitHub Desktop.
A twine script to CSV organizer for Lucy
/*
For Sugarlumps by Sugarlumps
REQUIREMENTS:
You need to have nodejs on your computer:
https://nodejs.org/en/download/
Run these commands on your computer first in the same folder as this script.
Once they are installed you never need to run them again.
npm i -s csv-writer
npm i -s twine-parser
*/
const fs = require("fs");
const createCsvWriter = require('csv-writer').createObjectCsvWriter;
const {
parseHTML,
parseURL,
parsePhilomeLa,
toCytoscapeGraph
} = require('twine-parser');
var graph = []
var blacklist = []
var dialogue = []
pattern = /^([A-Z]+:)(.*)/
if (process.argv.length < 3){
console.log("USAGE: node " + process.argv[1] + " <path to twine HTML file>")
process.exit()
}
const getData = () => fs.readFileSync(process.argv[2]).toString()
const parsedGame = parseHTML(getData())
graph = parsedGame.passages
const stem = process.argv[2].replace(/\.[^/.]+$/, "")
const csvWriter = createCsvWriter({
path: `${stem}.csv`,
header: [
{id: 'character', title: 'Character'},
{id: 'dialogue', title: 'Dialogue'},
],
fieldDelimiter: ';'
});
const addDialogue = function(c,d){
dialogue.push({'character': c, 'dialogue': d});
}
const getNodeByName = function(name){
var ret = 0
graph.forEach( function (node){
if (name.localeCompare(node.name) == 0){
ret = node
}
});
return ret
}
const dfs = function(node) {
if (node){
if (blacklist.indexOf(node.name) == -1){
addDialogue('','')
addDialogue('', `["${node.name}"]`)
var dialogueArray = node.text.split('\n')
dialogueArray.forEach( function(line) {
if (pattern.test(line)) {
var match = line.match(pattern)
addDialogue(match[1], match[2])
} else {
if (line){
addDialogue('', line)
}
}
})
blacklist.push(node.name)
node.links.forEach( function(l){
result = dfs(getNodeByName(l.destination.name.split('->').slice(-1)[0]))
})
}
}
}
graph.forEach((n) => {dfs(n)})
csvWriter.writeRecords(dialogue)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment