extwee 2.0.6 → 2.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (233) hide show
  1. package/.eslintrc.json +25 -25
  2. package/.github/FUNDING.yml +3 -0
  3. package/.github/dependabot.yml +11 -0
  4. package/.github/workflows/nodejs.yml +25 -24
  5. package/.travis.yml +13 -13
  6. package/CODE_OF_CONDUCT.md +82 -82
  7. package/LICENSE +21 -21
  8. package/README.md +173 -36
  9. package/SECURITY.md +12 -12
  10. package/babel.config.json +18 -22
  11. package/build/extwee +0 -0
  12. package/build/extwee.exe +0 -0
  13. package/build/extwee.web.min.js +2 -0
  14. package/build/extwee.web.min.js.LICENSE.txt +1 -0
  15. package/docs/.nojekyll +0 -0
  16. package/docs/README.md +167 -0
  17. package/docs/_sidebar.md +20 -0
  18. package/docs/examples/dynamicPassages.md +28 -0
  19. package/docs/examples/jsonToTwee.md +23 -0
  20. package/docs/examples/twsToTwee.md +25 -0
  21. package/docs/formats/json.md +17 -0
  22. package/docs/formats/twee.md +13 -0
  23. package/docs/formats/twine1HTML.md +13 -0
  24. package/docs/formats/twine2ArchiveHTML.md +13 -0
  25. package/docs/formats/twine2HTML.md +13 -0
  26. package/docs/formats/tws.md +9 -0
  27. package/docs/index.html +26 -0
  28. package/docs/install/binaries.md +9 -0
  29. package/docs/install/npm.md +20 -0
  30. package/docs/install/npx.md +9 -0
  31. package/docs/objects/passage.md +47 -0
  32. package/docs/objects/story.md +70 -0
  33. package/docs/objects/storyformat.md +27 -0
  34. package/index.html +22 -0
  35. package/index.js +29 -31
  36. package/package.json +68 -58
  37. package/src/JSON/parse.js +128 -0
  38. package/src/Passage.js +279 -202
  39. package/src/Story.js +653 -523
  40. package/src/StoryFormat/parse.js +134 -0
  41. package/src/StoryFormat.js +259 -300
  42. package/src/TWS/parse.js +86 -0
  43. package/src/Twee/parse.js +157 -0
  44. package/src/Twine1HTML/compile.js +58 -0
  45. package/src/Twine1HTML/parse.js +134 -0
  46. package/src/Twine2ArchiveHTML/compile.js +36 -0
  47. package/src/Twine2ArchiveHTML/parse.js +49 -0
  48. package/src/Twine2HTML/compile.js +35 -0
  49. package/src/Twine2HTML/parse.js +349 -0
  50. package/src/extwee.js +206 -0
  51. package/test/CLI/CLI.test.js +49 -0
  52. package/test/CLI/files/example.json +1 -0
  53. package/test/CLI/files/example6.twee +22 -0
  54. package/test/{Roundtrip → CLI/files}/harlowe.js +2 -2
  55. package/test/CLI/{input.html → files/input.html} +47 -47
  56. package/test/CLI/files/output/test.twee +0 -0
  57. package/test/CLI/{tweeExample.twee → files/tweeExample.twee} +17 -17
  58. package/test/CLI/files/twine1/LICENSE.txt +32 -0
  59. package/test/CLI/files/twine1/code.js +5 -0
  60. package/test/CLI/files/twine1/engine.js +43 -0
  61. package/test/CLI/files/twine1/header.html +325 -0
  62. package/test/CLI/files/twine1Test.html +371 -0
  63. package/test/CLI/{twineExample.html → files/twineExample.html} +16 -15
  64. package/test/JSON/JSON.Parse.test.js +316 -0
  65. package/test/Passage.test.js +244 -104
  66. package/test/Roundtrip/{Example1.html → Files/Example1.html} +63 -63
  67. package/test/Roundtrip/Files/LICENSE +19 -0
  68. package/test/Roundtrip/Files/example1.twee +10 -0
  69. package/test/Roundtrip/{example2.twee → Files/example2.twee} +27 -18
  70. package/test/Roundtrip/{example4.twee → Files/example4.twee} +27 -27
  71. package/test/{StoryFormatParser → Roundtrip/Files}/harlowe.js +2 -2
  72. package/test/Roundtrip/{round.html → Files/round.html} +6 -4
  73. package/test/Roundtrip/Roundtrip.test.js +54 -0
  74. package/test/Story.test.js +677 -423
  75. package/test/StoryFormat/StoryFormat.Parse.test.js +91 -0
  76. package/test/{StoryFormatParser → StoryFormat/StoryFormatParser}/example.js +3 -3
  77. package/test/{StoryFormatParser → StoryFormat/StoryFormatParser}/example2.js +3 -3
  78. package/test/{CLI → StoryFormat/StoryFormatParser}/harlowe.js +2 -2
  79. package/test/StoryFormat.test.js +152 -152
  80. package/test/TWS/Parse.test.js +78 -0
  81. package/test/TWS/TWSParser/Example1.tws +150 -0
  82. package/test/TWS/TWSParser/Example5.tws +414 -0
  83. package/test/TWS/TWSParser/noscale.tws +0 -0
  84. package/test/TWS/TWSParser/nostory.tws +0 -0
  85. package/test/Twee/Twee.Parse.test.js +76 -0
  86. package/test/{TweeParser → Twee/TweeParser}/emptytags.twee +2 -2
  87. package/test/{TweeParser → Twee/TweeParser}/example.twee +32 -32
  88. package/test/{TweeParser → Twee/TweeParser}/missing.twee +19 -19
  89. package/test/{TweeParser → Twee/TweeParser}/multipleScriptPassages.twee +19 -19
  90. package/test/{TweeParser → Twee/TweeParser}/multipleStyleTag.twee +19 -19
  91. package/test/{TweeParser → Twee/TweeParser}/multipletags.twee +10 -10
  92. package/test/{TweeParser → Twee/TweeParser}/noTitle.twee +2 -2
  93. package/test/{TweeParser → Twee/TweeParser}/notes.twee +16 -16
  94. package/test/{TweeParser → Twee/TweeParser}/pasagemetadataerror.twee +2 -2
  95. package/test/{TweeParser → Twee/TweeParser}/scriptPassage.twee +16 -16
  96. package/test/{TweeParser → Twee/TweeParser}/singletag.twee +13 -13
  97. package/test/{TweeParser → Twee/TweeParser}/startMetadata.twee +14 -14
  98. package/test/{TweeParser → Twee/TweeParser}/storydataerror.twee +25 -25
  99. package/test/{TweeParser → Twee/TweeParser}/stylePassage.twee +16 -16
  100. package/test/{Story → Twee/TweeParser}/test.twee +25 -25
  101. package/test/Twine1HTML/Twine1HTML.Compile.test.js +180 -0
  102. package/test/Twine1HTML/Twine1HTML.Parse.test.js +183 -0
  103. package/test/Twine1HTML/Twine1HTMLCompiler/Twine1/LICENSE +674 -0
  104. package/test/Twine1HTML/Twine1HTMLCompiler/Twine1/engine.js +43 -0
  105. package/test/Twine1HTML/Twine1HTMLCompiler/Twine1/jquery.js +4 -0
  106. package/test/Twine1HTML/Twine1HTMLCompiler/Twine1/modernizr.js +4 -0
  107. package/test/Twine1HTML/Twine1HTMLCompiler/engineTest.html +1 -0
  108. package/test/Twine1HTML/Twine1HTMLCompiler/jonah-1.4.2/LICENSE +32 -0
  109. package/test/Twine1HTML/Twine1HTMLCompiler/jonah-1.4.2/code.js +4 -0
  110. package/test/Twine1HTML/Twine1HTMLCompiler/jonah-1.4.2/header.html +327 -0
  111. package/test/Twine1HTML/Twine1HTMLCompiler/test.html +0 -0
  112. package/test/Twine1HTML/Twine1HTMLCompiler/test1.html +6 -0
  113. package/test/Twine1HTML/Twine1HTMLCompiler/test2.html +6 -0
  114. package/test/Twine1HTML/Twine1HTMLCompiler/test3.html +43 -0
  115. package/test/Twine1HTML/Twine1HTMLCompiler/test4.html +372 -0
  116. package/test/Twine1HTML/Twine1HTMLCompiler/test5.html +372 -0
  117. package/test/Twine2ArchiveHTML/Twine2ArchiveHTML.Compile.test.js +35 -0
  118. package/test/Twine2ArchiveHTML/Twine2ArchiveHTML.Parse.test.js +25 -0
  119. package/test/Twine2ArchiveHTML/Twine2ArchiveHTMLCompiler/test1.html +6 -0
  120. package/test/Twine2ArchiveHTML/Twine2ArchiveHTMLParser/test1.html +3 -0
  121. package/test/Twine2HTML/Twine2HTML.Compile.test.js +224 -0
  122. package/test/Twine2HTML/Twine2HTML.Parse.test.js +173 -0
  123. package/test/{HTMLWriter → Twine2HTML/Twine2HTMLCompiler}/creator.html +4 -4
  124. package/test/{CLI → Twine2HTML/Twine2HTMLCompiler}/example6.twee +15 -15
  125. package/test/{HTMLWriter → Twine2HTML/Twine2HTMLCompiler}/missingStoryTitle.twee +29 -29
  126. package/test/{HTMLWriter → Twine2HTML/Twine2HTMLCompiler}/test2.html +10 -8
  127. package/test/{HTMLWriter → Twine2HTML/Twine2HTMLCompiler}/test3.html +1 -1
  128. package/test/{HTMLWriter → Twine2HTML/Twine2HTMLCompiler}/test4.html +4 -4
  129. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/Example1.html +52 -52
  130. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/Tags.html +15 -15
  131. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/lyingStartnode.html +15 -15
  132. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/lyingTagColors.html +48 -48
  133. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/missingCreator.html +11 -11
  134. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/missingCreatorVersion.html +11 -11
  135. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/missingFormat.html +11 -11
  136. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/missingFormatVersion.html +11 -11
  137. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/missingIFID.html +11 -11
  138. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/missingName.html +33 -33
  139. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/missingPID.html +15 -15
  140. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/missingPassageName.html +15 -15
  141. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/missingPassageTags.html +15 -15
  142. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/missingPosition.html +15 -15
  143. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/missingScript.html +14 -14
  144. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/missingSize.html +35 -35
  145. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/missingStartnode.html +11 -11
  146. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/missingStyle.html +14 -14
  147. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/missingZoom.html +11 -11
  148. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/twineExample.html +23 -23
  149. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/twineExample2.html +15 -15
  150. package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/twineExample3.html +15 -15
  151. package/test/Twine2HTML/Twine2HTMLParser/unescaping.html +33 -0
  152. package/tsconfig.json +21 -0
  153. package/types/index.d.ts +14 -0
  154. package/types/src/JSON/parse.d.ts +8 -0
  155. package/types/src/Passage.d.ts +72 -0
  156. package/types/src/Story.d.ts +161 -0
  157. package/types/src/StoryFormat/parse.d.ts +7 -0
  158. package/types/src/StoryFormat.d.ts +97 -0
  159. package/types/src/TWS/parse.d.ts +10 -0
  160. package/types/src/Twee/parse.d.ts +10 -0
  161. package/types/src/Twine1HTML/compile.d.ts +17 -0
  162. package/types/src/Twine1HTML/parse.d.ts +10 -0
  163. package/types/src/Twine2ArchiveHTML/compile.d.ts +6 -0
  164. package/types/src/Twine2ArchiveHTML/parse.d.ts +6 -0
  165. package/types/src/Twine2HTML/compile.d.ts +9 -0
  166. package/types/src/Twine2HTML/parse.d.ts +17 -0
  167. package/types/src/extwee.d.ts +2 -0
  168. package/web-index.js +29 -0
  169. package/webpack.config.js +12 -0
  170. package/bin/extwee.js +0 -47
  171. package/src/FileReader.js +0 -33
  172. package/src/HTMLParser.js +0 -345
  173. package/src/HTMLWriter.js +0 -231
  174. package/src/StoryFormatParser.js +0 -142
  175. package/src/TweeParser.js +0 -161
  176. package/src/TweeWriter.js +0 -121
  177. package/story-formats/chapbook-1.2.0/format.js +0 -1
  178. package/story-formats/chapbook-1.2.0/logo.svg +0 -1
  179. package/story-formats/harlowe-1.2.4/format.js +0 -1
  180. package/story-formats/harlowe-1.2.4/icon.svg +0 -78
  181. package/story-formats/harlowe-2.1.0/format.js +0 -2
  182. package/story-formats/harlowe-2.1.0/icon.svg +0 -78
  183. package/story-formats/harlowe-3.1.0/format.js +0 -3
  184. package/story-formats/harlowe-3.1.0/icon.svg +0 -78
  185. package/story-formats/paperthin-1.0.0/format.js +0 -1
  186. package/story-formats/paperthin-1.0.0/icon.svg +0 -5
  187. package/story-formats/snowman-1.4.0/format.js +0 -1
  188. package/story-formats/snowman-1.4.0/icon.svg +0 -436
  189. package/story-formats/snowman-2.0.2/format.js +0 -1
  190. package/story-formats/snowman-2.0.2/icon.svg +0 -436
  191. package/story-formats/sugarcube-1.0.35/LICENSE +0 -23
  192. package/story-formats/sugarcube-1.0.35/format.js +0 -1
  193. package/story-formats/sugarcube-1.0.35/icon.svg +0 -56
  194. package/story-formats/sugarcube-2.31.1/LICENSE +0 -22
  195. package/story-formats/sugarcube-2.31.1/format.js +0 -1
  196. package/story-formats/sugarcube-2.31.1/icon.svg +0 -56
  197. package/test/CLI/test2.html +0 -45
  198. package/test/CLI.test.js +0 -30
  199. package/test/FileReader/t1.txt +0 -1
  200. package/test/FileReader.test.js +0 -14
  201. package/test/HTMLParser.test.js +0 -177
  202. package/test/HTMLWriter/example6.twee +0 -16
  203. package/test/HTMLWriter.test.js +0 -287
  204. package/test/Roundtrip/example1.twee +0 -21
  205. package/test/Roundtrip.test.js +0 -48
  206. package/test/Story/startmeta.twee +0 -29
  207. package/test/StoryFormatParser.test.js +0 -91
  208. package/test/TweeParser/test.twee +0 -25
  209. package/test/TweeParser.test.js +0 -79
  210. package/test/TweeWriter/test1.twee +0 -18
  211. package/test/TweeWriter/test3.twee +0 -12
  212. package/test/TweeWriter/test4.twee +0 -14
  213. package/test/TweeWriter/test5.twee +0 -20
  214. package/test/TweeWriter/test6.twee +0 -15
  215. package/test/TweeWriter/test7.twee +0 -15
  216. package/test/TweeWriter.test.js +0 -107
  217. /package/test/CLI/{test.twee → files/test.twee} +0 -0
  218. /package/test/{StoryFormatParser → StoryFormat/StoryFormatParser}/format.js +0 -0
  219. /package/test/{StoryFormatParser → StoryFormat/StoryFormatParser}/format_doublename.js +0 -0
  220. /package/test/{StoryFormatParser → StoryFormat/StoryFormatParser}/missingAuthor.js +0 -0
  221. /package/test/{StoryFormatParser → StoryFormat/StoryFormatParser}/missingDescription.js +0 -0
  222. /package/test/{StoryFormatParser → StoryFormat/StoryFormatParser}/missingImage.js +0 -0
  223. /package/test/{StoryFormatParser → StoryFormat/StoryFormatParser}/missingLicense.js +0 -0
  224. /package/test/{StoryFormatParser → StoryFormat/StoryFormatParser}/missingName.js +0 -0
  225. /package/test/{StoryFormatParser → StoryFormat/StoryFormatParser}/missingProofing.js +0 -0
  226. /package/test/{StoryFormatParser → StoryFormat/StoryFormatParser}/missingSource.js +0 -0
  227. /package/test/{StoryFormatParser → StoryFormat/StoryFormatParser}/missingURL.js +0 -0
  228. /package/test/{StoryFormatParser → StoryFormat/StoryFormatParser}/missingVersion.js +0 -0
  229. /package/test/{StoryFormatParser → StoryFormat/StoryFormatParser}/versionWrong.js +0 -0
  230. /package/test/{HTMLWriter → Twine2HTML/Twine2HTMLCompiler}/TestTags.html +0 -0
  231. /package/test/{HTMLWriter → Twine2HTML/Twine2HTMLCompiler}/test11.html +0 -0
  232. /package/test/{HTMLWriter → Twine2HTML/Twine2HTMLCompiler}/test6.html +0 -0
  233. /package/test/{HTMLParser → Twine2HTML/Twine2HTMLParser}/tagColors.html +0 -0
