Skip to content

Commit 668d708

Browse files
committed
refactor(models) Separate CiceroMark from CommonMark model, clean up testing
Signed-off-by: Jerome Simeon <[email protected]>
1 parent 6cd56c2 commit 668d708

File tree

12 files changed

+460
-65
lines changed

12 files changed

+460
-65
lines changed

index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,14 @@
1919
* @module markdown-transform
2020
*/
2121

22+
module.exports.Logger = require('./lib/Logger');
23+
module.exports.Stack = require('./lib/Stack');
24+
2225
module.exports.CommonmarkParser = require('./lib/CommonmarkParser');
2326
module.exports.CommonmarkToString = require('./lib/CommonmarkToString');
27+
module.exports.ToStringVisitor = require('./lib/ToStringVisitor');
28+
2429
module.exports.CommonmarkToAP = require('./lib/CommonmarkToAP');
2530
module.exports.CommonmarkFromAP = require('./lib/CommonmarkFromAP');
26-
module.exports.ToStringVisitor = require('./lib/ToStringVisitor');
2731
module.exports.ToAPVisitor = require('./lib/ToAPVisitor');
2832
module.exports.FromAPVisitor = require('./lib/FromAPVisitor');
29-
module.exports.Stack = require('./lib/Stack');

src/Commands.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@
1414

1515
'use strict';
1616

17-
const fs = require('fs');
17+
const Fs = require('fs');
1818
const Logger = require('./Logger');
19+
20+
const ModelManager = require('composer-concerto').ModelManager;
21+
const Factory = require('composer-concerto').Factory;
22+
const Serializer = require('composer-concerto').Serializer;
23+
1924
const CommonmarkParser = require('./CommonmarkParser');
2025
const CommonmarkToString = require('./CommonmarkToString');
2126
const CommonmarkToAP = require('./CommonmarkToAP');
2227
const CommonmarkFromAP = require('./CommonmarkFromAP');
28+
const { commonmarkModel, ciceromarkModel } = require('./Models');
2329

2430
/**
2531
* Utility class that implements the commands exposed by the CLI.
@@ -46,15 +52,15 @@ class Commands {
4652
if (Array.isArray(argv[argName])) {
4753
// All files should exist
4854
for (let i = 0; i < argv[argName].length; i++) {
49-
if (fs.existsSync(argv[argName][i]) && argExists) {
55+
if (Fs.existsSync(argv[argName][i]) && argExists) {
5056
argExists = true;
5157
} else {
5258
argExists = false;
5359
}
5460
}
5561
} else {
5662
// This file should exist
57-
argExists = fs.existsSync(argv[argName]);
63+
argExists = Fs.existsSync(argv[argName]);
5864
}
5965

6066
if (!argExists){
@@ -91,7 +97,7 @@ class Commands {
9197
*/
9298
static parse(samplePath, outPath, generateMarkdown, withAP) {
9399
const parser = new CommonmarkParser();
94-
const markdownText = fs.readFileSync(samplePath, 'utf8');
100+
const markdownText = Fs.readFileSync(samplePath, 'utf8');
95101
let concertoObject = parser.parse(markdownText);
96102
if (withAP) {
97103
concertoObject = CommonmarkToAP(concertoObject);
@@ -103,12 +109,18 @@ class Commands {
103109
}
104110
result = CommonmarkToString(concertoObject);
105111
} else {
106-
const json = parser.getSerializer().toJSON(concertoObject);
112+
const modelManager = new ModelManager();
113+
modelManager.addModelFile(commonmarkModel, 'commonmark.cto');
114+
modelManager.addModelFile(ciceromarkModel, 'ciceromark.cto');
115+
const factory = new Factory(modelManager);
116+
const serializer = new Serializer(factory, modelManager);
117+
118+
const json = serializer.toJSON(concertoObject);
107119
result = JSON.stringify(json);
108120
}
109121
if (outPath) {
110122
Logger.info('Creating file: ' + outPath);
111-
fs.writeFileSync(outPath, result);
123+
Fs.writeFileSync(outPath, result);
112124
}
113125
return Promise.resolve(result);
114126
}

src/CommonmarkAP.test.js

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,34 @@
1818

