Class: Cabriolet::HLP::WinHelp::Compressor

Inherits:
Object
  • Object
show all
Defined in:
lib/cabriolet/hlp/winhelp/compressor.rb

Overview

Compressor creates Windows Help (.HLP) files

Creates WinHelp 3.x and 4.x format files with Zeck LZ77 compression. Supports creating |SYSTEM, |TOPIC, and other internal files.

Constant Summary collapse

BLOCK_SIZE =

Default block size for WinHelp files (4096 bytes)

4096

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io_system = nil) ⇒ Compressor

Initialize compressor

Parameters:



22
23
24
25
26
# File 'lib/cabriolet/hlp/winhelp/compressor.rb', line 22

def initialize(io_system = nil)
  @io_system = io_system || System::IOSystem.new
  @internal_files = {}
  @version = :winhelp3
end

Instance Attribute Details

#io_systemObject (readonly)

Returns the value of attribute io_system.



14
15
16
# File 'lib/cabriolet/hlp/winhelp/compressor.rb', line 14

def io_system
  @io_system
end

Instance Method Details

#add_internal_file(name, data) ⇒ void

This method returns an undefined value.

Add an internal file to the WinHelp archive

Parameters:

  • name (String)

    Internal filename (e.g., “|SYSTEM”, “|TOPIC”)

  • data (String)

    File data



33
34
35
# File 'lib/cabriolet/hlp/winhelp/compressor.rb', line 33

def add_internal_file(name, data)
  @internal_files[name] = data
end

#add_system_file(**options) ⇒ void

This method returns an undefined value.

Add |SYSTEM file with metadata

Parameters:

  • options (Hash)

    System file options

Options Hash (**options):

  • :title (String)

    Help file title

  • :copyright (String)

    Copyright text

  • :contents (String)

    Contents file path



44
45
46
47
# File 'lib/cabriolet/hlp/winhelp/compressor.rb', line 44

def add_system_file(**options)
  system_data = build_system_file(options)
  add_internal_file("|SYSTEM", system_data)
end

#add_topic_file(topics, compress: true) ⇒ void

This method returns an undefined value.

Add |TOPIC file with compressed topics

Parameters:

  • topics (Array<String>)

    Array of topic texts

  • compress (Boolean) (defaults to: true)

    Whether to compress topics



54
55
56
57
# File 'lib/cabriolet/hlp/winhelp/compressor.rb', line 54

def add_topic_file(topics, compress: true)
  topic_data = build_topic_file(topics, compress)
  add_internal_file("|TOPIC", topic_data)
end

#generate(output_file, **options) ⇒ Integer

Generate WinHelp file

Parameters:

  • output_file (String)

    Path to output file

  • options (Hash)

    Generation options

Options Hash (**options):

  • :version (Symbol)

    Format version (:winhelp3 or :winhelp4)

Returns:

  • (Integer)

    Bytes written

Raises:



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cabriolet/hlp/winhelp/compressor.rb', line 65

def generate(output_file, **options)
  @version = options.fetch(:version, :winhelp3)

  if @internal_files.empty?
    raise ArgumentError,
          "No internal files added"
  end
  raise ArgumentError, "Invalid version" unless i[winhelp3
                                                   winhelp4].include?(@version)

  # Build structure
  structure = build_structure

  # Write to file
  output_handle = @io_system.open(output_file, Constants::MODE_WRITE)
  begin
    write_winhelp_file(output_handle, structure)
  ensure
    @io_system.close(output_handle)
  end
end