-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Dan Cox
authored and
Dan Cox
committed
Sep 25, 2023
1 parent
67a4235
commit 0e8ebc5
Showing
5 changed files
with
73 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters