ebd4f9d981d5ed7a454806e26046e31bc059b8ad
[software/sisu] / lib / sisu / develop / src_shared.rb
1 # encoding: utf-8
2 =begin
3
4 * Name: SiSU
5
6 ** Description: documents, structuring, processing, publishing, search
7 *** create sisupod filetype and copy it to output directory!
8
9 ** Author: Ralph Amissah
10 <ralph@amissah.com>
11 <ralph.amissah@gmail.com>
12
13 ** Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
14 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Ralph Amissah,
15 All Rights Reserved.
16
17 ** License: GPL 3 or later:
18
19 SiSU, a framework for document structuring, publishing and search
20
21 Copyright (C) Ralph Amissah
22
23 This program is free software: you can redistribute it and/or modify it
24 under the terms of the GNU General Public License as published by the Free
25 Software Foundation, either version 3 of the License, or (at your option)
26 any later version.
27
28 This program is distributed in the hope that it will be useful, but WITHOUT
29 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
30 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
31 more details.
32
33 You should have received a copy of the GNU General Public License along with
34 this program. If not, see <http://www.gnu.org/licenses/>.
35
36 If you have Internet connection, the latest version of the GPL should be
37 available at these locations:
38 <http://www.fsf.org/licensing/licenses/gpl.html>
39 <http://www.gnu.org/licenses/gpl.html>
40
41 <http://www.sisudoc.org/sisu/en/manifest/gpl.fsf.html>
42
43 ** SiSU uses:
44 * Standard SiSU markup syntax,
45 * Standard SiSU meta-markup syntax, and the
46 * Standard SiSU object citation numbering and system
47
48 ** Hompages:
49 <http://www.jus.uio.no/sisu>
50 <http://www.sisudoc.org>
51
52 ** Git
53 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=summary>
54 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=blob;f=lib/sisu/develop/src_shared.rb;hb=HEAD>
55
56 =end
57 module SiSU_Source
58 include SiSU_Env
59 class SiSUpodSource
60 require_relative 'se' # se.rb
61 require_relative 'se_hub_particulars' # se_hub_particulars.rb
62 def initialize(opt,build=nil,place=nil)
63 @opt=opt
64 @date=SiSU_Env::InfoDate.new.dt
65 @env=SiSU_Env::InfoEnv.new(opt.fns)
66 #@ver=SiSU_Env::InfoVersion.instance.get_version
67 @v=(@opt.act[:verbose_plus][:set]==:on \
68 || @opt.act[:maintenance][:set]==:on) \
69 ? 'v' : ''
70 @particulars=SiSU_Particulars::CombinedSingleton.instance.get_all(opt)
71 @file=@particulars.file
72 @local_path="#{@file.output_path.sisupod.dir}"
73 processing_sisupod=@env.processing_path.processing_sisupod(opt)
74 processing_sisupod.make
75 path_pod=processing_sisupod.paths[:sisupod]
76 path_pod_fnb=processing_sisupod.paths[:fnb]
77 @doc_import_list=[]
78 FileUtils::mkdir_p(path_pod) unless FileTest.directory?(path_pod)
79 @path_pod={
80 fnb: path_pod_fnb,
81 pod: path_pod,
82 doc: path_pod + '/' + Gt[:doc] + '/' + opt.lng,
83 po: path_pod + '/' + Gt[:po] + '/' + opt.lng,
84 pot: path_pod + '/' + Gt[:pot],
85 conf: path_pod + '/' + Gt[:conf],
86 image: path_pod + '/' + Gt[:image],
87 audio: path_pod + '/' + Gt[:audio],
88 video: path_pod + '/' + Gt[:video],
89 }
90 end
91 def read
92 unless @opt.act[:quiet][:set]==:on
93 (@opt.act[:verbose][:set]==:on \
94 || @opt.act[:verbose_plus][:set]==:on \
95 || @opt.act[:maintenance][:set]==:on) \
96 ? SiSU_Screen::Ansi.new(
97 @opt.act[:color_state][:set],
98 'Assemble SiSU source',
99 "[#{@opt.f_pth[:lng_is]}] #{@opt.fno}").
100 green_hi_blue
101 : ''
102 end
103 unless @opt.fns.empty?
104 directories
105 file_array=IO.readlines(@opt.fno,'')
106 doc_import_list=pod_source_build(file_array) #this needs to be built in case of multi-lingual for all of them, before single pass tar
107 doc_import_list=[@opt.fno, doc_import_list].flatten
108 image_extraction(doc_import_list)
109 language_versions
110 end
111 end
112 def images_extract(f,images) # consider using param info
113 rgx_image=/(?:^|[^_\\])\{(?:\s*|\~\^\s+)(\S+?\.(?:png|jpg|gif)\b)/m
114 if f !~/^%+\s/ \
115 and f =~rgx_image
116 images << f.scan(rgx_image).uniq
117 end
118 images.flatten
119 end
120 def image_extraction(doc_import_list)
121 @rgx_rb_image=/["']\S*?([a-zA-Z0-9_-]+?\.(?:png|jpg|gif))["']/
122 @rgx_image=/(?:^|[^_\\])\{\s*(\S+?\.(?:png|jpg|gif))/
123 doc_import_dir=@opt.sub_location
124 images=[]
125 if doc_import_list.length > 0
126 doc_import_list=doc_import_list.uniq.flatten
127 doc_import_list.each do |fn|
128 file_array=IO.readlines(fn,'')
129 file_array.each do |f| #% work area
130 f=f.gsub(/<:=(\S+?)>/,'{ c_\1.png 14x14 }image') # embedded symbol (image)
131 if f !~/^%+\s/ \
132 and f =~@rgx_image
133 images=images_extract(f,images)
134 end
135 end
136 end
137 end
138 gi=SiSU_Env::GetInit.new
139 unless FileTest.file?("#{@path_pod[:conf]}/#{gi.makefile_name}")
140 if gi.makefile \
141 && FileTest.file?(gi.makefile)
142 FileUtils::mkdir_p(@path_pod[:conf]) \
143 unless FileTest.directory?(@path_pod[:conf])
144 FileUtils::cp(gi.makefile,"#{@path_pod[:conf]}/#{gi.makefile_name}")
145 end
146 #get images from makefile, consider placing in param
147 end
148 if images \
149 and images.length > 1
150 images=images.flatten.uniq
151 images.delete_if {|x| x =~/https?:\/\// }
152 #images=images.sort
153 FileUtils::mkdir_p(@path_pod[:image])
154 images_pwd=@opt.image_src_path
155 ##sequence copies base images, defaults used in all html outputs
156 #image_source_base='/usr/share/sisu/image'
157 #dir_pwd=Dir.pwd
158 #Dir.chdir(image_source_base)
159 #base_images=Dir.glob('*')
160 #base_images.each do |i|
161 # FileUtils::cp_r(i,"#{images_path_pod}/#{i}")
162 #end
163 #Dir.chdir(dir_pwd)
164 if FileTest.directory?(images_pwd)
165 images=images.uniq
166 images.each do |i|
167 if FileTest.file?("#{images_pwd}/#{i}")
168 FileUtils::cp("#{images_pwd}/#{i}","#{@path_pod[:image]}/#{i}")
169 else
170 STDERR.puts \
171 %{\t*WARN* did not find image - "#{images_pwd}/#{i}" [#{__FILE__}:#{__LINE__}]}
172 end
173 end
174 else
175 STDERR.puts \
176 %{\t*WARN* did not find - #{images_pwd} #{@path_pod[:image]} [#{__FILE__}:#{__LINE__}]}
177 end
178 end
179 if doc_import_list.length > 0 \
180 and @opt.fno =~/\.ssm$/
181 doc_import_list.each do |f|
182 if FileTest.file?("#{@opt.base_path}#{doc_import_dir}/#{f}")
183 FileUtils::cp("#{@opt.base_path}#{doc_import_dir}/#{f}","#{@path_pod[:doc]}/#{f}")
184 else
185 STDERR.puts \
186 %{\t*WARN* did not find image - "#{@opt.base_path}#{doc_import_dir}/#{f}" [#{__FILE__}:#{__LINE__}]}
187 end
188 end
189 end
190 end
191 def language_versions
192 x=@env.document_language_versions_found #check multiple document language versions (param not used)
193 doc_import_dir=@opt.sub_location
194 if x[:f] \
195 and x[:f].length > 0 #store multiple document language versions, sisupod
196 x[:f].each do |f|
197 FileUtils::mkdir_p(@path_pod[:doc]) unless FileTest.directory?(@path_pod[:doc])
198 if f[:f] =~/\~(\S{2,3})\.ss[tm]$/
199 lng_f=$1
200 if @opt.lng == lng_f
201 if @opt.fno =~/\.ssm$/
202 if FileTest.file?("#{@opt.base_path}#{doc_import_dir}/#{f[:f]}")
203 FileUtils::cp("#{@opt.base_path}#{doc_import_dir}/#{f[:f]}",
204 "#{@path_pod[:doc]}/#{f[:n]}")
205 else
206 STDERR.puts \
207 %{\t*WARN* did not find - "#{@opt.base_path}#{doc_import_dir}/#{f[:f]}" [#{__FILE__}:#{__LINE__}]}
208 end
209 else
210 if FileTest.file?("#{@opt.base_path}/#{f[:f]}")
211 cpy= :no
212 cpy=if f[:f] =~ /^#{@opt.f_pth[:lng_is]}\// \
213 or f[:f] =~ /~#{@opt.f_pth[:lng_is]}\.sst/
214 :yes
215 elsif f[:f] !~ /^(?:#{Px[:lng_lst_rgx]})\/|~(?:#{Px[:lng_lst_rgx]})\.sst/ \
216 and @opt.f_pth[:lng_is] == 'en'
217 :yes
218 else :no
219 end
220 if cpy == :yes
221 FileUtils::cp("#{@opt.base_path}/#{f[:f]}",
222 "#{@path_pod[:doc]}/#{f[:n]}")
223 end
224 else
225 STDERR.puts \
226 %{\t*WARN* did not find - "#{@opt.base_path}/#{f[:f]}" [#{__FILE__}:#{__LINE__}]}
227 end
228 end
229 end
230 else
231 if @opt.fno =~/\.ssm$/
232 if FileTest.file?("#{@opt.base_path}#{doc_import_dir}/#{f[:f]}")
233 FileUtils::cp_r("#{@opt.base_path}#{doc_import_dir}/#{f[:f]}",
234 "#{@path_pod[:doc]}/#{f[:n]}")
235 else
236 STDERR.puts \
237 %{\t*WARN* did not find - "#{@opt.base_path}#{doc_import_dir}/#{f[:f]}" [#{__FILE__}:#{__LINE__}]}
238 end
239 else
240 if FileTest.file?("#{@opt.base_path}#{doc_import_dir}/#{f[:f]}")
241 cpy= :no
242 cpy=if f[:f] =~ /^#{@opt.f_pth[:lng_is]}\// \
243 or f[:f] =~ /~#{@opt.f_pth[:lng_is]}\.sst/
244 :yes
245 elsif f[:f] !~ /^(?:#{Px[:lng_lst_rgx]})\/|~(?:#{Px[:lng_lst_rgx]})\.sst/ \
246 and @opt.f_pth[:lng_is] == 'en'
247 :yes
248 else :no
249 end
250 if cpy == :yes
251 FileUtils::cp("#{@opt.base_path}#{doc_import_dir}/#{f[:f]}",
252 "#{@path_pod[:doc]}/#{f[:n]}")
253 end
254 else
255 STDERR.puts \
256 %{\t*WARN* did not find - "#{@opt.base_path}#{doc_import_dir}/#{f[:f]}" [#{__FILE__}:#{__LINE__}]}
257 end
258 end
259 end
260 end
261 end #NB not all possibilies met, revisit, also in case of composite file may wish to add README
262 end
263 def directories
264 SiSU_Env::InfoEnv.new.sisupod_v4(@opt)
265 end
266 def ssm_doc_import_list(f)
267 if @opt.fno =~/\.ss[mi]$/
268 doc_import_list=[]
269 if f =~@rgx_doc_import
270 doc_import_list << f.scan(@rgx_doc_import)
271 end
272 doc_import_list.flatten.each do |i|
273 if i =~/.ssi/
274 file_array=IO.readlines(i,'')
275 pod_source_build(file_array)
276 end
277 end
278 end
279 doc_import_list.uniq.flatten
280 end
281 def pod_source_build(file_array)
282 @pwd=Dir.pwd
283 @rgx_doc_import=/^<<\s*(\S+?\.ss[ti])/
284 doc_import_list=[]
285 file_array.each do |f| #% work area
286 if f !~/^%+\s/
287 if @opt.fno =~/\.ss[mi]$/
288 @doc_import_list << ssm_doc_import_list(f)
289 doc_import_list=@doc_import_list.flatten.uniq
290 end
291 end
292 end
293 doc_import_list
294 end
295 end
296 end
297 __END__
298 question?: should you permit the packing of multiple documents in single .xz ?
299
300 open @opt.fns, parse file
301 extract from file content:
302 images and copy each image from whatever image source to _sisu/sisupod/sisu/_sisu/image
303
304 remove previously existing contents of _/sisu/sisupod &
305 make directory structure:
306
307 v3 -->
308 _sisu
309 sisupod
310 doc
311 manifest.txt
312 en/content.sst [file content]
313 fr/content.sst
314 _sisu
315 conf
316 image (ln -s ../../image)
317 audio (ln -s ../../audio)
318 video (ln -s ../../video)
319 image [all images for specific document gathered here]
320 audio
321 video
322
323 v2 -->
324 _sisu
325 sisupod
326 content.sst [file content]
327 filename.sst [link to content.sst]
328 _sisu/
329 image/ [all images for specific document gathered here]
330
331 sisu
332 _sisu
333 sisurc.yml
334 convert/
335 standard_terms/
336 image
337 processing
338 ao/
339 tex/
340 texinfo/
341 tune/
342 sisupod
343
344 special case
345
346 composite file (master), e.g.
347 SiSU.ssm