Skip to content

Commit e546dc8

Browse files
committed
fix(clause) Add clauseText to clause nodes
Signed-off-by: Jerome Simeon <[email protected]>
1 parent 14d20c3 commit e546dc8

File tree

7 files changed

+68
-15
lines changed

7 files changed

+68
-15
lines changed

bin/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,19 @@ require('yargs')
3838
type: 'boolean',
3939
default: false
4040
});
41+
yargs.option('--noWrap', {
42+
describe: 'do not wrap variables',
43+
type: 'boolean',
44+
default: false
45+
});
4146
}, (argv) => {
4247
if (argv.verbose) {
4348
Logger.info(`parse sample ${argv.sample} markdown`);
4449
}
4550

4651
try {
4752
argv = Commands.validateParseArgs(argv);
48-
return Commands.parse(argv.sample, argv.out, argv.generateMarkdown, argv.withCicero)
53+
return Commands.parse(argv.sample, argv.out, argv.generateMarkdown, argv.withCicero, argv.noWrap)
4954
.then((result) => {
5055
if(result) {Logger.info('\n'+result);}
5156
})

src/CiceroMark.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class CiceroMark {
5555
fromCommonMark(concertoObject) {
5656
// Add Cicero nodes
5757
const parameters = {
58+
ciceroMark: this,
5859
commonMark: this.commonMark,
5960
modelManager : this.modelManager,
6061
serializer : this.serializer,
@@ -70,23 +71,34 @@ class CiceroMark {
7071
/**
7172
* Converts a ciceromark document back to a regular commmark document
7273
* @param {*} concertoObject concerto cicero object
74+
* @param {object} [options] configuration options
7375
* @returns {*} concertoObject concerto commonmark
7476
*/
75-
toCommonMark(concertoObject) {
77+
toCommonMark(concertoObject, options) {
7678
// Add Cicero nodes
7779
const parameters = {
7880
commonMark: this.commonMark,
7981
modelManager : this.modelManager,
8082
serializer : this.serializer
8183
};
82-
const visitor = new FromCiceroVisitor();
84+
const visitor = new FromCiceroVisitor(options);
8385
concertoObject.accept( visitor, parameters );
8486

8587
// Validate
8688
const json = this.serializer.toJSON(concertoObject);
8789
return this.serializer.fromJSON(json);
8890
}
8991

92+
/**
93+
* Converts a ciceromark document back to markdown string
94+
* @param {*} concertoObject concerto cicero object
95+
* @param {object} [options] configuration options
96+
* @returns {*} concertoObject concerto commonmark
97+
*/
98+
toString(concertoObject, options) {
99+
return this.commonMark.toString(this.toCommonMark(concertoObject, options));
100+
}
101+
90102
/**
91103
* Retrieve the serializer used by the parser
92104
*

src/Commands.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ class Commands {
8686
* @param {string} outPath to an output file
8787
* @param {boolean} generateMarkdown whether to transform back to markdown
8888
* @param {boolean} withCicero whether to further transform for Cicero
89+
* @param {boolean} noWrap whether to avoid wrapping Cicero variables in XML tags
8990
* @returns {object} Promise to the result of parsing
9091
*/
91-
static parse(samplePath, outPath, generateMarkdown, withCicero) {
92+
static parse(samplePath, outPath, generateMarkdown, withCicero, noWrap) {
9293
const commonMark = new CommonMark();
9394
const ciceroMark = new CiceroMark();
9495
const markdownText = Fs.readFileSync(samplePath, 'utf8');
@@ -99,7 +100,8 @@ class Commands {
99100
let result;
100101
if (generateMarkdown) {
101102
if (withCicero) {
102-
concertoObject = ciceroMark.toCommonMark(concertoObject);
103+
const options = noWrap ? { wrapVariables: false } : null;
104+
concertoObject = ciceroMark.toCommonMark(concertoObject, options);
103105
}
104106
result = commonMark.toString(concertoObject);
105107
} else {

src/CommonMark.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -258,16 +258,17 @@ class CommonMark {
258258

259259
/**
260260
* Converts a commonmark document to a markdown string
261-
* @param {*} concertoObject concerto commonmark object
261+
* @param {*} concertoObject - concerto commonmark object
262+
* @param {*} options - options (e.g., wrapVariables)
262263
* @returns {string} the markdown string
263264
*/
264-
toString(concertoObject) {
265+
toString(concertoObject, options) {
265266
const parameters = {};
266267
parameters.result = '';
267268
parameters.first = true;
268269
parameters.indent = 0;
269-
const visitor = new ToStringVisitor();
270-
concertoObject.accept( visitor, parameters );
270+
const visitor = new ToStringVisitor(options);
271+
concertoObject.accept(visitor, parameters);
271272
return parameters.result.trim();
272273
}
273274

src/FromCiceroVisitor.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ const { COMMON_NS_PREFIX } = require('./Models');
2424
* markdown content. The resulting AST *should* be equivalent however.
2525
*/
2626
class FromCiceroVisitor {
27+
/**
28+
* Construct the visitor
29+
* @param {object} [options] configuration options
30+
*/
31+
constructor(options) {
32+
this.options = options ? options : { wrapVariables: true };
33+
}
2734

2835
/**
2936
* Visits a sub-tree and return the markdown
@@ -108,7 +115,11 @@ class FromCiceroVisitor {
108115
// Create the text for that document
109116
const content = '';
110117
const attributeString = `id="${thing.id}" value="${thing.value}"`;
111-
thing.text = `<variable ${attributeString}/>`;
118+
if (this.options && !this.options.wrapVariables) {
119+
thing.text = decodeURI(thing.value); // to decode
120+
} else {
121+
thing.text = `<variable ${attributeString}/>`;
122+
}
112123

113124
// Create the proper tag
114125
let tag = {};
@@ -144,7 +155,11 @@ class FromCiceroVisitor {
144155
// Create the text for that document
145156
const content = '';
146157
const attributeString = `value="${thing.value}"`;
147-
thing.text = `<computed ${attributeString}/>`;
158+
if (this.options && !this.options.wrapVariables) {
159+
thing.text = decodeURI(thing.value); // to decode
160+
} else {
161+
thing.text = `<computed ${attributeString}/>`;
162+
}
148163

149164
// Create the proper tag
150165
let tag = {};

src/ToCiceroVisitor.js

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

1515
'use strict';
1616

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

1919
/**
2020
* Converts a commonmark model instance to a markdown string.
@@ -68,10 +68,20 @@ class ToCiceroVisitor {
6868

6969
thing.nodes = parameters.commonMark.fromString(thing.text).nodes;
7070
ToCiceroVisitor.visitNodes(this, thing.nodes, parameters);
71-
thing.clauseText = thing.text;
71+
7272
thing.text = null; // Remove text
73+
thing.clauseText = '';
7374
delete thing.tag;
7475
delete thing.info;
76+
77+
// Go over the loaded clause to generate the unwrapped text
78+
let clone = parameters.serializer.toJSON(thing);
79+
clone.$class = COMMON_NS_PREFIX + 'Paragraph';
80+
delete clone.clauseid;
81+
delete clone.src;
82+
delete clone.clauseText;
83+
clone = parameters.serializer.fromJSON(clone);
84+
thing.clauseText = parameters.ciceroMark.toString(clone, { wrapVariables: false });
7585
}
7686
else if (tag.attributes[1].name === 'src' &&
7787
tag.attributes[0].name === 'clauseid') {
@@ -81,10 +91,19 @@ class ToCiceroVisitor {
8191

8292
thing.nodes = parameters.commonMark.fromString(thing.text).nodes;
8393
ToCiceroVisitor.visitNodes(this, thing.nodes, parameters);
84-
thing.clauseText = thing.text;
8594
thing.text = null; // Remove text
95+
thing.clauseText = '';
8696
delete thing.tag;
8797
delete thing.info;
98+
99+
// Go over the loaded clause to generate the unwrapped text
100+
let clone = parameters.serializer.toJSON(thing);
101+
clone.$class = COMMON_NS_PREFIX + 'Paragraph';
102+
delete clone.clauseid;
103+
delete clone.src;
104+
delete clone.clauseText;
105+
clone = parameters.serializer.fromJSON(clone);
106+
thing.clauseText = parameters.ciceroMark.toString(clone, { wrapVariables: false });
88107
} else {
89108
//console.log('Found Clause but without \'clauseid\' and \'src\' attributes ');
90109
}

src/ToStringVisitor.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
* markdown content. The resulting AST *should* be equivalent however.
2323
*/
2424
class ToStringVisitor {
25-
2625
/**
2726
* Visits a sub-tree and return the markdown
2827
* @param {*} visitor the visitor to use

0 commit comments

Comments
 (0)