Skip to content

Commit

Permalink
refactor(models) Separate CiceroMark from CommonMark model, clean up …
Browse files Browse the repository at this point in the history
…testing

Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
jeromesimeon committed Sep 11, 2019
1 parent 6cd56c2 commit 668d708
Show file tree
Hide file tree
Showing 12 changed files with 460 additions and 65 deletions.
7 changes: 5 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@
* @module markdown-transform
*/

module.exports.Logger = require('./lib/Logger');
module.exports.Stack = require('./lib/Stack');

module.exports.CommonmarkParser = require('./lib/CommonmarkParser');
module.exports.CommonmarkToString = require('./lib/CommonmarkToString');
module.exports.ToStringVisitor = require('./lib/ToStringVisitor');

module.exports.CommonmarkToAP = require('./lib/CommonmarkToAP');
module.exports.CommonmarkFromAP = require('./lib/CommonmarkFromAP');
module.exports.ToStringVisitor = require('./lib/ToStringVisitor');
module.exports.ToAPVisitor = require('./lib/ToAPVisitor');
module.exports.FromAPVisitor = require('./lib/FromAPVisitor');
module.exports.Stack = require('./lib/Stack');
24 changes: 18 additions & 6 deletions src/Commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,18 @@

'use strict';

const fs = require('fs');
const Fs = require('fs');
const Logger = require('./Logger');

const ModelManager = require('composer-concerto').ModelManager;
const Factory = require('composer-concerto').Factory;
const Serializer = require('composer-concerto').Serializer;

const CommonmarkParser = require('./CommonmarkParser');
const CommonmarkToString = require('./CommonmarkToString');
const CommonmarkToAP = require('./CommonmarkToAP');
const CommonmarkFromAP = require('./CommonmarkFromAP');
const { commonmarkModel, ciceromarkModel } = require('./Models');

/**
* Utility class that implements the commands exposed by the CLI.
Expand All @@ -46,15 +52,15 @@ class Commands {
if (Array.isArray(argv[argName])) {
// All files should exist
for (let i = 0; i < argv[argName].length; i++) {
if (fs.existsSync(argv[argName][i]) && argExists) {
if (Fs.existsSync(argv[argName][i]) && argExists) {
argExists = true;
} else {
argExists = false;
}
}
} else {
// This file should exist
argExists = fs.existsSync(argv[argName]);
argExists = Fs.existsSync(argv[argName]);
}

if (!argExists){
Expand Down Expand Up @@ -91,7 +97,7 @@ class Commands {
*/
static parse(samplePath, outPath, generateMarkdown, withAP) {
const parser = new CommonmarkParser();
const markdownText = fs.readFileSync(samplePath, 'utf8');
const markdownText = Fs.readFileSync(samplePath, 'utf8');
let concertoObject = parser.parse(markdownText);
if (withAP) {
concertoObject = CommonmarkToAP(concertoObject);
Expand All @@ -103,12 +109,18 @@ class Commands {
}
result = CommonmarkToString(concertoObject);
} else {
const json = parser.getSerializer().toJSON(concertoObject);
const modelManager = new ModelManager();
modelManager.addModelFile(commonmarkModel, 'commonmark.cto');
modelManager.addModelFile(ciceromarkModel, 'ciceromark.cto');
const factory = new Factory(modelManager);
const serializer = new Serializer(factory, modelManager);

const json = serializer.toJSON(concertoObject);
result = JSON.stringify(json);
}
if (outPath) {
Logger.info('Creating file: ' + outPath);
fs.writeFileSync(outPath, result);
Fs.writeFileSync(outPath, result);
}
return Promise.resolve(result);
}
Expand Down
23 changes: 18 additions & 5 deletions src/CommonmarkAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,34 @@

const fs = require('fs');
const diff = require('jest-diff');

const ModelManager = require('composer-concerto').ModelManager;
const Factory = require('composer-concerto').Factory;
const Serializer = require('composer-concerto').Serializer;

const CommonmarkParser = require('./CommonmarkParser');
const CommonmarkToAP = require('./CommonmarkToAP');
const CommonmarkFromAP = require('./CommonmarkFromAP');
const CommonmarkToString = require('./CommonmarkToString');
const { commonmarkModel, ciceromarkModel } = require('./Models');

let parser = null;

const modelManager = new ModelManager();
modelManager.addModelFile(commonmarkModel, 'commonmark.cto');
modelManager.addModelFile(ciceromarkModel, 'ciceromark.cto');
const factory = new Factory(modelManager);
const serializer = new Serializer(factory, modelManager);

