Skip to content

Commit 14d20c3

Browse files
committed
fix(codeblock) Clauses are now recognized through code block's info field, bug in cicero to commonmark
Signed-off-by: Jerome Simeon <[email protected]>
1 parent 8b9af9a commit 14d20c3

File tree

8 files changed

+144
-73
lines changed

8 files changed

+144
-73
lines changed

src/CommonMark.js

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,16 +72,24 @@ class CommonMark {
7272
}
7373

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

84+
/**
85+
* Is it a Code Block node?
86+
* @param {*} json the JS Object for the AST
87+
* @return {boolean} whether it's a leaf node
88+
*/
89+
static isCodeBlockNode(json) {
90+
return json.$class === (COMMON_NS_PREFIX + 'CodeBlock');
91+
}
92+
8593
/**
8694
* Parses a markdown string into a Concerto DOM object.
8795
*
@@ -107,8 +115,9 @@ class CommonMark {
107115
if (CommonMark.isLeafNode(head)) {
108116
head.text = t;
109117
}
110-
if (CommonMark.isHtmlNode(head)) {
111-
const tagInfo = CommonMark.parseHtmlBlock(head.text);
118+
if (CommonMark.isHtmlNode(head) || CommonMark.isCodeBlockNode(head)) {
119+
const maybeHtmlText = CommonMark.isHtmlNode(head) ? head.text : head.info;
120+
const tagInfo = CommonMark.parseHtmlBlock(maybeHtmlText);
112121
if (tagInfo) {
113122
head.tag = {};
114123
head.tag.$class = COMMON_NS_PREFIX + 'TagInfo';

src/FromCiceroVisitor.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ class FromCiceroVisitor {
5050
let jsonSource = {};
5151
let jsonTarget = {};
5252

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

@@ -62,7 +63,7 @@ class FromCiceroVisitor {
6263
const content = parameters.commonMark.toString(parameters.serializer.fromJSON(jsonSource));
6364
const attributeString = `src="${clauseJson.src}" clauseid="${clauseJson.clauseid}"`;
6465

65-
jsonTarget.text = `<clause ${attributeString}>\n${content}\n</clause>\n`;
66+
jsonTarget.text = content + '\n';
6667

6768
// Create the proper tag
6869
let tag = {};
@@ -91,11 +92,13 @@ class FromCiceroVisitor {
9192

9293
delete thing.clauseid;
9394
delete thing.src;
95+
delete thing.clauseText;
9496

9597
thing.$classDeclaration = validatedTarget.$classDeclaration;
9698
thing.tag = validatedTarget.tag;
9799
thing.nodes = validatedTarget.nodes;
98100
thing.text = validatedTarget.text;
101+
thing.info = `<clause ${attributeString}/>`;
99102
}
100103
break;
101104
case 'Variable': {

src/Models.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ import org.accordproject.commonmark.*
133133
concept Clause extends Child {
134134
o String clauseid
135135
o String src
136+
o String clauseText
136137
}
137138
138139
concept Variable extends Child {

src/ToCiceroVisitor.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,25 @@ class ToCiceroVisitor {
6666
thing.src = tag.attributes[0].value;
6767
thing.clauseid = tag.attributes[1].value;
6868

69-
thing.nodes = parameters.commonMark.fromString(tag.content).nodes;
69+
thing.nodes = parameters.commonMark.fromString(thing.text).nodes;
7070
ToCiceroVisitor.visitNodes(this, thing.nodes, parameters);
71+
thing.clauseText = thing.text;
7172
thing.text = null; // Remove text
7273
delete thing.tag;
74+
delete thing.info;
7375
}
7476
else if (tag.attributes[1].name === 'src' &&
7577
tag.attributes[0].name === 'clauseid') {
7678
thing.$classDeclaration = parameters.commonMark.fromString.getType(CICERO_NS_PREFIX + 'Clause');
7779
thing.clauseid = tag.attributes[0].value;
7880
thing.src = tag.attributes[1].value;
7981

80-
thing.nodes = parameters.commonMark.fromString(tag.content).nodes;
82+
thing.nodes = parameters.commonMark.fromString(thing.text).nodes;
8183
ToCiceroVisitor.visitNodes(this, thing.nodes, parameters);
84+
thing.clauseText = thing.text;
8285
thing.text = null; // Remove text
8386
delete thing.tag;
87+
delete thing.info;
8488
} else {
8589
//console.log('Found Clause but without \'clauseid\' and \'src\' attributes ');
8690
}

src/ToStringVisitor.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class ToStringVisitor {
124124
switch(thing.getType()) {
125125
case 'CodeBlock':
126126
ToStringVisitor.newParagraph(parameters);
127-
parameters.result += `\`\`\`${thing.info ? thing.info : ''}\n${thing.text}\`\`\`\n\n`;
127+
parameters.result += `\`\`\`${thing.info ? ' ' + thing.info : ''}\n${thing.text}\`\`\`\n\n`;
128128
break;
129129
case 'Code':
130130
parameters.result += `\`${thing.text}\``;

src/__snapshots__/CiceroMark.test.js.snap

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,22 @@ Object {
4949
"info": "<clause src=\\"ap://[email protected]#721d1aa0999a5d278653e211ae2a64b75fdd8ca6fa1f34255533c942404c5c1f\\" clauseid=\\"479adbb4-dc55-4d1a-ab12-b6c5e16900c0\\">",
5050
"tag": Object {
5151
"$class": "org.accordproject.commonmark.TagInfo",
52-
"attributeString": "id = \\"shipper\\" value = \\"%22Party%20A%22\\" ",
52+
"attributeString": "src = \\"ap://[email protected]#721d1aa0999a5d278653e211ae2a64b75fdd8ca6fa1f34255533c942404c5c1f\\" clauseid = \\"479adbb4-dc55-4d1a-ab12-b6c5e16900c0\\" ",
5353
"attributes": Array [
5454
Object {
5555
"$class": "org.accordproject.commonmark.Attribute",
56-
"name": "id",
57-
"value": "shipper",
56+
"name": "src",
57+
"value": "ap://[email protected]#721d1aa0999a5d278653e211ae2a64b75fdd8ca6fa1f34255533c942404c5c1f",
5858
},
5959
Object {
6060
"$class": "org.accordproject.commonmark.Attribute",
61-
"name": "value",
62-
"value": "%22Party%20A%22",
61+
"name": "clauseid",
62+
"value": "479adbb4-dc55-4d1a-ab12-b6c5e16900c0",
6363
},
6464
],
6565
"closed": false,
6666
"content": "",
67-
"tagName": "variable",
67+
"tagName": "clause",
6868
},
6969
"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\\"/>.
7070

@@ -239,8 +239,39 @@ Object {
239239
},
240240
],
241241
},
242+
Object {
243+
"$class": "org.accordproject.commonmark.Paragraph",
244+
"nodes": Array [
245+
Object {
246+
"$class": "org.accordproject.commonmark.Text",
247+
"text": "This is a fixed interest loan to the amount of 100000.0",
248+
},
249+
Object {
250+
"$class": "org.accordproject.commonmark.Softbreak",
251+
},
252+
Object {
253+
"$class": "org.accordproject.commonmark.Text",
254+
"text": "at the yearly interest rate of 2.5%",
255+
},
256+
Object {
257+
"$class": "org.accordproject.commonmark.Softbreak",
258+
},
259+
Object {
260+
"$class": "org.accordproject.commonmark.Text",
261+
"text": "with a loan term of 15,",
262+
},
263+
Object {
264+
"$class": "org.accordproject.commonmark.Softbreak",
265+
},
266+
Object {
267+
"$class": "org.accordproject.commonmark.Text",
268+
"text": "and monthly payments of 667.0",
269+
},
270+
],
271+
},
242272
Object {
243273
"$class": "org.accordproject.commonmark.CodeBlock",
274+
"info": "<clause src=\\"foo\\" clauseid=\\"bar\\"/>",
244275
"tag": Object {
245276
"$class": "org.accordproject.commonmark.TagInfo",
246277
"attributeString": "src = \\"foo\\" clauseid = \\"bar\\" ",
@@ -256,21 +287,14 @@ Object {
256287
"value": "bar",
257288
},
258289
],
259-
"closed": false,
260-
"content": "
261-
this is
262-
multiline
263-
264-
content
265-
",
290+
"closed": true,
291+
"content": "",
266292
"tagName": "clause",
267293
},
268-
"text": "<clause src=\\"foo\\" clauseid=\\"bar\\">
269-
this is
270-
multiline
294+
"text": "this is
295+
multiline <variable id=\\"rate\\" value=\\"2.5\\"/>
271296

272297
content
273-
</clause>
274298
",
275299
},
276300
],
@@ -301,6 +325,20 @@ Object {
301325
Object {
302326
"$class": "org.accordproject.commonmark.CodeBlock",
303327
"info": "<video src=\\"https://www.youtube.com/embed/dQw4w9WgXcQ\\"/>",
328+
"tag": Object {
329+
"$class": "org.accordproject.commonmark.TagInfo",
330+
"attributeString": "src = \\"https://www.youtube.com/embed/dQw4w9WgXcQ\\" ",
331+
"attributes": Array [
332+
Object {
333+
"$class": "org.accordproject.commonmark.Attribute",
334+
"name": "src",
335+
"value": "https://www.youtube.com/embed/dQw4w9WgXcQ",
336+
},
337+
],
338+
"closed": true,
339+
"content": "",
340+
"tagName": "video",
341+
},
304342
"text": " this is a multiline
305343
code
306344

@@ -10231,14 +10269,6 @@ Object {
1023110269
},
1023210270
Object {
1023310271
"$class": "org.accordproject.commonmark.CodeBlock",
10234-
"tag": Object {
10235-
"$class": "org.accordproject.commonmark.TagInfo",
10236-
"attributeString": "",
10237-
"attributes": Array [],
10238-
"closed": false,
10239-
"content": "",
10240-
"tagName": "div",
10241-
},
1024210272
"text": "<div>
1024310273
",
1024410274
},
@@ -10500,16 +10530,6 @@ Object {
1050010530
},
1050110531
Object {
1050210532
"$class": "org.accordproject.commonmark.CodeBlock",
10503-
"tag": Object {
10504-
"$class": "org.accordproject.commonmark.TagInfo",
10505-
"attributeString": "",
10506-
"attributes": Array [],
10507-
"closed": false,
10508-
"content": "
10509-
Hi
10510-
",
10511-
"tagName": "td",
10512-
},
1051310533
"text": "<td>
1051410534
Hi
1051510535
</td>
@@ -11702,14 +11722,6 @@ Object {
1170211722
"nodes": Array [
1170311723
Object {
1170411724
"$class": "org.accordproject.commonmark.CodeBlock",
11705-
"tag": Object {
11706-
"$class": "org.accordproject.commonmark.TagInfo",
11707-
"attributeString": "",
11708-
"attributes": Array [],
11709-
"closed": false,
11710-
"content": "",
11711-
"tagName": "a",
11712-
},
1171311725
"text": "<a/>
1171411726
*hi*
1171511727

src/__snapshots__/CommonMark.test.js.snap

Lines changed: 55 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,22 @@ Object {
4949
"info": "<clause src=\\"ap://[email protected]#721d1aa0999a5d278653e211ae2a64b75fdd8ca6fa1f34255533c942404c5c1f\\" clauseid=\\"479adbb4-dc55-4d1a-ab12-b6c5e16900c0\\">",
5050
"tag": Object {
5151
"$class": "org.accordproject.commonmark.TagInfo",
52-
"attributeString": "id = \\"shipper\\" value = \\"%22Party%20A%22\\" ",
52+
"attributeString": "src = \\"ap://[email protected]#721d1aa0999a5d278653e211ae2a64b75fdd8ca6fa1f34255533c942404c5c1f\\" clauseid = \\"479adbb4-dc55-4d1a-ab12-b6c5e16900c0\\" ",
5353
"attributes": Array [
5454
Object {
5555
"$class": "org.accordproject.commonmark.Attribute",
56-
"name": "id",
57-
"value": "shipper",
56+
"name": "src",
57+
"value": "ap://[email protected]#721d1aa0999a5d278653e211ae2a64b75fdd8ca6fa1f34255533c942404c5c1f",
5858
},
5959
Object {
6060
"$class": "org.accordproject.commonmark.Attribute",
61-
"name": "value",
62-
"value": "%22Party%20A%22",
61+
"name": "clauseid",
62+
"value": "479adbb4-dc55-4d1a-ab12-b6c5e16900c0",
6363
},
6464
],
6565
"closed": false,
6666
"content": "",
67-
"tagName": "variable",
67+
"tagName": "clause",
6868
},
6969
"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\\"/>.
7070

@@ -239,8 +239,39 @@ Object {
239239
},
240240
],
241241
},
242+
Object {
243+
"$class": "org.accordproject.commonmark.Paragraph",
244+
"nodes": Array [
245+
Object {
246+
"$class": "org.accordproject.commonmark.Text",
247+
"text": "This is a fixed interest loan to the amount of 100000.0",
248+
},
249+
Object {
250+
"$class": "org.accordproject.commonmark.Softbreak",
251+
},
252+
Object {
253+
"$class": "org.accordproject.commonmark.Text",
254+
"text": "at the yearly interest rate of 2.5%",
255+
},
256+
Object {
257+
"$class": "org.accordproject.commonmark.Softbreak",
258+
},
259+
Object {
260+
"$class": "org.accordproject.commonmark.Text",
261+
"text": "with a loan term of 15,",
262+
},
263+
Object {
264+
"$class": "org.accordproject.commonmark.Softbreak",
265+
},
266+
Object {
267+
"$class": "org.accordproject.commonmark.Text",
268+
"text": "and monthly payments of 667.0",
269+
},
270+
],
271+
},
242272
Object {
243273
"$class": "org.accordproject.commonmark.CodeBlock",
274+
"info": "<clause src=\\"foo\\" clauseid=\\"bar\\"/>",
244275
"tag": Object {
245276
"$class": "org.accordproject.commonmark.TagInfo",
246277
"attributeString": "src = \\"foo\\" clauseid = \\"bar\\" ",
@@ -256,21 +287,14 @@ Object {
256287
"value": "bar",
257288
},
258289
],
259-
"closed": false,
260-
"content": "
261-
this is
262-
multiline
263-
264-
content
265-
",
290+
"closed": true,
291+
"content": "",
266292
"tagName": "clause",
267293
},
268-
"text": "<clause src=\\"foo\\" clauseid=\\"bar\\">
269-
this is
270-
multiline
294+
"text": "this is
295+
multiline <variable id=\\"rate\\" value=\\"2.5\\"/>
271296

272297
content
273-
</clause>
274298
",
275299
},
276300
],
@@ -301,6 +325,20 @@ Object {
301325
Object {
302326
"$class": "org.accordproject.commonmark.CodeBlock",
303327
"info": "<video src=\\"https://www.youtube.com/embed/dQw4w9WgXcQ\\"/>",
328+
"tag": Object {
329+
"$class": "org.accordproject.commonmark.TagInfo",
330+
"attributeString": "src = \\"https://www.youtube.com/embed/dQw4w9WgXcQ\\" ",
331+
"attributes": Array [
332+
Object {
333+
"$class": "org.accordproject.commonmark.Attribute",
334+
"name": "src",
335+
"value": "https://www.youtube.com/embed/dQw4w9WgXcQ",
336+
},
337+
],
338+
"closed": true,
339+
"content": "",
340+
"tagName": "video",
341+
},
304342
"text": " this is a multiline
305343
code
306344

0 commit comments

Comments
 (0)