-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP(template) Generate markdown_template from templatemark
Signed-off-by: Jerome Simeon <[email protected]>
- Loading branch information
1 parent
1752554
commit 9fefb45
Showing
35 changed files
with
276 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
packages/markdown-template/lib/ToMarkdownTemplateVisitor.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const CommonMarkUtils = require('@accordproject/markdown-common').CommonMarkUtils; | ||
const FromCommonMarkVisitor = require('@accordproject/markdown-common').FromCommonMarkVisitor; | ||
const fromcommonmarkrules = require('@accordproject/markdown-common').fromcommonmarkrules; | ||
const fromtemplatemarkrules = require('./fromtemplatemarkrules'); | ||
|
||
/** | ||
* Fixes up the root note, removing Clause or Contract indication | ||
* @param {object} input the input templatemark | ||
* @return {object} the fixed up templatemark | ||
*/ | ||
function fixupRootNode(input) { | ||
const rootNode = { | ||
'$class': 'org.accordproject.commonmark.Document', | ||
'xmlns' : 'http://commonmark.org/xml/1.0', | ||
'nodes': input.nodes[0].nodes | ||
}; | ||
return rootNode; | ||
} | ||
|
||
/** | ||
* Converts a TemplateMark DOM to a template markdown string. | ||
*/ | ||
class ToMarkdownTemplateVisitor extends FromCommonMarkVisitor { | ||
/** | ||
* Construct the visitor. | ||
* @param {object} [options] configuration options | ||
* @param {*} resultSeq how to sequentially combine results | ||
* @param {object} rules how to process each node type | ||
*/ | ||
constructor(options) { | ||
const resultString = (result) => { | ||
return result; | ||
}; | ||
const resultSeq = (parameters,result) => { | ||
result.forEach((next) => { | ||
parameters.result += next; | ||
}); | ||
}; | ||
const setFirst = (thingType) => { | ||
return thingType === 'Item' || thingType === 'ClauseDefinition' || thingType === 'ListBlockDefinition' ? true : false; | ||
}; | ||
const rules = fromcommonmarkrules; | ||
Object.assign(rules,fromtemplatemarkrules); | ||
super(options,resultString,resultSeq,rules,setFirst); | ||
} | ||
|
||
/** | ||
* Converts a TemplateMark DOM to a template markdown string. | ||
* @param {*} serializer - TemplateMark serializer | ||
* @param {*} input - TemplateMark DOM (JSON) | ||
* @returns {string} the template markdown string | ||
*/ | ||
toMarkdownTemplate(serializer,input) { | ||
const parameters = {}; | ||
const fixedInput = serializer.fromJSON(fixupRootNode(input)); | ||
parameters.result = this.resultString(''); | ||
parameters.stack = CommonMarkUtils.blocksInit(); | ||
fixedInput.accept(this, parameters); | ||
return parameters.result.trim(); | ||
} | ||
} | ||
|
||
module.exports = ToMarkdownTemplateVisitor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const CommonMarkUtils = require('@accordproject/markdown-common').CommonMarkUtils; | ||
|
||
const rules = {}; | ||
// Inlines | ||
rules.VariableDefinition = (visitor,thing,children,parameters,resultString,resultSeq) => { | ||
const result = [resultString('{{'),resultString(thing.name),resultString('}}')]; | ||
resultSeq(parameters,result); | ||
}; | ||
rules.FormattedVariableDefinition = (visitor,thing,children,parameters,resultString,resultSeq) => { | ||
const result = [resultString('{{'),resultString(thing.name),resultString(' as "'),resultString(thing.format),resultString('"}}')]; | ||
resultSeq(parameters,result); | ||
}; | ||
rules.EnumVariableDefinition = (visitor,thing,children,parameters,resultString,resultSeq) => { | ||
const result = [resultString('{{'),resultString(thing.name),resultString('}}')]; | ||
resultSeq(parameters,result); | ||
}; | ||
rules.FormulaDefinition = (visitor,thing,children,parameters,resultString,resultSeq) => { | ||
const result = [resultString('{{%'),resultString(thing.code),resultString('%}}')]; | ||
resultSeq(parameters,result); | ||
}; | ||
rules.ConditionalDefinition = (visitor,thing,children,parameters,resultString,resultSeq) => { | ||
const next1 = `{{#if ${thing.name}}}`; | ||
const whenTrue = visitor.visitChildren(visitor,thing,parameters,'whenTrue'); | ||
const whenFalse = visitor.visitChildren(visitor,thing,parameters,'whenFalse'); | ||
const next2 = '{{/if}}'; | ||
let result; | ||
if (whenFalse) { | ||
const next3 = '{{else}}'; | ||
result = [resultString(next1),resultString(whenTrue),resultString(next3),resultString(whenFalse),resultString(next2)]; | ||
} else { | ||
result = [resultString(next1),resultString(whenTrue),resultString(next2)]; | ||
} | ||
resultSeq(parameters,result); | ||
}; | ||
rules.OptionalDefinition = (visitor,thing,children,parameters,resultString,resultSeq) => { | ||
const next1 = `{{#optional ${thing.name}}}`; | ||
const whenSome = visitor.visitChildren(visitor,thing,parameters,'whenSome'); | ||
const whenNone = visitor.visitChildren(visitor,thing,parameters,'whenNone'); | ||
const next2 = '{{/optional}}'; | ||
let result; | ||
if (whenNone) { | ||
const next3 = '{{else}}'; | ||
result = [resultString(next1),resultString(whenSome),resultString(next3),resultString(whenNone),resultString(next2)]; | ||
} else { | ||
result = [resultString(next1),resultString(whenSome),resultString(next2)]; | ||
} | ||
resultSeq(parameters,result); | ||
}; | ||
rules.WithDefinition = (visitor,thing,children,parameters,resultString,resultSeq) => { | ||
const next1 = `{{#with ${thing.name}}}`; | ||
const next2 = '{{/with}}'; | ||
const result = [resultString(next1),children,resultString(next2)]; | ||
resultSeq(parameters,result); | ||
}; | ||
rules.JoinDefinition = (visitor,thing,children,parameters,resultString,resultSeq) => { | ||
const sepAttr = thing.separator ? ' separator="' + thing.separator + '"' : ''; | ||
const next1 = `{{#join ${thing.name}${sepAttr}}}`; | ||
const next2 = '{{/join}}'; | ||
const result = [resultString(next1),children,resultString(next2)]; | ||
resultSeq(parameters,result); | ||
}; | ||
// Container blocks | ||
rules.ListBlockDefinition = (visitor,thing,children,parameters,resultString,resultSeq) => { | ||
const listKind = thing.type === 'bullet' ? 'ulist' : 'olist'; | ||
const prefix = CommonMarkUtils.mkPrefix(parameters,1); | ||
const next1 = prefix; | ||
const next2 = `{{#${listKind} ${thing.name}}}\n`; | ||
const next3 = prefix; | ||
const next4 = `{{/${listKind}}}`; | ||
const result = [resultString(next1),resultString(next2),children,resultString(next3),resultString(next4)]; | ||
resultSeq(parameters,result); | ||
}; | ||
rules.ClauseDefinition = (visitor,thing,children,parameters,resultString,resultSeq) => { | ||
const next1 = CommonMarkUtils.mkPrefix(parameters,2); | ||
const srcAttr = thing.src ? ' src="' + thing.src + '"' : ''; | ||
const next2 = `{{#clause ${thing.name}${srcAttr}}}\n`; | ||
const next3 = '\n{{/clause}}'; | ||
const result = [resultString(next1),resultString(next2),children,resultString(next3)]; | ||
resultSeq(parameters,result); | ||
}; | ||
|
||
module.exports = rules; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 6 additions & 3 deletions
9
packages/markdown-template/test/data/acceptance-of-delivery/grammar.tem.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
# Write your clause template here | ||
Write your clause template here | ||
==== |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.