Created
June 22, 2020 22:36
-
-
Save sachdeva-shrey/527667e502859d9f65987c9747bf7dab to your computer and use it in GitHub Desktop.
CiceroMark --> OOXML
This file contains hidden or 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
const addToContract = async (templateIndex, templateUri) => { | |
/* global Word */ | |
Word.run(async function (context) { | |
// load the template | |
const hashIndex = templateUri.indexOf('#'); | |
const templateId = templateUri.substring(5, hashIndex); | |
const templateDetails = templateIndex[templateId]; | |
const url = templateDetails.url; | |
const template = await Template.fromUrl(url); | |
const sample = template.getMetadata().getSample(); | |
// console.log(sample); | |
const clause = new Clause(template); | |
clause.parse(sample); | |
// console.log(JSON.stringify(clause.getData(), null, 2)) | |
const sampleWrapped = await clause.draft({ wrapVariables: true }); | |
// console.log(sampleWrapped); | |
const ciceroMarkTransformer = new CiceroMarkTransformer(); | |
const dom = ciceroMarkTransformer.fromMarkdown(sampleWrapped, 'json'); | |
console.log(dom) | |
const nodes = { | |
computedVariable: 'org.accordproject.ciceromark.ComputedVariable', | |
heading: 'org.accordproject.commonmark.Heading', | |
item: 'org.accordproject.commonmark.Item', | |
list: 'org.accordproject.commonmark.List', | |
listVariable: 'org.accordproject.ciceromark.ListVariable', | |
paragraph: 'org.accordproject.commonmark.Paragraph', | |
softbreak: 'org.accordproject.commonmark.Softbreak', | |
text: 'org.accordproject.commonmark.Text', | |
variable: 'org.accordproject.ciceromark.Variable', | |
} | |
function InsertHeading(value) { | |
context.document.body.insertParagraph(value, "End").font.set({ | |
color: 'black', | |
size: 13 | |
}); | |
context.document.body.insertParagraph(" ", "End"); | |
} | |
function InsertLineBreak() { | |
context.document.body.insertParagraph(" ", "End").font.set({ | |
color: 'black', | |
size: 11 | |
}); | |
} | |
function InsertSoftBreak() { | |
context.document.body.insertText(" ", "End").font.set({ | |
color: 'black', | |
size: 11 | |
}); | |
} | |
function InsertText(value) { | |
context.document.body.insertText(value, "End").font.set({ | |
color: 'black', | |
size: 11 | |
}); | |
} | |
function InsertVariable(id, value) { | |
let variableText = context.document.body.insertText(value, "End"); | |
variableText.insertContentControl().font.set({ | |
color: 'blue', | |
size: 11 | |
}); | |
variableText.title = id; | |
variableText.tag = id; | |
} | |
dom.nodes.forEach((node) => { | |
let mainClass = node.$class; | |
node.nodes.forEach((subNode) => { | |
let subClass = subNode.$class; | |
let text = subNode.text; | |
let variable = subNode.value; | |
let id = subNode.id; | |
if(mainClass === nodes.heading) { | |
InsertHeading(text); | |
} | |
else if(mainClass === nodes.paragraph) { | |
if(subClass === nodes.text) { | |
InsertText(text); | |
} | |
else if(subClass === nodes.softbreak) { | |
InsertSoftBreak(); | |
} | |
else if(subClass === nodes.variable || subClass === nodes.computedVariable) { | |
InsertVariable(id, variable); | |
} | |
} | |
else if(mainClass === nodes.listVariable || mainClass === nodes.list) { | |
if(subClass === nodes.item) { | |
InsertLineBreak(); | |
subNode.nodes.forEach((itemNode) => { | |
itemNode.nodes.forEach((rootNode) => { | |
let rootNodeClass = rootNode.$class; | |
let variable = rootNode.value; | |
let id = rootNode.id; | |
let text = rootNode.text; | |
if(rootNodeClass === nodes.variable) { | |
InsertVariable(id, variable); | |
} | |
else if (rootNodeClass === nodes.text) { | |
InsertText(text); | |
} | |
}) | |
}) | |
} | |
} | |
}) | |
}) | |
InsertLineBreak(); | |
await context.sync() | |
let contentControl = context.document.contentControls; | |
context.load(contentControl, 'text'); | |
return context.sync(); | |
}) | |
.catch(function (error) { | |
console.log("Error: " + error); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment