spec-up-t 0.11.22 → 0.11.23

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/index.js CHANGED
@@ -6,6 +6,10 @@ module.exports = function(options = {}) {
6
6
  validateReferences,
7
7
  findExternalSpecByKey
8
8
  } = require('./references.js');
9
+
10
+ const { createTermRelations } = require('./src/create-term-relations.js');
11
+ createTermRelations();
12
+
9
13
  const gulp = require('gulp');
10
14
  const fs = require('fs-extra');
11
15
  const path = require('path');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "spec-up-t",
3
- "version": "0.11.22",
3
+ "version": "0.11.23",
4
4
  "description": "Technical specification drafting tool that generates rich specification documents from markdown. Forked from https://github.com/decentralized-identity/spec-up by Daniel Buchner (https://github.com/csuwildcat)",
5
5
  "main": "./index",
6
6
  "repository": {
@@ -0,0 +1,116 @@
1
+ /**
2
+ * @file This file creates a json and a js file with an an object that contains the relations between terms
3
+ * @author Kor Dwarshuis
4
+ * @version 1.0.0
5
+ * @since 2024-06-22
6
+ */
7
+
8
+ const fs = require('fs-extra');
9
+ const config = fs.readJsonSync('specs.json');
10
+ const specDirectories = config.specs.map(spec => spec.spec_directory + '/' + spec.spec_terms_directory);
11
+
12
+ // Create directory named “output” in the project root if it does not yet exist
13
+ if (!fs.existsSync('output')) {
14
+ fs.mkdirSync('output');
15
+ }
16
+
17
+ // Create a path for the output file in the project root
18
+ const outputPathJSON = 'output/term-relations-data.json';
19
+ const outputPathJS = 'output/term-relations-data.js';
20
+
21
+ function createTermRelations() {
22
+ let allDefs = {};
23
+ allDefs.defs = new Set();
24
+
25
+ // Go through all directories that contain files with a term and definition
26
+ console.log('All “spec_directory” found in specs.json: ', specDirectories);
27
+ specDirectories.forEach(specDirectory => {
28
+ // read directory
29
+ fs.readdirSync(specDirectory).forEach(file => {
30
+ // read file
31
+ if (file.endsWith('.md')) {
32
+ const markdown = fs.readFileSync(`${specDirectory}/${file}`, 'utf8');
33
+
34
+ let regexDef = /\[\[def:.*?\]\]/g;
35
+ let regexRef = /\[\[ref:.*?\]\]/g;
36
+ let regexXref = /\[\[xref:.*?\]\]/g;
37
+
38
+ let entry = {};
39
+
40
+ if (regexDef.test(markdown)) {
41
+ const defs = markdown.match(regexDef);
42
+ let refs = markdown.match(regexRef);
43
+ let xrefs = markdown.match(regexXref);
44
+
45
+ defs.forEach(def => {
46
+ // remove “[[def:” from the beginning of every value in allMatches
47
+ def = def.replace(/\[\[def:/, '');
48
+
49
+ // remove “]]” from the end of every value in allMatches
50
+ def = def.replace(/\]\]/, '');
51
+
52
+ // trim every entry of allMatches
53
+ def = def.trim();
54
+
55
+ // Split the input on the first comma
56
+ let [term, rest] = def.split(/,(.+)/);
57
+
58
+ // Trim the term
59
+ term = term.trim();
60
+
61
+ entry.term = term;
62
+ // Split the rest into an array of synonyms if it exists, otherwise use a placeholder
63
+ let synonyms = rest ? rest.split(',').map(s => s.trim()) : [];
64
+ entry.synonyms = synonyms;
65
+ });
66
+
67
+ if (refs !== null) {
68
+ entry.refs = [];
69
+ refs.forEach(ref => {
70
+ // remove “[[ref:” from the beginning of every value in allMatches
71
+ ref = ref.replace(/\[\[ref:/, '');
72
+ // remove “]]” from the end of every value in allMatches
73
+ ref = ref.replace(/\]\]/, '');
74
+ // trim every entry of allMatches
75
+ ref = ref.trim();
76
+ entry.refs.push(ref);
77
+ });
78
+ }
79
+
80
+ if (xrefs !== null) {
81
+ entry.xrefs = [];
82
+ xrefs.forEach(xref => {
83
+ // remove “[[xref:” from the beginning of every value in allMatches
84
+ xref = xref.replace(/\[\[xref:/, '');
85
+ // remove “]]” from the end of every value in allMatches
86
+ xref = xref.replace(/\]\]/, '');
87
+ // trim every entry of allMatches
88
+ xref = xref.trim();
89
+ entry.xrefs.push(xref);
90
+ });
91
+ }
92
+ }
93
+ allDefs.defs.add(entry);
94
+ }
95
+ });
96
+ })
97
+ // Convert the Set back to an Array if needed
98
+ allDefs.defs = Array.from(allDefs.defs);
99
+
100
+ // Convert allXrefsStr to a JSON string with indentation
101
+ const allDefsStr = JSON.stringify(allDefs, null, 2);
102
+
103
+ // Write the JSON code to a .json file
104
+ fs.writeFileSync(outputPathJSON, allDefsStr, 'utf8');
105
+
106
+
107
+ // Create the JS code for the assignment
108
+ const stringReadyForFileWrite = `const allTermRelations = ${allDefsStr};`;
109
+
110
+ // Write the JS code to a .js file
111
+ fs.writeFileSync(outputPathJS, stringReadyForFileWrite, 'utf8');
112
+ }
113
+
114
+ module.exports = {
115
+ createTermRelations
116
+ }