Changeset 683 for lang/ruby/chm

Show
Ignore:
Timestamp:
10/24/07 20:55:49 (6 years ago)
Author:
cho45
Message:

lang/ruby/chm/lib/chm.rb:

各種バグ修正
トピックツリーのサポート

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/ruby/chm/lib/chm.rb

    r676 r683  
    44 
    55require "chmlib" 
     6require "strscan" 
    67require "nkf" 
    78 
     
    1415 
    1516        def initialize(filename) 
     17                @filename = filename 
    1618                @h = Chmlib.chm_open(filename) 
    1719                raise ChmError, "Not exists?" unless @h 
     
    2729        end 
    2830 
     31        def home 
     32                if File.basename(@filename, ".chm") == File.basename(@home) 
     33                        @home = self.topics.flatten.find {|i| i[:local] }[:local] 
     34                else 
     35                        @home 
     36                end 
     37        end 
     38 
     39        def unescape!(n) 
     40                n.gsub!(/&lt;/, "<") 
     41                n.gsub!(/&gt;/, ">") 
     42                n.gsub!(/&quot;/, "\"") 
     43                n.gsub!(/&amp;/, "&") 
     44                n 
     45        end 
     46 
     47        # keyword index 
    2948        def index 
    3049                return nil unless @index 
    3150                return @index_cache if @index_cache 
    3251 
    33                 text = retrieve_object(@index) 
     52                text = NKF.nkf("-w", retrieve_object(@index)) 
    3453                #<OBJECT type="text/sitemap"> 
    3554                #<param name="Name" value="pushd(path = nil, &amp;block) (c/m Shell) (ruby-src:doc/shell.rd)"> 
     
    4564                                next unless n 
    4665                                next if n.empty? or n.match(/^\s+$/) 
    47                                 n.gsub!(/&lt;/, "<") 
    48                                 n.gsub!(/&gt;/, ">") 
    49                                 n.gsub!(/&quot;/, "\"") 
    50                                 n.gsub!(/&amp;/, "&") 
    51                                 n = NKF.nkf("-w", n) 
     66                                unescape!(n) 
    5267                                (index[n] ||= []) << local 
    5368                        end 
    5469                end 
    5570                @index_cache = index.to_a 
     71        end 
     72 
     73        # table of contents 
     74        def topics 
     75                return nil unless @topics 
     76                return @topics_cache if @topics_cache 
     77 
     78                text = NKF.nkf("-w", retrieve_object(@topics)) 
     79                result = [] 
     80 
     81                s = StringScanner.new(text) 
     82                s.skip(/.*?<UL>\s*/m) 
     83 
     84                current = result 
     85                level   = [] 
     86                while s.scan(/<(LI|UL|\/UL)>\s*/) 
     87                        case s[1] 
     88                        when "LI" 
     89                                s.skip(%r{<OBJECT type="text/sitemap">\s*}) 
     90                                s.scan(%r{<param name="Name" value="([^"]+)">\s*(<param name="Local" value="([^"]+)">)?\s*}) 
     91                                current << { 
     92                                        :name   => unescape!(s[1]), 
     93                                        :local  => s[3] || "", 
     94                                        :children => [] 
     95                                } 
     96                                s.skip(%r{.*?</OBJECT>\s*}) 
     97                        when "UL" 
     98                                level << current 
     99                                current = current.last[:children] 
     100                        when "/UL" 
     101                                current = level.pop 
     102                        end 
     103                end 
     104 
     105 
     106                # result = [ 
     107                #     {name:"name",local:"",child:[]}, 
     108                #     ], 
     109                # 
     110                @topics_cache = result 
    56111        end 
    57112 
     
    179234 
    180235if $0 == __FILE__ 
    181         chm = Chmlib::Chm.new("/Users/cho45/htmlhelp/rubymanjp.chm") 
    182236        require "pp" 
    183         chm.index #cache 
    184         puts "ok" 
    185         pp chm.index.select {|k,v| /split/i === k } 
     237        #chm = Chmlib::Chm.new("/Users/cho45/htmlhelp/rubymanjp.chm") 
     238#       chm.index #cache 
     239#       puts "ok" 
     240#       pp chm.index.select {|k,v| /split/i === k } 
     241        chm = Chmlib::Chm.new("/Users/cho45/htmlhelp/kr2doc.chm") 
     242        pp chm.home 
     243        #pp chm.topics 
    186244end