bokuweb / docx-rs

:memo: A .docx file writer with Rust/WebAssembly.
https://bokuweb.github.io/docx-rs/
MIT License
323 stars 56 forks source link

Footnote support #716

Closed git-noise closed 3 weeks ago

git-noise commented 1 month ago

Is your feature request related to a problem? Please describe.

It seems that one cannot currently insert footnotes

Describe the solution you'd like

A way to include a footnote within a paragraph.

Describe alternatives you've considered

Edit the document XML afterwards, and create footnotes.xml

Additional context

I have not gone too far yet, but a solution could be along the following: 1- extend Paragraph with a add_footnote_reference: The XML seems to look like:

<w:r>
  <w:rPr>
    <w:rStyle w:val="FootnoteReference"/>
   </w:rPr>
   <w:footnoteReference w:id="1"/>
</w:r>

2- create footnotes.xml as a separate XML file within the archive.

From a quick look, it seems that 1 is doable (extend Paragraph and RunChild essentially). One could even do some of that externally using Trait as Run and RunChild seem essentially public, but RunChild as an Enum would probably need to be extended with a new variant for FootnoteReference.

However 2 may be a bit more challenging. Creating the XML itself seems straight forward, but then one would also need to keep track of the footnotes to include in footnotes.xml at docx build time.

Thanks for your feedback

git-noise commented 1 month ago

I have forked the repo and added initial support for footnotes with the following scope/limitations:

@bokuweb, please let me know if you have interest in merging a PR. Thank you

bokuweb commented 1 month ago

@git-noise Thanks for your great work. I'm interested in your change :)

git-noise commented 1 month ago

Great thanks! I'll work on a merge-able PR and send it your way for initial comment/review.