carbone
carbone copied to clipboard
[Feature Request]: Ability to Inject Word XML into our Templates
Problem to solve It would be nice to be able to inject and render valid Word XML tags into our document templates. Using the following XML as an example:
<w:r>
<w:rPr>
<w:b />
</w:rPr>
<w:t>This text is bold</w:t>
</w:r>
Injecting this XML into a template field would make the text bold (This text is bold).
Proposed solution
Provide a function, e.g., {d.xml_string:parseXml()} that can be used to parse and inject valid Word XML into the document.
Describe alternatives you've considered As an alternative, a configuration option could be provided to not escape XML that is passed to fields in the template.
Additional context I work on a team where this would be very useful. Specifically, we would like to use it to create the XML required to render complex nested lists of varying depths.
Hi @exzizt, thank you for reaching us! I will discuss about it with the team and come back to you.
Hi @exzizt, thank you for reaching us! I will discuss about it with the team and come back to you.
Thank you! We are very interested in this feature. Do you know, is it possible currently to insert/render Word XML into a document using Carbone? Perhaps through custom formatters, or another function?
@steevepay Hi Steve, just pinging you again in case you missed my message. We are still very curious and interested if this is possible to do using Carbone in its current state. We had a member on our team successfully create a custom formatter that uses the "canInjextXML=true" flag and was able to inject Word XML to create new lines, however, all attempts to generate "more complex" XML such as lists have failed. Any tips?
Hi @exzizt, sorry for the delay
For now, the only solution is to use the flag canInjextXML=true. It may result in some overflow and the final report may appear invalid because it does not follow the Open XML specifications.
The team is working on rendering HTML into templates, and I think it will be a much better solution to inject complex content into the document.
If you don't mind, can you share a report you have generated with a list? I would like to investigate on my side. My email is available on my Github profile.
For those who want to inject XML into a template, it is possible to create a custom formatter:
const carbone = require('carbone');
function injectXML (data) { // data = d.myCustomXML
if (data) {
return data.replace(/&/g, '&').replace(/</g, "<").replace(/>/g, ">");
}
return "";
}
//flag used by the builder function to inject raw XML
injectXML.canInjectXML = true;
carbone.addFormatters({
// this formatter can be used in a template with {d.myCustomXML:injectXML()}
injectXML
});
I tried @steevepay workaround for inject XML into an ODT template, but it's always injected inside a text:p tag. I don't know if it's the expected behauvior...
Yes it is the expected behavior, the formatter injects raw XML inside the content. Preprocess can be added to remove the <text:p> or other levels of parent tags. Even if the text editors are following the Open XML specification, the tags are most of the time really different and depend on the file type.
Hi @steevepay . Thanks for your answer. Sorry but I don't know how I can add the preprocess. I was reading the code and I did not found something similar to formatters.
You can add an XML preprocess function into https://github.com/Ideolys/carbone/blob/master/lib/preprocessor.js#L12, you can parse the content and remove the tags around injectXML. It is going to be executed before the building process.
Hi @exzizt, sorry for the delay For now, the only solution is to use the flag
canInjextXML=true. It may result in some overflow and the final report may appear invalid because it does not follow the Open XML specifications. The team is working on rendering HTML into templates, and I think it will be a much better solution to inject complex content into the document.If you don't mind, can you share a report you have generated with a list? I would like to investigate on my side. My email is available on my Github profile.
For those who want to inject XML into a template, it is possible to create a custom formatter:
const carbone = require('carbone'); function injectXML (data) { // data = d.myCustomXML if (data) { return data.replace(/&/g, '&').replace(/</g, "<").replace(/>/g, ">"); } return ""; } //flag used by the builder function to inject raw XML injectXML.canInjectXML = true; carbone.addFormatters({ // this formatter can be used in a template with {d.myCustomXML:injectXML()} injectXML });
Thank you! saved the day.