Skip to content

Commit

Permalink
fix(codeblock) Clauses are now recognized through code block's info f…
Browse files Browse the repository at this point in the history
…ield, bug in cicero to commonmark

Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
jeromesimeon committed Sep 11, 2019
1 parent 8b9af9a commit 14d20c3
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 73 deletions.
19 changes: 14 additions & 5 deletions src/CommonMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,24 @@ class CommonMark {
}

/**
* Is it a HTML-ish node? (html blocks or html inlines but also code blocks)
* Is it a HTML node? (html blocks or html inlines)
* @param {*} json the JS Object for the AST
* @return {boolean} whether it's a leaf node
*/
static isHtmlNode(json) {
return (json.$class === (COMMON_NS_PREFIX + 'CodeBlock') ||
json.$class === (COMMON_NS_PREFIX + 'HtmlInline') ||
return (json.$class === (COMMON_NS_PREFIX + 'HtmlInline') ||
json.$class === (COMMON_NS_PREFIX + 'HtmlBlock'));
}

/**
* Is it a Code Block node?
* @param {*} json the JS Object for the AST
* @return {boolean} whether it's a leaf node
*/
static isCodeBlockNode(json) {
return json.$class === (COMMON_NS_PREFIX + 'CodeBlock');
}

/**
* Parses a markdown string into a Concerto DOM object.
*
Expand All @@ -107,8 +115,9 @@ class CommonMark {
if (CommonMark.isLeafNode(head)) {
head.text = t;
}
if (CommonMark.isHtmlNode(head)) {
const tagInfo = CommonMark.parseHtmlBlock(head.text);
if (CommonMark.isHtmlNode(head) || CommonMark.isCodeBlockNode(head)) {
const maybeHtmlText = CommonMark.isHtmlNode(head) ? head.text : head.info;
const tagInfo = CommonMark.parseHtmlBlock(maybeHtmlText);
if (tagInfo) {
head.tag = {};
head.tag.$class = COMMON_NS_PREFIX + 'TagInfo';
Expand Down
5 changes: 4 additions & 1 deletion src/FromCiceroVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class FromCiceroVisitor {
let jsonSource = {};
let jsonTarget = {};

FromCiceroVisitor.visitChildren(this, thing, parameters);
// Revert to CodeBlock
jsonTarget.$class = COMMON_NS_PREFIX + 'CodeBlock';

Expand All @@ -62,7 +63,7 @@ class FromCiceroVisitor {
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`;
jsonTarget.text = content + '\n';

// Create the proper tag
let tag = {};
Expand Down Expand Up @@ -91,11 +92,13 @@ class FromCiceroVisitor {

delete thing.clauseid;
delete thing.src;
delete thing.clauseText;

thing.$classDeclaration = validatedTarget.$classDeclaration;
thing.tag = validatedTarget.tag;
thing.nodes = validatedTarget.nodes;
thing.text = validatedTarget.text;
thing.info = `<clause ${attributeString}/>`;
}
break;
case 'Variable': {
Expand Down
1 change: 1 addition & 0 deletions src/Models.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ import org.accordproject.commonmark.*
concept Clause extends Child {
o String clauseid
o String src
o String clauseText
}
concept Variable extends Child {
Expand Down
8 changes: 6 additions & 2 deletions src/ToCiceroVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,25 @@ class ToCiceroVisitor {
thing.src = tag.attributes[0].value;
thing.clauseid = tag.attributes[1].value;

thing.nodes = parameters.commonMark.fromString(tag.content).nodes;
thing.nodes = parameters.commonMark.fromString(thing.text).nodes;
ToCiceroVisitor.visitNodes(this, thing.nodes, parameters);
thing.clauseText = thing.text;
thing.text = null; // Remove text
delete thing.tag;
delete thing.info;
}
else if (tag.attributes[1].name === 'src' &&
tag.attributes[0].name === 'clauseid') {
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.commonMark.fromString(tag.content).nodes;
thing.nodes = parameters.commonMark.fromString(thing.text).nodes;
ToCiceroVisitor.visitNodes(this, thing.nodes, parameters);
thing.clauseText = thing.text;
thing.text = null; // Remove text
delete thing.tag;
delete thing.info;
} else {
//console.log('Found Clause but without \'clauseid\' and \'src\' attributes ');
}
Expand Down
2 changes: 1 addition & 1 deletion src/ToStringVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class ToStringVisitor {
switch(thing.getType()) {
case 'CodeBlock':
ToStringVisitor.newParagraph(parameters);
parameters.result += `\`\`\`${thing.info ? thing.info : ''}\n${thing.text}\`\`\`\n\n`;
parameters.result += `\`\`\`${thing.info ? ' ' + thing.info : ''}\n${thing.text}\`\`\`\n\n`;
break;
case 'Code':
parameters.result += `\`${thing.text}\``;
Expand Down
98 changes: 55 additions & 43 deletions src/__snapshots__/CiceroMark.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ Object {
"info": "<clause src=\\"ap://[email protected]#721d1aa0999a5d278653e211ae2a64b75fdd8ca6fa1f34255533c942404c5c1f\\" clauseid=\\"479adbb4-dc55-4d1a-ab12-b6c5e16900c0\\">",
"tag": Object {
"$class": "org.accordproject.commonmark.TagInfo",
"attributeString": "id = \\"shipper\\" value = \\"%22Party%20A%22\\" ",
"attributeString": "src = \\"ap://[email protected]#721d1aa0999a5d278653e211ae2a64b75fdd8ca6fa1f34255533c942404c5c1f\\" clauseid = \\"479adbb4-dc55-4d1a-ab12-b6c5e16900c0\\" ",
"attributes": Array [
Object {
"$class": "org.accordproject.commonmark.Attribute",
"name": "id",
"value": "shipper",
"name": "src",
"value": "ap://[email protected]#721d1aa0999a5d278653e211ae2a64b75fdd8ca6fa1f34255533c942404c5c1f",
},
Object {
"$class": "org.accordproject.commonmark.Attribute",
"name": "value",
"value": "%22Party%20A%22",
"name": "clauseid",
"value": "479adbb4-dc55-4d1a-ab12-b6c5e16900c0",
},
],
"closed": false,
"content": "",
"tagName": "variable",
"tagName": "clause",
},
"text": "Acceptance of Delivery. <variable id=\\"shipper\\" value=\\"%22Party%20A%22\\"/> will be deemed to have completed its delivery obligations if in <variable id=\\"receiver\\" value=\\"%22Party%20B%22\\"/>'s opinion, the <variable id=\\"deliverable\\" value=\\"%22Widgets%22\\"/> satisfies the Acceptance Criteria, and <variable id=\\"receiver\\" value=\\"%22Party%20B%22\\"/> notifies <variable id=\\"shipper\\" value=\\"%22Party%20A%22\\"/> in writing that it is accepting the <variable id=\\"deliverable\\" value=\\"%22Widgets%22\\"/>.

Expand Down Expand Up @@ -239,8 +239,39 @@ Object {
},
],
},
Object {
"$class": "org.accordproject.commonmark.Paragraph",
"nodes": Array [
Object {
"$class": "org.accordproject.commonmark.Text",
"text": "This is a fixed interest loan to the amount of 100000.0",
},
Object {
"$class": "org.accordproject.commonmark.Softbreak",
},
Object {
"$class": "org.accordproject.commonmark.Text",
"text": "at the yearly interest rate of 2.5%",
},
Object {
"$class": "org.accordproject.commonmark.Softbreak",
},
Object {
"$class": "org.accordproject.commonmark.Text",
"text": "with a loan term of 15,",
},
Object {
"$class": "org.accordproject.commonmark.Softbreak",
},
Object {
"$class": "org.accordproject.commonmark.Text",
"text": "and monthly payments of 667.0",
},
],
},
Object {
"$class": "org.accordproject.commonmark.CodeBlock",
"info": "<clause src=\\"foo\\" clauseid=\\"bar\\"/>",
"tag": Object {
"$class": "org.accordproject.commonmark.TagInfo",
"attributeString": "src = \\"foo\\" clauseid = \\"bar\\" ",
Expand All @@ -256,21 +287,14 @@ Object {
"value": "bar",
},
],
"closed": false,
"content": "
this is
multiline

content
",
"closed": true,
"content": "",
"tagName": "clause",
},
"text": "<clause src=\\"foo\\" clauseid=\\"bar\\">
this is
multiline
"text": "this is
multiline <variable id=\\"rate\\" value=\\"2.5\\"/>

content
</clause>
",
},
],
Expand Down Expand Up @@ -301,6 +325,20 @@ Object {
Object {
"$class": "org.accordproject.commonmark.CodeBlock",
"info": "<video src=\\"https://www.youtube.com/embed/dQw4w9WgXcQ\\"/>",
"tag": Object {
"$class": "org.accordproject.commonmark.TagInfo",
"attributeString": "src = \\"https://www.youtube.com/embed/dQw4w9WgXcQ\\" ",
"attributes": Array [
Object {
"$class": "org.accordproject.commonmark.Attribute",
"name": "src",
"value": "https://www.youtube.com/embed/dQw4w9WgXcQ",
},
],
"closed": true,
"content": "",
"tagName": "video",
},
"text": " this is a multiline
code

Expand Down Expand Up @@ -10231,14 +10269,6 @@ Object {
},
Object {
"$class": "org.accordproject.commonmark.CodeBlock",
"tag": Object {
"$class": "org.accordproject.commonmark.TagInfo",
"attributeString": "",
"attributes": Array [],
"closed": false,
"content": "",
"tagName": "div",
},
"text": "<div>
",
},
Expand Down Expand Up @@ -10500,16 +10530,6 @@ Object {
},
Object {
"$class": "org.accordproject.commonmark.CodeBlock",
"tag": Object {
"$class": "org.accordproject.commonmark.TagInfo",
"attributeString": "",
"attributes": Array [],
"closed": false,
"content": "
Hi
",
"tagName": "td",
},
"text": "<td>
Hi
</td>
Expand Down Expand Up @@ -11702,14 +11722,6 @@ Object {
"nodes": Array [
Object {
"$class": "org.accordproject.commonmark.CodeBlock",
"tag": Object {
"$class": "org.accordproject.commonmark.TagInfo",
"attributeString": "",
"attributes": Array [],
"closed": false,
"content": "",
"tagName": "a",
},
"text": "<a/>
*hi*

Expand Down
72 changes: 55 additions & 17 deletions src/__snapshots__/CommonMark.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ Object {
"info": "<clause src=\\"ap://[email protected]#721d1aa0999a5d278653e211ae2a64b75fdd8ca6fa1f34255533c942404c5c1f\\" clauseid=\\"479adbb4-dc55-4d1a-ab12-b6c5e16900c0\\">",
"tag": Object {
"$class": "org.accordproject.commonmark.TagInfo",
"attributeString": "id = \\"shipper\\" value = \\"%22Party%20A%22\\" ",
"attributeString": "src = \\"ap://[email protected]#721d1aa0999a5d278653e211ae2a64b75fdd8ca6fa1f34255533c942404c5c1f\\" clauseid = \\"479adbb4-dc55-4d1a-ab12-b6c5e16900c0\\" ",
"attributes": Array [
Object {
"$class": "org.accordproject.commonmark.Attribute",
"name": "id",
"value": "shipper",
"name": "src",
"value": "ap://[email protected]#721d1aa0999a5d278653e211ae2a64b75fdd8ca6fa1f34255533c942404c5c1f",
},
Object {
"$class": "org.accordproject.commonmark.Attribute",
"name": "value",
"value": "%22Party%20A%22",
"name": "clauseid",
"value": "479adbb4-dc55-4d1a-ab12-b6c5e16900c0",
},
],
"closed": false,
"content": "",
"tagName": "variable",
"tagName": "clause",
},
"text": "Acceptance of Delivery. <variable id=\\"shipper\\" value=\\"%22Party%20A%22\\"/> will be deemed to have completed its delivery obligations if in <variable id=\\"receiver\\" value=\\"%22Party%20B%22\\"/>'s opinion, the <variable id=\\"deliverable\\" value=\\"%22Widgets%22\\"/> satisfies the Acceptance Criteria, and <variable id=\\"receiver\\" value=\\"%22Party%20B%22\\"/> notifies <variable id=\\"shipper\\" value=\\"%22Party%20A%22\\"/> in writing that it is accepting the <variable id=\\"deliverable\\" value=\\"%22Widgets%22\\"/>.

Expand Down Expand Up @@ -239,8 +239,39 @@ Object {
},
],
},
Object {
"$class": "org.accordproject.commonmark.Paragraph",
"nodes": Array [
Object {
"$class": "org.accordproject.commonmark.Text",
"text": "This is a fixed interest loan to the amount of 100000.0",
},
Object {
"$class": "org.accordproject.commonmark.Softbreak",
},
Object {
"$class": "org.accordproject.commonmark.Text",
"text": "at the yearly interest rate of 2.5%",
},
Object {
"$class": "org.accordproject.commonmark.Softbreak",
},
Object {
"$class": "org.accordproject.commonmark.Text",
"text": "with a loan term of 15,",
},
Object {
"$class": "org.accordproject.commonmark.Softbreak",
},
Object {
"$class": "org.accordproject.commonmark.Text",
"text": "and monthly payments of 667.0",
},
],
},
Object {
"$class": "org.accordproject.commonmark.CodeBlock",
"info": "<clause src=\\"foo\\" clauseid=\\"bar\\"/>",
"tag": Object {
"$class": "org.accordproject.commonmark.TagInfo",
"attributeString": "src = \\"foo\\" clauseid = \\"bar\\" ",
Expand All @@ -256,21 +287,14 @@ Object {
"value": "bar",
},
],
"closed": false,
"content": "
this is
multiline

content
",
"closed": true,
"content": "",
"tagName": "clause",
},
"text": "<clause src=\\"foo\\" clauseid=\\"bar\\">
this is
multiline
"text": "this is
multiline <variable id=\\"rate\\" value=\\"2.5\\"/>

content
</clause>
",
},
],
Expand Down Expand Up @@ -301,6 +325,20 @@ Object {
Object {
"$class": "org.accordproject.commonmark.CodeBlock",
"info": "<video src=\\"https://www.youtube.com/embed/dQw4w9WgXcQ\\"/>",
"tag": Object {
"$class": "org.accordproject.commonmark.TagInfo",
"attributeString": "src = \\"https://www.youtube.com/embed/dQw4w9WgXcQ\\" ",
"attributes": Array [
Object {
"$class": "org.accordproject.commonmark.Attribute",
"name": "src",
"value": "https://www.youtube.com/embed/dQw4w9WgXcQ",
},
],
"closed": true,
"content": "",
"tagName": "video",
},
"text": " this is a multiline
code

Expand Down
Loading

0 comments on commit 14d20c3

Please sign in to comment.