Class: DocuBot::CHMWriter

Inherits:
HTMLWriter show all
Defined in:
lib/docubot/writers/chm.rb

Constant Summary collapse

SUPPORT =
DocuBot::Writer::DIR / 'chm'

Constants inherited from Writer

Writer::DIR, Writer::HAML_OPTIONS, Writer::INSTALLED_WRITERS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Writer

by_type, handles_type

Constructor Details

#initialize(bundle) ⇒ CHMWriter

Returns a new instance of CHMWriter.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/docubot/writers/chm.rb', line 11

def initialize( bundle )
  super
  @toc = @bundle.toc
  @global = @bundle.global
  if @global.default
    # User tried to specify the default page

    @default_topic = @bundle.pages_by_title[ @global.default ].first
    if @default_topic
      if @default_topic.file =~ /\s/
        warn "'#{@global.default}' cannot be the default CHM page; it has a space in the file name."
        @default_topic = nil
      end
    else
      warn "The requested default page '#{@global.default}' could not be found. (Did the title change?)"
    end
  end
  if @default_topic.nil?
    @default_topic = @toc.descendants.find{ |node| node.link =~ /^\S+$/ }
    @default_topic &&= @default_topic.page
  end
  warn "No default page is set, because no page has a path without spaces." unless @default_topic    

end

Instance Attribute Details

#chm_pathObject (readonly)

Returns the value of attribute chm_path.



9
10
11
# File 'lib/docubot/writers/chm.rb', line 9

def chm_path
  @chm_path
end

#default_topicObject (readonly)

Returns the value of attribute default_topic.



9
10
11
# File 'lib/docubot/writers/chm.rb', line 9

def default_topic
  @default_topic
end

#hhcObject (readonly)

Returns the value of attribute hhc.



9
10
11
# File 'lib/docubot/writers/chm.rb', line 9

def hhc
  @hhc
end

#hhkObject (readonly)

Returns the value of attribute hhk.



9
10
11
# File 'lib/docubot/writers/chm.rb', line 9

def hhk
  @hhk
end

#hhpObject (readonly)

Returns the value of attribute hhp.



9
10
11
# File 'lib/docubot/writers/chm.rb', line 9

def hhp
  @hhp
end

Instance Method Details

#write(destination = nil) ⇒ Object



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/docubot/writers/chm.rb', line 35

def write( destination=nil )
  super( nil )

  lap = Time.now
  @chm_path = File.expand_path( destination || "#{@bundle.source}.chm" )
  @hhc = @chm_path.sub( /[^.]+$/, 'hhc' )
  @hhp = @chm_path.sub( /[^.]+$/, 'hhp' )
  @hhk = @chm_path.sub( /[^.]+$/, 'hhk' )
  write_hhc
  write_hhk
  write_hhp
  puts "...%.2fs to write the CHM support files" % (Time.now-lap)
  lap = Time.now
  
  # This will fail if a handle is open to it on Windows

  begin
    FileUtils.rm( @chm_path ) if File.exists?( @chm_path )
  rescue Errno::EACCES
    require 'win32ole'
    for process in WIN32OLE.connect("winmgmts://").ExecQuery("select Name,CommandLine from win32_process where Name='hh.exe'") do
      process.Terminate if process.CommandLine.include? @chm_path.gsub('/','\\')
    end
  end
  
  # Help find hhc.exe

  possible_hhc_spots = [ "C:\\Program Files\\HTML Help Workshop", "C:\\Program Files (x86)\\HTML Help Workshop" ]
  path_directories   = ENV['PATH'].split(';').concat( possible_hhc_spots )
  ENV['PATH'] = path_directories.join(';')
  unless path_directories.any?{ |dir| File.exists?( File.join(dir, 'hhc.exe' ) ) }
    warn "Cannot find hhc.exe in your PATH or the standard install spots.\nDid you install HTML Help Workshop?"
    FileUtils.rm( [ @hhc, @hhp, @hhk ] )
    FileUtils.rm_r( @html_path )
    exit 1
  end
  
  `hhc.exe "#{FileUtils.win_path @hhp}"`.gsub( /[\r\n]+/, "\n" )
  puts "...%.2fs to create the CHM" % (Time.now-lap)
  lap = Time.now
  
  # Clean out the intermediary files

  FileUtils.rm( [ @hhc, @hhp, @hhk ] ) unless defined?(ARGS) && ARGS[:logfile] 
  FileUtils.rm_r( @html_path )
  puts "...%.2fs to clean up temporary files" % (Time.now-lap)
  lap = Time.now
  
  unless defined?(ARGS) && ARGS[:nopreview]
    # Spin a new thread so it doesn't hold up the Ruby process, but sleep long enough for it to get going.

    Thread.new{ `hh.exe "#{FileUtils.win_path @chm_path}"` }
    sleep 0.1 if Object.const_defined? "Encoding" # This sleep does not help on 1.8.6

  else
    puts "...Skipping .chm preview"
  end
end

#write_hhcObject



89
90
91
92
93
# File 'lib/docubot/writers/chm.rb', line 89

def write_hhc
  contents = ERB.new( IO.read( SUPPORT / 'hhc.erb', encoding:'utf-8' ) ).result( binding )
  # puts contents

  File.open( @hhc, 'w' ){ |f| f << contents }
end

#write_hhkObject



101
102
103
104
# File 'lib/docubot/writers/chm.rb', line 101

def write_hhk
  contents = ERB.new( IO.read( SUPPORT / 'hhk.erb', encoding:'utf-8' ) ).result( binding )
  File.open( @hhk, 'w' ){ |f| f << contents }
end

#write_hhpObject



95
96
97
98
99
# File 'lib/docubot/writers/chm.rb', line 95

def write_hhp
  contents = ERB.new( IO.read( SUPPORT / 'hhp.erb', encoding:'utf-8' ) ).result( binding )
  # puts contents

  File.open( @hhp, 'w' ){ |f| f << contents }
end