/tools/Ruby/lib/ruby/1.8/rdoc/generators/chm_generator.rb

http://github.com/agross/netopenspace · Ruby · 112 lines · 104 code · 7 blank · 1 comment · 1 complexity · 0f16d888e7b24d6daf7f90e5f4c3b330 MD5 · raw file

  1. require 'rdoc/generators/html_generator'
  2. module Generators
  3. class CHMGenerator < HTMLGenerator
  4. HHC_PATH = "c:/Program Files/HTML Help Workshop/hhc.exe"
  5. # Standard generator factory
  6. def CHMGenerator.for(options)
  7. CHMGenerator.new(options)
  8. end
  9. def initialize(*args)
  10. super
  11. @op_name = @options.op_name || "rdoc"
  12. check_for_html_help_workshop
  13. end
  14. def check_for_html_help_workshop
  15. stat = File.stat(HHC_PATH)
  16. rescue
  17. $stderr <<
  18. "\n.chm output generation requires that Microsoft's Html Help\n" <<
  19. "Workshop is installed. RDoc looks for it in:\n\n " <<
  20. HHC_PATH <<
  21. "\n\nYou can download a copy for free from:\n\n" <<
  22. " http://msdn.microsoft.com/library/default.asp?" <<
  23. "url=/library/en-us/htmlhelp/html/hwMicrosoftHTMLHelpDownloads.asp\n\n"
  24. exit 99
  25. end
  26. # Generate the html as normal, then wrap it
  27. # in a help project
  28. def generate(info)
  29. super
  30. @project_name = @op_name + ".hhp"
  31. create_help_project
  32. end
  33. # The project contains the project file, a table of contents
  34. # and an index
  35. def create_help_project
  36. create_project_file
  37. create_contents_and_index
  38. compile_project
  39. end
  40. # The project file links together all the various
  41. # files that go to make up the help.
  42. def create_project_file
  43. template = TemplatePage.new(RDoc::Page::HPP_FILE)
  44. values = { "title" => @options.title, "opname" => @op_name }
  45. files = []
  46. @files.each do |f|
  47. files << { "html_file_name" => f.path }
  48. end
  49. values['all_html_files'] = files
  50. File.open(@project_name, "w") do |f|
  51. template.write_html_on(f, values)
  52. end
  53. end
  54. # The contents is a list of all files and modules.
  55. # For each we include as sub-entries the list
  56. # of methods they contain. As we build the contents
  57. # we also build an index file
  58. def create_contents_and_index
  59. contents = []
  60. index = []
  61. (@files+@classes).sort.each do |entry|
  62. content_entry = { "c_name" => entry.name, "ref" => entry.path }
  63. index << { "name" => entry.name, "aref" => entry.path }
  64. internals = []
  65. methods = entry.build_method_summary_list(entry.path)
  66. content_entry["methods"] = methods unless methods.empty?
  67. contents << content_entry
  68. index.concat methods
  69. end
  70. values = { "contents" => contents }
  71. template = TemplatePage.new(RDoc::Page::CONTENTS)
  72. File.open("contents.hhc", "w") do |f|
  73. template.write_html_on(f, values)
  74. end
  75. values = { "index" => index }
  76. template = TemplatePage.new(RDoc::Page::CHM_INDEX)
  77. File.open("index.hhk", "w") do |f|
  78. template.write_html_on(f, values)
  79. end
  80. end
  81. # Invoke the windows help compiler to compiler the project
  82. def compile_project
  83. system(HHC_PATH, @project_name)
  84. end
  85. end
  86. end