diff --git a/src/Twine1HTMLWriter.js b/src/Twine1HTMLWriter.js index e69de29b..a714e233 100644 --- a/src/Twine1HTMLWriter.js +++ b/src/Twine1HTMLWriter.js @@ -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!'); + } + } +} diff --git a/src/Twine2HTMLWriter.js b/src/Twine2HTMLWriter.js index 78a6f82f..27551e1b 100644 --- a/src/Twine2HTMLWriter.js +++ b/src/Twine2HTMLWriter.js @@ -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!'); } } } diff --git a/test/Twine1HTMLWriter.test.js b/test/Twine1HTMLWriter.test.js new file mode 100644 index 00000000..34af4b2a --- /dev/null +++ b/test/Twine1HTMLWriter.test.js @@ -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(); + }); + }); +}); diff --git a/test/Twine2HTMLWriter/creator.html b/test/Twine2HTMLWriter/creator.html index 3fedbc27..73a057c0 100644 --- a/test/Twine2HTMLWriter/creator.html +++ b/test/Twine2HTMLWriter/creator.html @@ -9,7 +9,7 @@ -