Class: Uniword::Wordprocessingml::DocumentRoot
- Inherits:
-
Lutaml::Model::Serializable
- Object
- Lutaml::Model::Serializable
- Uniword::Wordprocessingml::DocumentRoot
- Includes:
- 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.
-
#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 body element_order 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)
123 124 125 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 123 def app_properties @app_properties ||= Uniword::Ooxml::AppProperties.new end |
#bibliography_sources ⇒ Object
Bibliography sources for sources.xml
111 112 113 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 111 def bibliography_sources @bibliography_sources end |
#bookmarks ⇒ Hash{String => Object}
Get bookmarks from document paragraphs
239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 239 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 }
109 110 111 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 109 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
94 95 96 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 94 def comments @comments end |
#content_types ⇒ Object
Round-trip parts (copied from DocxPackage during load)
113 114 115 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 113 def content_types @content_types end |
#core_properties ⇒ Uniword::Ooxml::CoreProperties
Get core_properties (lazy initialization)
144 145 146 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 144 def core_properties @core_properties ||= Uniword::Ooxml::CoreProperties.new end |
#custom_properties ⇒ Object
Round-trip parts (copied from DocxPackage during load)
113 114 115 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 113 def custom_properties @custom_properties end |
#custom_xml_items ⇒ Object
Round-trip parts (copied from DocxPackage during load)
113 114 115 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 113 def custom_xml_items @custom_xml_items end |
#document_rels ⇒ Object
Round-trip parts (copied from DocxPackage during load)
113 114 115 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 113 def document_rels @document_rels end |
#endnotes ⇒ Object
Footnotes and endnotes (separate XML parts in DOCX package)
103 104 105 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 103 def endnotes @endnotes end |
#font_table ⇒ Object
Round-trip parts (copied from DocxPackage during load)
113 114 115 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 113 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.
97 98 99 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 97 def @footers end |
#footnotes ⇒ Object
Footnotes and endnotes (separate XML parts in DOCX package)
103 104 105 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 103 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]
101 102 103 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 101 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.
97 98 99 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 97 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 }
106 107 108 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 106 def image_parts @image_parts end |
#numbering_configuration ⇒ Object
Accessor for numbering_configuration (lazy init for builder API)
128 129 130 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 128 def numbering_configuration @numbering_configuration ||= NumberingConfiguration.new end |
#package_rels ⇒ Object
Round-trip parts (copied from DocxPackage during load)
113 114 115 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 113 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
94 95 96 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 94 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
94 95 96 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 94 def revisions @revisions end |
#settings ⇒ Object
Round-trip parts (copied from DocxPackage during load)
113 114 115 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 113 def settings @settings end |
#styles_configuration ⇒ Object
Lazy initialization for styles_configuration
154 155 156 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 154 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
94 95 96 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 94 def theme @theme end |
#theme_rels ⇒ Object
Round-trip parts (copied from DocxPackage during load)
113 114 115 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 113 def theme_rels @theme_rels end |
#web_settings ⇒ Object
Round-trip parts (copied from DocxPackage during load)
113 114 115 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 113 def web_settings @web_settings end |
Instance Method Details
#apply_styles_from(source_path, strategy: :keep_existing) ⇒ self
Apply styles from another document
331 332 333 334 335 336 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 331 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
296 297 298 299 300 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 296 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
343 344 345 346 347 348 349 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 343 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.
262 263 264 265 266 267 268 269 270 271 272 273 274 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 262 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
281 282 283 284 285 286 287 288 289 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 281 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
320 321 322 323 324 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 320 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).
312 313 314 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 312 def auto_transition_theme Resource::ThemeTransition.auto_transition!(self) end |
#images ⇒ Array<Drawing>
Get all drawings (image references) from the document. Walks all paragraphs and collects Drawing elements from runs.
205 206 207 208 209 210 211 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 205 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
354 355 356 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 354 def inspect "#<#{self.class} @body=...>" end |
#numbering_configuration_loaded? ⇒ Boolean
Whether numbering_configuration was explicitly loaded from source (vs lazily created for builder API)
137 138 139 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 137 def numbering_configuration_loaded? !!@numbering_configuration end |
#paragraphs ⇒ Array<Paragraph>
Get all paragraphs (convenience accessor)
190 191 192 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 190 def paragraphs body&.paragraphs || [] end |
#save(path, format: :auto, profile: nil) ⇒ Object
Save document to file
165 166 167 168 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 165 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)
197 198 199 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 197 def tables body&.tables || [] end |
#text ⇒ String
Get all paragraph text
181 182 183 184 185 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 181 def text return "" unless body&.paragraphs body.paragraphs.map(&:text).join("\n") end |
#title ⇒ Object
Get document title (delegates to core_properties)
149 150 151 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 149 def title core_properties.title end |
#to_file(path, profile: nil) ⇒ Object
Save document to DOCX file using DocxPackage
174 175 176 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 174 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
361 362 363 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 361 def to_html_document Uniword::Transformation::OoxmlToHtmlConverter.document_to_html(self) end |
#to_xml(options = {}) ⇒ Object
Override to_xml to sync body element_order before serialization. lutaml-model’s compiled serializer may bypass Body#to_xml when serializing Body as a child element, so we sync here at the DocumentRoot level to ensure programmatically added paragraphs and tables are included.
87 88 89 90 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 87 def to_xml( = {}) body&.sync_element_order_for_serialization 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.
218 219 220 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 218 def valid? Validation::StructuralValidator.new(self).valid? end |
#validation_errors ⇒ Array<String>
Get structural validation errors.
225 226 227 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 225 def validation_errors Validation::StructuralValidator.new(self).errors end |
#validation_warnings ⇒ Array<String>
Get structural validation warnings.
232 233 234 |
# File 'lib/uniword/wordprocessingml/document_root.rb', line 232 def validation_warnings Validation::StructuralValidator.new(self).warnings end |