日期:2014-05-17  浏览次数:20940 次

利用JavaDoc制作windows的CHM文档
使用了开源的一个ruby脚本来利用JavaDoc制作windows的CHM文档。

输出文件编码的时候一定记得选择中文,例如file.puts "Language=0x804 Chinese"。
def createProjectFile(prjname, basedir)
    
    packages = scanPackageTree(basedir)

    createContentsFile(prjname+'.hhc', basedir, packages)
    createIndexFile(prjname+'.hhk', basedir, packages)
    
    assetsexplise = /^$/
    title = getIndexTitle(basedir)
    
    file = open(prjname+'.hhp', 'w')
    file.puts "[OPTIONS]"
    file.puts "Compatibility=1.1 or later"
    file.puts "Compiled file=#{prjname}.chm"
    file.puts "Contents file=#{prjname}.hhc"
    file.puts "Default Window=default"
    file.puts "Display compile progress=Yes"
    file.puts "Default topic=#{basedir}/overview-summary.html"
    file.puts "Full-text search=Yes"
    file.puts "Index file=#{prjname}.hhk"
    # file.puts "Language=0x411 Japanese"
    file.puts "Language=0x409 English (U.S.)"
	# file.puts "Language=0x804 Chinese"
    file.puts "Title=#{title}"
    file.puts ""
    file.puts "[WINDOWS]"
    file.puts "default=\"#{title}\",\"#{prjname}.hhc\",\"#{prjname}.hhk\",\"#{basedir}/overview-summary.html\",\"#{basedir}/overview-summary.html\",,,,,0x2520,,0x384e,,,,,,,,0"
    file.puts ""
    file.puts ""
    file.puts "[FILES]"
    collectAssets(basedir, assetsexplise).each() do |path|
        file.puts(path)
    end
    file.puts ""
    file.close()
end

def getIndexTitle(basedir)
    title = ""
    open(basedir + '/index.html', 'r') do |fh|
        fh.read() =~ /\<title\>\n*(.+?)\n*\<\/title\>/i
        title = $1
    end
    return title
end

def collectAssets(assetsdir, assetsexplise)
    assets = []
    Dir.foreach(assetsdir) do |asset|
        assetpath = assetsdir + '/' + asset
        if File.stat(assetpath).ftype == 'file' then
            assets.push(assetpath) unless assetsexplise =~ assetpath
        elsif File.stat(assetpath).ftype == 'directory' and asset[0,1] != '.' then
            assets.concat(collectAssets(assetpath, assetsexplise))
        end
    end
    return assets
end

HEADER = '<HTML><HEAD><meta name="GENERATOR" content="Microsoft&reg; HTML Help Workshop 4.1"><!-- Sitemap 1.0 --></HEAD><BODY><OBJECT type="text/site properties"></OBJECT>'
FOOTER = '</BODY></HTML>'

def createContentsFile(filename, basedir, packages)
    
    file = open(filename, 'w')
    file.puts HEADER
    
    file.puts "[list]"
    file.puts "\t" + '<LI> <OBJECT type="text/sitemap">'
    file.puts "\t" * 2 + '<param name="Name" value="Overview">'
    file.puts "\t" * 2 + '<param name="Local" value="' + basedir + '/overview-summary.html">'
    file.puts "\t" * 2 + '<param name="ImageNumber" value="21">'
    file.puts "\t" * 2 + '</OBJECT>'
    
    packages.each() do |pkg|
        file.puts "\t" + formatTopicItem(pkg['name'], basedir+'/'+pkg['file'])
        file.puts "\t" * 1 + "[list]"
        pkg['classes'].each() do |cls|
            file.puts "\t" * 2 + formatTopicItem(cls['name'], basedir+'/'+cls['file'])
            file.puts "\t" * 2 + "[list]"
            cls['details'].each() do |det|
                file.puts "\t" * 3 + formatTopicItem(det, basedir+'/'+cls['file']+'#'+det)
            end
            file.puts "\t" * 2 + "[/list]"
        end
        file.puts "\t" * 1 + "[/list]"
    end
    file.puts "[/list]"
    
    file.puts FOOTER
    file.close()
end

def createIndexFile(filename, basedir, packages)
    
    file = open(filename, 'w')
    file.puts HEADER
    
    indexes = {}
    packages.each() do |pkg|
        addKeywordToIndex(indexes, pkg['name'], pkg['name'], basedir+'/'+pkg['file'])
        pkg['classes'].each() do |cls|
            addKeywordToIndex(indexes, cls['name'], pkg['name']+'.'+cls['name'], basedir+'/'+cls['file'])
            cls['details'].each() do |det|
                addKeywordToIndex(indexes, det, pkg['name']+'.'+cls['name']+