aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/sisu/v3/sisupod_make.rb
blob: b154bc1d960fbd264504e425e3e71943576ae5f5 (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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# encoding: utf-8
=begin

 * Name: SiSU

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

 * Author: Ralph Amissah

 * Copyright: (C) 1997 - 2011, 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.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: create sisupod filetype and copy it to output directory!

=end
module SiSU_Doc
  include SiSU_Env
  class Source
    require_relative 'sysenv'                           # sysenv.rb
    require_relative 'response'                         # response.rb
    require_relative 'particulars'                      # particulars.rb
    def initialize(opt)
      @opt=opt
      m=/.+\/(?:src\/)?(\S+)/im
      @date=SiSU_Env::Info_date.new.dt
      @env=SiSU_Env::Info_env.new(@opt.fns)
      @ver=SiSU_Env::Info_version.instance.get_version
      @ans=SiSU_Response::Response.new
      pod_path="#{@env.processing_path.processing}/sisupod"
      @v=(@opt.cmd =~/[VM]/) ? 'v' : ''
      @particulars=SiSU_Particulars::Combined_singleton.instance.get_all(opt)
      @file=@particulars.file
      @local_path="#{@file.output_path.sisupod.dir}"
      @zipfile=(@opt.fns =~/\.ssm\.sst$/) \
      ? @opt.fns.gsub(/(?:\~\S{2,3})?\.ssm\.sst$/,'.ssm')
      : @opt.fns.gsub(/(?:\~\S{2,3})?(\.sst)$/,'\1')
      FileUtils::mkdir_p(pod_path) unless FileTest.directory?(pod_path)
      FileUtils::rm_rf("#{@env.processing_path.processing}sisupod/*") if FileTest.directory?("#{@env.processing_path.processing}sisupod")
    end
    def read
      unless @opt.cmd =~/q/
        @opt.cmd=~/[MVvz]/ \
        ? SiSU_Screen::Ansi.new(@opt.cmd,'Share SiSU Document (txz)',@opt.fns).green_hi_blue
        : SiSU_Screen::Ansi.new(@opt.cmd,'Share SiSU Document (txz)',@opt.fns).green_title_hi
      end
      unless @opt.fns.empty?
        unless @opt.cmd =~/q/
          SiSU_Screen::Ansi.new(@opt.cmd,'Make sisu document (txz) and place in output directory',"#{@opt.fns} -> file://#{@file.output_path.sisupod.dir}/#{@zipfile}.txz").warn if @opt.cmd =~/[MVv]/
        end
        directories
        sisupod_build
        sisupod_xz
      else
        if @opt.cmd =~/[MVv]/
          SiSU_Screen::Ansi.new(@opt.cmd,'Make (zip (txz)) of sisu work directory',"#{@env.stub_pwd}\_#{@date}.txz").warn
          SiSU_Screen::Ansi.new(@opt.cmd,'').blue_tab
        end
        ans=@ans.response?('make sisupod of entire directory?')
        if ans; sisupod_zip_directory
        end
      end
    end
    def sisupod_zip_directory
      sisupod_directory="sisupod-#{@env.stub_pwd}-#{@date}.ssp"
      unless File.symlink?('sisupod'); File.symlink(Dir.pwd,'sisupod')
      end
      re_base_dir='^sisupod/([^.][^/]*\.(ssm|ssi|sst)$|_sisu/)'
      re_files='/[a-zA-Z0-9.~_-]+\.(ssm|ssi|sst|rb|css|png|jpg|gif|yaml)$'
      re_ignore='/[.~_-][^/]+$'
      if File.symlink?('sisupod')
        info="SiSU sisupod #{@ver[:version]} directory contents #{sisupod_directory}"
        system(%{
          echo "#{info}"
          echo "#{info}" > sisu_zip.txt
          zip -qz #{sisupod_directory} sisu_zip.txt < sisu_zip.txt
          for I in `find -H sisupod -print | \
            egrep "#{re_base_dir}" | \
            egrep "#{re_files}" | \
            egrep -v "#{re_ignore}" `
          do zip #{sisupod_directory} $I
          done
        })
      else puts "the required systemlink 'sisupod' could not be created on the current directory: remove the file or directory named 'sisupod' within #{Dir.pwd}"
      end
      if @opt.cmd =~/[MVv]/
        SiSU_Screen::Ansi.new(@opt.cmd,'Share SiSU Document (tar xz)',@opt.fns).green_hi_blue
        SiSU_Screen::Ansi.new(@opt.cmd,'Zipped directory (tgz)',sisupod_directory).warn
        SiSU_Screen::Ansi.new('',sisupod_directory).blue_tab
      end
    end
    def directories
      SiSU_Env::Info_env.new.sisupod_v3(@opt)
    end
    def select_skin(skin='')                                                   #skin loading logic here
      load "#{SiSU_lib}/defaults.rb"
      @skin={}
      skin_path = [
        "#{@env.path.pwd}/_sisu/skin",
        "#{@env.path.home}/.sisu/skin",
        '/etc/sisu/skin',
        "#{@env.processing_path.processing}/external_document/skin"
      ]
      sk_doc,sk_dir="doc/#{skin}.rb","dir/skin_#{@env.stub_pwd}.rb"
      skin_path.each do |v|                                                    #document skin priority 1
        if FileTest.file?("#{v}/#{sk_doc}")
          @skin[:name],@skin[:type]="#{v}/#{sk_doc}",'doc'
          break
        end
      end
      unless @skin.length > 0
        skin_path.each do |v|                                                  #directory skin priority 2
          if FileTest.file?("#{v}/#{sk_dir}")
            @skin[:name],@skin[:type]="#{v}/#{sk_dir}",'dir'
            break
          end
        end
      end
      @skin
    end
    def images_extract(f,images)
      rgx_image=/(?:^|[^_\\])\{\s*(\S+?\.(?:png|jpg|gif))/
      if f !~/^%+\s/ \
      and f =~rgx_image
        images << f.scan(rgx_image).uniq
      end
      images.flatten
    end
    def sisupod_build                                                          #see also sisupod in sysenv
      @pwd=Dir.pwd
      @rgx_rb_image=/["'](\S+?\.(?:png|jpg|gif))["']/
      @rgx_image=/(?:^|[^_\\])\{\s*(\S+?\.(?:png|jpg|gif))/
      @rgx_skin=/^\s+:skin:\s+(\S+)/
      @rgx_doc_import=/^<<\s*(\S+?\.ss[ti])/
      use_file=@opt.fns =~/(?:\.ssm|\.ssm\.sst)$/ \
      ? @opt.fns.gsub(/\.ssm\.sst$/,'.ssm')
      : @opt.fns
      file_array=IO.readlines(use_file,'')
      skin,images,doc_import=[],[],[]
      doc_import_dir=@opt.sub_location
      file_array.each do |f|                                                   #% work area
        if f !~/^%+\s/
          skin << f.scan(@rgx_skin).uniq.flatten if f =~@rgx_skin
          if f !~/^%+\s/ \
          and f =~@rgx_image
            images=images_extract(f,images)
          end
          if use_file =~/\.ssm$/
            doc_import << f.scan(@rgx_doc_import) if f =~@rgx_doc_import
          end
        end
      end
      if doc_import.length > 0
        doc_import=doc_import.uniq.flatten
        doc_import.each do |fn|
          file_array=IO.readlines(fn,'')
          file_array.each do |f|                                                   #% work area
            if f !~/^%+\s/ \
            and f =~@rgx_image
              images=images_extract(f,images)
            end
          end
        end
      end
      docskin=nil
      if skin \
      and skin.length > 0
        docskin=skin.pop.flatten.join
        skin_source=select_skin(docskin)
      else
        skin_source=select_skin
      end
      docskin_place="#{@env.processing_path.processing}/sisupod/doc/_sisu/skin/#{skin_source[:type]}"
      if skin_source[:type] =~/dir/
        docskin_with_path="#{docskin_place}/skin_#{@env.stub_pwd}.rb"
        docskin=[docskin_with_path.gsub(/.+?\/(skin_\S+?)\.rb/,'\1')]
        docskin='skin_sisupod'
      end
      if skin_source \
      and docskin #imperfect, revisit
        unless skin_source[:name].nil? \
        or skin_source[:name].empty?
          FileUtils::cp_r(skin_source[:name],"#{docskin_place}/#{docskin}.rb")
          skinfile_array=IO.readlines(skin_source[:name],'')
          para_images=[]
          skinfile_array.each do |f|                                           #% work area
            unless f =~/^%+ / #hmmm
              images << f.scan(@rgx_rb_image).uniq if f =~@rgx_rb_image
              #does not really discriminate, may duplicate images in sisu file, and may take images from default image pool
            end
          end
        end
      end
      #1. mapping in doc dir?
      #2. need images used by skin, scan skin??
      if images \
      and images.length > 1
        images.flatten!
        images.uniq!
        images.delete_if {|x| x =~/https?:\/\// }
        #images.sort!
        path_pod="#{@env.processing_path.processing}/sisupod"
        path_pod_conf="#{@env.processing_path.processing}/sisupod/_sisu"
        images_path_pod="#{path_pod}/image"
        #unattractive hard coding ... !
        image_path='_sisu/image'
        images_pwd="#{@env.path.pwd}/#{image_path}"
        ##sequence copies base images, defaults used in all html outputs
          #image_source_base='/usr/share/sisu/image'
          #dir_pwd=Dir.pwd
          #Dir.chdir(image_source_base)
          #base_images=Dir.glob('*')
          #base_images.each do |i|
          #  FileUtils::cp_r(i,"#{images_path_pod}/#{i}")
          #end
          #Dir.chdir(dir_pwd)
        if FileTest.directory?(images_pwd)
          images=images.uniq
          images.each do |i|
            FileUtils::cp_r("#{images_pwd}/#{i}","#{images_path_pod}/#{i}") if FileTest.file?("#{images_pwd}/#{i}")
          end
        else puts "\tWARN, did not find - #{images_pwd} #{images_path_pod}"
        end
      end
      if doc_import.length > 0 \
      and use_file =~/\.ssm$/
        doc_import.each do |f|
          FileUtils::cp_r("#{@env.path.pwd}#{doc_import_dir}/#{f}","#{@env.processing_path.processing}/sisupod/doc/#{@opt.lng}/#{f}")
        end
      end
      x=@env.document_language_versions_found                                  #check multiple document language versions (param not used)
      if x[:f] \
      and x[:f].length > 0                                                     #store multiple document language versions, sisupod
        x[:f].each do |f|
          (use_file =~/\.ssm$/) \
          ? FileUtils::cp_r("#{@env.path.pwd}#{doc_import_dir}/#{f[:f]}","#{@env.processing_path.processing}/sisupod/doc/#{f[:l]}/#{f[:n]}")
          : FileUtils::cp_r("#{@env.path.pwd}/#{f[:f]}","#{@env.processing_path.processing}/sisupod/doc/#{f[:l]}/#{f[:n]}")
        end
      end #NB not all possibilies met, revisit, also in case of composite file may wish to add README
    end
    def sisupod_xz
      FileUtils::mkdir_p(@file.output_path.sisupod.dir) unless FileTest.directory?(@file.output_path.sisupod.dir)
      tree=(@opt.cmd =~/[vVM]/ \
      && SiSU_Env::System_call.new.program_found?('tree')) \
      ? 'tree sisupod'
      : ''
      if FileTest.directory?(@env.processing_path.processing)
        Dir.chdir(@env.processing_path.processing)
        system(%{
          #{tree}
          tar -cJf #{@zipfile}.txz sisupod
          #echo "#{@file.place_file.sisupod.dir}"
        })
        FileUtils::mv("#{@zipfile}.txz", @file.place_file.sisupod.dir)
        FileUtils::rm_r(Dir.glob("sisupod/*"))
        Dir.chdir(@env.path.pwd)
        SiSU_Screen::Ansi.new('',"#{@opt.fns}.txz").blue_tab unless @opt.cmd =~/q/
      else
        SiSU_Screen::Ansi.new('',"#{@opt.fns}.txz not built").blue_tab
      end
    end
  end
end
__END__
question?:                   should you permit the packing of multiple files in single .gz ?

  open @opt.fns, parse file
    extract from file content:
      images and copy each image from whatever image source to _sisu/sisupod/sisu/_sisu/image
      skin and copy active skin from whatever source to _sisu/sisupod/sisu/_sisu/skin/doc
      extract from skin images required by skin

   remove previously existing contents of _/sisu/sisupod &
   make directory structure:

v3 -->
   _sisu
     sisupod
       doc
         manifest.txt
         en/content.sst                [file content]
         fr/content.sst
         _sisu
           conf
             skin/
               doc                     [relevant skin if any other than default]
           image (ln -s ../../image)
           audio (ln -s ../../audio)
           video (ln -s ../../video)
       image                           [all images for specific document gathered here]
       audio
       video

v2 -->
   _sisu
     sisupod
       content.sst                     [file content]
       filename.sst                    [link to content.sst]
       _sisu/
         skin/
           doc                         [relevant skin if any other than default]
         image/                        [all images for specific document gathered here]

sisu
  _sisu
    sisurc.yml
    skin/
      dir/
      doc/
      misc/
      site/
      yaml/
    convert/
    standard_terms/
    image
    processing
      dal/
      tex/
      texinfo/
      tune/
    sisupod

special case

composite file (master), e.g.
SiSU.ssm