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
-
#allocator ⇒ Object
Central ID allocator — preserves IDs across build/save cycle.
-
#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).
-
#embeddings ⇒ Object
OLE/embedded object binaries (word/embeddings/*).
-
#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
#allocator ⇒ Object
Central ID allocator — preserves IDs across build/save cycle
120 121 122 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 120 def allocator @allocator end |
#app_properties ⇒ Uniword::Ooxml::AppProperties
Get app_properties (lazy initialization)
129 130 131 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 129 def app_properties @app_properties ||= Uniword::Ooxml::AppProperties.new end |
#bibliography_sources ⇒ Object
Bibliography sources for sources.xml
113 114 115 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 113 def bibliography_sources @bibliography_sources end |
#bookmarks ⇒ Hash{String => Object}
Get bookmarks from document paragraphs
254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 254 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 }
111 112 113 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 111 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
96 97 98 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 96 def comments @comments end |
#content_types ⇒ Object
Round-trip parts (copied from DocxPackage during load)
117 118 119 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 117 def content_types @content_types end |
#core_properties ⇒ Uniword::Ooxml::CoreProperties
Get core_properties (lazy initialization)
150 151 152 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 150 def core_properties @core_properties ||= Uniword::Ooxml::CoreProperties.new end |
#custom_properties ⇒ Object
Round-trip parts (copied from DocxPackage during load)
117 118 119 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 117 def custom_properties @custom_properties end |
#custom_xml_items ⇒ Object
Round-trip parts (copied from DocxPackage during load)
117 118 119 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 117 def custom_xml_items @custom_xml_items end |
#document_rels ⇒ Object
Round-trip parts (copied from DocxPackage during load)
117 118 119 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 117 def document_rels @document_rels end |
#embeddings ⇒ Object
OLE/embedded object binaries (word/embeddings/*)
115 116 117 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 115 def @embeddings end |
#endnotes ⇒ Object
Footnotes and endnotes (separate XML parts in DOCX package)
105 106 107 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 105 def endnotes @endnotes end |
#font_table ⇒ Object
Round-trip parts (copied from DocxPackage during load)
117 118 119 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 117 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.
99 100 101 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 99 def @footers end |
#footnotes ⇒ Object
Footnotes and endnotes (separate XML parts in DOCX package)
105 106 107 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 105 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]
103 104 105 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 103 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.
99 100 101 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 99 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 }
108 109 110 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 108 def image_parts @image_parts end |
#numbering_configuration ⇒ Object
Accessor for numbering_configuration (lazy init for builder API)
134 135 136 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 134 def numbering_configuration @numbering_configuration ||= NumberingConfiguration.new end |
#package_rels ⇒ Object
Round-trip parts (copied from DocxPackage during load)
117 118 119 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 117 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
96 97 98 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 96 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
96 97 98 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 96 def revisions @revisions end |
#settings ⇒ Object
Round-trip parts (copied from DocxPackage during load)
117 118 119 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 117 def settings @settings end |
#styles_configuration ⇒ Object
Lazy initialization for styles_configuration
160 161 162 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 160 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
96 97 98 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 96 def theme @theme end |
#theme_rels ⇒ Object
Round-trip parts (copied from DocxPackage during load)
117 118 119 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 117 def theme_rels @theme_rels end |
#web_settings ⇒ Object
Round-trip parts (copied from DocxPackage during load)
117 118 119 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 117 def web_settings @web_settings end |
Instance Method Details
#apply_styles_from(source_path, strategy: :keep_existing) ⇒ self
Apply styles from another document
346 347 348 349 350 351 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 346 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
311 312 313 314 315 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 311 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
358 359 360 361 362 363 364 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 358 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.
277 278 279 280 281 282 283 284 285 286 287 288 289 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 277 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
296 297 298 299 300 301 302 303 304 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 296 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
335 336 337 338 339 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 335 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).
327 328 329 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 327 def auto_transition_theme Resource::ThemeTransition.auto_transition!(self) end |
#document_stats ⇒ Hash
Returns Document statistics (paragraphs, tables, images).
220 221 222 223 224 225 226 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 220 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.
211 212 213 214 215 216 217 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 211 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
369 370 371 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 369 def inspect "#<#{self.class} @body=...>" end |
#numbering_configuration_loaded? ⇒ Boolean
Whether numbering_configuration was explicitly loaded from source (vs lazily created for builder API)
143 144 145 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 143 def numbering_configuration_loaded? !!@numbering_configuration end |
#paragraphs ⇒ Array<Paragraph>
Get all paragraphs (convenience accessor)
196 197 198 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 196 def paragraphs body&.paragraphs || [] end |
#save(path, format: :auto, profile: nil) ⇒ Object
Save document to file
171 172 173 174 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 171 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)
203 204 205 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 203 def tables body&.tables || [] end |
#text ⇒ String
Get all paragraph text
187 188 189 190 191 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 187 def text return "" unless body&.paragraphs body.paragraphs.map(&:text).join("\n") end |
#title ⇒ Object
Get document title (delegates to core_properties)
155 156 157 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 155 def title core_properties.title end |
#to_file(path, profile: nil) ⇒ Object
Save document to DOCX file using DocxPackage
180 181 182 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 180 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
376 377 378 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 376 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.
87 88 89 90 91 92 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 87 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.
233 234 235 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 233 def valid? Validation::StructuralValidator.new(self).valid? end |
#validation_errors ⇒ Array<String>
Get structural validation errors.
240 241 242 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 240 def validation_errors Validation::StructuralValidator.new(self).errors end |
#validation_warnings ⇒ Array<String>
Get structural validation warnings.
247 248 249 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 247 def validation_warnings Validation::StructuralValidator.new(self).warnings end |