Skip to content

Commit

Permalink
fix(clause) Add clauseText to clause nodes
Browse files Browse the repository at this point in the history
Signed-off-by: Jerome Simeon <[email protected]>
  • Loading branch information
jeromesimeon committed Sep 11, 2019
1 parent 14d20c3 commit e546dc8
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 15 deletions.
7 changes: 6 additions & 1 deletion bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,19 @@ require('yargs')
type: 'boolean',
default: false
});
yargs.option('--noWrap', {
describe: 'do not wrap variables',
type: 'boolean',
default: false
});
}, (argv) => {
if (argv.verbose) {
Logger.info(`parse sample ${argv.sample} markdown`);
}

try {
argv = Commands.validateParseArgs(argv);
return Commands.parse(argv.sample, argv.out, argv.generateMarkdown, argv.withCicero)
return Commands.parse(argv.sample, argv.out, argv.generateMarkdown, argv.withCicero, argv.noWrap)
.then((result) => {
if(result) {Logger.info('\n'+result);}
})
Expand Down
16 changes: 14 additions & 2 deletions src/CiceroMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class CiceroMark {
fromCommonMark(concertoObject) {
// Add Cicero nodes
const parameters = {
ciceroMark: this,
commonMark: this.commonMark,
modelManager : this.modelManager,
serializer : this.serializer,
Expand All @@ -70,23 +71,34 @@ class CiceroMark {
/**
* Converts a ciceromark document back to a regular commmark document
* @param {*} concertoObject concerto cicero object
* @param {object} [options] configuration options
* @returns {*} concertoObject concerto commonmark
*/
toCommonMark(concertoObject) {
toCommonMark(concertoObject, options) {
// Add Cicero nodes
const parameters = {
commonMark: this.commonMark,
modelManager : this.modelManager,
serializer : this.serializer
};
const visitor = new FromCiceroVisitor();
const visitor = new FromCiceroVisitor(options);
concertoObject.accept( visitor, parameters );

// Validate
const json = this.serializer.toJSON(concertoObject);
return this.serializer.fromJSON(json);
}

/**
* Converts a ciceromark document back to markdown string
* @param {*} concertoObject concerto cicero object
* @param {object} [options] configuration options
* @returns {*} concertoObject concerto commonmark
*/
toString(concertoObject, options) {
return this.commonMark.toString(this.toCommonMark(concertoObject, options));
}

/**
* Retrieve the serializer used by the parser
*
Expand Down
6 changes: 4 additions & 2 deletions src/Commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,10 @@ class Commands {
* @param {string} outPath to an output file
* @param {boolean} generateMarkdown whether to transform back to markdown
* @param {boolean} withCicero whether to further transform for Cicero
* @param {boolean} noWrap whether to avoid wrapping Cicero variables in XML tags
* @returns {object} Promise to the result of parsing
*/
static parse(samplePath, outPath, generateMarkdown, withCicero) {
static parse(samplePath, outPath, generateMarkdown, withCicero, noWrap) {
const commonMark = new CommonMark();
const ciceroMark = new CiceroMark();
const markdownText = Fs.readFileSync(samplePath, 'utf8');
Expand All @@ -99,7 +100,8 @@ class Commands {
let result;
if (generateMarkdown) {
if (withCicero) {
concertoObject = ciceroMark.toCommonMark(concertoObject);
const options = noWrap ? { wrapVariables: false } : null;
concertoObject = ciceroMark.toCommonMark(concertoObject, options);
}
result = commonMark.toString(concertoObject);
} else {
Expand Down
9 changes: 5 additions & 4 deletions src/CommonMark.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,17 @@ class CommonMark {

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

Expand Down
19 changes: 17 additions & 2 deletions src/FromCiceroVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ const { COMMON_NS_PREFIX } = require('./Models');
* markdown content. The resulting AST *should* be equivalent however.
*/
class FromCiceroVisitor {
/**
* Construct the visitor
* @param {object} [options] configuration options
*/
constructor(options) {
this.options = options ? options : { wrapVariables: true };
}

/**
* Visits a sub-tree and return the markdown
Expand Down Expand Up @@ -108,7 +115,11 @@ class FromCiceroVisitor {
// Create the text for that document
const content = '';
const attributeString = `id="${thing.id}" value="${thing.value}"`;
thing.text = `<variable ${attributeString}/>`;
if (this.options && !this.options.wrapVariables) {
thing.text = decodeURI(thing.value); // to decode
} else {
thing.text = `<variable ${attributeString}/>`;
}

// Create the proper tag
let tag = {};
Expand Down Expand Up @@ -144,7 +155,11 @@ class FromCiceroVisitor {
// Create the text for that document
const content = '';
const attributeString = `value="${thing.value}"`;
thing.text = `<computed ${attributeString}/>`;
if (this.options && !this.options.wrapVariables) {
thing.text = decodeURI(thing.value); // to decode
} else {
thing.text = `<computed ${attributeString}/>`;
}

// Create the proper tag
let tag = {};
Expand Down
25 changes: 22 additions & 3 deletions src/ToCiceroVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

'use strict';

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

/**
* Converts a commonmark model instance to a markdown string.
Expand Down Expand Up @@ -68,10 +68,20 @@ class ToCiceroVisitor {

thing.nodes = parameters.commonMark.fromString(thing.text).nodes;
ToCiceroVisitor.visitNodes(this, thing.nodes, parameters);
thing.clauseText = thing.text;

thing.text = null; // Remove text
thing.clauseText = '';
delete thing.tag;
delete thing.info;

// Go over the loaded clause to generate the unwrapped text
let clone = parameters.serializer.toJSON(thing);
clone.$class = COMMON_NS_PREFIX + 'Paragraph';
delete clone.clauseid;
delete clone.src;
delete clone.clauseText;
clone = parameters.serializer.fromJSON(clone);
thing.clauseText = parameters.ciceroMark.toString(clone, { wrapVariables: false });
}
else if (tag.attributes[1].name === 'src' &&
tag.attributes[0].name === 'clauseid') {
Expand All @@ -81,10 +91,19 @@ class ToCiceroVisitor {

thing.nodes = parameters.commonMark.fromString(thing.text).nodes;
ToCiceroVisitor.visitNodes(this, thing.nodes, parameters);
thing.clauseText = thing.text;
thing.text = null; // Remove text
thing.clauseText = '';
delete thing.tag;
delete thing.info;

// Go over the loaded clause to generate the unwrapped text
let clone = parameters.serializer.toJSON(thing);
clone.$class = COMMON_NS_PREFIX + 'Paragraph';
delete clone.clauseid;
delete clone.src;
delete clone.clauseText;
clone = parameters.serializer.fromJSON(clone);
thing.clauseText = parameters.ciceroMark.toString(clone, { wrapVariables: false });
} else {
//console.log('Found Clause but without \'clauseid\' and \'src\' attributes ');
}
Expand Down
1 change: 0 additions & 1 deletion src/ToStringVisitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
* markdown content. The resulting AST *should* be equivalent however.
*/
class ToStringVisitor {

/**
* Visits a sub-tree and return the markdown
* @param {*} visitor the visitor to use
Expand Down

0 comments on commit e546dc8

Please sign in to comment.