aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/sisu/v4/epub_concordance.rb
blob: b8db317d780f68c5807799cba794ab6e35c6ed9f (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# encoding: 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, 2009, 2010, 2011, 2012 Ralph Amissah, All Rights Reserved.

 * License: GPL 3 or later:

   SiSU, a framework for document structuring, publishing and search

   Copyright (C) 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/licenses/gpl.html>

   <http://www.sisudoc.org/sisu/en/manifest/gpl.fsf.html>

 * 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.sisudoc.org/sisu/en/SiSU/download.html>

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

 ** Description: epub concordance file (html concordance, wordmap, linked index
    of words in document)

=end
module SiSU_EPUB_Concordance
  require_relative 'particulars'                        # particulars.rb
    include SiSU_Particulars
  require_relative 'sysenv'                             # sysenv.rb
    include SiSU_Env
  require_relative 'defaults'                           # defaults.rb
    include SiSU_Viz
  require_relative 'epub_format'                        # epub_format.rb
    include SiSU_EPUB_Format
  class Source
    def initialize(opt)
      @opt=opt
      @particulars=SiSU_Particulars::CombinedSingleton.instance.get_all(opt)
    end
    def read
      begin
        @env,@md=@particulars.env,@particulars.md
        wordmax=@env.concord_max
        unless @md.wc_words.nil?
          if @md.wc_words < wordmax
            SiSU_EPUB_Concordance::Source::Words.new(@particulars).songsheet
          else
            SiSU_Screen::Ansi.new(@md.opt.cmd,"*WARN* concordance skipped, large document has over #{wordmax} words (#{@md.wc_words})").warn unless @md.opt.cmd =~/q/
          end
        else
          SiSU_Screen::Ansi.new(@md.opt.cmd,"*WARN* wc (word count) is off, concordance will be processed for all files including those over the max set size of: #{wordmax} words").warn unless @md.opt.cmd =~/q/
          SiSU_EPUB_Concordance::Source::Words.new(@particulars).songsheet
        end
      rescue
        SiSU_Errors::InfoError.new($!,$@,@md.opt.cmd,@md.fns).error do
          __LINE__.to_s + ':' + __FILE__
        end
      ensure
      end
    end
    private
    class DocTitle
      include SiSU_Viz
      #revisit, both requires (html & shared_xml) needed for stand alone operation (sisu -w [filename])
      require_relative 'epub'                           # epub.rb
      def initialize(particulars)
        @particulars,@md=particulars,particulars.md
        @data=SiSU_EPUB::Source::XHTML_Environment.new(particulars).tuned_file_instructions
        @vz=SiSU_Env::GetInit.instance.skin
        txt_path=%{#{@md.dir_out}}
        SiSU_Env::InfoSkin.new(@md).select
        @fnb=@md.fnb
        @lex_button=%{<a href="http://www.jus.uio.no/sisu/" target="_top"><img border="0" height="44" width="144" valign="center" src="../_sisu/image/sisu.png" alt="SiSU home --&gt;"></a>}
        @doc_details =<<WOK
<table summary="links to text related to this rudimentary index" width="96%" border="0" bgcolor="white" cellpadding="0" align="center"><tr><td width="2%" align="right">&nbsp;</td><td width="94%" valign="top" align="justify"><h1 class="small"><a href="#{@md.file.base_filename.epub}"><b>#{@md.title.full}</b></a></h1><p class="bold">#{@md.creator.author}</p></td></tr></table>
WOK
      end
      def create
        @css=SiSU_Env::CSS_Stylesheet.new(@particulars.md)
        format_head_toc=SiSU_EPUB_Format::HeadToc.new(@md)
        dochead=format_head_toc.head
        <<WOK
#{dochead}
<div class="content">
 #{@doc_details}
<p>Word index links are to html versions of the text the segmented version followed by the scroll (single document) version.<br />[For segmented text references [T1], [T2] or [T3] appearing without a link, indicates that the word appears in a title (or subtitle) of the text (that is identifiable by the appended object citation number).]</p>
<p>(The word listing/index is Case sensitive: Capitalized words appear before lower case)</p>
  <p>
    <b>word</b> (number of occurences)<br />linked references to word within document <br />
    [if number of occurences exceed number of references - word occurs more than once in at least one reference. Footnote/endnotes are either assigned to the paragraph from which they are referenced or ignored, so it is relevant to check the footnotes referenced from within a paragraph as well.]
  </p>
  <p>
    (After the page is fully loaded) you can jump directly to a word by appending a hash (#) and the word to the url for this text, (do not forget that words are case sensitive, and may be listed twice (starting with and without an upper case letter)), #your_word # [&nbsp;http://[web host]/#{@fnb}/concordance.html#your_word&nbsp;]
  </p>
WOK
      end
    end
    class Word
      @@word_previous=''
      def initialize(word,freq)
        @word,@freq=word,freq
      end
      def html
        w=if @word.capitalize==@@word_previous
          %{\n<p class="concordance_word">#{@word}</p><p class="concordance_count">(#{@freq})</p>\n\t<p class="concordance_object"> }
        else n=@word.strip.gsub(/\s+/,'_') #also need to convert extended character set to html
          %{\n<p class="concordance_word"><a name="#{n}">#{@word}</a></p><p class="concordance_count">(#{@freq})</p>\n\t<p class="concordance_object"> }
        end
        @@word_previous=@word.capitalize
        w
      end
    end
    class Words
      require_relative 'defaults'                       # defaults.rb
        include SiSU_Viz
      require_relative 'epub_format'                    # epub_format.rb
        include SiSU_EPUB_Format
      require_relative 'sysenv'                         # sysenv.rb
        include SiSU_Screen
      def initialize(particulars)
        @particulars=particulars
        begin
          @vz=SiSU_Env::GetInit.instance.skin
          @env,@md,@dal_array=particulars.env,particulars.md,particulars.dal_array
          @path="#{@env.processing_path.epub}"
          @freq=Hash.new(0)
          @rxp_lv1=/^#{Mx[:lv_o]}1:/ #fix Mx[:lv_o]
          @rxp_lv2=/^#{Mx[:lv_o]}2:/ #fix Mx[:lv_o]
          @rxp_lv3=/^#{Mx[:lv_o]}3:/ #fix Mx[:lv_o]
          @rxp_seg=/^#{Mx[:lv_o]}4:(\S+?)#{Mx[:lv_c]}/
          @rxp_title=Regexp.new("^#{Mx[:meta_o]}title#{Mx[:meta_c]}\s*(.+?)\s*$")
          @rxp_t1=Regexp.new('^T1')
          @rxp_t2=Regexp.new('^T2')
          @rxp_t3=Regexp.new('^T3')
          @rxp_excluded1=/(?:https?|file|ftp):\/\/\S+/
          @rxp_excluded0=/^(?:#{Mx[:fa_bold_o]}|#{Mx[:fa_italics_o]})?(?:to\d+|\d+|&nbsp;|#{Mx[:br_endnotes]}|EOF|#{Mx[:br_eof]}|thumb_\S+|snap_\S+|_+|-+|[(]?(?:ii+|iv|vi+|ix|xi+|xiv|xv|xvi+|xix|xx)[).]?|\S+?_\S+|[\d_]+\w\S+|[\w\d]{1,2}|\d{1,3}\w?|[0-9a-f]{16,64}|\d{2,3}x\d{2,3}|\S{0,2}sha\d|\S{0,3}\d{4}w\d\d|\b\w\d+|\d_all\b|e\.?g\.?)(?:#{Mx[:fa_bold_c]}|#{Mx[:fa_italics_c]})?$/mi #this regex causes and cures a stack dump in ruby 1.9 !!!
          @rgx_splitlist=%r{[—.,;:-]+|#{Mx[:nbsp]}+}mi
          @rgx_scanlist=%r{#{Mx[:fa_italics_o]}[a-zA-Z0-9"\s]{2,12}#{Mx[:fa_italics_c]}|#{Mx[:fa_bold_o]}[a-zA-Z0-9"\s]{2,12}#{Mx[:fa_bold_c]}|#{Mx[:url_o]}https?://\S+?#{Mx[:url_c]}|file://\S+|<\S+?>|\w+|[a-zA-Z]+}mi
        rescue
          SiSU_Errors::InfoError.new($!,$@,@md.opt.cmd,@md.fns).error do
            __LINE__.to_s + ':' + __FILE__
          end
        end
      end
      def songsheet
        begin
          #fix to use
          p __LINE__.to_s + ':' + __FILE__
          p "#{@path}/content/#{@md.fn[:epub_concord]}"
          p "#{@md.file.output_path.epub.dir}/#{@md.file.base_filename.epub}"
          @file_concordance=File.open("#{@path}/content/#{@md.fn[:epub_concord]}",'w')
          map_para
        rescue
          SiSU_Errors::InfoError.new($!,$@,@md.opt.cmd,@md.fns).error do
            __LINE__.to_s + ':' + __FILE__
          end
        ensure
          @file_concordance.close
        end
      end
    protected
      def location_scroll(wordlocation,show)
        @wordlocation=wordlocation
        %{<a href="doc#{Sfx[:epub_xhtml]}\##{@wordlocation}">#{@wordlocation}</a>;  }
      end
      def location_seg(wordlocation,show)
        @wordlocation,@show=wordlocation,show
        @word_location_seg=wordlocation.gsub(/(.+?)\#(\d+)/,"\\1#{Sfx[:epub_xhtml]}#o\\2") unless wordlocation.nil?
        case @wordlocation
        when @rxp_t1
          %{[<a href="doc#{Sfx[:epub_xhtml]}##{@show}">H</a>]#{@show},  }
        when @rxp_t2
          %{[<a href="doc#{Sfx[:epub_xhtml]}##{@show}">H</a>]#{@show},  }
        when @rxp_t3
          %{[<a href="doc#{Sfx[:epub_xhtml]}##{@show}">H</a>]#{@show},  }
        else %{<a href="#{@word_location_seg}">#{@show}</a>,  }
        end
      end
      def map_para
        @seg,toy=nil,nil
        @word_map={}
        @dal_array.each do |line|
          if defined? line.ocn
            if (line.is ==:heading \
            || line.is ==:heading_insert) \
            && line.ln==4
              @seg=line.name
            end
            if line.ocn.to_s =~/\d+/ then toy=line.ocn.to_s
            end
            if toy =~/\d+/ \
            and toy !~/^0$/
              line.obj=line.obj.split(@rgx_splitlist).join(' ') #%take in word or other match
              for word in line.obj.scan(@rgx_scanlist) #%take in word or other match
                word=word.gsub(/#{Mx[:lnk_o]}|#{Mx[:lnk_c]}|#{Mx[:url_o]}|#{Mx[:url_c]}/,'').
                  gsub(/#{Mx[:fa_o]}\S+?#{Mx[:fa_o_c]}/,'').
                  gsub(/#{Mx[:fa_c_o]}\S+?#{Mx[:fa_c]}/,'').
                  gsub(/#{Mx[:gl_o]}#[a-z]+#{Mx[:gl_c]}/,'').
                  gsub(/#{Mx[:gl_o]}#[0-9]+#{Mx[:gl_c]}/,'').
                  gsub(/^\S$/,'')
                word=nil if word.empty?
                word=nil if word =~@rxp_excluded0 #watch
                word=nil if word =~@rxp_excluded1 #watch
                word=nil if word =~/^\S$/
                if word
                  word=word.gsub(/#{Mx[:br_nl]}|#{Mx[:br_line]}/,' ').
                    gsub(/#{Mx[:fa_o]}[a-z]{1,7}#{Mx[:fa_o_c]}|#{Mx[:fa_c_o]}[a-z]{1,7}#{Mx[:fa_c]}/,'').
                    gsub(/#{Mx[:mk_o]}(?:[0-9a-f]{32}:[0-9a-f]{32}|[0-9a-f]{64}:[0-9a-f]{64})#{Mx[:mk_c]}/,'').
                    gsub(/#{Mx[:mk_o]}(?:[0-9a-f]{32}|[0-9a-f]{64})#{Mx[:mk_c]}/,'').
                    gsub(/#{Mx[:en_a_o]}(?:\d|[*+])*|#{Mx[:en_b_o]}(?:\d|[*+])*|#{Mx[:en_a_c]}|#{Mx[:en_b_c]}/mi,'').
                    gsub(/#{Mx[:fa_o]}\S+?#{Mx[:fa_o_c]}/,'').gsub(/#{Mx[:fa_c_o]}\S+?#{Mx[:fa_c]}/,'').
                    gsub(/<\/?\S+?>/,'').
                    gsub(/^\@+/,'').
                    strip.
                    gsub(/#{Mx[:tc_p]}.+/,'').
                    gsub(/[\.,;:"]$/,'').
                    gsub(/["]/,'').
                    gsub(/^\s*[\(]/,'').
                    gsub(/[\(]\s*$/,'').
                    gsub(/^(?:See|e\.?g\.?).+/,'').
                    gsub(/^\s*[.,;:]\s*/,'').
                    strip.
                    gsub(/^\(?[a-zA-Z]\)$/,'').
                    gsub(/^\d+(st|nd|rd|th)$/,'').
                    gsub(/^(\d+\.?)+$/, '').
                    gsub(/#{Mx[:mk_o]}|#{Mx[:mk_c]}/,'').
                    gsub(/:name#\S+/,'').
                    gsub(/^\S$/,'')
                  word=nil if word =~/^\S$/
                  word=nil if word =~/^\s*$/ #watch
                  if word
                    unless word =~/[A-Z][A-Z]/ \
                    or word =~/\w+\s\w+/
                      word=word.capitalize
                    end
                    @freq[word] +=1
                    @word_map[word] ||= []
                    if line !~@rxp_lv1 \
                    and line !~@rxp_lv2 \
                    and line !~@rxp_lv3
                      @word_map[word] << location_seg("#{@seg}\##{toy}",toy)
                    else
                      @word_map[word] << case line
                      when @rxp_lv1; location_seg('T1',toy)
                      when @rxp_lv2; location_seg('T2',toy)
                      when @rxp_lv3; location_seg('T3',toy)
                      end
                    end
                  end
                end
              end
            end
          end
        end
        scr='<font size="1" color="#777777" face=times new roman><img border="0" height="15" width="15" src="../_sisu/image/b_doc.png" alt="Full Text">&nbsp;scroll:&nbsp;</font><font size="1" color="#222222" face=times new roman>doc#&nbsp;</font> '
        seg=''
        @file_concordance << SiSU_EPUB_Concordance::Source::DocTitle.new(@particulars).create
        alph=%W[A B C D E F G H I J K L M N O P Q R S T U V W X Y Z]
        @file_concordance << '<p>'
        alph.each {|x| @file_concordance << %{<a href="##{x}">#{x}</a>,&nbsp;}}
        @file_concordance << '</p>'
        letter=alph.shift
        @file_concordance << %{\n<p class="letter"><a name="A">A</a></p>}
        for word in @freq.keys.sort! {|a,b| a.downcase<=>b.downcase}
          f=/^(\S)/.match(word)[1]
          if letter < f.upcase
            while letter < f.upcase
              if alph.length > 0
                letter=alph.shift
                @file_concordance << %{\n<p class="letter"><a name="#{letter}">#{letter}</a></p>}
              else break
              end
            end
          end
          keyword=SiSU_EPUB_Concordance::Source::Word.new(word,@freq[word]).html
          if keyword !~ @rxp_excluded0
            if @word_map[word][0] =~ /\d+/
              wm=[]
              @file_concordance << %{#{keyword}#{seg}#{@word_map[word].uniq.compact.join}}
            end
            @file_concordance << '</p>'
          end
          # special cases endnotes and header levels 1 - 3
        end
        credits=@vz.credits_sisu_epub
        @file_concordance << %{</div>>#{credits}</body>\n</html>} # footer
      end
    end
  end
end
__END__