Bots Writing Twine: Part 1: Setting up the Node.js Project

Bots Writing Twine

Twee is a human-readable format of the “code” of Twine stories. Using a Twee compiler like Extwee, Twee can be converted into a Twine-compatible HTML file.


Setting up the Node.js Project

Extwee is written in JavaScript and registered on NPM.

As a Twee compiler, it is able to take a Twee file and a story format and produce a HTML file. As it is open-source and under the MIT license, that also means parts of it can be used to create new projects using its ability to understand Twee and create Twine-compatible HTML files.

Installing Extwee

Assuming Node.js is installed, create a new directory for this project called “WritingTwee” (or whatever you would like to name it) and run the following:

npm init

Initialize the project with the default settings or change them as you see fit. Then, install the Extwee module.

npm i extwee

Once the dependencies are ready, create a new file named “index.js”

Examining Extwee

Looking at Extwee’s “index.js” file GitHub shows that the main modules work in the following way when moving between Twee and HTML:


let fr = new FileReader(argv.input);
let tp = new TweeParser(fr.contents);
let sfp = new StoryFormatParser(argv.format);
let hw = new HTMLWriter(argv.output, tp.story, sfp.JSON);

The FileReader object reads in a Twee file and sends its contents to TweeParser. The StoryFormatParser object reads in a story format file.

Finally, the HTMLWriter object takes in the name of the file to write, the story object from TweeParser and the JSON contents from the StoryFortmatParser.

Reorganizing Code

Based on the “index.js” file, a smaller version of the code might look like the following:

Following how the Extwee “index.js” file worked, the same general objects can be imported and used in the same way. Now, however, instead of using its parsing of the arguments sent to it from the command-line, they can be fed via the values of other variables.

What about the story format?

Twee is compiled into HTML through using a story format as a medium. In order to produce a Twine-compatible HTML file, a story format is needed.

Extwee does not keep story formats in its repository because of the different licenses of their code. However, TweeGo, another Twee compiler, keeps a collection of them.

On the TweeGo website, under “Story Formats”, the same story formats that ship with Twine can be found. These can be downloaded and used through including the “format.js” file from one of them in the same directory as the project.

What about Twee?

The example code calls for a “test.twee” file. The following is a great first example:


:: StoryTitle
Autogenerated!

:: Start
Some content
[[Another passage]]

:: Another passage

The above is not strictly Twee 3-compatible, as it does not have a StoryData passage with an IFID, but Extwee will supply those defaults.

(As long as there is at least a Start passage, Extwee will generate a HTML file.)

Twee + Story Format = HTML

With the ability to read Twee (via TweeParser) and story formats (via StoryFormatParser), HTML files can be created. With a story formats’s “format.js” file in the same directory (or updating the variable’s value), Twee compiling can easily happen.

Changing the contents of the “test.twee” file and then re-running the “index.js” file will produce new HTML example files.

The next step is to generate Twee and then have these same processes take it and compile it into HTML.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.