Class: Uniword::Stylesets::StyleSetImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/uniword/stylesets/styleset_importer.rb

Overview

Imports .dotx StyleSet files and converts them to YAML format

Leverages lutaml-model’s from_xml and to_yaml serialization for simple, reliable conversion from .dotx to YAML format.

Examples:

Import single StyleSet

importer = StyleSetImporter.new
importer.import('Distinctive.dotx', 'data/stylesets/distinctive.yml')

Import all StyleSets from directory

importer = StyleSetImporter.new
importer.import_all('stylesets/', 'data/stylesets/')

Instance Method Summary collapse

Instance Method Details

#import(dotx_path, output_path) ⇒ void

This method returns an undefined value.

Import .dotx file to YAML

Parameters:

  • dotx_path (String)

    Path to .dotx file

  • output_path (String)

    Output YAML path



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/uniword/stylesets/styleset_importer.rb', line 25

def import(dotx_path, output_path)
  # Extract .dotx package
  reader = StyleSetPackageReader.new
  files = reader.extract(dotx_path)

  # Parse styles.xml using lutaml-model
  styles_config = ::Uniword::Wordprocessingml::StylesConfiguration.from_xml(
    files[:styles],
    encoding: "UTF-8",
  )

  # Create StyleSet model
  styleset = ::Uniword::StyleSet.new(
    name: extract_name_from_path(dotx_path),
    styles: styles_config.styles,
    source_file: File.basename(dotx_path),
  )

  # Ensure output directory exists
  FileUtils.mkdir_p(File.dirname(output_path))

  # Serialize to YAML using lutaml-model
  File.write(output_path, styleset.to_yaml)
end

#import_all(source_dir, output_dir) ⇒ Integer

Import all StyleSets from directory

Parameters:

  • source_dir (String)

    Directory with .dotx files

  • output_dir (String)

    Output directory for YAML files

Returns:

  • (Integer)

    Number of StyleSets imported



55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/uniword/stylesets/styleset_importer.rb', line 55

def import_all(source_dir, output_dir)
  count = 0

  Dir.glob(File.join(source_dir, "*.dotx")).each do |dotx_file|
    styleset_name = styleset_name_from_file(dotx_file)
    output_file = File.join(output_dir, "#{styleset_name}.yml")

    puts "Importing #{File.basename(dotx_file)} -> #{File.basename(output_file)}"
    import(dotx_file, output_file)
    count += 1
  end

  count
end