Class: Uniword::Generation::StyleExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/generation/style_extractor.rb

Overview

Extracts styles from DOCX files into serializable format.

Reads styles from an existing DOCX via DocumentFactory and produces a plain hash representation suitable for YAML serialization.

Examples:

Extract styles to hash

styles = StyleExtractor.extract_from_docx("template.docx")

Extract styles to YAML file

StyleExtractor.extract_to_yaml("template.docx", "output.yml")

Class Method Summary collapse

Class Method Details

.extract_from_docx(docx_path) ⇒ Hash

Extract styles from a DOCX file as a hash.

Parameters:

  • docx_path (String)

    Path to source DOCX file

Returns:

  • (Hash)

    Hash with :name and :styles keys

Raises:

  • (ArgumentError)

    if file does not exist



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/uniword/generation/style_extractor.rb', line 23

def self.extract_from_docx(docx_path)
  validate_path(docx_path)

  doc = DocumentFactory.from_file(docx_path)
  config = doc.styles_configuration

  styles_data = config.styles.map do |style|
    style_to_hash(style)
  end

  {
    name: extract_name(docx_path),
    source_file: File.basename(docx_path),
    styles: styles_data,
  }
end

.extract_to_yaml(docx_path, output_path) ⇒ void

This method returns an undefined value.

Extract styles from a DOCX file and write to YAML.

Parameters:

  • docx_path (String)

    Path to source DOCX file

  • output_path (String)

    Path for output YAML file



45
46
47
48
49
50
51
52
# File 'lib/uniword/generation/style_extractor.rb', line 45

def self.extract_to_yaml(docx_path, output_path)
  data = extract_from_docx(docx_path)

  dir = File.dirname(output_path)
  FileUtils.mkdir_p(dir)

  File.write(output_path, YAML.dump(stringify_keys(data)))
end