Class: Uniword::StyleSetCLI

Inherits:
Thor
  • Object
show all
Includes:
CLIHelpers
Defined in:
lib/uniword/cli/styleset_cli.rb

Overview

StyleSet subcommands for Uniword CLI.

Manages bundled and imported StyleSets -- collections of paragraph, character, and table styles that provide consistent formatting.

Instance Method Summary collapse

Methods included from CLIHelpers

included

Instance Method Details

#apply(input_path, output_path) ⇒ Object



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/uniword/cli/styleset_cli.rb', line 110

def apply(input_path, output_path)
  unless options[:name] || options[:file]
    say "Error: Must specify either --name for bundled StyleSet or --file for .dotx file",
        :red
    exit 1
  end

  if options[:name] && options[:file]
    say "Error: Cannot specify both --name and --file", :red
    exit 1
  end

  say "Loading document #{input_path}...", :green if options[:verbose]

  doc = load_document(input_path)

  if options[:verbose]
    say "  Loaded document:", :cyan
    say "    Paragraphs: #{doc.paragraphs.count}"
    say "    Current styles: #{doc.styles.count}"
  end

  apply_styleset_to(doc)

  if options[:verbose]
    say "  Applied StyleSet:", :cyan
    say "    Total styles: #{doc.styles.count}"
    say "    Strategy: #{options[:strategy]}"
  end

  doc.save(output_path)
  say "StyleSet applied successfully to #{output_path}", :green
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e, verbose: options[:verbose])
end

#extract(docx_path) ⇒ Object



151
152
153
154
155
156
157
158
159
160
# File 'lib/uniword/cli/styleset_cli.rb', line 151

def extract(docx_path)
  output = options[:output] ||
    "data/stylesets/#{options[:name]}.yml"
  Generation::StyleExtractor.extract_to_yaml(docx_path, output)
  say("StyleSet extracted: #{output}", :green)
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e)
end

#importObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/uniword/cli/styleset_cli.rb', line 66

def import
  if options[:verbose]
    say "Importing StyleSet from #{options[:source]}...",
        :green
  end

  importer = Stylesets::StyleSetImporter.new
  count = importer.import_all(options[:source], options[:output])

  say "Successfully imported #{count} StyleSets to #{options[:output]}/",
      :green

  if options[:verbose]
    stylesets = Dir.glob(File.join(options[:output], "*.yml")).map do |f|
      File.basename(f, ".yml")
    end.sort
    say "\nAvailable StyleSets:", :cyan
    stylesets.each { |name| say "  - #{name}" }
  end
rescue StandardError => e
  say "Error importing StyleSet: #{e.message}", :red
  exit 1
end

#listObject



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

def list
  stylesets = StyleSet.available_stylesets

  if stylesets.empty?
    say "No bundled StyleSets found.", :yellow
    say "Run 'uniword styleset import' to import .dotx StyleSets.", :yellow
    return
  end

  say "Available StyleSets (#{stylesets.count}):", :green
  stylesets.each do |styleset_name|
    if options[:verbose]
      begin
        styleset = StyleSet.load(styleset_name)
        say "  #{styleset_name}:", :cyan
        say "    Name: #{styleset.name}"
        say "    Styles: #{styleset.styles.count}"
        say "    Paragraph styles: #{styleset.paragraph_styles.count}"
        say "    Character styles: #{styleset.character_styles.count}"
        say "    Table styles: #{styleset.table_styles.count}"
      rescue StandardError => e
        say "  #{styleset_name}: Error loading - #{e.message}", :red
      end
    else
      say "  - #{styleset_name}"
    end
  end
end