drakongen 1.0.0 → 1.2.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/README.md CHANGED
@@ -15,7 +15,7 @@ Include the `drakongen.js` script on the web page.
15
15
  <script src="browser/drakongen.js"></script>
16
16
  ```
17
17
 
18
- Call `toPseudocode` or `toTree` functions.
18
+ Call `drakongen.toPseudocode` or `drakongen.toTree` functions.
19
19
 
20
20
  ```html
21
21
  <script>
@@ -23,10 +23,10 @@ var drakon = ... // Get the drakon chart from DrakonWidget
23
23
  var name = "Diagram one"
24
24
  var filename = "Diagram one.drakon"
25
25
 
26
- var pseudo = toPseudocode(drakon, name, filename, "en")
26
+ var pseudo = drakongen.toPseudocode(drakon, name, filename, "en")
27
27
  console.log(pseudo)
28
28
 
29
- var tree = toTree(drakon, name, filename, "en")
29
+ var tree = drakongen.toTree(drakon, name, filename, "en")
30
30
  console.log(tree)
31
31
 
32
32
  </script>
@@ -502,20 +502,22 @@ function markLoopBody(nodes, start, filename) {
502
502
  module.exports = { drakonToStruct, drakonToGraph };
503
503
  },{"./structFlow":6,"./tools":8}],4:[function(require,module,exports){
504
504
  const { drakonToPseudocode } = require('./drakonToPromptStruct');
505
- const {htmlToString} = require("./browserTools")
505
+ const { htmlToString } = require("./browserTools")
506
506
  const { setUpLanguage, translate } = require("./translate")
507
- const {drakonToStruct} = require("./drakonToStruct");
507
+ const { drakonToStruct } = require("./drakonToStruct");
508
508
 
509
509
 
510
- window.toPseudocode = function(drakonJson, name, filename, language) {
511
- setUpLanguage(language)
512
- return drakonToPseudocode(drakonJson, name, filename, htmlToString, translate).text
513
- }
510
+ window.drakongen = {
511
+ toPseudocode: function (drakonJson, name, filename, language) {
512
+ setUpLanguage(language)
513
+ return drakonToPseudocode(drakonJson, name, filename, htmlToString, translate).text
514
+ },
514
515
 
515
- window.toTree = function(drakonJson, name, filename, language) {
516
- setUpLanguage(language)
517
- var result = drakonToStruct(drakonJson, name, filename, translate)
518
- return JSON.stringify(result, null, 4)
516
+ toTree: function (drakonJson, name, filename, language) {
517
+ setUpLanguage(language)
518
+ var result = drakonToStruct(drakonJson, name, filename, translate)
519
+ return JSON.stringify(result, null, 4)
520
+ }
519
521
  }
520
522
  },{"./browserTools":1,"./drakonToPromptStruct":2,"./drakonToStruct":3,"./translate":9}],5:[function(require,module,exports){
521
523
  var {addRange} = require("./tools")
@@ -648,21 +650,14 @@ function printPseudo(algorithm, translate, output, htmlToString) {
648
650
  yesBody.push(indent2 + translate("pass"))
649
651
  }
650
652
  var content = step.content
651
- if (empty(yesBody)) {
652
- content = {operator:"not",operand:step.content}
653
- }
654
653
  var lines = printStructuredContentNoIdent(content)
655
654
  lines[0] = translate("if") + " " + lines[0]
656
655
  printWithIndent(lines, indent, output)
657
- if (empty(yesBody)) {
658
- addRange(output, noBody)
659
- } else {
660
- addRange(output, yesBody)
661
- if (!empty(noBody)) {
662
- output.push(indent + translate("else"))
663
- addRange(output, noBody)
664
- }
665
- }
656
+ addRange(output, yesBody)
657
+ if (!empty(noBody)) {
658
+ output.push(indent + translate("else"))
659
+ addRange(output, noBody)
660
+ }
666
661
  }
667
662
 
668
663
  function printLoop(step, depth, output) {
@@ -688,6 +683,7 @@ module.exports = {printPseudo}
688
683
  },{"./tools":8}],6:[function(require,module,exports){
689
684
  var {buildTree} = require("./technicalTree")
690
685
  const { createError, sortByProperty } = require("./tools");
686
+ const { optimizeTree } = require("./treeTools")
691
687
 
692
688
  function redirectNode(nodes, node, from, to) {
693
689
  if (node.one === from) {
@@ -1058,7 +1054,8 @@ function structFlow(nodes, branches, filename, translate) {
1058
1054
  name: branch.content,
1059
1055
  branchId: branch.branchId,
1060
1056
  start: branch.next,
1061
- body: body2
1057
+ refs: branch.prev.length,
1058
+ body: optimizeTree(body2)
1062
1059
  })
1063
1060
  }
1064
1061
 
@@ -1068,7 +1065,7 @@ function structFlow(nodes, branches, filename, translate) {
1068
1065
  return structMain()
1069
1066
  }
1070
1067
  module.exports = { structFlow, redirectNode };
1071
- },{"./technicalTree":7,"./tools":8}],7:[function(require,module,exports){
1068
+ },{"./technicalTree":7,"./tools":8,"./treeTools":10}],7:[function(require,module,exports){
1072
1069
  function buildTree(nodes, nodeId, body, stopId) {
1073
1070
  while (nodeId) {
1074
1071
  if (nodeId === stopId) {return;}
@@ -1300,4 +1297,63 @@ function setUpLanguage(language) {
1300
1297
 
1301
1298
 
1302
1299
  module.exports = { setUpLanguage, translate };
1300
+ },{}],10:[function(require,module,exports){
1301
+
1302
+ function optimizeTree(steps) {
1303
+ var result = []
1304
+
1305
+ for (var step of steps) {
1306
+ if (step.type === "end" || step.type === "branch" || step.type === "comment" || step.type === "loopend") { continue }
1307
+ if (step.type === "action" && !step.content) { continue }
1308
+ var copy
1309
+ if (step.type === "question") {
1310
+ copy = optimizeQuestion(step)
1311
+ } else if (step.type === "loop") {
1312
+ copy = optimizeLoop(step)
1313
+ } else {
1314
+ copy = step
1315
+ }
1316
+ result.push(copy)
1317
+ }
1318
+
1319
+ return result
1320
+ }
1321
+
1322
+ function optimizeLoop(step) {
1323
+ return {
1324
+ type: step.type,
1325
+ content: step.content,
1326
+ body: optimizeTree(step.body)
1327
+ }
1328
+ }
1329
+
1330
+ function optimizeQuestion(step) {
1331
+ var yes = optimizeTree(step.yes)
1332
+ var no = optimizeTree(step.no)
1333
+ if (yes.length === 0 && no.length === 0) {
1334
+ return {
1335
+ type: step.type,
1336
+ content: step.content,
1337
+ yes: [],
1338
+ no: []
1339
+ }
1340
+ }
1341
+ if (yes.length === 0) {
1342
+ return {
1343
+ type: step.type,
1344
+ content: {operator:"not",operand:step.content},
1345
+ yes: no,
1346
+ no: []
1347
+ }
1348
+ }
1349
+ return {
1350
+ type: step.type,
1351
+ content: step.content,
1352
+ yes: yes,
1353
+ no: no
1354
+ }
1355
+ }
1356
+
1357
+
1358
+ module.exports = {optimizeTree}
1303
1359
  },{}]},{},[4]);
package/browsertest.html CHANGED
@@ -66,9 +66,9 @@
66
66
  try {
67
67
  var content
68
68
  if (format.value === "pseudo") {
69
- content = toPseudocode(input.value, "Drakongen browser test", "Drakongen browser test.drakon", language.value)
69
+ content = drakongen.toPseudocode(input.value, "Drakongen browser test", "Drakongen browser test.drakon", language.value)
70
70
  } else {
71
- content = toTree(input.value, "Drakongen browser test", "Drakongen browser test.drakon", language.value)
71
+ content = drakongen.toTree(input.value, "Drakongen browser test", "Drakongen browser test.drakon", language.value)
72
72
  }
73
73
  setText(output, content)
74
74
  } catch (ex) {
@@ -104,7 +104,8 @@
104
104
  "111": {
105
105
  "type": "branch",
106
106
  "branchId": 0,
107
- "one": "4"
107
+ "one": "4",
108
+ "content": ""
108
109
  },
109
110
  "header": {
110
111
  "type": "header",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "drakongen",
3
- "version": "1.0.0",
3
+ "version": "1.2.0",
4
4
  "description": "Generates pseudocode and AST from drakon flowcharts built with DrakonWidget or DrakonHub.",
5
5
  "main": "src/index.js",
6
6
  "bin": {
package/src/drakongen.js CHANGED
@@ -1,16 +1,18 @@
1
1
  const { drakonToPseudocode } = require('./drakonToPromptStruct');
2
- const {htmlToString} = require("./browserTools")
2
+ const { htmlToString } = require("./browserTools")
3
3
  const { setUpLanguage, translate } = require("./translate")
4
- const {drakonToStruct} = require("./drakonToStruct");
4
+ const { drakonToStruct } = require("./drakonToStruct");
5
5
 
6
6
 
7
- window.toPseudocode = function(drakonJson, name, filename, language) {
8
- setUpLanguage(language)
9
- return drakonToPseudocode(drakonJson, name, filename, htmlToString, translate).text
10
- }
7
+ window.drakongen = {
8
+ toPseudocode: function (drakonJson, name, filename, language) {
9
+ setUpLanguage(language)
10
+ return drakonToPseudocode(drakonJson, name, filename, htmlToString, translate).text
11
+ },
11
12
 
12
- window.toTree = function(drakonJson, name, filename, language) {
13
- setUpLanguage(language)
14
- var result = drakonToStruct(drakonJson, name, filename, translate)
15
- return JSON.stringify(result, null, 4)
13
+ toTree: function (drakonJson, name, filename, language) {
14
+ setUpLanguage(language)
15
+ var result = drakonToStruct(drakonJson, name, filename, translate)
16
+ return JSON.stringify(result, null, 4)
17
+ }
16
18
  }
@@ -128,21 +128,14 @@ function printPseudo(algorithm, translate, output, htmlToString) {
128
128
  yesBody.push(indent2 + translate("pass"))
129
129
  }
130
130
  var content = step.content
131
- if (empty(yesBody)) {
132
- content = {operator:"not",operand:step.content}
133
- }
134
131
  var lines = printStructuredContentNoIdent(content)
135
132
  lines[0] = translate("if") + " " + lines[0]
136
133
  printWithIndent(lines, indent, output)
137
- if (empty(yesBody)) {
138
- addRange(output, noBody)
139
- } else {
140
- addRange(output, yesBody)
141
- if (!empty(noBody)) {
142
- output.push(indent + translate("else"))
143
- addRange(output, noBody)
144
- }
145
- }
134
+ addRange(output, yesBody)
135
+ if (!empty(noBody)) {
136
+ output.push(indent + translate("else"))
137
+ addRange(output, noBody)
138
+ }
146
139
  }
147
140
 
148
141
  function printLoop(step, depth, output) {
package/src/structFlow.js CHANGED
@@ -1,5 +1,6 @@
1
1
  var {buildTree} = require("./technicalTree")
2
2
  const { createError, sortByProperty } = require("./tools");
3
+ const { optimizeTree } = require("./treeTools")
3
4
 
4
5
  function redirectNode(nodes, node, from, to) {
5
6
  if (node.one === from) {
@@ -370,7 +371,8 @@ function structFlow(nodes, branches, filename, translate) {
370
371
  name: branch.content,
371
372
  branchId: branch.branchId,
372
373
  start: branch.next,
373
- body: body2
374
+ refs: branch.prev.length,
375
+ body: optimizeTree(body2)
374
376
  })
375
377
  }
376
378
 
@@ -0,0 +1,58 @@
1
+
2
+ function optimizeTree(steps) {
3
+ var result = []
4
+
5
+ for (var step of steps) {
6
+ if (step.type === "end" || step.type === "branch" || step.type === "comment" || step.type === "loopend") { continue }
7
+ if (step.type === "action" && !step.content) { continue }
8
+ var copy
9
+ if (step.type === "question") {
10
+ copy = optimizeQuestion(step)
11
+ } else if (step.type === "loop") {
12
+ copy = optimizeLoop(step)
13
+ } else {
14
+ copy = step
15
+ }
16
+ result.push(copy)
17
+ }
18
+
19
+ return result
20
+ }
21
+
22
+ function optimizeLoop(step) {
23
+ return {
24
+ type: step.type,
25
+ content: step.content,
26
+ body: optimizeTree(step.body)
27
+ }
28
+ }
29
+
30
+ function optimizeQuestion(step) {
31
+ var yes = optimizeTree(step.yes)
32
+ var no = optimizeTree(step.no)
33
+ if (yes.length === 0 && no.length === 0) {
34
+ return {
35
+ type: step.type,
36
+ content: step.content,
37
+ yes: [],
38
+ no: []
39
+ }
40
+ }
41
+ if (yes.length === 0) {
42
+ return {
43
+ type: step.type,
44
+ content: {operator:"not",operand:step.content},
45
+ yes: no,
46
+ no: []
47
+ }
48
+ }
49
+ return {
50
+ type: step.type,
51
+ content: step.content,
52
+ yes: yes,
53
+ no: no
54
+ }
55
+ }
56
+
57
+
58
+ module.exports = {optimizeTree}