package/src/Passage.js CHANGED
@@ -1,202 +1,279 @@
1
- /**
2
- * @class Passage
3
- * @module Passage
4
- */
5
- export default class Passage {
6
- /**
7
- * Name of the Passage
8
- *
9
- * @private
10
- */
11
- #_name = null;
12
-
13
- /**
14
- * Internal array of tags
15
- *
16
- * @private
17
- */
18
- #_tags = [];
19
-
20
- /**
21
- * Internal metadata of passage
22
- *
23
- * @private
24
- */
25
- #_metadata = {};
26
-
27
- /**
28
- * Internal text of the passage
29
- *
30
- * @private
31
- */
32
- #_text = '';
33
-
34
- /**
35
- * Internal PID of passage
36
- *
37
- * @private
38
- */
39
- #_pid = -1;
40
-
41
- /**
42
- * @function Passage
43
- * @class
44
- * @param {string} name - Name
45
- * @param {string} text - Content
46
- * @param {Array} tags - Tags
47
- * @param {object} metadata - Metadata
48
- * @param {number} pid - Passage ID (PID)
49
- */
50
- constructor (name = '', text = '', tags = [], metadata = {}, pid = -1) {
51
- // Set name
52
- this.name = name;
53
-
54
- // Set tags
55
- this.tags = tags;
56
-
57
- // Set metadata
58
- this.metadata = metadata;
59
-
60
- // Sets text
61
- this.text = text;
62
-
63
- // Sets pid
64
- this.pid = pid;
65
- }
66
-
67
- /**
68
- * Name
69
- *
70
- * @public
71
- * @memberof Passage
72
- * @returns {string} Name
73
- */
74
- get name () { return this.#_name; }
75
-
76
- /**
77
- * @param {string} s - Name to replace
78
- */
79
- set name (s) {
80
- if (typeof s === 'string') {
81
- this.#_name = s;
82
- } else {
83
- throw new Error('Name must be a String!');
84
- }
85
- }
86
-
87
- /**
88
- * Tags
89
- *
90
- * @public
91
- * @memberof Passage
92
- * @returns {Array} Tags
93
- */
94
- get tags () { return this.#_tags; }
95
-
96
- /**
97
- * @param {Array} t - Replacement array
98
- */
99
- set tags (t) {
100
- // Test if tags is an array
101
- if (Array.isArray(t)) {
102
- this.#_tags = t;
103
- } else {
104
- throw new Error('Tags must be an array!');
105
- }
106
- }
107
-
108
- /**
109
- * Metadata
110
- *
111
- * @public
112
- * @memberof Passage
113
- * @returns {object} Metadata
114
- */
115
- get metadata () { return this.#_metadata; }
116
-
117
- /**
118
- * @param {object} m - Replacement object
119
- */
120
- set metadata (m) {
121
- // Test if metadata was an object
122
- if (typeof m === 'object') {
123
- this.#_metadata = m;
124
- } else {
125
- throw new Error('Metadata should be an object literal!');
126
- }
127
- }
128
-
129
- /**
130
- * Text
131
- *
132
- * @public
133
- * @memberof Passage
134
- * @returns {string} Text
135
- */
136
- get text () { return this.#_text; }
137
-
138
- /**
139
- * @param {string} t - Replacement text
140
- */
141
- set text (t) {
142
- // Test if text is a String
143
- if (typeof t === 'string') {
144
- this.#_text = t;
145
- } else {
146
- throw new Error('Text should be a String!');
147
- }
148
- }
149
-
150
- /**
151
- * Passage ID (PID)
152
- *
153
- * @public
154
- * @memberof Passage
155
- * @returns {number} Passage ID (PID)
156
- */
157
- get pid () { return this.#_pid; }
158
-
159
- /**
160
- * @param {number} p - Replacement PID
161
- */
162
- set pid (p) {
163
- // Test if PID is a number
164
- if (Number.isInteger(p)) {
165
- this.#_pid = p;
166
- } else {
167
- throw new Error('PID should be a number!');
168
- }
169
- }
170
-
171
- /**
172
- * Return a String representation
173
- *
174
- * @public
175
- * @memberof Passage
176
- * @returns {string} String form of passage
177
- */
178
- toString () {
179
- // Start empty string.
180
- let content = '';
181
- // Write the name
182
- content += `:: ${this.name}`;
183
-
184
- // Test if it has any tags
185
- if (this.tags.length > 0) {
186
- // Write output of tags
187
- content += ` [${this.tags.join(' ')}]`;
188
- }
189
-
190
- // Check if any properties exist
191
- if (Object.keys(this.metadata).length > 0) {
192
- // Write out a space and then passage metadata
193
- content += ` ${JSON.stringify(this.metadata)}`;
194
- }
195
-
196
- // Add newline, text, and two newlines
197
- content += `\n${this.text}\n\n`;
198
-
199
- // Return string.
200
- return content;
201
- }
202
- }
1
+ import { encode } from 'html-entities';
2
+
3
+ /**
4
+ * A passage is the smallest unit of a Twine story.
5
+ */
6
+ export default class Passage {
7
+ /**
8
+ * Name of the Passage
9
+ * @private
10
+ */
11
+ #_name = null;
12
+
13
+ /**
14
+ * Internal array of tags
15
+ * @private
16
+ */
17
+ #_tags = [];
18
+
19
+ /**
20
+ * Internal metadata of passage
21
+ * @private
22
+ */
23
+ #_metadata = {};
24
+
25
+ /**
26
+ * Internal text of the passage
27
+ * @private
28
+ */
29
+ #_text = '';
30
+
31
+ /**
32
+ * Create a passage.
33
+ * @param {string} name - Name
34
+ * @param {string} text - Content
35
+ * @param {Array} tags - Tags
36
+ * @param {object} metadata - Metadata
37
+ */
38
+ constructor (name = '', text = '', tags = [], metadata = {}) {
39
+ // Set name
40
+ this.name = name;
41
+
42
+ // Set tags
43
+ this.tags = tags;
44
+
45
+ // Set metadata
46
+ this.metadata = metadata;
47
+
48
+ // Sets text
49
+ this.text = text;
50
+ }
51
+
52
+ /**
53
+ * Name
54
+ * @returns {string} Name
55
+ */
56
+ get name () { return this.#_name; }
57
+
58
+ /**
59
+ * @param {string} s - Name to replace
60
+ */
61
+ set name (s) {
62
+ if (typeof s === 'string') {
63
+ this.#_name = s;
64
+ } else {
65
+ throw new Error('Name must be a String!');
66
+ }
67
+ }
68
+
69
+ /**
70
+ * Tags
71
+ * @returns {Array} Tags
72
+ */
73
+ get tags () { return this.#_tags; }
74
+
75
+ /**
76
+ * @param {Array} t - Replacement array
77
+ */
78
+ set tags (t) {
79
+ // Test if tags is an array
80
+ if (Array.isArray(t)) {
81
+ // Set the tags.
82
+ this.#_tags = t;
83
+ } else {
84
+ throw new Error('Tags must be an array!');
85
+ }
86
+ }
87
+
88
+ /**
89
+ * Metadata
90
+ * @returns {object} Metadata
91
+ */
92
+ get metadata () { return this.#_metadata; }
93
+
94
+ /**
95
+ * @param {object} m - Replacement object
96
+ */
97
+ set metadata (m) {
98
+ // Test if metadata was an object
99
+ if (typeof m === 'object') {
100
+ this.#_metadata = m;
101
+ } else {
102
+ throw new Error('Metadata should be an object literal!');
103
+ }
104
+ }
105
+
106
+ /**
107
+ * Text
108
+ * @returns {string} Text
109
+ */
110
+ get text () { return this.#_text; }
111
+
112
+ /**
113
+ * @param {string} t - Replacement text
114
+ */
115
+ set text (t) {
116
+ // Test if text is a String
117
+ if (typeof t === 'string') {
118
+ this.#_text = t;
119
+ } else {
120
+ throw new Error('Text should be a String!');
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Return a Twee representation.
126
+ * @returns {string} String form of passage.
127
+ */
128
+ toTwee () {
129
+ // Start empty string.
130
+ let content = '';
131
+
132
+ // Write the name.
133
+ content += `:: ${this.name}`;
134
+
135
+ // Test if it has any tags.
136
+ if (this.tags.length > 0) {
137
+ // Write output of tags.
138
+ content += ` [${this.tags.join(' ')}]`;
139
+ }
140
+
141
+ // Check if any properties exist.
142
+ if (Object.keys(this.metadata).length > 0) {
143
+ // Write out a space and then passage metadata.
144
+ content += ` ${JSON.stringify(this.metadata)}`;
145
+ }
146
+
147
+ // Add newline, text, and two newlines.
148
+ content += `\n${this.text}\n\n`;
149
+
150
+ // Return string.
151
+ return content;
152
+ }
153
+
154
+ /**
155
+ * Return JSON representation.
156
+ * @returns {string} JSON string.
157
+ */
158
+ toJSON () {
159
+ // Create an initial object for later serialization.
160
+ const p = {
161
+ name: this.name,
162
+ tags: this.tags,
163
+ metadata: this.metadata,
164
+ text: this.text
165
+ };
166
+
167
+ // Return stringified JSON from simple object.
168
+ return JSON.stringify(p);
169
+ }
170
+
171
+ /**
172
+ * Return Twine 2 HTML representation.
173
+ * (Default Passage ID is 1.)
174
+ * @param {number} pid - Passage ID (PID) to record in HTML.
175
+ * @returns {string} Twine 2 HTML string.
176
+ */
177
+ toTwine2HTML (pid = 1) {
178
+ // Start the passage element.
179
+ let passageData = '\t<tw-passagedata';
180
+
181
+ /**
182
+ * pid: (string) Required.
183
+ * The Passage ID (PID).
184
+ */
185
+ passageData += ` pid="${pid}"`;
186
+
187
+ /**
188
+ * name: (string) Required.
189
+ * The name of the passage.
190
+ */
191
+ passageData += ` name="${ encode( this.name ) }"`;
192
+
193
+ /**
194
+ * tags: (string) Optional.
195
+ * Any tags for the passage separated by spaces.
196
+ */
197
+ passageData += ` tags="${ encode( this.#_tags.join(' ') ) }" `;
198
+
199
+ /**
200
+ * position: (string) Optional.
201
+ * Comma-separated X and Y position of the upper-left of the passage
202
+ * when viewed within the Twine 2 editor.
203
+ */
204
+ if (Object.prototype.hasOwnProperty.call(this.#_metadata, 'position')) {
205
+ passageData += ` position="${this.#_metadata.position}" `;
206
+ }
207
+
208
+ /**
209
+ * size: (string) Optional.
210
+ * Comma-separated width and height of the passage
211
+ * when viewed within the Twine 2 editor.
212
+ */
213
+ if (Object.prototype.hasOwnProperty.call(this.#_metadata, 'size')) {
214
+ passageData += `size="${this.#_metadata.size}" `;
215
+ }
216
+
217
+ // Add the text and close the element.
218
+ passageData += `>${ encode( this.text ) }</tw-passagedata>\n`;
219
+
220
+ // Return the Twine 2 HTML element.
221
+ return passageData;
222
+ }
223
+
224
+ /**
225
+ * Return Twine 1 HTML representation.
226
+ * @returns {string} Twine 1 HTML string.
227
+ */
228
+ toTwine1HTML () {
229
+ /**
230
+ * <div
231
+ created="2023 06 02 012 1"
232
+ modifier="twee"
233
+ twine-position="10,10">[[One passage]]</div>
234
+ */
235
+ // Start the passage element
236
+ let passageData = '\t<div';
237
+
238
+ /**
239
+ * tiddler: (string) Required.
240
+ * The name of the passage.
241
+ */
242
+ passageData += ` tiddler="${ encode( this.name ) }"`;
243
+
244
+ /**
245
+ * tags: (string) Required.
246
+ * Any tags for the passage separated by spaces.
247
+ */
248
+ passageData += ` tags="${ encode( this.#_tags.join(' ') ) }" `;
249
+
250
+ /**
251
+ * modifier: (string) Optional.
252
+ * Name of the tool that last edited the passage.
253
+ * Generally, for versions of Twine 1, this value will be "twee".
254
+ * Twee compilers may place their own name (e.g. "tweego" for Tweego).
255
+ */
256
+ passageData += ' modifier="extwee"';
257
+
258
+ /**
259
+ * twine-position: (string) Required.
260
+ * Comma-separated X and Y coordinates of the passage within Twine 1.
261
+ */
262
+ // If the metadata contains 'position', we will use it.
263
+ if (Object.prototype.hasOwnProperty.call(this.#_metadata, 'position')) {
264
+ passageData += ` twine-position="${this.#_metadata.position}"`;
265
+ } else {
266
+ // Default is 10, 10
267
+ passageData += ' twine-position="10,10"';
268
+ }
269
+
270
+ /**
271
+ * text: (string) Required.
272
+ * Text content of the passage.
273
+ */
274
+ passageData += `>${ encode( this.#_text ) }</div>`;
275
+
276
+ // Return the HTML representation.
277
+ return passageData;
278
+ }
279
+ }