aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/sisu/v0/dal_idx.rb
blob: 607686a3c8be618f25c0bf6568546448d0bdfd02 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# coding: utf-8
=begin

 * Name: SiSU

 * Description: a framework for document structuring, publishing and search
   #___#

 * Author: Ralph Amissah

 * Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
   2007, 2008 Ralph Amissah All Rights Reserved.

 * License: GPL 3 or later:

   SiSU, a framework for document structuring, publishing and search

   Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
   2007 Ralph Amissah

   This program is free software: you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the Free
   Software Foundation, either version 3 of the License, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
   more details.

   You should have received a copy of the GNU General Public License along with
   this program. If not, see <http://www.gnu.org/licenses/>.

   If you have Internet connection, the latest version of the GPL should be
   available at these locations:
   <http://www.fsf.org/licensing/licenses/gpl.html>
   <http://www.gnu.org/copyleft/gpl.html>

   <http://www.jus.uio.no/sisu/gpl.fsf/toc.html>
   <http://www.jus.uio.no/sisu/gpl.fsf/doc.html>
   <http://www.jus.uio.no/sisu/gpl.fsf/plain.txt>

 * SiSU uses:
   * Standard SiSU markup syntax,
   * Standard SiSU meta-markup syntax, and the
   * Standard SiSU object citation numbering and system

 * Hompages:
   <http://www.jus.uio.no/sisu>
   <http://www.sisudoc.org>

 * Download:
   <http://www.jus.uio.no/sisu/SiSU/download.html>

 * Ralph Amissah
   <ralph@amissah.com>
   <ralph.amissah@gmail.com>

 ** Description: system environment, resource control and configuration details

=end
module SiSU_book_index
  class Book_index
    def initialize(data)
      @data=data
    end
    def indexing_song
      @rgx_idx=/#{Mx[:idx_o]}(?:.+?)#{Mx[:idx_c]}\s*/
      #@rgx_idx=/\s*#{Mx[:idx_o]}(?:.+?)#{Mx[:idx_c]}\s*/
      @rgx_idx_ocn_init=/#{Mx[:idx_o]}(.+?)#{Mx[:idx_c]}\s*#{Mx[:id_o]}~(\d+)\S+?#{Mx[:id_c]}/
      @rgx_idx_ocn=/(.+?)~(\d+)/
      @data=extract_book_index(@data)
      @data=clean_index(@data)
      @data
    end
    def extract_book_index(data)
      tuned_file=[]
      idx_array=[]
      data.each do |para|
        idx_array << @rgx_idx_ocn_init.match(para)[0].gsub(@rgx_idx_ocn_init,'\1~\2') if para =~ @rgx_idx_ocn_init
        tuned_file << para if para
      end
      #idx_array.each do |i|
      #  i.gsub!(@rgx_idx_ocn_init,'\1~\2')
      #end
      idx_array=construct_idx_array(idx_array) if idx_array.length > 0
      if idx_array.length > 0
        the_idx=construct_book_index(idx_array)
        screen_print(the_idx)
      end
      tuned_file
    end
    def construct_idx_array(idx_array)
      idx_lst=[]
      idx_array.each do |idx|
        idx_list,ocn=@rgx_idx_ocn.match(idx)[1,2]
        idx_lst <<=if idx_list =~/;/
          g=idx_list.scan(/[^;]+/)
          idxl=[]
          g.each do |i|
            idxl << { :rough_idx => i, :ocn => ocn }
          end
          idxl
        else { :rough_idx => idx_list, :ocn => ocn }
        end
      end
      idx_lst.flatten!
      idx_lst
    end
    def construct_book_index(idx_array)
      @the_idx={}
      idx_array.each do |idx|
        idx_lst=idx[:rough_idx].scan(/[^|:]+/)
        if idx_lst[0] =~/.+?\+\d+/
          use,plus=/(.+?)\+(\d+)/.match(idx_lst[0])[1,2]
        else use=idx_lst[0]
        end
        @the_idx[use]={} unless @the_idx[use] and defined? @the_idx[use]
        idx_lst.each do |i|
          i.strip!
          i,r=/(.+?)\+(\d+)/.match(i)[1,2] if i =~/.+?\+\d+/
          x=if idx_lst.length == 1 or idx_lst[0] == i
            @the_idx[use]['a1']=[] unless @the_idx[use]['a1'] and defined? @the_idx[use]['a1']
            x=if r
              @the_idx[use]['a1'] << { :ocn => idx[:ocn], :range => "#{idx[:ocn]}-#{idx[:ocn].to_i+r.to_i}" }
              "#{i} #{idx[:ocn]}-#{idx[:ocn].to_i+r.to_i}"
            else
              @the_idx[use]['a1'] << { :ocn => idx[:ocn] }
              "#{i} #{idx[:ocn]}"
            end
          else
            @the_idx[use]['b1']={} unless @the_idx[use]['b1'] and defined? @the_idx[use]['b1']
            @the_idx[use]['b1'][i]=[] unless @the_idx[use]['b1'][i] and defined? @the_idx[use]['b1'][i]
            x=if r
              @the_idx[use]['b1'][i] << { :ocn => idx[:ocn], :range => "#{idx[:ocn]}-#{idx[:ocn].to_i+r.to_i}" }
              "#{idx_lst[0]}:#{i} #{idx[:ocn]}-#{idx[:ocn].to_i+r.to_i}"
            else
              @the_idx[use]['b1'][i] << { :ocn => idx[:ocn] }
              "#{idx_lst[0]}:#{i} #{idx[:ocn]}"
            end
          end
        end
      end
      the_idx=@the_idx.sort
      #p the_idx; p '-----'
      the_idx
    end
    def screen_print(the_idx)
      the_idx.each do |i|
        i.each do |x|
          if x.class == String
            print "\n" + x + ', '
          elsif x.class == Array
            p 'array error? -->'
            print x
          elsif x.class == Hash
            if x['a1'].class == Array
              x['a1'].each do |a|
                if a[:range]
                  print a[:range] + ', '
                elsif a[:ocn]
                  print a[:ocn] + ', '
                else p 'error'
                end
              end
            end
            if x['b1']
              m=x['b1']
              m=m.sort
              m.each do |k,y|
                if k !~/a1/
                  print "\n\t" + k + ', '
                  #p y
                  y.each do |z|
                    if z[:range]
                      print z[:range] + ', '
                    elsif z[:ocn]
                      print z[:ocn] + ', '
                    else p 'error'
                    end
                  end
                end
              end
            end
          end
        end
      end
    end
    def clean_index(data)
      tuned_file=[]
      data.each do |para|
        para.gsub!(/\n*#{@rgx_idx}/m,'')
        tuned_file << para
      end
      tuned_file
    end
  end
end

__END__