From c624ffb71445c2bc3d68bb508949661600f9f2ba Mon Sep 17 00:00:00 2001 From: Nathaniel Nelson Date: Mon, 18 Dec 2017 10:26:54 -0700 Subject: [PATCH 1/3] Adding contributor info --- README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 14d16f1..1af61c6 100644 --- a/README.md +++ b/README.md @@ -182,7 +182,7 @@ A template is a regular HTML file with a couple of special macros. You also need to define:
- + ...which should be empty. This is where the active section's story text will be displayed on the page. Optionally, you can also include: @@ -330,4 +330,8 @@ Creator and primary developer **Nat Quayle Nelson**
Major contributor -- GitHub: [@NQNStudios](https://github.com/NQNStudios) +- Website: [natquaylenelson.com](https://natquaylenelson.com) +- Fractive Projects: + - [SpaceFractive](https://github.com/NQNStudios/SpaceFractive): Fractive integrated with [Phaser](https://phaser.io) for multimedia-enhanced stories. + - [Bring Me a Reuben](https://nqn.itch.io/bring-me-a-reuben) (Ongoing) + - [Ballad of the Space Bard](https://balladofthespacebard.com) (In Development) From f025235eb898b2ac7f74f8f911e7c905146678cc Mon Sep 17 00:00:00 2001 From: Nathaniel Nelson Date: Mon, 18 Dec 2017 10:54:41 -0700 Subject: [PATCH 2/3] Refactor Compiler with additional comments and error message --- src/Compiler.ts | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/src/Compiler.ts b/src/Compiler.ts index 2a1611c..fef0c22 100644 --- a/src/Compiler.ts +++ b/src/Compiler.ts @@ -103,6 +103,29 @@ export namespace Compiler let nextInlineID : number = 0; let sectionCount : number = 0; + /** + * Inserts the given html snippet into the template HTML at EVERY point where + * a specially formatted comment appears. + * @param snippet The html to insert + * @param template The template in which to insert + * @return The complete resulting html file contents + */ + function InsertHtmlAtMark(snippet : string, template : string, mark : string) : string + { + // The mark has to be placed inside an HTML comment formatted like so: + let markComment : string = ``; + + // Throw an error if the mark doesn't exist + if (template.indexOf(markComment) === -1) + { + console.log(`Template file must contain mark: ${markComment}`); + process.exit(1); + } + + // Insert the snippet + return template.split(markComment).join(snippet); + } + /** * Inserts the given story text (html) and scripts (javascript) into an html template, and returns the complete resulting html file contents * @param html The html-formatted story text to insert into the template @@ -111,6 +134,7 @@ export namespace Compiler */ function ApplyTemplate(basePath : string, html : string, javascript : string) : string { + // Ensure that the template file exists let templatePath : string = path.resolve(basePath, project.template); if(!fs.existsSync(templatePath)) { @@ -136,16 +160,16 @@ export namespace Compiler // Insert all bundled scripts, including Core.js scriptSection += `${javascript}`; scriptSection += ""; - template = template.split("").join(scriptSection); + template = InsertHtmlAtMark(scriptSection, template, 'script'); // Insert html-formatted story text - template = template.split("").join(html); + template = InsertHtmlAtMark(html, template, 'story'); // Insert the back button if specified to do so if(project.includeBackButton) { - let backButtonHTML = '' + project.backButtonHTML + ''; - template = template.split("").join(backButtonHTML); + let backButtonHtml = '' + project.backButtonHtml + ''; + template = InsertHtmlAtMark(backButtonHtml, template, 'backButton'); } // Auto-start at the "Start" section From 310bf9114ed7257c6de99a8686e0f9afdbc57026 Mon Sep 17 00:00:00 2001 From: Nathaniel Nelson Date: Thu, 21 Dec 2017 10:39:09 -0700 Subject: [PATCH 3/3] Stories can now define template path relative to Fractive templates folder --- doc/build/index.html | 3 ++- doc/fractive.json | 5 ++--- doc/template.html | 3 ++- examples/basic/fractive.json | 4 ++-- examples/callbacks/fractive.json | 2 +- examples/macros/fractive.json | 2 +- examples/tags/fractive.json | 2 +- src/Compiler.ts | 28 ++++++++++++++++++++-------- src/ProjectSchema.json | 2 +- 9 files changed, 32 insertions(+), 19 deletions(-) diff --git a/doc/build/index.html b/doc/build/index.html index c855508..fa0b9bd 100644 --- a/doc/build/index.html +++ b/doc/build/index.html @@ -1185,7 +1185,8 @@

Scripting

- Back + + Back

diff --git a/doc/fractive.json b/doc/fractive.json index 4b21ccd..9d65e8a 100644 --- a/doc/fractive.json +++ b/doc/fractive.json @@ -40,6 +40,5 @@ "prepend": false } }, - "includeBackButton": true, - "backButtonHTML": "Back" -} \ No newline at end of file + "includeBackButton": false +} diff --git a/doc/template.html b/doc/template.html index 9a00aef..cd7c66c 100644 --- a/doc/template.html +++ b/doc/template.html @@ -191,7 +191,8 @@

