#!/usr/bin/env ruby
#
# author: secondlife < http://rails2u.com/ >
#
# This program is dual-licensed free software
# you can redistribute it and/or modify it under the terms of the MIT License or the Academic Free License v2.1.
#
# This script setting to svn:mochikit/trunk/scripts/htmlhelp.rb
# $ ruby htmlhelp.rb
#
require 'pathname'
require 'erb'
require 'logger'
class MochiKitHtmlhelpGenerator
ITEM_TEMPLATE =<<-EOF
<% for item in items -%>
<% end -%>
EOF
HTML_PATH = 'doc/html/MochiKit/'
def initialize(rootpath, logger = nil)
@logger = logger || Logger.new(STDOUT)
@root = Pathname.new rootpath
@html_path = Pathname.new HTML_PATH
@rev = svn_revison root
@files = []
end
attr_reader :logger, :root, :html_path, :rev
def generate(options = {})
logger.info 'generating files...'
options = {
:project_file => 'mochikit.hhp',
:contents_file => 'mochikit.hhc',
:index_file => 'mochikit.hhk',
:compile_file => "mochikit#{rev ? '.' + rev : ''}.chm",
}.update options
@files.clear
generate_item options[:project_file]
generate_contents options[:contents_file]
generate_project options
end
private
def svn_revison(path)
begin
ENV.update 'LANG' => 'C'
if m = `svn info #{path}`.match(%r{Revision:\s+(\d+)})
logger.info 'MochiKit SVN Revision: ' + m[1]
return m[1]
end
rescue Exception => e ;end
nil
end
def generate_item(file)
items = []
re = %r{([^<]+):}
Dir.glob(root.join(html_path) + '*.html').each do |html|
@files << html_path.join(html.sub(%r{^.+/},''))
IO.read(html).each_line do |line|
if m = line.match(re)
items << { :name => m[2], :link => "#{html_path.join(File.basename(html))}##{m[1]}" }
end
end
end
items = items.sort_by {|i| i[:name].downcase }.uniq
source = compile_template items, binding
save file, source
end
def generate_contents(file)
items = [{:name => 'MochiKit Documentation', :link => html_path.join('index.html').to_s } ]
re = %r{
([^<]+)}
root.join(html_path).join('index.html').read.each_line do |line|
if m = line.match(re)
items << { :name => m[2], :link => html_path.join(m[1]).to_s }
end
end
source = compile_template items, binding
save file, source
end
def generate_project(options)
img_path = Pathname.new 'include/_img/'
Dir.glob(root.join(img_path) + '*.*').each do |img|
@files << img_path.join(File.basename(img))
end
title = 'MochiKit Documentation'
title << " (rev: #{rev})" if rev
source = <<-EOF
[OPTIONS]
Compatibility=1.0
Compiled file=#{options[:compile_file]}
Contents file=#{options[:contents_file]}
Default Window=Main
Default topic=doc\\html\\MochiKit\\index.html
Display compile progress=Yes
Enhanced decompilation=Yes
Full-text search=Yes
Index file=#{options[:index_file]}
Language=0x809 English
Title=#{title}
[INFOTYPES]
[FILES]
#{@files.map {|f| f.to_s.gsub('/','\\') }.join("\n") }
EOF
save options[:project_file], source
end
def save(file, source)
outfile = root.join(file)
outfile.open('w') {|f| f.puts source }
logger.info "generate: #{outfile}"
end
def compile_template(items, b)
ERB.new(ITEM_TEMPLATE, nil, '-').result b
end
end
mg = MochiKitHtmlhelpGenerator.new Pathname.new(File.dirname(__FILE__)).parent
mg.generate