Class: Cabriolet::CHM::CommandHandler

Inherits:
Cabriolet::Commands::BaseCommandHandler show all
Defined in:
lib/cabriolet/chm/command_handler.rb

Overview

Command handler for CHM (Compiled HTML Help) format

This handler implements the unified command interface for CHM files, wrapping the existing CHM::Decompressor and CHM::Compressor classes.

Instance Method Summary collapse

Methods inherited from Cabriolet::Commands::BaseCommandHandler

#initialize

Constructor Details

This class inherits a constructor from Cabriolet::Commands::BaseCommandHandler

Instance Method Details

#create(output, files = [], options = {}) ⇒ void

This method returns an undefined value.

Create a new CHM archive

Creates a CHM file from HTML source files.

Parameters:

  • output (String)

    Output CHM file path

  • files (Array<String>) (defaults to: [])

    List of input HTML files

  • options (Hash) (defaults to: {})

    Additional options

Options Hash (options):

  • :window_bits (Integer)

    LZX window size (15-21, default: 16)

Raises:



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cabriolet/chm/command_handler.rb', line 80

def create(output, files = [], options = {})
  raise ArgumentError, "No files specified" if files.empty?

  files.each do |f|
    raise ArgumentError, "File does not exist: #{f}" unless File.exist?(f)
  end

  window_bits = options[:window_bits] || 16

  compressor = Compressor.new
  files.each do |f|
    # Default to compressed section for .html, uncompressed for images
    section = f.end_with?(".html", ".htm") ? :compressed : :uncompressed
    compressor.add_file(f, "/#{File.basename(f)}", section: section)
  end

  puts "Creating #{output} with #{files.size} file(s) (window_bits: #{window_bits})" if verbose?
  bytes = compressor.generate(output, window_bits: window_bits)
  puts "Created #{output} (#{bytes} bytes, #{files.size} files)"
end

#extract(file, output_dir = nil, _options = {}) ⇒ void

This method returns an undefined value.

Extract files from CHM archive

Extracts all non-system files from the CHM file to the specified output directory.

Parameters:

  • file (String)

    Path to the CHM file

  • output_dir (String) (defaults to: nil)

    Output directory path (default: current directory)

  • options (Hash)

    Additional options (unused)



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/cabriolet/chm/command_handler.rb', line 44

def extract(file, output_dir = nil, _options = {})
  validate_file_exists(file)

  output_dir ||= "."
  output_dir = ensure_output_dir(output_dir)

  decompressor = Decompressor.new
  chm = decompressor.open(file)

  count = 0
  chm.all_files.each do |f|
    next if f.system_file?

    output_path = File.join(output_dir, f.filename)
    output_subdir = File.dirname(output_path)
    FileUtils.mkdir_p(output_subdir)

    puts "Extracting: #{f.filename}" if verbose?
    decompressor.extract(f, output_path)
    count += 1
  end

  decompressor.close
  puts "Extracted #{count} file(s) to #{output_dir}"
end

#info(file, _options = {}) ⇒ void

This method returns an undefined value.

Display detailed CHM file information

Shows comprehensive information about the CHM structure, including directory, sections, and files.

Parameters:

  • file (String)

    Path to the CHM file

  • options (Hash)

    Additional options (unused)



109
110
111
112
113
114
115
116
117
118
# File 'lib/cabriolet/chm/command_handler.rb', line 109

def info(file, _options = {})
  validate_file_exists(file)

  decompressor = Decompressor.new
  chm = decompressor.open(file)

  display_chm_info(chm)

  decompressor.close
end

#list(file, _options = {}) ⇒ void

This method returns an undefined value.

List CHM file contents

Displays information about the CHM file including version, language, and lists all contained files with their sizes.

Parameters:

  • file (String)

    Path to the CHM file

  • options (Hash)

    Additional options (unused)



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/cabriolet/chm/command_handler.rb', line 23

def list(file, _options = {})
  validate_file_exists(file)

  decompressor = Decompressor.new
  chm = decompressor.open(file)

  display_header(chm)
  display_files(chm.all_files)

  decompressor.close
end

#test(file, _options = {}) ⇒ void

This method returns an undefined value.

Test CHM file integrity

Verifies the CHM file structure.

Parameters:

  • file (String)

    Path to the CHM file

  • options (Hash)

    Additional options (unused)



127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cabriolet/chm/command_handler.rb', line 127

def test(file, _options = {})
  validate_file_exists(file)

  decompressor = Decompressor.new
  chm = decompressor.open(file)

  puts "Testing #{chm.filename}..."
  puts "OK: CHM file structure is valid (#{chm.all_files.size} files)"
  puts "Note: Full integrity validation not yet implemented"

  decompressor.close
end