From e546dc8712e30fa94dbd964c0767c4ca6437fd80 Mon Sep 17 00:00:00 2001 From: Jerome Simeon Date: Wed, 11 Sep 2019 12:51:34 -0400 Subject: [PATCH] fix(clause) Add clauseText to clause nodes Signed-off-by: Jerome Simeon --- bin/index.js | 7 ++++++- src/CiceroMark.js | 16 ++++++++++++++-- src/Commands.js | 6 ++++-- src/CommonMark.js | 9 +++++---- src/FromCiceroVisitor.js | 19 +++++++++++++++++-- src/ToCiceroVisitor.js | 25 ++++++++++++++++++++++--- src/ToStringVisitor.js | 1 - 7 files changed, 68 insertions(+), 15 deletions(-) diff --git a/bin/index.js b/bin/index.js index f08f597f..57776e74 100755 --- a/bin/index.js +++ b/bin/index.js @@ -38,6 +38,11 @@ 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`); @@ -45,7 +50,7 @@ require('yargs') 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);} }) diff --git a/src/CiceroMark.js b/src/CiceroMark.js index ee4fbe92..b0d9f92d 100644 --- a/src/CiceroMark.js +++ b/src/CiceroMark.js @@ -55,6 +55,7 @@ class CiceroMark { fromCommonMark(concertoObject) { // Add Cicero nodes const parameters = { + ciceroMark: this, commonMark: this.commonMark, modelManager : this.modelManager, serializer : this.serializer, @@ -70,16 +71,17 @@ 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 @@ -87,6 +89,16 @@ class CiceroMark { 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 * diff --git a/src/Commands.js b/src/Commands.js index 4160b6f6..689c69a8 100644 --- a/src/Commands.js +++ b/src/Commands.js @@ -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'); @@ -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 { diff --git a/src/CommonMark.js b/src/CommonMark.js index 9734f471..f690cb60 100644 --- a/src/CommonMark.js +++ b/src/CommonMark.js @@ -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(); } diff --git a/src/FromCiceroVisitor.js b/src/FromCiceroVisitor.js index 86793b19..e656bac6 100644 --- a/src/FromCiceroVisitor.js +++ b/src/FromCiceroVisitor.js @@ -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 @@ -108,7 +115,11 @@ class FromCiceroVisitor { // Create the text for that document const content = ''; const attributeString = `id="${thing.id}" value="${thing.value}"`; - thing.text = ``; + if (this.options && !this.options.wrapVariables) { + thing.text = decodeURI(thing.value); // to decode + } else { + thing.text = ``; + } // Create the proper tag let tag = {}; @@ -144,7 +155,11 @@ class FromCiceroVisitor { // Create the text for that document const content = ''; const attributeString = `value="${thing.value}"`; - thing.text = ``; + if (this.options && !this.options.wrapVariables) { + thing.text = decodeURI(thing.value); // to decode + } else { + thing.text = ``; + } // Create the proper tag let tag = {}; diff --git a/src/ToCiceroVisitor.js b/src/ToCiceroVisitor.js index cad1c7c4..dcfc855f 100644 --- a/src/ToCiceroVisitor.js +++ b/src/ToCiceroVisitor.js @@ -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. @@ -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') { @@ -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 '); } diff --git a/src/ToStringVisitor.js b/src/ToStringVisitor.js index ef96bbf0..2011b5e2 100644 --- a/src/ToStringVisitor.js +++ b/src/ToStringVisitor.js @@ -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