Class: Cabriolet::LIT::CommandHandler

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

Overview

Command handler for LIT (Microsoft Reader eBook) format

This handler implements the unified command interface for LIT files, wrapping the existing LIT::Decompressor and LIT::Compressor classes. LIT files use LZX compression and may include DRM protection.

Instance Method Summary collapse

Methods inherited from 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 LIT archive

Creates a LIT file from HTML source files. Non-encrypted LIT files are created (DRM not supported).

Parameters:

  • output (String)

    Output LIT file path

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

    List of input files to add

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

    Additional options

Options Hash (options):

  • :language_id (Integer)

    Language ID (default: 0x409 English)

  • :version (Integer)

    LIT format version (default: 1)

Raises:



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/cabriolet/lit/command_handler.rb', line 76

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

  language_id = options[:language_id] || 0x409
  version = options[:version] || 1

  compressor = Compressor.new
  files.each do |f|
    # Default to adding with compression
    lit_path = "/#{File.basename(f)}"
    compressor.add_file(f, lit_path, compress: true)
  end

  puts "Creating #{output} with #{files.size} file(s) (v#{version}, lang: 0x#{Integer(language_id).to_s(16)})" if verbose?
  bytes = compressor.generate(output, version: version,
                                      language_id: language_id)
  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 LIT archive

Extracts all files from the LIT file to the specified output directory. Uses manifest for filename restoration if available.

Parameters:

  • file (String)

    Path to the LIT file

  • output_dir (String) (defaults to: nil)

    Output directory path (default: current directory)

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

    Additional options

Options Hash (options):

  • :use_manifest (Boolean)

    Use manifest for filenames (default: true)



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/cabriolet/lit/command_handler.rb', line 47

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

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

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

  use_manifest = options.fetch(:use_manifest, true)
  count = decompressor.extract_all(lit_file, output_dir,
                                   use_manifest: use_manifest)

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

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

This method returns an undefined value.

Display detailed LIT file information

Shows comprehensive information about the LIT structure, including sections, manifest, and files.

Parameters:

  • file (String)

    Path to the LIT file

  • options (Hash)

    Additional options (unused)



107
108
109
110
111
112
113
114
115
116
# File 'lib/cabriolet/lit/command_handler.rb', line 107

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

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

  display_lit_info(lit_file)

  decompressor.close(lit_file)
end

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

This method returns an undefined value.

List LIT file contents

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

Parameters:

  • file (String)

    Path to the LIT file

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

    Additional options

Options Hash (options):

  • :use_manifest (Boolean)

    Use manifest for original filenames



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/cabriolet/lit/command_handler.rb', line 25

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

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

  display_header(lit_file)
  display_files(lit_file, decompressor, options)

  decompressor.close(lit_file)
end

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

This method returns an undefined value.

Test LIT file integrity

Verifies the LIT file structure.

Parameters:

  • file (String)

    Path to the LIT file

  • options (Hash)

    Additional options (unused)



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cabriolet/lit/command_handler.rb', line 125

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

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

  puts "Testing #{file}..."
  # Check for DRM
  if lit_file.encrypted?
    puts "WARNING: LIT file is DRM-encrypted (level: #{lit_file.drm_level})"
    puts "Encryption is not supported by this implementation"
  else
    puts "OK: LIT file structure is valid (#{lit_file.directory.entries.size} files)"
  end

  decompressor.close(lit_file)
end