Skip to content

Commit

Permalink
refactor(CommonMark) Reorganizes commonmark class for consistency wit…
Browse files Browse the repository at this point in the history
…h the ciceromark one

Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
jeromesimeon committed Sep 11, 2019
1 parent b7fe4ce commit e000910
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 86 deletions.
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

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

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

module.exports.CiceroMark = require('./lib/CiceroMark');
Expand Down
13 changes: 6 additions & 7 deletions src/CiceroMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ 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 CommonMark = require('./CommonMark');
const ToCiceroVisitor = require('./ToCiceroVisitor');
const FromCiceroVisitor = require('./FromCiceroVisitor');
const { commonmarkModel, ciceromarkModel } = require('./Models');
Expand All @@ -38,7 +37,7 @@ class CiceroMark {
this.options = options;

// Setup for Nested Parsing
this.commonmarkParser = new CommonmarkParser();
this.commonMark = new CommonMark();

// Setup for validation
this.modelManager = new ModelManager();
Expand All @@ -53,10 +52,10 @@ class CiceroMark {
* @param {*} concertoObject concerto commonmark object
* @returns {*} concertoObject concerto ciceromark object
*/
fromCommonmark(concertoObject) {
fromCommonMark(concertoObject) {
// Add Cicero nodes
const parameters = {
parser: this.commonmarkParser,
commonMark: this.commonMark,
modelManager : this.modelManager,
serializer : this.serializer,
};
Expand All @@ -73,10 +72,10 @@ class CiceroMark {
* @param {*} concertoObject concerto cicero object
* @returns {*} concertoObject concerto commonmark
*/
toCommonmark(concertoObject) {
toCommonMark(concertoObject) {
// Add Cicero nodes
const parameters = {
commonmarkToString: CommonmarkToString,
commonMark: this.commonMark,
modelManager : this.modelManager,
serializer : this.serializer
};
Expand Down
21 changes: 10 additions & 11 deletions src/Ciceromark.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,21 @@
const fs = require('fs');
const diff = require('jest-diff');

const CommonmarkParser = require('./CommonmarkParser');
const CommonMark = require('./CommonMark');
const CiceroMark = require('./CiceroMark');
const CommonmarkToString = require('./CommonmarkToString');

let parser = null;
let commonMark = null;
let ciceroMark = null;
let serializer = null;

expect.extend({
toMarkdownRoundtrip(markdownText) {
let concertoObject1 = parser.parse(markdownText);
concertoObject1 = ciceroMark.toCommonmark(ciceroMark.fromCommonmark(concertoObject1));
let concertoObject1 = commonMark.fromString(markdownText);
concertoObject1 = ciceroMark.toCommonMark(ciceroMark.fromCommonMark(concertoObject1));
const json1 = serializer.toJSON(concertoObject1);
const newMarkdown = CommonmarkToString(concertoObject1);
let concertoObject2 = parser.parse(newMarkdown);
concertoObject2 = ciceroMark.toCommonmark(ciceroMark.fromCommonmark(concertoObject2));
const newMarkdown = commonMark.toString(concertoObject1);
let concertoObject2 = commonMark.fromString(newMarkdown);
concertoObject2 = ciceroMark.toCommonMark(ciceroMark.fromCommonMark(concertoObject2));
const json2 = serializer.toJSON(concertoObject2);
const pass = JSON.stringify(json1) === JSON.stringify(json2);

Expand Down Expand Up @@ -64,7 +63,7 @@ expect.extend({

// @ts-ignore
beforeAll(() => {
parser = new CommonmarkParser();
commonMark = new CommonMark();
ciceroMark = new CiceroMark();
serializer = ciceroMark.getSerializer();
});
Expand Down Expand Up @@ -134,7 +133,7 @@ function extractSpecTests(testfile) {
describe('markdown', () => {
getMarkdownFiles().forEach( ([file, markdownText]) => {
it(`converts ${file} to concerto`, () => {
const concertoObject = parser.parse(markdownText);
const concertoObject = commonMark.fromString(markdownText);
const json = serializer.toJSON(concertoObject);
expect(json).toMatchSnapshot();
});
Expand All @@ -148,7 +147,7 @@ describe('markdown', () => {
describe('markdown-spec', () => {
getMarkdownSpecFiles().forEach( ([file, markdownText]) => {
it(`converts ${file} to concerto`, () => {
const concertoObject = parser.parse(markdownText);
const concertoObject = commonMark.fromString(markdownText);
const json = serializer.toJSON(concertoObject);
expect(json).toMatchSnapshot();
});
Expand Down
13 changes: 6 additions & 7 deletions src/Commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
const Fs = require('fs');
const Logger = require('./Logger');

const CommonmarkParser = require('./CommonmarkParser');
const CommonmarkToString = require('./CommonmarkToString');
const CommonMark = require('./CommonMark');
const CiceroMark = require('./CiceroMark');

/**
Expand Down Expand Up @@ -90,19 +89,19 @@ class Commands {
* @returns {object} Promise to the result of parsing
*/
static parse(samplePath, outPath, generateMarkdown, withCicero) {
const parser = new CommonmarkParser();
const commonMark = new CommonMark();
const ciceroMark = new CiceroMark();
const markdownText = Fs.readFileSync(samplePath, 'utf8');
let concertoObject = parser.parse(markdownText);
let concertoObject = commonMark.fromString(markdownText);
if (withCicero) {
concertoObject = ciceroMark.fromCommonmark(concertoObject);
concertoObject = ciceroMark.fromCommonMark(concertoObject);
}
let result;
if (generateMarkdown) {
if (withCicero) {
concertoObject = ciceroMark.toCommonmark(concertoObject);
concertoObject = ciceroMark.toCommonMark(concertoObject);
}
result = CommonmarkToString(concertoObject);
result = commonMark.toString(concertoObject);
} else {
const json = ciceroMark.getSerializer().toJSON(concertoObject);
result = JSON.stringify(json);
Expand Down
36 changes: 27 additions & 9 deletions src/CommonmarkParser.js → src/CommonMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ const commonmark = require('commonmark');
const sax = require('sax');
const ModelManager = require('composer-concerto').ModelManager;
const Factory = require('composer-concerto').Factory;

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

const Stack = require('./Stack');
const ToStringVisitor = require('./ToStringVisitor');

const { DOMParser } = require('xmldom');
const { COMMON_NS_PREFIX, commonmarkModel } = require('./Models');

Expand All @@ -28,7 +32,7 @@ const { COMMON_NS_PREFIX, commonmarkModel } = require('./Models');
* intermediate representation: a JSON object that adheres to
* the 'org.accordproject.commonmark' Concerto model.
*/
class CommonmarkParser {
class CommonMark {

/**
* Construct the parser.
Expand Down Expand Up @@ -84,7 +88,7 @@ class CommonmarkParser {
* @param {string} markdown the string to parse
* @returns {*} a Concerto object (DOM) for the markdown content
*/
parse(markdown) {
fromString(markdown) {
let stack = new Stack();
const that = this;
const parser = sax.parser(true, {position: true});
Expand All @@ -100,11 +104,11 @@ class CommonmarkParser {

const head = stack.peek();
if(t.length > 0 && head) {
if (CommonmarkParser.isLeafNode(head)) {
if (CommonMark.isLeafNode(head)) {
head.text = t;
}
if (CommonmarkParser.isHtmlNode(head)) {
const tagInfo = CommonmarkParser.parseHtmlBlock(head.text);
if (CommonMark.isHtmlNode(head)) {
const tagInfo = CommonMark.parseHtmlBlock(head.text);
if (tagInfo) {
head.tag = {};
head.tag.$class = COMMON_NS_PREFIX + 'TagInfo';
Expand All @@ -129,7 +133,7 @@ class CommonmarkParser {
};
parser.onopentag = function (node) {
const newNode = {};
newNode.$class = CommonmarkParser.toClass(node.name);
newNode.$class = CommonMark.toClass(node.name);
if(that.options && that.options.enableSourceLocation) {
newNode.line = parser.line;
newNode.column = parser.column;
Expand Down Expand Up @@ -206,7 +210,7 @@ class CommonmarkParser {
*/
static toClass(name) {
const camelCased = name.replace(/_([a-z])/g, function (g) { return g[1].toUpperCase(); });
return COMMON_NS_PREFIX + CommonmarkParser.capitalizeFirstLetter(camelCased);
return COMMON_NS_PREFIX + CommonMark.capitalizeFirstLetter(camelCased);
}

/**
Expand Down Expand Up @@ -243,7 +247,21 @@ class CommonmarkParser {
}
}

}
/**
* Converts a commonmark document to a markdown string
* @param {*} concertoObject concerto commonmark object
* @returns {string} the markdown string
*/
toString(concertoObject) {
const parameters = {};
parameters.result = '';
parameters.first = true;
parameters.indent = 0;
const visitor = new ToStringVisitor();
concertoObject.accept( visitor, parameters );
return parameters.result.trim();
}

}

module.exports = CommonmarkParser;
module.exports = CommonMark;
28 changes: 15 additions & 13 deletions src/Commonmark.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@

const fs = require('fs');
const diff = require('jest-diff');
const CommonmarkParser = require('./CommonmarkParser');
const CommonmarkToString = require('./CommonmarkToString');
let parser = null;
const CommonMark = require('./CommonMark');

let commonMark = null;
let serializer = null;

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

const message = pass
Expand Down Expand Up @@ -57,7 +58,8 @@ expect.extend({

// @ts-ignore
beforeAll(() => {
parser = new CommonmarkParser();
commonMark = new CommonMark();
serializer = commonMark.getSerializer();
});

/**
Expand Down Expand Up @@ -125,8 +127,8 @@ function extractSpecTests(testfile) {
describe.only('markdown', () => {
getMarkdownFiles().forEach( ([file, markdownText]) => {
it(`converts ${file} to concerto`, () => {
const concertoObject = parser.parse(markdownText);
const json = parser.getSerializer().toJSON(concertoObject);
const concertoObject = commonMark.fromString(markdownText);
const json = serializer.toJSON(concertoObject);
expect(json).toMatchSnapshot();
});

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

Expand Down
34 changes: 0 additions & 34 deletions src/CommonmarkToString.js

This file was deleted.

2 changes: 1 addition & 1 deletion src/FromCiceroVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class FromCiceroVisitor {
jsonSource.xmlns = 'http://commonmark.org/xml/1.0';
jsonSource.nodes = clauseJson.nodes;

const content = parameters.commonmarkToString(parameters.serializer.fromJSON(jsonSource));
const content = parameters.commonMark.toString(parameters.serializer.fromJSON(jsonSource));
const attributeString = `src="${clauseJson.src}" clauseid="${clauseJson.clauseid}"`;

jsonTarget.text = `<clause ${attributeString}>\n${content}\n</clause>\n`;
Expand Down
4 changes: 2 additions & 2 deletions src/ToCiceroVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ class ToCiceroVisitor {
thing.$classDeclaration = parameters.modelManager.getType(CICERO_NS_PREFIX + 'Clause');
thing.src = tag.attributes[0].value;
thing.clauseid = tag.attributes[1].value;
thing.nodes = parameters.parser.parse(tag.content).nodes; // Parse text as markdown (in the nodes for the root)
thing.nodes = parameters.commonMark.fromString(tag.content).nodes; // Parse text as markdown (in the nodes for the root)
thing.text = null; // Remove text
delete thing.tag;
}
else if (tag.attributes[1].name === 'src' &&
tag.attributes[0].name === 'clauseid') {
thing.$classDeclaration = parameters.modelManager.getType(CICERO_NS_PREFIX + 'Clause');
thing.$classDeclaration = parameters.commonMark.fromString.getType(CICERO_NS_PREFIX + 'Clause');
thing.clauseid = tag.attributes[0].value;
thing.src = tag.attributes[1].value;
thing.nodes = parameters.parser.parse(tag.content).nodes; // Parse text as markdown (in the nodes for the root)
Expand Down

0 comments on commit e000910

Please sign in to comment.