expect.extend({
toMarkdownRoundtrip(markdownText) {
let concertoObject1 = parser.parse(markdownText);
concertoObject1 = CommonmarkToAP(concertoObject1);
const json1 = parser.getSerializer().toJSON(concertoObject1);
concertoObject1 = CommonmarkFromAP(CommonmarkToAP(concertoObject1));
const json1 = serializer.toJSON(concertoObject1);
const newMarkdown = CommonmarkToString(concertoObject1);
let concertoObject2 = parser.parse(newMarkdown);
concertoObject2 = CommonmarkFromAP(CommonmarkToAP(concertoObject2));
const json2 = parser.getSerializer().toJSON(concertoObject2);
const json2 = serializer.toJSON(concertoObject2);
const pass = JSON.stringify(json1) === JSON.stringify(json2);

const message = pass
Expand Down Expand Up @@ -130,7 +143,7 @@ describe('markdown', () => {
getMarkdownFiles().forEach( ([file, markdownText]) => {
it(`converts ${file} to concerto`, () => {
const concertoObject = parser.parse(markdownText);
const json = parser.getSerializer().toJSON(concertoObject);
const json = serializer.toJSON(concertoObject);
expect(json).toMatchSnapshot();
});

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

Expand Down
5 changes: 3 additions & 2 deletions src/CommonmarkFromAP.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const Serializer = require('composer-concerto').Serializer;

const FromAPVisitor = require('./FromAPVisitor');
const CommonmarkToString = require('./CommonmarkToString');
const { commonmarkModel } = require('./Models');
const { commonmarkModel, ciceromarkModel } = require('./Models');

/**
* Converts a commonmark document to a markdown string
Expand All @@ -30,7 +30,8 @@ const { commonmarkModel } = require('./Models');
function commonMarkFromAP(concertoObject) {
// Setup for validation
const modelManager = new ModelManager();
modelManager.addModelFile( commonmarkModel, 'commonmark.cto');
modelManager.addModelFile(commonmarkModel, 'commonmark.cto');
modelManager.addModelFile(ciceromarkModel, 'ciceromark.cto');
const factory = new Factory(modelManager);
const serializer = new Serializer(factory, modelManager);

Expand Down
26 changes: 13 additions & 13 deletions src/CommonmarkParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const Factory = require('composer-concerto').Factory;
const Serializer = require('composer-concerto').Serializer;
const Stack = require('./Stack');
const { DOMParser } = require('xmldom');
const { NS_PREFIX, commonmarkModel } = require('./Models');
const { COMMON_NS_PREFIX, commonmarkModel } = require('./Models');

/**
* Parses markdown using the commonmark parser into the
Expand All @@ -40,7 +40,7 @@ class CommonmarkParser {
constructor(options) {
this.options = options;
const modelManager = new ModelManager();
modelManager.addModelFile( commonmarkModel, 'commonmark.cto');
modelManager.addModelFile(commonmarkModel, 'commonmark.cto');
const factory = new Factory(modelManager);
this.serializer = new Serializer(factory, modelManager);
}
Expand All @@ -60,11 +60,11 @@ class CommonmarkParser {
* @return {boolean} whether it's a leaf node
*/
static isLeafNode(json) {
return (json.$class === (NS_PREFIX + 'Text') ||
json.$class === (NS_PREFIX + 'CodeBlock') ||
json.$class === (NS_PREFIX + 'HtmlInline') ||
json.$class === (NS_PREFIX + 'HtmlBlock') ||
json.$class === (NS_PREFIX + 'Code'));
return (json.$class === (COMMON_NS_PREFIX + 'Text') ||
json.$class === (COMMON_NS_PREFIX + 'CodeBlock') ||
json.$class === (COMMON_NS_PREFIX + 'HtmlInline') ||
json.$class === (COMMON_NS_PREFIX + 'HtmlBlock') ||
json.$class === (COMMON_NS_PREFIX + 'Code'));
}

/**
Expand All @@ -73,9 +73,9 @@ class CommonmarkParser {
* @return {boolean} whether it's a leaf node
*/
static isHtmlNode(json) {
return (json.$class === (NS_PREFIX + 'CodeBlock') ||
json.$class === (NS_PREFIX + 'HtmlInline') ||
json.$class === (NS_PREFIX + 'HtmlBlock'));
return (json.$class === (COMMON_NS_PREFIX + 'CodeBlock') ||
json.$class === (COMMON_NS_PREFIX + 'HtmlInline') ||
json.$class === (COMMON_NS_PREFIX + 'HtmlBlock'));
}

/**
Expand Down Expand Up @@ -107,15 +107,15 @@ class CommonmarkParser {
const tagInfo = CommonmarkParser.parseHtmlBlock(head.text);
if (tagInfo) {
head.tag = {};
head.tag.$class = NS_PREFIX + 'TagInfo';
head.tag.$class = COMMON_NS_PREFIX + 'TagInfo';
head.tag.tagName = tagInfo.tag;
head.tag.attributeString = tagInfo.attributeString;
head.tag.attributes = [];
for (const attName in tagInfo.attributes) {
if (tagInfo.attributes.hasOwnProperty(attName)) {
const attValue = tagInfo.attributes[attName];
head.tag.attributes.push({
'$class': NS_PREFIX + 'Attribute',
'$class': COMMON_NS_PREFIX + 'Attribute',
'name': attName,
'value': attValue,
});
Expand Down Expand Up @@ -208,7 +208,7 @@ class CommonmarkParser {
*/
static toClass(name) {
const camelCased = name.replace(/_([a-z])/g, function (g) { return g[1].toUpperCase(); });
return NS_PREFIX + CommonmarkParser.capitalizeFirstLetter(camelCased);
return COMMON_NS_PREFIX + CommonmarkParser.capitalizeFirstLetter(camelCased);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/CommonmarkToAP.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const Serializer = require('composer-concerto').Serializer;

const CommonmarkParser = require('./CommonmarkParser');
const ToAPVisitor = require('./ToAPVisitor');
const { commonmarkModel } = require('./Models');
const { commonmarkModel, ciceromarkModel } = require('./Models');

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

// Setup for validation
const modelManager = new ModelManager();
modelManager.addModelFile( commonmarkModel, 'commonmark.cto');
modelManager.addModelFile(commonmarkModel, 'commonmark.cto');
modelManager.addModelFile(ciceromarkModel, 'ciceromark.cto');
const factory = new Factory(modelManager);
const serializer = new Serializer(factory, modelManager);

Expand Down
26 changes: 13 additions & 13 deletions src/FromAPVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

'use strict';

const { NS_PREFIX } = require('./Models');
const { COMMON_NS_PREFIX } = require('./Models');

/**
* Converts a commonmark model instance to a markdown string.
Expand Down Expand Up @@ -51,11 +51,11 @@ class FromAPVisitor {
let jsonTarget = {};

// Revert to CodeBlock
jsonTarget.$class = NS_PREFIX + 'CodeBlock';
jsonTarget.$class = COMMON_NS_PREFIX + 'CodeBlock';

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

Expand All @@ -66,21 +66,21 @@ class FromAPVisitor {

// Create the proper tag
let tag = {};
tag.$class = NS_PREFIX + 'TagInfo';
tag.$class = COMMON_NS_PREFIX + 'TagInfo';
tag.tagName = 'clause';
tag.attributeString = attributeString;
tag.content = content;
tag.closed = false;
tag.attributes = [];

let attribute1 = {};
attribute1.$class = NS_PREFIX + 'Attribute';
attribute1.$class = COMMON_NS_PREFIX + 'Attribute';
attribute1.name = 'src';
attribute1.value = clauseJson.src;
tag.attributes.push(attribute1);

let attribute2 = {};
attribute2.$class = NS_PREFIX + 'Attribute';
attribute2.$class = COMMON_NS_PREFIX + 'Attribute';
attribute2.name = 'clauseid';
attribute2.value = clauseJson.clauseid;
tag.attributes.push(attribute2);
Expand All @@ -100,7 +100,7 @@ class FromAPVisitor {
break;
case 'Variable': {
// Revert to HtmlInline
thing.$classDeclaration = parameters.modelManager.getType(NS_PREFIX + 'HtmlInline');
thing.$classDeclaration = parameters.modelManager.getType(COMMON_NS_PREFIX + 'HtmlInline');

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

// Create the proper tag
let tag = {};
tag.$class = NS_PREFIX + 'TagInfo';
tag.$class = COMMON_NS_PREFIX + 'TagInfo';
tag.tagName = 'variable';
tag.attributeString = attributeString;
tag.content = content;
tag.closed = true;
tag.attributes = [];

let attribute1 = {};
attribute1.$class = NS_PREFIX + 'Attribute';
attribute1.$class = COMMON_NS_PREFIX + 'Attribute';
attribute1.name = 'id';
attribute1.value = thing.id;
tag.attributes.push(attribute1);

let attribute2 = {};
attribute2.$class = NS_PREFIX + 'Attribute';
attribute2.$class = COMMON_NS_PREFIX + 'Attribute';
attribute2.name = 'value';
attribute2.value = thing.value;
tag.attributes.push(attribute2);
Expand All @@ -136,7 +136,7 @@ class FromAPVisitor {
break;
case 'ComputedVariable': {
// Revert to HtmlInline
thing.$classDeclaration = parameters.modelManager.getType(NS_PREFIX + 'HtmlInline');
thing.$classDeclaration = parameters.modelManager.getType(COMMON_NS_PREFIX + 'HtmlInline');

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

// Create the proper tag
let tag = {};
tag.$class = NS_PREFIX + 'TagInfo';
tag.$class = COMMON_NS_PREFIX + 'TagInfo';
tag.tagName = 'computed';
tag.attributeString = attributeString;
tag.content = content;
tag.closed = true;
tag.attributes = [];

let attribute1 = {};
attribute1.$class = NS_PREFIX + 'Attribute';
attribute1.$class = COMMON_NS_PREFIX + 'Attribute';
attribute1.name = 'value';
attribute1.value = thing.value;
tag.attributes.push(attribute1);
Expand Down
Loading

0 comments on commit 668d708

Please sign in to comment.