Skip to content

Commit

Permalink
Working toward Twine 1 HTML writing
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Cox authored and Dan Cox committed Sep 25, 2023
1 parent 67a4235 commit 0e8ebc5
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
55 changes: 55 additions & 0 deletions src/Twine1HTMLWriter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @external Story
* @see Story.js
* @external StoryFormat
* @see StoryFormat.js
*/

import fs from 'fs';
import Story from './Story.js';
import StoryFormat from './StoryFormat.js';

/**
* @class Twine1HTMLWriter
* @module Twine1HTMLWriter
*/
export default class Twine1HTMLWriter {
/**
* Write a combination of Story + StoryFormat into Twine 1 HTML file.
* @public
* @static
* @function writeFile
* @param {string} file - File to write.
* @param {Story} story - Story object to write.
* @param {StoryFormat} storyFormat - StoryFormat to write.
*/
static write (file, story, storyFormat) {
if (!(story instanceof Story)) {
throw new Error('Error: story must be a Story object!');
}

if (!(storyFormat instanceof StoryFormat)) {
throw new Error('storyFormat must be a StoryFormat object!');
}

let outputContents = '';
const storyData = story.toTwine1HTML();

// Replace the story name in the source file.
storyFormat.source = storyFormat.source.replaceAll(/{{STORY_NAME}}/gm, story.name);

// Replace the story data.
storyFormat.source = storyFormat.source.replaceAll(/{{STORY_DATA}}/gm, storyData);

// Combine everything together.
outputContents += storyFormat.source;

try {
// Try to write.
fs.writeFileSync(file, outputContents);
} catch (event) {
// Throw error.
throw new Error('Error: Cannot write Twine 1 HTML file!');
}
}
}
2 changes: 1 addition & 1 deletion src/Twine2HTMLWriter.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default class Twine2HTMLWriter {
fs.writeFileSync(file, outputContents);
} catch (event) {
// Throw error
throw new Error('Error: Cannot write HTML file!');
throw new Error('Error: Cannot write Twine 2 HTML file!');
}
}
}
15 changes: 15 additions & 0 deletions test/Twine1HTMLWriter.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import Twine1HTMLWriter from '../src/Twine2HTMLWriter.js';
import Story from '../src/Story.js';

describe('Twine1HTMLWriter', () => {
describe('write()', () => {
it('story should be instanceof Story', () => {
expect(() => { Twine1HTMLWriter.write('test/Twine1HTMLWriter/test.html', {}); }).toThrow();
});

it('storyFormat should be instanceof StoryFormat', () => {
const s = new Story();
expect(() => { Twine1HTMLWriter.write('test/Twine1HTMLWriter/test.html', s, {}); }).toThrow();
});
});
});
2 changes: 1 addition & 1 deletion test/Twine2HTMLWriter/creator.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</head>
<body>
<tw-story tags></tw-story>
<tw-storydata name="Title" startnode="4" creator="extwee" creator-version="2.2.0" ifid="838927FE-CCE0-47FF-8DAE-390D78F2137F" zoom="0" format="" format-version="" options hidden>
<tw-storydata name="Title" startnode="4" creator="extwee" creator-version="2.2.0" ifid="C7A776A5-D63D-4B5B-9648-2CF1855B6200" zoom="0" format="" format-version="" options hidden>
<style role="stylesheet" id="twine-user-stylesheet" type="text/twine-css"></style>
<script role="script" id="twine-user-script" type="text/twine-javascript"></script>
<tw-passagedata pid="1" name="A" tags="" ></tw-passagedata>
Expand Down
2 changes: 1 addition & 1 deletion test/Twine2HTMLWriter/test4.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</head>
<body>
<tw-story tags></tw-story>
<tw-storydata name="Title" startnode="4" creator="extwee" creator-version="2.2.0" ifid="57981E20-F5A9-4A42-BE79-42374600164D" zoom="0" format="" format-version="" options hidden>
<tw-storydata name="Title" startnode="4" creator="extwee" creator-version="2.2.0" ifid="5878E0D8-3FFB-42B9-AB7A-635C33E352AD" zoom="0" format="" format-version="" options hidden>
<style role="stylesheet" id="twine-user-stylesheet" type="text/twine-css"></style>
<script role="script" id="twine-user-script" type="text/twine-javascript"></script>
<tw-passagedata pid="1" name="A" tags="" ></tw-passagedata>
Expand Down

0 comments on commit 0e8ebc5

Please sign in to comment.