Class: Himg::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/himg/cli.rb

Defined Under Namespace

Classes: Document

Constant Summary collapse

CLI_ONLY_OPTIONS =
[:stdin, :http_headers].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/himg/cli.rb', line 93

def self.exit_on_failure?
  true
end

Instance Method Details

#screenshot(first_arg = nil, second_arg = nil) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/himg/cli.rb', line 53

def screenshot(first_arg = nil, second_arg = nil)
  url, destination = parse_screenshot_args(first_arg, second_arg)

  options[:http_headers]&.transform_values!(&:strip)

  Document.new(url, options).load do |content|
    render_options = options.transform_keys(&:to_sym)
      .reject { |k, _| CLI_ONLY_OPTIONS.include?(k) }
    render_options[:base_url] ||= base_directory_url(url) if Document.http_url?(url)

    png = Himg.render(content, **render_options)

    File.open(destination, "wb") { |f| f.write(png) }
  end
end

#usageObject



13
14
15
16
17
18
19
20
# File 'lib/himg/cli.rb', line 13

def usage
  puts "himg v#{VERSION}"
  puts
  puts "Converts HTML to PNG images using a lightweight, minimal renderer."
  puts "Ideal for generating OpenGraph images from purpose-built HTML."
  puts
  CLI.command_help(Thor::Base.shell.new, 'screenshot')
end