- Back + + Back

diff --git a/examples/basic/fractive.json b/examples/basic/fractive.json index e392843..84fe995 100644 --- a/examples/basic/fractive.json +++ b/examples/basic/fractive.json @@ -1,11 +1,11 @@ { "title": "basic", - "template": "../../templates/basic.html", + "template": "{examples}/basic.html", "linkTooltips": true, "linkTags": { "external": { "html": " " } }, - "backButtonHTML": " Back" + "backButtonHtml": " Back" } diff --git a/examples/callbacks/fractive.json b/examples/callbacks/fractive.json index bf2c96f..22b7a39 100644 --- a/examples/callbacks/fractive.json +++ b/examples/callbacks/fractive.json @@ -1,4 +1,4 @@ { "title": "callbacks", - "template": "../../templates/basic.html" + "template": "{examples}/basic.html" } diff --git a/examples/macros/fractive.json b/examples/macros/fractive.json index f111d63..b89a9b7 100644 --- a/examples/macros/fractive.json +++ b/examples/macros/fractive.json @@ -4,7 +4,7 @@ { "alias": "TestAdjective", "replaceWith": "awesome" }, { "alias": "red", "replaceWith": "", "end": "" } ], - "template": "../../templates/basic.html", + "template": "{examples}/basic.html", "linkTooltips": true, "linkTags": { "external": { diff --git a/examples/tags/fractive.json b/examples/tags/fractive.json index b0b46f7..90badde 100644 --- a/examples/tags/fractive.json +++ b/examples/tags/fractive.json @@ -1,6 +1,6 @@ { "title": "basic", - "template": "../../templates/basic.html", + "template": "{examples}/basic.html", "linkTooltips": true, "linkTags": { "external": { diff --git a/src/Compiler.ts b/src/Compiler.ts index fef0c22..a9e6494 100644 --- a/src/Compiler.ts +++ b/src/Compiler.ts @@ -74,7 +74,7 @@ export let ProjectDefaults : FractiveProject = { } }, includeBackButton: true, - backButtonHTML: "Back" + backButtonHtml: "Back" }; import * as globby from "globby"; @@ -105,18 +105,20 @@ export namespace Compiler /** * Inserts the given html snippet into the template HTML at EVERY point where - * a specially formatted comment appears. + * a specially formatted comment appears: * @param snippet The html to insert * @param template The template in which to insert + * @param mark The mark at which to insert Html + * @param requiired Whether the mark is required or not. Default: true * @return The complete resulting html file contents */ - function InsertHtmlAtMark(snippet : string, template : string, mark : string) : string + function InsertHtmlAtMark(snippet : string, template : string, mark : string, required : boolean = true) : string { // The mark has to be placed inside an HTML comment formatted like so: let markComment : string = ``; // Throw an error if the mark doesn't exist - if (template.indexOf(markComment) === -1) + if (template.indexOf(markComment) === -1 && required) { console.log(`Template file must contain mark: ${markComment}`); process.exit(1); @@ -134,8 +136,19 @@ export namespace Compiler */ function ApplyTemplate(basePath : string, html : string, javascript : string) : string { + let templatePath : string = ""; + + // If project.template begins with the macro '{examples}/', resolve relative + // to the templates folder of the Fractive installation, not the build path + if (project.template.indexOf("{examples}") == 0) { + templatePath = project.template.replace("{examples}", `${__dirname}/../templates`); + } + // Otherwise, the path is relative to the story's root directory + else { + templatePath = path.resolve(basePath, project.template); + } + // Ensure that the template file exists - let templatePath : string = path.resolve(basePath, project.template); if(!fs.existsSync(templatePath)) { console.log(`Template file not found: "${templatePath}"`); @@ -165,9 +178,8 @@ export namespace Compiler // Insert html-formatted story text template = InsertHtmlAtMark(html, template, 'story'); - // Insert the back button if specified to do so - if(project.includeBackButton) - { + // Insert the back button everywhere the template defines + if (project.includeBackButton) { let backButtonHtml = '' + project.backButtonHtml + ''; template = InsertHtmlAtMark(backButtonHtml, template, 'backButton'); } diff --git a/src/ProjectSchema.json b/src/ProjectSchema.json index 1af1955..8d5f270 100644 --- a/src/ProjectSchema.json +++ b/src/ProjectSchema.json @@ -84,7 +84,7 @@ } }, "includeBackButton": { "type": "boolean", "description": "Whether to include a back button in your story." }, - "backButtonHTML": { "type": "string", "description": "Raw HTML inserted in the place where the back button is defined to be." } + "backButtonHtml": { "type": "string", "description": "Raw HTML inserted in the place where the back button is defined to be." } }, "additionalProperties": false }