Class: Uniword::Wordprocessingml::DocumentRoot
- Inherits:
-
Lutaml::Model::Serializable
- Object
- Lutaml::Model::Serializable
- Uniword::Wordprocessingml::DocumentRoot
- Includes:
- DocumentInput, FeatureFacade
- Defined in:
- lib/uniword/wordprocessingml/document_root.rb,
lib/uniword/wordprocessingml/document_root/feature_facade.rb
Overview
Root element of a WordprocessingML document
Generated from OOXML schema: wordprocessingml.yml Element: <w:document>
Defined Under Namespace
Modules: FeatureFacade
Instance Attribute Summary collapse
-
#app_properties ⇒ Uniword::Ooxml::AppProperties
Get app_properties (lazy initialization).
-
#bibliography_sources ⇒ Object
Bibliography sources for sources.xml.
-
#bookmarks ⇒ Hash{String => Object}
Get bookmarks from document paragraphs.
-
#chart_parts ⇒ Object
Chart parts to embed in the DOCX package Hash: r_id => { xml: String, target: String }.
-
#comments ⇒ Object
Additional attributes for DOCX metadata (not part of document.xml) These are stored in separate files within the DOCX package.
-
#content_types ⇒ Object
Round-trip parts (copied from DocxPackage during load).
-
#core_properties ⇒ Uniword::Ooxml::CoreProperties
Get core_properties (lazy initialization).
-
#custom_properties ⇒ Object
Round-trip parts (copied from DocxPackage during load).
-
#custom_xml_items ⇒ Object
Round-trip parts (copied from DocxPackage during load).
-
#document_rels ⇒ Object
Round-trip parts (copied from DocxPackage during load).
-
#endnotes ⇒ Object
Footnotes and endnotes (separate XML parts in DOCX package).
-
#font_table ⇒ Object
Round-trip parts (copied from DocxPackage during load).
-
#footers ⇒ Object
Headers and footers (stored as hash: type => Header/Footer) Single-section only.
-
#footnotes ⇒ Object
Footnotes and endnotes (separate XML parts in DOCX package).
-
#header_footer_parts ⇒ Object
Multi-section headers/footers (ordered array): [“rIdH1”, target: “header1.xml”, rel_type: “…”, content_type: “…”, content: Header|Footer].
-
#headers ⇒ Object
Headers and footers (stored as hash: type => Header/Footer) Single-section only.
-
#image_parts ⇒ Object
Image parts to embed in the DOCX package Hash: r_id => { path: String, data: String, content_type: String, target: String }.
-
#numbering_configuration ⇒ Object
Accessor for numbering_configuration (lazy init for builder API).
-
#package_rels ⇒ Object
Round-trip parts (copied from DocxPackage during load).
-
#raw_html ⇒ Object
Additional attributes for DOCX metadata (not part of document.xml) These are stored in separate files within the DOCX package.
-
#revisions ⇒ Object
Additional attributes for DOCX metadata (not part of document.xml) These are stored in separate files within the DOCX package.
-
#settings ⇒ Object
Round-trip parts (copied from DocxPackage during load).
-
#styles_configuration ⇒ Object
Lazy initialization for styles_configuration.
-
#theme ⇒ Object
Additional attributes for DOCX metadata (not part of document.xml) These are stored in separate files within the DOCX package.
-
#theme_rels ⇒ Object
Round-trip parts (copied from DocxPackage during load).
-
#web_settings ⇒ Object
Round-trip parts (copied from DocxPackage during load).
Instance Method Summary collapse
-
#apply_styles_from(source_path, strategy: :keep_existing) ⇒ self
Apply styles from another document.
-
#apply_styleset(name, strategy: :keep_existing) ⇒ self
Apply StyleSet to document.
-
#apply_template(template_path, strategy: :keep_existing) ⇒ self
Apply both theme and styles from a template document.
-
#apply_theme(name, **options) ⇒ self
Apply theme to document.
-
#apply_theme_file(path, variant: nil) ⇒ self
Apply theme from .thmx file.
-
#apply_theme_from(source_path) ⇒ self
Apply theme from another document.
-
#auto_transition_theme ⇒ Hash?
Auto-transition from MS theme to Uniword equivalent.
-
#document_stats ⇒ Hash
Document statistics (paragraphs, tables, images).
-
#images ⇒ Array<Drawing>
Get all drawings (image references) from the document.
-
#inspect ⇒ String
Custom inspect for readable output.
-
#numbering_configuration_loaded? ⇒ Boolean
Whether numbering_configuration was explicitly loaded from source (vs lazily created for builder API).
-
#paragraphs ⇒ Array<Paragraph>
Get all paragraphs (convenience accessor).
-
#save(path, format: :auto, profile: nil) ⇒ Object
Save document to file.
-
#tables ⇒ Array<Table>
Get all tables (convenience accessor).
-
#text ⇒ String
Get all paragraph text.
-
#title ⇒ Object
Get document title (delegates to core_properties).
-
#to_file(path, profile: nil) ⇒ Object
Save document to DOCX file using DocxPackage.
-
#to_html_document ⇒ String
Convert OOXML document to HTML document.
-
#to_xml(options = {}) ⇒ Object
Override to_xml to sync element_order on child collections before serialization.
-
#valid? ⇒ Boolean
Check if document structure is valid.
-
#validation_errors ⇒ Array<String>
Get structural validation errors.
-
#validation_warnings ⇒ Array<String>
Get structural validation warnings.
Methods included from FeatureFacade
#accept_all_changes, #add_comment, #add_footer, #add_header, #add_watermark, #clear_comments, #diff, #extract_images, #generate_toc, #insert_image, #insert_toc, #list_comments, #list_footers, #list_headers, #list_images, #list_watermarks, #protect, #protection_active?, #protection_info, #reject_all_changes, #remove_footers, #remove_headers, #remove_image, #remove_watermark, #spellcheck, #unprotect, #update_toc, #watermark?
Instance Attribute Details
#app_properties ⇒ Uniword::Ooxml::AppProperties
Get app_properties (lazy initialization)
126 127 128 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 126 def app_properties @app_properties ||= Uniword::Ooxml::AppProperties.new end |
#bibliography_sources ⇒ Object
Bibliography sources for sources.xml
114 115 116 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 114 def bibliography_sources @bibliography_sources end |
#bookmarks ⇒ Hash{String => Object}
Get bookmarks from document paragraphs
251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 251 def bookmarks result = {} return result unless body&.paragraphs body.paragraphs.each do |para| para.bookmark_starts&.each do |bs| result[bs.name.to_s] = bs if bs.name end end result end |
#chart_parts ⇒ Object
Chart parts to embed in the DOCX package Hash: r_id => { xml: String, target: String }
112 113 114 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 112 def chart_parts @chart_parts end |
#comments ⇒ Object
Additional attributes for DOCX metadata (not part of document.xml) These are stored in separate files within the DOCX package
97 98 99 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 97 def comments @comments end |
#content_types ⇒ Object
Round-trip parts (copied from DocxPackage during load)
116 117 118 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 116 def content_types @content_types end |
#core_properties ⇒ Uniword::Ooxml::CoreProperties
Get core_properties (lazy initialization)
147 148 149 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 147 def core_properties @core_properties ||= Uniword::Ooxml::CoreProperties.new end |
#custom_properties ⇒ Object
Round-trip parts (copied from DocxPackage during load)
116 117 118 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 116 def custom_properties @custom_properties end |
#custom_xml_items ⇒ Object
Round-trip parts (copied from DocxPackage during load)
116 117 118 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 116 def custom_xml_items @custom_xml_items end |
#document_rels ⇒ Object
Round-trip parts (copied from DocxPackage during load)
116 117 118 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 116 def document_rels @document_rels end |
#endnotes ⇒ Object
Footnotes and endnotes (separate XML parts in DOCX package)
106 107 108 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 106 def endnotes @endnotes end |
#font_table ⇒ Object
Round-trip parts (copied from DocxPackage during load)
116 117 118 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 116 def font_table @font_table end |
#footers ⇒ Object
Headers and footers (stored as hash: type => Header/Footer) Single-section only. For multi-section, use header_footer_parts.
100 101 102 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 100 def @footers end |
#footnotes ⇒ Object
Footnotes and endnotes (separate XML parts in DOCX package)
106 107 108 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 106 def footnotes @footnotes end |
#header_footer_parts ⇒ Object
Multi-section headers/footers (ordered array): [“rIdH1”, target: “header1.xml”, rel_type: “…”,
content_type: "...", content: Header|Footer]
104 105 106 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 104 def @header_footer_parts end |
#headers ⇒ Object
Headers and footers (stored as hash: type => Header/Footer) Single-section only. For multi-section, use header_footer_parts.
100 101 102 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 100 def headers @headers end |
#image_parts ⇒ Object
Image parts to embed in the DOCX package Hash: r_id => { path: String, data: String, content_type: String, target: String }
109 110 111 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 109 def image_parts @image_parts end |
#numbering_configuration ⇒ Object
Accessor for numbering_configuration (lazy init for builder API)
131 132 133 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 131 def numbering_configuration @numbering_configuration ||= NumberingConfiguration.new end |
#package_rels ⇒ Object
Round-trip parts (copied from DocxPackage during load)
116 117 118 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 116 def package_rels @package_rels end |
#raw_html ⇒ Object
Additional attributes for DOCX metadata (not part of document.xml) These are stored in separate files within the DOCX package
97 98 99 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 97 def raw_html @raw_html end |
#revisions ⇒ Object
Additional attributes for DOCX metadata (not part of document.xml) These are stored in separate files within the DOCX package
97 98 99 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 97 def revisions @revisions end |
#settings ⇒ Object
Round-trip parts (copied from DocxPackage during load)
116 117 118 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 116 def settings @settings end |
#styles_configuration ⇒ Object
Lazy initialization for styles_configuration
157 158 159 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 157 def styles_configuration @styles_configuration ||= StylesConfiguration.new end |
#theme ⇒ Object
Additional attributes for DOCX metadata (not part of document.xml) These are stored in separate files within the DOCX package
97 98 99 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 97 def theme @theme end |
#theme_rels ⇒ Object
Round-trip parts (copied from DocxPackage during load)
116 117 118 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 116 def theme_rels @theme_rels end |
#web_settings ⇒ Object
Round-trip parts (copied from DocxPackage during load)
116 117 118 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 116 def web_settings @web_settings end |
Instance Method Details
#apply_styles_from(source_path, strategy: :keep_existing) ⇒ self
Apply styles from another document
343 344 345 346 347 348 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 343 def apply_styles_from(source_path, strategy: :keep_existing) source_doc = Uniword.load(source_path) styles_configuration.merge(source_doc.styles_configuration, conflict_resolution: strategy) self end |
#apply_styleset(name, strategy: :keep_existing) ⇒ self
Apply StyleSet to document
308 309 310 311 312 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 308 def apply_styleset(name, strategy: :keep_existing) styleset = Uniword::Stylesets::YamlStyleSetLoader.load_bundled(name.to_s) styleset.apply_to(self, strategy: strategy) self end |
#apply_template(template_path, strategy: :keep_existing) ⇒ self
Apply both theme and styles from a template document
355 356 357 358 359 360 361 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 355 def apply_template(template_path, strategy: :keep_existing) template_doc = Uniword.load(template_path) self.theme = template_doc.theme.dup if template_doc.theme styles_configuration.merge(template_doc.styles_configuration, conflict_resolution: strategy) self end |
#apply_theme(name, **options) ⇒ self
Apply theme to document
Applies a Uniword theme by name, updating doc defaults and built-in heading/hyperlink styles to reference the theme.
274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 274 def apply_theme(name, **) friendly = Themes::Theme.load(name.to_s) [:colors]&.each do |key, value| friendly.color_scheme[key.to_s] = value end friendly.font_scheme.major_font = [:major_font] if [:major_font] friendly.font_scheme.minor_font = [:minor_font] if [:minor_font] word_theme = Themes::ThemeTransformation.new.to_word(friendly) Themes::ThemeApplicator.new.apply(word_theme, self) self end |
#apply_theme_file(path, variant: nil) ⇒ self
Apply theme from .thmx file
293 294 295 296 297 298 299 300 301 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 293 def apply_theme_file(path, variant: nil) loader = Themes::ThemeLoader.new self.theme = if variant loader.load_with_variant(path, variant) else loader.load(path) end self end |
#apply_theme_from(source_path) ⇒ self
Apply theme from another document
332 333 334 335 336 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 332 def apply_theme_from(source_path) source_doc = Uniword.load(source_path) self.theme = source_doc.theme.dup if source_doc.theme self end |
#auto_transition_theme ⇒ Hash?
Auto-transition from MS theme to Uniword equivalent
Detects the MS theme in the document’s embedded theme and replaces it with the corresponding Uniword theme (font-substituted, renamed).
324 325 326 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 324 def auto_transition_theme Resource::ThemeTransition.auto_transition!(self) end |
#document_stats ⇒ Hash
Returns Document statistics (paragraphs, tables, images).
217 218 219 220 221 222 223 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 217 def document_stats { paragraphs: paragraphs.count, tables: tables.count, images: images.count, } end |
#images ⇒ Array<Drawing>
Get all drawings (image references) from the document. Walks all paragraphs and collects Drawing elements from runs.
208 209 210 211 212 213 214 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 208 def images return [] unless body&.paragraphs body.paragraphs.flat_map do |para| (para.runs || []).flat_map(&:drawings) end.compact end |
#inspect ⇒ String
Custom inspect for readable output
366 367 368 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 366 def inspect "#<#{self.class} @body=...>" end |
#numbering_configuration_loaded? ⇒ Boolean
Whether numbering_configuration was explicitly loaded from source (vs lazily created for builder API)
140 141 142 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 140 def numbering_configuration_loaded? !!@numbering_configuration end |
#paragraphs ⇒ Array<Paragraph>
Get all paragraphs (convenience accessor)
193 194 195 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 193 def paragraphs body&.paragraphs || [] end |
#save(path, format: :auto, profile: nil) ⇒ Object
Save document to file
168 169 170 171 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 168 def save(path, format: :auto, profile: nil) writer = DocumentWriter.new(self) writer.save(path, format: format, profile: profile) end |
#tables ⇒ Array<Table>
Get all tables (convenience accessor)
200 201 202 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 200 def tables body&.tables || [] end |
#text ⇒ String
Get all paragraph text
184 185 186 187 188 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 184 def text return "" unless body&.paragraphs body.paragraphs.map(&:text).join("\n") end |
#title ⇒ Object
Get document title (delegates to core_properties)
152 153 154 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 152 def title core_properties.title end |
#to_file(path, profile: nil) ⇒ Object
Save document to DOCX file using DocxPackage
177 178 179 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 177 def to_file(path, profile: nil) Docx::Package.to_file(self, path, profile: profile) end |
#to_html_document ⇒ String
Convert OOXML document to HTML document
373 374 375 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 373 def to_html_document Uniword::Transformation::OoxmlToHtmlConverter.document_to_html(self) end |
#to_xml(options = {}) ⇒ Object
Override to_xml to sync element_order on child collections before serialization. lutaml-model’s compiled serializer may bypass child #to_xml when serializing nested elements, so we sync here at the DocumentRoot level.
88 89 90 91 92 93 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 88 def to_xml( = {}) body&.sync_element_order_for_serialization footnotes&.sync_element_order if footnotes endnotes&.sync_element_order if endnotes super end |
#valid? ⇒ Boolean
Check if document structure is valid. Runs structural checks via Validation::StructuralValidator. Use the verify CLI command for full OPC + XSD + semantic validation.
230 231 232 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 230 def valid? Validation::StructuralValidator.new(self).valid? end |
#validation_errors ⇒ Array<String>
Get structural validation errors.
237 238 239 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 237 def validation_errors Validation::StructuralValidator.new(self).errors end |
#validation_warnings ⇒ Array<String>
Get structural validation warnings.
244 245 246 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 244 def validation_warnings Validation::StructuralValidator.new(self).warnings end |