Class: Uniword::ImagesCLI

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

Overview

Images subcommands for Uniword CLI.

Provides commands for listing, extracting, inserting, and removing images in DOCX documents.

Examples:

$ uniword images list document.docx
$ uniword images list document.docx --json
$ uniword images extract document.docx ./output_images
$ uniword images insert document.docx photo.png -o out.docx
$ uniword images remove document.docx image1.png -o out.docx

Instance Method Summary collapse

Methods included from CLIHelpers

included

Instance Method Details

#extract(path, output_dir) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/uniword/cli/images_cli.rb', line 80

def extract(path, output_dir)
  doc = load_document(path)
  manager = Images::ImageManager.new(doc)
  count = manager.extract(output_dir)

  if count.positive?
    say "Extracted #{count} image(s) to #{output_dir}", :green
  else
    say "No images to extract.", :yellow
  end
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e)
end

#insert(path, image_path) ⇒ Object



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

def insert(path, image_path)
  doc = load_document(path)

  unless File.exist?(image_path)
    say "Image not found: #{image_path}", :red
    exit 1
  end

  manager = Images::ImageManager.new(doc)
  insert_options = {}
  insert_options[:position] = options[:position] if options[:position]
  insert_options[:width] = options[:width] if options[:width]
  insert_options[:height] = options[:height] if options[:height]
  if options[:description]
    insert_options[:description] =
      options[:description]
  end

  r_id = manager.insert(image_path, **insert_options)
  doc.save(options[:output])

  say "Inserted image (#{r_id}) into #{options[:output]}", :green
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e)
end

#list(path) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/uniword/cli/images_cli.rb', line 32

def list(path)
  doc = load_document(path)
  manager = Images::ImageManager.new(doc)
  images = manager.list

  if images.empty?
    say "No images found.", :yellow
    return
  end

  if options[:json]
    data = images.map do |img|
      {
        name: img.name,
        path: img.path,
        content_type: img.content_type,
        size: img.size,
        width: img.width,
        height: img.height,
      }
    end
    puts JSON.pretty_generate(data)
  else
    say "Images (#{images.count}):", :green
    images.each_with_index do |img, idx|
      dims = img.width && img.height ? " #{img.width}x#{img.height}" : ""
      say "  #{idx + 1}. #{img.name}"
      say "     Type:  #{img.content_type}"
      say "     Size:  #{img.size} bytes#{dims}"
      say "     Path:  #{img.path}"
    end
  end
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e)
end

#remove(path, image_name) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/uniword/cli/images_cli.rb', line 149

def remove(path, image_name)
  doc = load_document(path)
  manager = Images::ImageManager.new(doc)

  unless manager.remove(image_name)
    say "Image '#{image_name}' not found.", :red
    exit 1
  end

  doc.save(options[:output])
  say "Removed '#{image_name}' from #{options[:output]}", :green
rescue Uniword::Error => e
  handle_error(e)
rescue StandardError => e
  handle_error(e)
end