d: composite documents (.ssm), extract insert files list
[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 require_relative 'utils_composite' # utils_composite.rb
63 include SiSU_Composite_Doc_Utils # composite doc, .ssm, extract all related insert files, array of filenames test
64 def initialize(opt,build=nil,place=nil)
65 @opt=opt
66 @date=SiSU_Env::InfoDate.new.dt
67 @env=SiSU_Env::InfoEnv.new(opt.fns)
68 #@ver=SiSU_Env::InfoVersion.instance.get_version
69 @v=(@opt.act[:verbose_plus][:set]==:on \
70 || @opt.act[:maintenance][:set]==:on) \
71 ? 'v' : ''
72 @particulars=SiSU_Particulars::CombinedSingleton.instance.get_all(opt)
73 @file=@particulars.file
74 @local_path="#{@file.output_path.sisupod.dir}"
75 processing_sisupod=@env.processing_path.processing_sisupod(opt)
76 processing_sisupod.make
77 path_pod=processing_sisupod.paths[:sisupod]
78 path_pod_fnb=processing_sisupod.paths[:fnb]
79 FileUtils::mkdir_p(path_pod) unless FileTest.directory?(path_pod)
80 @path_pod={
81 fnb: path_pod_fnb,
82 pod: path_pod,
83 doc: path_pod + '/' + Gt[:doc] + '/' + opt.lng,
84 po: path_pod + '/' + Gt[:po] + '/' + opt.lng,
85 pot: path_pod + '/' + Gt[:pot],
86 conf: path_pod + '/' + Gt[:conf],
87 image: path_pod + '/' + Gt[:image],
88 audio: path_pod + '/' + Gt[:audio],
89 video: path_pod + '/' + Gt[:video],
90 }
91 end
92 def read
93 unless @opt.act[:quiet][:set]==:on
94 (@opt.act[:verbose][:set]==:on \
95 || @opt.act[:verbose_plus][:set]==:on \
96 || @opt.act[:maintenance][:set]==:on) \
97 ? SiSU_Screen::Ansi.new(
98 @opt.act[:color_state][:set],
99 'Assemble SiSU source',
100 "[#{@opt.f_pth[:lng_is]}] #{@opt.fno}").
101 green_hi_blue
102 : ''
103 end
104 unless @opt.fns.empty?
105 directories
106 doc_import_list=composite_and_imported_filenames_array(@opt.fno)
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(
169 "#{images_pwd}/#{i}",
170 "#{@path_pod[:image]}/#{i}"
171 )
172 else
173 STDERR.puts \
174 %{\t*WARN* did not find image - "#{images_pwd}/#{i}" [#{__FILE__}:#{__LINE__}]}
175 end
176 end
177 else
178 STDERR.puts \
179 %{\t*WARN* did not find - #{images_pwd} #{@path_pod[:image]} [#{__FILE__}:#{__LINE__}]}
180 end
181 end
182 if doc_import_list.length > 0 \
183 and @opt.fno =~/\.ssm$/
184 doc_import_list.each do |f|
185 if FileTest.file?("#{@opt.base_path}#{doc_import_dir}/#{f}")
186 FileUtils::cp(
187 "#{@opt.base_path}#{doc_import_dir}/#{f}",
188 "#{@path_pod[:doc]}/#{f}"
189 )
190 else
191 STDERR.puts \
192 %{\t*WARN* did not find image - "#{@opt.base_path}#{doc_import_dir}/#{f}" [#{__FILE__}:#{__LINE__}]}
193 end
194 end
195 end
196 end
197 def language_versions
198 x=@env.document_language_versions_found #check multiple document language versions (param not used)
199 doc_import_dir=@opt.sub_location
200 if x[:f] \
201 and x[:f].length > 0 #store multiple document language versions, sisupod
202 x[:f].each do |f|
203 FileUtils::mkdir_p(@path_pod[:doc]) \
204 unless FileTest.directory?(@path_pod[:doc])
205 if f[:f] =~/\~(\S{2,3})\.ss[tm]$/
206 lng_f=$1
207 if @opt.lng == lng_f
208 if @opt.fno =~/\.ssm$/
209 if FileTest.file?("#{@opt.base_path}#{doc_import_dir}/#{f[:f]}")
210 FileUtils::cp(
211 "#{@opt.base_path}#{doc_import_dir}/#{f[:f]}",
212 "#{@path_pod[:doc]}/#{f[:n]}"
213 )
214 else
215 STDERR.puts \
216 %{\t*WARN* did not find - "#{@opt.base_path}#{doc_import_dir}/#{f[:f]}" [#{__FILE__}:#{__LINE__}]}
217 end
218 else
219 if FileTest.file?("#{@opt.base_path}/#{f[:f]}")
220 cpy= :no
221 cpy=if f[:f] =~ /^#{@opt.f_pth[:lng_is]}\// \
222 or f[:f] =~ /~#{@opt.f_pth[:lng_is]}\.sst/
223 :yes
224 elsif f[:f] !~ /^(?:#{Px[:lng_lst_rgx]})\/|~(?:#{Px[:lng_lst_rgx]})\.sst/ \
225 and @opt.f_pth[:lng_is] == 'en'
226 :yes
227 else :no
228 end
229 if cpy == :yes
230 FileUtils::cp(
231 "#{@opt.base_path}/#{f[:f]}",
232 "#{@path_pod[:doc]}/#{f[:n]}"
233 )
234 end
235 else
236 STDERR.puts \
237 %{\t*WARN* did not find - "#{@opt.base_path}/#{f[:f]}" [#{__FILE__}:#{__LINE__}]}
238 end
239 end
240 end
241 else
242 if @opt.fno =~/\.ssm$/
243 if FileTest.file?("#{@opt.base_path}#{doc_import_dir}/#{f[:f]}")
244 FileUtils::cp_r(
245 "#{@opt.base_path}#{doc_import_dir}/#{f[:f]}",
246 "#{@path_pod[:doc]}/#{f[:n]}"
247 )
248 else
249 STDERR.puts \
250 %{\t*WARN* did not find - "#{@opt.base_path}#{doc_import_dir}/#{f[:f]}" [#{__FILE__}:#{__LINE__}]}
251 end
252 else
253 if FileTest.file?("#{@opt.base_path}#{doc_import_dir}/#{f[:f]}")
254 cpy= :no
255 cpy=if f[:f] =~ /^#{@opt.f_pth[:lng_is]}\// \
256 or f[:f] =~ /~#{@opt.f_pth[:lng_is]}\.sst/
257 :yes
258 elsif f[:f] !~ /^(?:#{Px[:lng_lst_rgx]})\/|~(?:#{Px[:lng_lst_rgx]})\.sst/ \
259 and @opt.f_pth[:lng_is] == 'en'
260 :yes
261 else :no
262 end
263 if cpy == :yes
264 FileUtils::cp(
265 "#{@opt.base_path}#{doc_import_dir}/#{f[:f]}",
266 "#{@path_pod[:doc]}/#{f[:n]}"
267 )
268 end
269 else
270 STDERR.puts \
271 %{\t*WARN* did not find - "#{@opt.base_path}#{doc_import_dir}/#{f[:f]}" [#{__FILE__}:#{__LINE__}]}
272 end
273 end
274 end
275 end
276 end #NB not all possibilies met, revisit, also in case of composite file may wish to add README
277 end
278 def directories
279 SiSU_Env::InfoEnv.new.sisupod_v4(@opt)
280 end
281 end
282 end
283 __END__
284 question?: should you permit the packing of multiple documents in single .xz ?
285
286 open @opt.fns, parse file
287 extract from file content:
288 images and copy each image from whatever image source to _sisu/sisupod/sisu/_sisu/image
289
290 remove previously existing contents of _/sisu/sisupod &
291 make directory structure:
292
293 v3 -->
294 _sisu
295 sisupod
296 doc
297 manifest.txt
298 en/content.sst [file content]
299 fr/content.sst
300 _sisu
301 conf
302 image (ln -s ../../image)
303 audio (ln -s ../../audio)
304 video (ln -s ../../video)
305 image [all images for specific document gathered here]
306 audio
307 video
308
309 v2 -->
310 _sisu
311 sisupod
312 content.sst [file content]
313 filename.sst [link to content.sst]
314 _sisu/
315 image/ [all images for specific document gathered here]
316
317 sisu
318 _sisu
319 sisurc.yml
320 convert/
321 standard_terms/
322 image
323 processing
324 ao/
325 tex/
326 texinfo/
327 tune/
328 sisupod
329
330 special case
331
332 composite file (master), e.g.
333 SiSU.ssm