Class: Uniword::CLI

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

Overview

Command-line interface for Uniword.

Provides commands for document conversion, inspection, validation, building, checking, batch processing, metadata, and verification.

Subcommands:

  • theme → ThemeCLI

  • styleset → StyleSetCLI

  • resources → ResourcesCLI

Instance Method Summary collapse

Methods included from CLIHelpers

included

Instance Method Details

#batch(pattern, output_dir) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'lib/uniword/cli/main.rb', line 239

def batch(pattern, output_dir)
  require "fileutils"
  FileUtils.mkdir_p(output_dir)

  files = Dir.glob(pattern)
  if files.empty?
    say("No files found matching '#{pattern}'", :yellow)
    return
  end

  say("Processing #{files.count} files...", :green) if options[:verbose]

  success = 0
  errors = []

  files.each do |input_path|
    basename = File.basename(input_path, ".*")
    ext = options[:format] == "mhtml" ? ".mhtml" : ".docx"
    output_path = File.join(output_dir, "#{basename}#{ext}")

    begin
      doc = DocumentFactory.from_file(input_path)
      doc.save(output_path, format: options[:format].to_sym)
      say("  #{File.basename(input_path)} -> #{File.basename(output_path)}") if options[:verbose]
      success += 1
    rescue StandardError => e
      errors << "#{File.basename(input_path)}: #{e.message}"
      if options[:verbose]
        say("  Error: #{File.basename(input_path)}: #{e.message}",
            :red)
      end
    end
  end

  say("\nBatch complete: #{success} converted, #{errors.count} errors",
      :green)
  return if errors.empty?

  say("Errors:", :red)
  errors.each { |e| say("  - #{e}", :red) }
rescue StandardError => e
  say "Error: #{e.message}", :red
  exit 1
end

#build(template_path, output_path) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/uniword/cli/main.rb', line 156

def build(template_path, output_path)
  unless File.exist?(template_path)
    say("Template not found: #{template_path}", :red)
    exit 1
  end

  template = Uniword::Template::Template.load(template_path)

  if options[:verbose]
    say("Loaded template: #{template_path}", :cyan)
    say("  Markers found: #{template.markers.count}")
  end

  data = load_build_data
  document = template.render(data)
  document.save(output_path)

  say("Built document: #{output_path}", :green)
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e, verbose: options[:verbose])
end

#check(path) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/uniword/cli/main.rb', line 194

def check(path)
  doc = load_document(path)
  check_type = options[:type]

  reports = {}

  if %w[all quality].include?(check_type)
    checker = Quality::DocumentChecker.new
    reports[:quality] = checker.check(doc)
  end

  if %w[all accessibility].include?(check_type)
    checker = Accessibility::AccessibilityChecker.new
    reports[:accessibility] = checker.check(doc)
  end

  if options[:json]
    require "json"
    output = reports.transform_values do |r|
      { valid: r.valid?,
        issues: r.respond_to?(:issues) ? r.issues.count : 0 }
    end
    puts JSON.pretty_generate(output)
  else
    display_check_reports(reports)
  end
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e)
end

#convert(input_path, output_path) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/uniword/cli/main.rb', line 49

def convert(input_path, output_path)
  from_format = options[:from]&.to_sym || :auto
  to_format = options[:to]&.to_sym || :auto

  if options[:verbose]
    say "Converting #{input_path} to #{output_path}...",
        :green
  end

  doc = DocumentFactory.from_file(input_path, format: from_format)

  if options[:verbose]
    say "  Loaded document:", :cyan
    say "    Paragraphs: #{doc.paragraphs.count}"
    say "    Tables: #{doc.tables.count}"
    say "    Styles: #{doc.styles_configuration&.styles&.count || 0}"
  end

  doc.save(output_path, format: to_format)
  say "Conversion complete!", :green if options[:verbose]
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e, verbose: options[:verbose])
end

#info(path) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/uniword/cli/main.rb', line 85

def info(path)
  say "Analyzing #{path}...", :green

  detector = ::Uniword::FormatDetector.new
  format = detector.detect(path)
  say "\nFormat: #{format.to_s.upcase}", :cyan

  doc = load_document(path)

  say "\nDocument Statistics:", :cyan
  say "  Paragraphs: #{doc.paragraphs.count}"
  say "  Tables: #{doc.tables.count}"
  say "  Text length: #{doc.text.length} characters"
  say "  Styles: #{doc.styles_configuration.styles.count}" if doc.styles_configuration&.styles&.any?

  display_verbose_info(doc) if options[:verbose]
  say "\nAnalysis complete!", :green
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e)
end

#metadata(path) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/uniword/cli/main.rb', line 301

def (path)
  doc = load_document(path)
  cp = doc.core_properties

  updated = (cp)

  if updated
    output_path = options[:output]
    unless output_path
      say("Error: --output is required when setting metadata", :red)
      exit 1
    end
    doc.save(output_path)
    say("Metadata updated and saved to #{output_path}", :green)
    return
  end

  (cp)
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e)
end

#validate(path) ⇒ Object



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
# File 'lib/uniword/cli/main.rb', line 118

def validate(path)
  say "Validating #{path}...", :green

  doc = load_document(path)
  say "File format is valid", :green

  if doc.valid?
    say "Document structure is valid", :green
  else
    say "Document has structural issues", :yellow
  end

  if doc.paragraphs.any? || doc.tables.any?
    say "Document contains content", :green
  else
    say "Document appears to be empty", :yellow
  end

  display_detailed_validation(doc) if options[:verbose]
  say "\nValidation complete!", :green
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e)
end

#verify(path) ⇒ Object



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
# File 'lib/uniword/cli/main.rb', line 344

def verify(path)
  unless File.exist?(path)
    say(Rainbow("  File not found: #{path}").red.bright)
    exit 1
  end

  orchestrator = Uniword::Validation::VerifyOrchestrator.new(
    xsd_validation: options[:xsd],
  )
  report = orchestrator.verify(path)

  if options[:json]
    puts report.to_json
  elsif options[:yaml]
    require "yaml"
    puts YAML.dump(JSON.parse(report.to_json))
  else
    formatter = Uniword::Validation::Report::TerminalFormatter.new
    puts formatter.format(report, verbose: options[:verbose])
  end

  exit report.valid ? 0 : 1
rescue StandardError => e
  say(Rainbow("  Error: #{e.message}").red.bright)
  exit 1
end

#versionObject



372
373
374
# File 'lib/uniword/cli/main.rb', line 372

def version
  say "Uniword version #{Uniword::VERSION}", :green
end