carbone icon indicating copy to clipboard operation
carbone copied to clipboard

[Feature Request]: Ability to Inject Word XML into our Templates

Open exzizt opened this issue 5 years ago • 9 comments

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.

exzizt avatar Apr 16 '20 22:04 exzizt

Hi @exzizt, thank you for reaching us! I will discuss about it with the team and come back to you.

steevepay avatar Apr 17 '20 08:04 steevepay

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?

exzizt avatar Apr 21 '20 02:04 exzizt

@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?

exzizt avatar May 06 '20 18:05 exzizt

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(/&amp;/g, '&').replace(/&lt;/g, "<").replace(/&gt;/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
});

steevepay avatar May 11 '20 14:05 steevepay

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...

chals-manson avatar May 18 '20 10:05 chals-manson

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.

steevepay avatar May 18 '20 15:05 steevepay

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.

chals-manson avatar May 25 '20 08:05 chals-manson

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.

steevepay avatar May 28 '20 09:05 steevepay

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(/&amp;/g, '&').replace(/&lt;/g, "<").replace(/&gt;/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.

sasithahtl avatar Jan 26 '23 13:01 sasithahtl