1919
const fs = require('fs');
2020
const diff = require('jest-diff');
21+
22+
const ModelManager = require('composer-concerto').ModelManager;
23+
const Factory = require('composer-concerto').Factory;
24+
const Serializer = require('composer-concerto').Serializer;
25+
2126
const CommonmarkParser = require('./CommonmarkParser');
2227
const CommonmarkToAP = require('./CommonmarkToAP');
2328
const CommonmarkFromAP = require('./CommonmarkFromAP');
2429
const CommonmarkToString = require('./CommonmarkToString');
30+
const { commonmarkModel, ciceromarkModel } = require('./Models');
31+
2532
let parser = null;
2633

34+
const modelManager = new ModelManager();
35+
modelManager.addModelFile(commonmarkModel, 'commonmark.cto');
36+
modelManager.addModelFile(ciceromarkModel, 'ciceromark.cto');
37+
const factory = new Factory(modelManager);
38+
const serializer = new Serializer(factory, modelManager);
39+
2740
expect.extend({
2841
toMarkdownRoundtrip(markdownText) {
2942
let concertoObject1 = parser.parse(markdownText);
30-
concertoObject1 = CommonmarkToAP(concertoObject1);
31-
const json1 = parser.getSerializer().toJSON(concertoObject1);
43+
concertoObject1 = CommonmarkFromAP(CommonmarkToAP(concertoObject1));
44+
const json1 = serializer.toJSON(concertoObject1);
3245
const newMarkdown = CommonmarkToString(concertoObject1);
3346
let concertoObject2 = parser.parse(newMarkdown);
3447
concertoObject2 = CommonmarkFromAP(CommonmarkToAP(concertoObject2));
35-
const json2 = parser.getSerializer().toJSON(concertoObject2);
48+
const json2 = serializer.toJSON(concertoObject2);
3649
const pass = JSON.stringify(json1) === JSON.stringify(json2);
3750

3851
const message = pass
@@ -130,7 +143,7 @@ describe('markdown', () => {
130143
getMarkdownFiles().forEach( ([file, markdownText]) => {
131144
it(`converts ${file} to concerto`, () => {
132145
const concertoObject = parser.parse(markdownText);
133-
const json = parser.getSerializer().toJSON(concertoObject);
146+
const json = serializer.toJSON(concertoObject);
134147
expect(json).toMatchSnapshot();
135148
});
136149

@@ -144,7 +157,7 @@ describe('markdown-spec', () => {
144157
getMarkdownSpecFiles().forEach( ([file, markdownText]) => {
145158
it(`converts ${file} to concerto`, () => {
146159
const concertoObject = parser.parse(markdownText);
147-
const json = parser.getSerializer().toJSON(concertoObject);
160+
const json = serializer.toJSON(concertoObject);
148161
expect(json).toMatchSnapshot();
149162
});
150163

src/CommonmarkFromAP.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const Serializer = require('composer-concerto').Serializer;
2020

2121
const FromAPVisitor = require('./FromAPVisitor');
2222
const CommonmarkToString = require('./CommonmarkToString');
23-
const { commonmarkModel } = require('./Models');
23+
const { commonmarkModel, ciceromarkModel } = require('./Models');
2424

2525
/**
2626
* Converts a commonmark document to a markdown string
@@ -30,7 +30,8 @@ const { commonmarkModel } = require('./Models');
3030
function commonMarkFromAP(concertoObject) {
3131
// Setup for validation
3232
const modelManager = new ModelManager();
33-
modelManager.addModelFile( commonmarkModel, 'commonmark.cto');
33+
modelManager.addModelFile(commonmarkModel, 'commonmark.cto');
34+
modelManager.addModelFile(ciceromarkModel, 'ciceromark.cto');
3435
const factory = new Factory(modelManager);
3536
const serializer = new Serializer(factory, modelManager);
3637

src/CommonmarkParser.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ const Factory = require('composer-concerto').Factory;
2121
const Serializer = require('composer-concerto').Serializer;
2222
const Stack = require('./Stack');
2323
const { DOMParser } = require('xmldom');
24-
const { NS_PREFIX, commonmarkModel } = require('./Models');
24+
const { COMMON_NS_PREFIX, commonmarkModel } = require('./Models');
2525

2626
/**
2727
* Parses markdown using the commonmark parser into the
@@ -40,7 +40,7 @@ class CommonmarkParser {
4040
constructor(options) {
4141
this.options = options;
4242
const modelManager = new ModelManager();
43-
modelManager.addModelFile( commonmarkModel, 'commonmark.cto');
43+
modelManager.addModelFile(commonmarkModel, 'commonmark.cto');
4444
const factory = new Factory(modelManager);
4545
this.serializer = new Serializer(factory, modelManager);
4646
}
@@ -60,11 +60,11 @@ class CommonmarkParser {
6060
* @return {boolean} whether it's a leaf node
6161
*/
6262
static isLeafNode(json) {
63-
return (json.$class === (NS_PREFIX + 'Text') ||
64-
json.$class === (NS_PREFIX + 'CodeBlock') ||
65-
json.$class === (NS_PREFIX + 'HtmlInline') ||
66-
json.$class === (NS_PREFIX + 'HtmlBlock') ||
67-
json.$class === (NS_PREFIX + 'Code'));
63+
return (json.$class === (COMMON_NS_PREFIX + 'Text') ||
64+
json.$class === (COMMON_NS_PREFIX + 'CodeBlock') ||
65+
json.$class === (COMMON_NS_PREFIX + 'HtmlInline') ||
66+
json.$class === (COMMON_NS_PREFIX + 'HtmlBlock') ||
67+
json.$class === (COMMON_NS_PREFIX + 'Code'));
6868
}
6969

7070
/**
@@ -73,9 +73,9 @@ class CommonmarkParser {
7373
* @return {boolean} whether it's a leaf node
7474
*/
7575
static isHtmlNode(json) {
76-
return (json.$class === (NS_PREFIX + 'CodeBlock') ||
77-
json.$class === (NS_PREFIX + 'HtmlInline') ||
78-
json.$class === (NS_PREFIX + 'HtmlBlock'));
76+
return (json.$class === (COMMON_NS_PREFIX + 'CodeBlock') ||
77+
json.$class === (COMMON_NS_PREFIX + 'HtmlInline') ||
78+
json.$class === (COMMON_NS_PREFIX + 'HtmlBlock'));
7979
}
8080

8181
/**
@@ -107,15 +107,15 @@ class CommonmarkParser {
107107
const tagInfo = CommonmarkParser.parseHtmlBlock(head.text);
108108
if (tagInfo) {
109109
head.tag = {};
110-
head.tag.$class = NS_PREFIX + 'TagInfo';
110+
head.tag.$class = COMMON_NS_PREFIX + 'TagInfo';
111111
head.tag.tagName = tagInfo.tag;
112112
head.tag.attributeString = tagInfo.attributeString;
113113
head.tag.attributes = [];
114114
for (const attName in tagInfo.attributes) {
115115
if (tagInfo.attributes.hasOwnProperty(attName)) {
116116
const attValue = tagInfo.attributes[attName];
117117
head.tag.attributes.push({
118-
'$class': NS_PREFIX + 'Attribute',
118+
'$class': COMMON_NS_PREFIX + 'Attribute',
119119
'name': attName,
120120
'value': attValue,
121121
});
@@ -208,7 +208,7 @@ class CommonmarkParser {
208208
*/
209209
static toClass(name) {
210210
const camelCased = name.replace(/_([a-z])/g, function (g) { return g[1].toUpperCase(); });
211-
return NS_PREFIX + CommonmarkParser.capitalizeFirstLetter(camelCased);
211+
return COMMON_NS_PREFIX + CommonmarkParser.capitalizeFirstLetter(camelCased);
212212
}
213213

214214
/**

src/CommonmarkToAP.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const Serializer = require('composer-concerto').Serializer;
2020

2121
const CommonmarkParser = require('./CommonmarkParser');
2222
const ToAPVisitor = require('./ToAPVisitor');
23-
const { commonmarkModel } = require('./Models');
23+
const { commonmarkModel, ciceromarkModel } = require('./Models');
2424

2525
/**
2626
* Converts a commonmark document to a markdown string
@@ -33,7 +33,8 @@ function commonMarkToAP(concertoObject) {
3333

3434
// Setup for validation
3535
const modelManager = new ModelManager();
36-
modelManager.addModelFile( commonmarkModel, 'commonmark.cto');
36+
modelManager.addModelFile(commonmarkModel, 'commonmark.cto');
37+
modelManager.addModelFile(ciceromarkModel, 'ciceromark.cto');
3738
const factory = new Factory(modelManager);
3839
const serializer = new Serializer(factory, modelManager);
3940

src/FromAPVisitor.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
'use strict';
1616

17-
const { NS_PREFIX } = require('./Models');
17+
const { COMMON_NS_PREFIX } = require('./Models');
1818

1919
/**
2020
* Converts a commonmark model instance to a markdown string.
@@ -51,11 +51,11 @@ class FromAPVisitor {
5151
let jsonTarget = {};
5252

5353
// Revert to CodeBlock
54-
jsonTarget.$class = NS_PREFIX + 'CodeBlock';
54+
jsonTarget.$class = COMMON_NS_PREFIX + 'CodeBlock';
5555

5656
// Get the content
5757
const clauseJson = parameters.serializer.toJSON(thing);
58-
jsonSource.$class = NS_PREFIX + 'Document';
58+
jsonSource.$class = COMMON_NS_PREFIX + 'Document';
5959
jsonSource.xmlns = 'http://commonmark.org/xml/1.0';
6060
jsonSource.nodes = clauseJson.nodes;
6161

@@ -66,21 +66,21 @@ class FromAPVisitor {
6666

6767
// Create the proper tag
6868
let tag = {};
69-
tag.$class = NS_PREFIX + 'TagInfo';
69+
tag.$class = COMMON_NS_PREFIX + 'TagInfo';
7070
tag.tagName = 'clause';
7171
tag.attributeString = attributeString;
7272
tag.content = content;
7373
tag.closed = false;
7474
tag.attributes = [];
7575

7676
let attribute1 = {};
77-
attribute1.$class = NS_PREFIX + 'Attribute';
77+
attribute1.$class = COMMON_NS_PREFIX + 'Attribute';
7878
attribute1.name = 'src';
7979
attribute1.value = clauseJson.src;
8080
tag.attributes.push(attribute1);
8181

8282
let attribute2 = {};
83-
attribute2.$class = NS_PREFIX + 'Attribute';
83+
attribute2.$class = COMMON_NS_PREFIX + 'Attribute';
8484
attribute2.name = 'clauseid';
8585
attribute2.value = clauseJson.clauseid;
8686
tag.attributes.push(attribute2);
@@ -100,7 +100,7 @@ class FromAPVisitor {
100100
break;
101101
case 'Variable': {
102102
// Revert to HtmlInline
103-
thing.$classDeclaration = parameters.modelManager.getType(NS_PREFIX + 'HtmlInline');
103+
thing.$classDeclaration = parameters.modelManager.getType(COMMON_NS_PREFIX + 'HtmlInline');
104104

105105
// Create the text for that document
106106
const content = '';
@@ -109,21 +109,21 @@ class FromAPVisitor {
109109

110110
// Create the proper tag
111111
let tag = {};
112-
tag.$class = NS_PREFIX + 'TagInfo';
112+
tag.$class = COMMON_NS_PREFIX + 'TagInfo';
113113
tag.tagName = 'variable';
114114
tag.attributeString = attributeString;
115115
tag.content = content;
116116
tag.closed = true;
117117
tag.attributes = [];
118118

119119
let attribute1 = {};
120-
attribute1.$class = NS_PREFIX + 'Attribute';
120+
attribute1.$class = COMMON_NS_PREFIX + 'Attribute';
121121
attribute1.name = 'id';
122122
attribute1.value = thing.id;
123123
tag.attributes.push(attribute1);
124124

125125
let attribute2 = {};
126-
attribute2.$class = NS_PREFIX + 'Attribute';
126+
attribute2.$class = COMMON_NS_PREFIX + 'Attribute';
127127
attribute2.name = 'value';
128128
attribute2.value = thing.value;
129129
tag.attributes.push(attribute2);
@@ -136,7 +136,7 @@ class FromAPVisitor {
136136
break;
137137
case 'ComputedVariable': {
138138
// Revert to HtmlInline
139-
thing.$classDeclaration = parameters.modelManager.getType(NS_PREFIX + 'HtmlInline');
139+
thing.$classDeclaration = parameters.modelManager.getType(COMMON_NS_PREFIX + 'HtmlInline');
140140

141141
// Create the text for that document
142142
const content = '';
@@ -145,15 +145,15 @@ class FromAPVisitor {
145145

146146
// Create the proper tag
147147
let tag = {};
148-
tag.$class = NS_PREFIX + 'TagInfo';
148+
tag.$class = COMMON_NS_PREFIX + 'TagInfo';
149149
tag.tagName = 'computed';
150150
tag.attributeString = attributeString;
151151
tag.content = content;
152152
tag.closed = true;
153153
tag.attributes = [];
154154

155155
let attribute1 = {};
156-
attribute1.$class = NS_PREFIX + 'Attribute';
156+
attribute1.$class = COMMON_NS_PREFIX + 'Attribute';
157157
attribute1.name = 'value';
158158
attribute1.value = thing.value;
159159
tag.attributes.push(attribute1);

0 commit comments

Comments
 (0)