Skip to content

Commit

Permalink
Story for 2.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Cox authored and Dan Cox committed Sep 23, 2023
1 parent 0989640 commit bf1c688
Show file tree
Hide file tree
Showing 12 changed files with 933 additions and 810 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"eslint-config-standard": "^17.1.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-jest": "^27.4.0",
"eslint-plugin-jsdoc": "^46.8.1",
"eslint-plugin-jsdoc": "^46.8.2",
"eslint-plugin-node": "^11.0.0",
"eslint-plugin-promise": "^6.1.1",
"jest": "^29.7.0",
Expand Down
110 changes: 40 additions & 70 deletions src/Passage.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,16 @@ export default class Passage {
*/
#_text = '';

/**
* Internal PID of passage
* @private
*/
#_pid = -1;

/**
* A passage is the smallest unit of a story.
* (The 'pid' property is only used in Twine 2 HTML.)
* @function Passage
* @class
* @param {string} name - Name
* @param {string} text - Content
* @param {Array} tags - Tags
* @param {object} metadata - Metadata
* @param {number} pid - Passage ID (PID)
*/
constructor (name = '', text = '', tags = [], metadata = {}, pid = -1) {
constructor (name = '', text = '', tags = [], metadata = {}) {
// Set name
this.name = name;

Expand All @@ -56,9 +48,6 @@ export default class Passage {

// Sets text
this.text = text;

// Sets pid
this.pid = pid;
}

/**
Expand Down Expand Up @@ -140,26 +129,6 @@ export default class Passage {
}
}

/**
* Passage ID (PID)
* @public
* @memberof Passage
* @returns {number} Passage ID (PID)
*/
get pid () { return this.#_pid; }

/**
* @param {number} p - Replacement PID
*/
set pid (p) {
// Test if PID is a number
if (Number.isInteger(p)) {
this.#_pid = p;
} else {
throw new Error('PID should be a number!');
}
}

/**
* Return a Twee representation.
* @public
Expand All @@ -170,7 +139,7 @@ export default class Passage {
toTwee () {
// Start empty string.
let content = '';

// Write the name.
content += `:: ${this.name}`;

Expand Down Expand Up @@ -200,7 +169,7 @@ export default class Passage {
* @memberof Passage
* @returns {string} JSON string.
*/
toJSON() {
toJSON () {
// Create an initial object for later serialization.
const p = {
name: this.name,
Expand All @@ -215,32 +184,34 @@ export default class Passage {

/**
* Return Twine 2 HTML representation.
* (Default Passage ID is 1.)
* @public
* @function toTwine2HTML
* @param {number} pid - Passage ID (PID) to record in HTML.
* @returns {string} Twine 2 HTML string.
*/
toTwine2HTML() {
// Start the passage element
toTwine2HTML (pid = 1) {
// Start the passage element.
let passageData = '\t<tw-passagedata';

/**
* pid: (string) Required.
* The Passage ID (PID).
*/
passageData += ` pid="${this.pid}"`;
passageData += ` pid="${pid}"`;

/**
* name: (string) Required.
* The name of the passage.
*/
* name: (string) Required.
* The name of the passage.
*/
passageData += ` name="${this.name}"`;

/**
* tags: (string) Optional.
* Any tags for the passage separated by spaces.
*/
passageData += ` tags="${this.#_tags.join(' ')}" `;

/**
* position: (string) Optional.
* Comma-separated X and Y position of the upper-left of the passage
Expand All @@ -249,7 +220,7 @@ export default class Passage {
if (Object.prototype.hasOwnProperty.call(this.#_metadata, 'position')) {
passageData += ` position="${this.#_metadata.position}" `;
}

/**
* size: (string) Optional.
* Comma-separated width and height of the passage
Expand All @@ -265,7 +236,6 @@ export default class Passage {
* @returns {string} Escaped text.
*/
const escape = function (text) {

const rules = [
['&', '&amp;'],
['<', '&lt;'],
Expand All @@ -280,8 +250,8 @@ export default class Passage {
});

return text;
}
};

// Add the text and close the element.
passageData += `>${escape(this.text)}</tw-passagedata>\n`;

Expand All @@ -290,53 +260,53 @@ export default class Passage {
}

/**
* Return Twine 1 HTML representation.
* @public
* @function toTwine2HTML
* @returns {string} Twine 1 HTML string.
*/
toTwine1HTML() {
* Return Twine 1 HTML representation.
* @public
* @function toTwine2HTML
* @returns {string} Twine 1 HTML string.
*/
toTwine1HTML () {
/**
* <div
created="202306020121"
modifier="twee"
* <div
created="2023 06 02 012 1"
modifier="twee"
twine-position="10,10">[[One passage]]</div>
*/
// Start the passage element
let passageData = '\t<div';
// Start the passage element
let passageData = '\t<div';

/**
* tiddler: (string) Required.
* The name of the passage.
*/
/**
* tiddler: (string) Required.
* The name of the passage.
*/
passageData += ` tiddler="${this.name}"`;

/**
* tags: (string) Required.
* Any tags for the passage separated by spaces.
*/
passageData += ` tags="${this.#_tags.join(' ')}" `;

/**
* modifier: (string) Optional.
* Name of the tool that last edited the passage.
* Generally, for versions of Twine 1, this value will be "twee".
* modifier: (string) Optional.
* Name of the tool that last edited the passage.
* Generally, for versions of Twine 1, this value will be "twee".
* Twee compilers may place their own name (e.g. "tweego" for Tweego).
*/
passageData += ` modifier="extwee"` ;
passageData += ' modifier="extwee"';

/**
* twine-position: (string) Required.
* Comma-separated X and Y coordinates of the passage within Twine 1.
*/
// If the metadata contains 'position', we will use it.
if (Object.prototype.hasOwnProperty.call(this.#_metadata, 'position')) {
passageData += ` twine-position="${this.#_metadata.position}"` ;
passageData += ` twine-position="${this.#_metadata.position}"`;
} else {
// Default is 10, 10
passageData += ` twine-position="10,10"` ;
passageData += ' twine-position="10,10"';
}

/**
* text: (string) Required.
* Text content of the passage.
Expand Down
Loading

0 comments on commit bf1c688

Please sign in to comment.