Class: Tahweel::Writers::Docx
- Inherits:
-
Object
- Object
- Tahweel::Writers::Docx
- Defined in:
- lib/tahweel/writers/docx.rb
Overview
Writer class for outputting text to a .docx file.
Instance Method Summary collapse
-
#extension ⇒ String
Returns the file extension for this writer.
-
#write(texts, destination, options = {}) ⇒ void
Writes the extracted texts to a file.
Instance Method Details
#extension ⇒ String
Returns the file extension for this writer.
12 |
# File 'lib/tahweel/writers/docx.rb', line 12 def extension = "docx" |
#write(texts, destination, options = {}) ⇒ void
This method returns an undefined value.
Writes the extracted texts to a file.
It applies several transformations to the text before writing:
- Normalizes all line endings (
\r\n,\r) to\n. - Collapses consecutive identical whitespace characters.
- Compacts the text by merging short lines if the page is too long (> 40 lines).
- Determines text alignment (RTL/LTR) based on content.
- Converts
\nto proper OOXML line breaks for cross-platform compatibility.
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/tahweel/writers/docx.rb', line 27 def write(texts, destination, = {}) # rubocop:disable Lint/UnusedMethodArgument Caracal::Document.save(destination) do |docx| texts.each_with_index do |text, index| text = text.gsub(/\r\n?/, "\n").gsub(/(\s)\1+/, '\1').strip text = compact_shortest_lines(text) while expected_lines_in_page(text) > 40 write_paragraph(docx, text) docx.page if index < texts.size - 1 end end end |