kni 4.0.3 → 5.0.0

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.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Kris Kowal
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -29,7 +29,7 @@ Use npm to install and run kni. The kni command line requires Node.js version 4
29
29
  or greater. Use a major version of kni explicitly.
30
30
 
31
31
  ```
32
- $ npm install kni@3
32
+ $ npm install kni@4
33
33
  $ PATH=$(pwd)/node_modules/.bin:$PATH
34
34
  $ kni
35
35
  ```
@@ -82,16 +82,16 @@ $ open hello.html
82
82
  - [Peruácru][peruacru] is an app for iOS and Android using Apache Cordova.
83
83
  A [slice](https://github.com/kriskowal/peruacru.then.land) of the game is open source for illustration.
84
84
 
85
- [![Peruácru Icon](https://github.com/kriskowal/kni/raw/master/examples/peruacru.png)][peruacru]
85
+ [![Peruácru Icon](examples/peruacru.png)][peruacru]
86
86
 
87
87
  - The [archery][] prototype illustrates a shop and gambling game.
88
88
 
89
- [![An Archery Shop](https://github.com/kriskowal/kni/raw/master/examples/archery.png)][archery]
89
+ [![An Archery Shop](examples/archery.png)][archery]
90
90
 
91
91
  - The [journey][] prototype illustrates a survival game over a
92
92
  procedurally-generated infinite road.
93
93
 
94
- [![A Journey through Ælfland](https://github.com/kriskowal/kni/raw/master/examples/journey.png)][journey]
94
+ [![A Journey through Ælfland](examples/journey.png)][journey]
95
95
 
96
96
  - The [airship][] prototype illustrates a narrative that includes
97
97
  a simulation of the control and behavior of a steampunk airship.
@@ -131,12 +131,12 @@ The command line tool can also:
131
131
  $ kni -J hello.json
132
132
  ```
133
133
 
134
- - Kni can also produce a diagnostique view of a story. The first column is
134
+ - Kni can also produce a diagnostic view of a story. The first column is
135
135
  the thread label, then the instruction type, a description of the
136
- instruction, and an indicator for the next thread. In the absense of an
136
+ instruction, and an indicator for the next thread. In the absence of an
137
137
  indicator, the engine proceeds to the next instruction. A forward arrow
138
138
  indicates a jump and a backward arrow indicates a return to a calling thread,
139
- a processsion to the next thread of a question or answer sequence, or an
139
+ a procession to the next thread of a question or answer sequence, or an
140
140
  exit.
141
141
 
142
142
  ```
@@ -162,3 +162,5 @@ DON’T PANIC
162
162
 
163
163
  Copyright © 2016 by Kristopher Kowal.
164
164
  All rights reserved.
165
+
166
+ [![Coverage Status](https://coveralls.io/repos/github/borkshop/kni/badge.svg)](https://coveralls.io/github/borkshop/kni)
package/console.js CHANGED
@@ -1,63 +1,61 @@
1
- 'use strict';
1
+ import Excerpt from './excerpt.js';
2
+ import Wrapper from './wrapper.js';
2
3
 
3
- module.exports = Console;
4
-
5
- var Excerpt = require('./excerpt');
6
- var Wrapper = require('./wrapper');
7
-
8
- function Console(writer) {
4
+ export default class Console {
5
+ constructor(writer) {
9
6
  this.writer = writer;
10
7
  this.wrapper = new Wrapper(writer);
11
8
  this.excerpt = new Excerpt();
12
9
  this.options = [];
13
10
  this.cursor = this.excerpt;
14
- }
11
+ }
15
12
 
16
- Console.prototype.write = function write(lift, text, drop) {
13
+ write(lift, text, drop) {
17
14
  this.cursor.digest(lift, text, drop);
18
- };
15
+ }
19
16
 
20
- Console.prototype.break = function _break() {
17
+ break() {
21
18
  this.cursor.break();
22
- };
19
+ }
23
20
 
24
- Console.prototype.paragraph = function paragraph() {
21
+ paragraph() {
25
22
  this.cursor.paragraph();
26
- };
23
+ }
27
24
 
28
- Console.prototype.startOption = function startOption() {
29
- var option = new Excerpt();
25
+ startOption() {
26
+ const option = new Excerpt();
30
27
  this.cursor = option;
31
28
  this.options.push(option);
32
- };
29
+ }
33
30
 
34
- Console.prototype.stopOption = function stopOption() {
31
+ stopOption() {
35
32
  this.cursor = this.excerpt;
36
- };
33
+ }
37
34
 
38
- Console.prototype.flush = function flush() {
35
+ flush() {
39
36
  this.writer.write('\n');
40
- };
37
+ }
41
38
 
42
- Console.prototype.pardon = function pardon() {
39
+ pardon() {
43
40
  this.writer.write('?\n');
44
- };
41
+ }
45
42
 
46
- Console.prototype.display = function display() {
43
+ display() {
47
44
  this.excerpt.write(this.wrapper);
48
- for (var i = 0; i < this.options.length; i++) {
49
- var number = i + 1;
50
- var lead = (number + '. ').slice(0, 3) + ' ';
51
- this.wrapper.word(lead);
52
- this.wrapper.flush = true;
53
- this.wrapper.push(' ', ' ');
54
- this.options[i].write(this.wrapper);
55
- this.wrapper.pop();
45
+ for (let i = 0; i < this.options.length; i++) {
46
+ const number = i + 1;
47
+ const lead = `${`${number}. `.slice(0, 3)} `;
48
+ this.wrapper.word(lead);
49
+ this.wrapper.flush = true;
50
+ this.wrapper.push(' ', ' ');
51
+ this.options[i].write(this.wrapper);
52
+ this.wrapper.pop();
56
53
  }
57
- };
54
+ }
58
55
 
59
- Console.prototype.clear = function clear() {
56
+ clear() {
60
57
  this.excerpt = new Excerpt();
61
58
  this.options = [];
62
59
  this.cursor = this.excerpt;
63
- };
60
+ }
61
+ }
package/describe.js CHANGED
@@ -1,116 +1,81 @@
1
- 'use strict';
1
+ const types = {};
2
2
 
3
- module.exports = describe;
3
+ const describe = node => {
4
+ return types[node.type](node);
5
+ };
4
6
 
5
- function describe(node) {
6
- return types[node.type](node);
7
- }
7
+ export default describe;
8
8
 
9
- var types = {};
9
+ types.text = node => node.text;
10
10
 
11
- types.text = function text(node) {
12
- return node.text;
13
- };
11
+ types.echo = node => S(node.expression);
14
12
 
15
- types.echo = function echo(node) {
16
- return S(node.expression);
17
- };
13
+ types.opt = node => `(Q ${node.question.join(' ')}) (A ${node.answer.join(' ')})`;
18
14
 
19
- types.opt = function opt(node) {
20
- return '(Q ' + node.question.join(' ') + ') (A ' + node.answer.join(' ') + ')';
21
- };
15
+ types.goto = _node => '';
22
16
 
23
- types.goto = function goto(node) {
24
- return '';
25
- };
17
+ types.call = node => `${node.label}(${node.args.map(S).join(' ')}) esc ${node.branch}`;
26
18
 
27
- types.call = function call(node) {
28
- return node.label + '(' + node.args.map(S).join(' ') + ') esc ' + node.branch;
29
- };
19
+ types.def = node => `(${node.locals.join(' ')})`;
30
20
 
31
- types.def = function def(node) {
32
- return '(' + node.locals.join(' ') + ')';
33
- };
21
+ types.jump = node => `${node.branch} if ${S(node.condition)}`;
34
22
 
35
- types.jump = function jump(node) {
36
- return node.branch + ' if ' + S(node.condition);
23
+ types.switch = node => {
24
+ let desc = '';
25
+ if (node.variable) {
26
+ desc += `(${node.variable}+${node.value}) ${S(node.expression)}`;
27
+ } else {
28
+ desc += S(node.expression);
29
+ }
30
+ desc += ` (${node.branches.join(' ')}) W(${node.weights.map(S).join(' ')})`;
31
+ return desc;
37
32
  };
38
33
 
39
- types.switch = function _switch(node) {
40
- var desc = '';
41
- if (node.variable) {
42
- desc += '(' + node.variable + '+' + node.value + ') ' + S(node.expression);
43
- } else {
44
- desc += S(node.expression);
45
- }
46
- desc += ' (' + node.branches.join(' ') + ') W(' + node.weights.map(S).join(' ') + ')';
47
- return desc;
48
- };
34
+ types.set = node => `${node.variable} ${S(node.expression)}`;
49
35
 
50
- types.set = function set(node) {
51
- return node.variable + ' ' + S(node.expression);
52
- };
36
+ types.move = node => `${S(node.source)} -> ${S(node.target)}`;
53
37
 
54
- types.move = function move(node) {
55
- return S(node.source) + ' -> ' + S(node.target);
56
- };
38
+ types.cue = node => node.cue;
57
39
 
58
- types.cue = function cue(node) {
59
- return node.cue;
60
- };
40
+ types.br = _node => '';
61
41
 
62
- types.br = function br(node) {
63
- return '';
64
- };
42
+ types.par = _node => '';
65
43
 
66
- types.par = function par(node) {
67
- return '';
68
- };
44
+ types.rule = _node => '';
69
45
 
70
- types.rule = function rule(node) {
71
- return '';
72
- };
46
+ types.startJoin = _node => '';
73
47
 
74
- types.startJoin = function startJoin(node) {
75
- return '';
76
- };
48
+ types.stopJoin = _node => '';
77
49
 
78
- types.stopJoin = function stopJoin(node) {
79
- return '';
80
- };
50
+ types.delimit = _node => '';
81
51
 
82
- types.delimit = function delimit(node) {
83
- return '';
84
- };
52
+ types.ask = _node => '';
85
53
 
86
- types.ask = function ask(node) {
87
- return '';
54
+ types.read = node => {
55
+ let label = node.variable;
56
+ if (node.cue != null) {
57
+ label += ` ${node.cue}`;
58
+ }
59
+ return label;
88
60
  };
89
61
 
90
- types.read = function ask(node) {
91
- var label = node.variable;
92
- if (node.cue != null) {
93
- label += ' ' + node.cue;
94
- }
95
- return label;
62
+ const S = args => {
63
+ if (args[0] === 'val' || args[0] === 'get') {
64
+ return args[1];
65
+ } else if (args[0] === 'var') {
66
+ return `(${args[0]} ${V(args[1], args[2])})`;
67
+ } else {
68
+ return `(${args[0]} ${args.slice(1).map(S).join(' ')})`;
69
+ }
96
70
  };
97
71
 
98
- function S(args) {
99
- if (args[0] === 'val' || args[0] === 'get') {
100
- return args[1];
101
- } else if (args[0] === 'var') {
102
- return '(' + args[0] + ' ' + V(args[1], args[2]) + ')';
103
- } else {
104
- return '(' + args[0] + ' ' + args.slice(1).map(S).join(' ') + ')';
105
- }
106
- }
107
-
108
- function V(source, target) {
109
- var r = '';
110
- for (var i = 0; i < target.length; i++) {
111
- r += source[i];
112
- r += '{' + S(target[i]) + '}';
113
- }
72
+ const V = (source, target) => {
73
+ let r = '';
74
+ let i;
75
+ for (i = 0; i < target.length; i++) {
114
76
  r += source[i];
115
- return r;
116
- }
77
+ r += `{${S(target[i])}}`;
78
+ }
79
+ r += source[i];
80
+ return r;
81
+ };
package/document.js CHANGED
@@ -1,9 +1,14 @@
1
- 'use strict';
1
+ const linkMatcher = /\s*(\w+:\/\/\S+)$/;
2
2
 
3
- module.exports = Document;
3
+ export default class Document {
4
+ constructor(element, options = {}) {
5
+ const {
6
+ createPage = undefined,
7
+ meterFaultButton = undefined,
8
+ pageTurnBehavior = 'log',
9
+ } = options;
4
10
 
5
- function Document(element, createPage) {
6
- var self = this;
11
+ const self = this;
7
12
  this.document = element.ownerDocument;
8
13
  this.parent = element;
9
14
  this.frame = null;
@@ -19,92 +24,114 @@ function Document(element, createPage) {
19
24
  this.options = null;
20
25
  this.p = false;
21
26
  this.br = false;
22
- this.onclick = onclick;
27
+ this.onclick = event => {
28
+ self.answer(event.target.number);
29
+ };
23
30
  this.createPage = createPage || this.createPage;
24
- function onclick(event) {
25
- self.answer(event.target.number);
26
- }
31
+ this.meterFaultButton = meterFaultButton;
32
+ this.pageTurnBehavior = pageTurnBehavior;
33
+
27
34
  Object.seal(this);
28
- }
35
+ }
29
36
 
30
- Document.prototype.write = function write(lift, text, drop) {
31
- var document = this.document;
37
+ write(lift, text, drop) {
38
+ const document = this.document;
32
39
  lift = this.carry || lift;
33
40
  if (this.p) {
34
- this.cursor = document.createElement("p");
35
- this.cursorParent.insertBefore(this.cursor, this.afterCursor);
36
- this.p = false;
37
- this.br = false;
38
- lift = '';
41
+ this.cursor = document.createElement('p');
42
+ this.cursorParent.insertBefore(this.cursor, this.afterCursor);
43
+ this.p = false;
44
+ this.br = false;
45
+ lift = '';
39
46
  }
40
47
  if (this.br) {
41
- this.cursor.appendChild(document.createElement("br"));
42
- this.br = false;
43
- lift = '';
48
+ this.cursor.appendChild(document.createElement('br'));
49
+ this.br = false;
50
+ lift = '';
51
+ }
52
+ const match = linkMatcher.exec(text);
53
+ if (match === null) {
54
+ // TODO merge with prior text node
55
+ this.cursor.appendChild(document.createTextNode(lift + text));
56
+ } else {
57
+ // Support a hyperlink convention.
58
+ if (lift !== '') {
59
+ this.cursor.appendChild(document.createTextNode(lift));
60
+ }
61
+ const link = document.createElement('a');
62
+ link.href = match[1];
63
+ link.target = '_blank';
64
+ link.rel = 'noreferrer';
65
+ link.appendChild(document.createTextNode(text.slice(0, match.index)));
66
+ this.cursor.appendChild(link);
44
67
  }
45
- // TODO merge with prior text node
46
- this.cursor.appendChild(document.createTextNode(lift + text));
47
68
  this.carry = drop;
48
- };
69
+ }
49
70
 
50
- Document.prototype.break = function _break() {
71
+ break() {
51
72
  this.br = true;
52
- };
73
+ }
53
74
 
54
- Document.prototype.paragraph = function paragraph() {
75
+ paragraph() {
55
76
  this.p = true;
56
- };
77
+ }
57
78
 
58
- Document.prototype.startOption = function startOption() {
79
+ startOption() {
59
80
  this.optionIndex++;
60
- var document = this.document;
61
- var tr = document.createElement("tr");
81
+ const document = this.document;
82
+ const tr = document.createElement('tr');
62
83
  this.options.appendChild(tr);
63
- var th = document.createElement("th");
84
+ const th = document.createElement('th');
64
85
  tr.appendChild(th);
65
- th.innerText = this.optionIndex + '.';
66
- var td = document.createElement("td");
86
+ th.innerText = `${this.optionIndex}.`;
87
+ const td = document.createElement('td');
67
88
  td.number = this.optionIndex;
68
89
  td.onclick = this.onclick;
69
- td.setAttribute("aria-role", "button");
90
+ td.setAttribute('aria-role', 'button');
70
91
  tr.appendChild(td);
71
92
  this.cursor = td;
72
93
  this.p = false;
73
94
  this.br = false;
74
95
  this.carry = '';
75
- };
96
+ }
76
97
 
77
- Document.prototype.stopOption = function stopOption() {
98
+ stopOption() {
78
99
  this.p = false;
79
100
  this.br = false;
80
- };
101
+ }
81
102
 
82
- Document.prototype.flush = function flush() {
103
+ flush() {
83
104
  // No-op (for console only)
84
- };
105
+ }
85
106
 
86
- Document.prototype.pardon = function pardon() {
107
+ pardon() {
87
108
  // No-op (for console only)
88
- };
109
+ }
89
110
 
90
- Document.prototype.display = function display() {
111
+ display() {
91
112
  this.frame.style.opacity = 0;
92
113
  this.frame.style.transform = 'translateX(2ex)';
93
114
  this.parent.appendChild(this.frame);
94
115
 
95
116
  // TODO not this
96
- var frame = this.frame;
97
- setTimeout(function () {
98
- frame.style.opacity = 1;
99
- frame.style.transform = 'translateX(0)';
117
+ const frame = this.frame;
118
+ setTimeout(() => {
119
+ frame.style.opacity = 1;
120
+ frame.style.transform = 'translateX(0)';
100
121
  }, 10);
101
- };
122
+ }
102
123
 
103
- Document.prototype.clear = function clear() {
124
+ clear() {
104
125
  if (this.frame) {
126
+ if (this.pageTurnBehavior === 'log') {
127
+ this.options.remove();
128
+ } else if (this.pageTurnBehavior === 'remove') {
129
+ this.frame.remove();
130
+ } else if (this.pageTurnBehavior === 'fade') {
105
131
  this.frame.style.opacity = 0;
106
132
  this.frame.style.transform = 'translateX(-2ex)';
107
- this.frame.addEventListener("transitionend", this);
133
+ this.frame.addEventListener('transitionend', this);
134
+ }
108
135
  }
109
136
  this.createPage(this.document, this);
110
137
  this.cursor = null;
@@ -114,46 +141,50 @@ Document.prototype.clear = function clear() {
114
141
  this.p = true;
115
142
  this.carry = '';
116
143
  this.optionIndex = 0;
117
- };
144
+ }
118
145
 
119
- Document.prototype.createPage = function createPage(document) {
120
- this.frame = document.createElement("div");
121
- this.frame.classList.add("kni-frame");
146
+ createPage(document) {
147
+ this.frame = document.createElement('div');
148
+ this.frame.classList.add('kni-frame');
122
149
  this.frame.style.opacity = 0;
123
150
 
124
- var A = document.createElement("div");
125
- A.classList.add("kni-frame-a");
151
+ const A = document.createElement('div');
152
+ A.classList.add('kni-frame-a');
126
153
  this.frame.appendChild(A);
127
154
 
128
- var B = document.createElement("div");
129
- B.classList.add("kni-frame-b");
155
+ const B = document.createElement('div');
156
+ B.classList.add('kni-frame-b');
130
157
  A.appendChild(B);
131
158
 
132
- var C = document.createElement("div");
133
- C.classList.add("kni-frame-c");
159
+ const C = document.createElement('div');
160
+ C.classList.add('kni-frame-c');
134
161
  B.appendChild(C);
135
162
 
136
- this.body = document.createElement("div");
137
- this.body.classList.add("kni-body");
163
+ this.body = document.createElement('div');
164
+ this.body.classList.add('kni-body');
138
165
  C.appendChild(this.body);
139
166
 
140
- this.options = document.createElement("table");
167
+ this.options = document.createElement('table');
141
168
  this.body.appendChild(this.options);
142
169
  this.afterBody = this.options;
143
- };
170
+ }
171
+
172
+ handleEvent(event) {
173
+ // transitionend on this.frame, only
174
+ event.target.remove();
175
+ }
144
176
 
145
- Document.prototype.handleEvent = function handleEvent(event) {
146
- if (event.target.parentNode === this.parent) {
147
- this.parent.removeChild(event.target);
177
+ meterFault() {
178
+ if (this.meterFaultButton) {
179
+ this.body.appendChild(this.meterFaultButton);
148
180
  }
149
- };
181
+ }
150
182
 
151
- Document.prototype.ask = function ask(cue) {
152
- };
183
+ ask(_cue) {}
153
184
 
154
- Document.prototype.answer = function answer(text) {
185
+ answer(text) {
155
186
  this.engine.answer(text);
156
- };
187
+ }
157
188
 
158
- Document.prototype.close = function close() {
159
- };
189
+ close() {}
190
+ }