d6a4867b155cfb37770401cf90e05f18d26f106e
[software/sisu] / lib / sisu / develop / ao_composite.rb
1 # encoding: utf-8
2 =begin
3
4 * Name: SiSU
5
6 ** Description: documents, structuring, processing, publishing, search
7 *** composite documents, assemble/build documents from other documents
8 or parts of marked up text
9
10 ** Author: Ralph Amissah
11 <ralph@amissah.com>
12 <ralph.amissah@gmail.com>
13
14 ** Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
15 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Ralph Amissah,
16 All Rights Reserved.
17
18 ** License: GPL 3 or later:
19
20 SiSU, a framework for document structuring, publishing and search
21
22 Copyright (C) Ralph Amissah
23
24 This program is free software: you can redistribute it and/or modify it
25 under the terms of the GNU General Public License as published by the Free
26 Software Foundation, either version 3 of the License, or (at your option)
27 any later version.
28
29 This program is distributed in the hope that it will be useful, but WITHOUT
30 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
31 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
32 more details.
33
34 You should have received a copy of the GNU General Public License along with
35 this program. If not, see <http://www.gnu.org/licenses/>.
36
37 If you have Internet connection, the latest version of the GPL should be
38 available at these locations:
39 <http://www.fsf.org/licensing/licenses/gpl.html>
40 <http://www.gnu.org/licenses/gpl.html>
41
42 <http://www.sisudoc.org/sisu/en/manifest/gpl.fsf.html>
43
44 ** SiSU uses:
45 * Standard SiSU markup syntax,
46 * Standard SiSU meta-markup syntax, and the
47 * Standard SiSU object citation numbering and system
48
49 ** Hompages:
50 <http://www.jus.uio.no/sisu>
51 <http://www.sisudoc.org>
52
53 ** Git
54 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=summary>
55 <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=blob;f=lib/sisu/develop/ao_composite.rb;hb=HEAD>
56
57 =end
58 module SiSU_Assemble
59 require_relative 'se' # se.rb
60 class RemoteImage
61 def initialize
62 @env=SiSU_Env::InfoEnv.new
63 end
64 def image(dir)
65 images=[]
66 images[0]=dir
67 images
68 end
69 def download_images(images_info)
70 path="#{@env.processing_path.processing}/external_document/image"
71 FileUtils::mkdir_p(path) \
72 unless FileTest.directory?(path)
73 download_from=images_info.shift
74 images_info.each do |i|
75 image="#{path}/#{i}"
76 imagefile=File.new(image,'w+')
77 open("#{download_from}/#{i}") do |g|
78 imagefile << g.read
79 end
80 imagefile.close
81 end
82 output_path="#{@env.path.webserv}/#{@env.path.base_markup_dir_stub}/_sisu/image_external"
83 FileUtils::mkdir_p(output_path) \
84 unless FileTest.directory?(output_path)
85 SiSU_Env::SystemCall.new("#{path}/*",output_path,'q').rsync
86 end
87 end
88 class Composite
89 def initialize(opt)
90 @opt=opt
91 @env=SiSU_Env::InfoEnv.new
92 end
93 def read
94 begin
95 pwd=Dir.pwd
96 Dir.chdir(@opt.f_pth[:pth])
97 if @opt.fno =~/\S+?\.ssm$/
98 SiSU_Screen::Ansi.new(
99 @opt.act[:color_state][:set],
100 'Composite Document',
101 "[#{@opt.f_pth[:lng_is]}] #{@opt.fno}",
102 ).grey_title_hi unless @opt.act[:quiet][:set]==:on
103 assembled=loadfile(@opt.fno)
104 #assembled=insertions?(fns_array)
105 write(assembled)
106 write(assembled)
107 end
108 Dir.chdir(pwd)
109 rescue
110 SiSU_Errors::Rescued.new($!,$@,@opt.selections.str,@opt.fns).location do
111 __LINE__.to_s + ':' + __FILE__
112 end
113 ensure
114 end
115 end
116 def insert?(para)
117 if para =~ /^<<\s+((?:https?|file):\/\/\S+?\.ss[it])$/ # and NetTest
118 url($1.strip)
119 elsif para =~/^<<\s+(\S+?\.ss[it])$/
120 loadfilename=$1.strip
121 insert_array=loadfile(loadfilename)
122 file=insertion(loadfilename,insert_array)
123 file[:prepared]
124 else para
125 end
126 end
127 def loadfile(loadfilename)
128 tuned_file=[]
129 begin
130 if FileTest.file?(loadfilename)
131 insert_array=IO.readlines(loadfilename,'')
132 if loadfilename =~/\S+?\.ss[itm]$/
133 if (@opt.act[:verbose][:set]==:on \
134 || @opt.act[:verbose_plus][:set]==:on \
135 || @opt.act[:maintenance][:set]==:on)
136 SiSU_Screen::Ansi.new(
137 @opt.act[:color_state][:set],
138 'loading:',
139 loadfilename,
140 ).txt_grey
141 end
142 if loadfilename =~/\S+?\.ss[im]$/
143 insert_array.each do |para|
144 tuned_file << insert?(para)
145 end
146 elsif loadfilename =~/\S+?\.sst$/
147 insert_array.each do |para|
148 tuned_file << para
149 end
150 end
151 end
152 end
153 tuned_file=tuned_file.flatten.compact
154 rescue
155 SiSU_Errors::Rescued.new($!,$@,@opt.selections.str,@opt.fns).location do
156 __LINE__.to_s + ':' + __FILE__
157 end
158 ensure
159 end
160 end
161 def url(loadfilename)
162 if loadfilename =~ /((?:https?|file):\/\/\S+?\.ss[it])$/ # and NetTest
163 loadfilename=$1
164 begin
165 require 'uri'
166 require 'open-uri'
167 require 'pp'
168 rescue LoadError
169 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
170 error('uri, open-uri or pp NOT FOUND (LoadError)')
171 end
172 insert=open(loadfilename)
173 insert_array=insert.dup
174 insert.close
175 file=insertion(loadfilename,insert_array)
176 file[:prepared]
177 end
178 end
179 def write(assembled)
180 assembled_file=File.new("#{@env.processing_path.composite_file}/#{@opt.fnb}.ssm.sst",'w+')
181 assembled.each {|a| assembled_file << a }
182 assembled_file.close
183 end
184 def download_images(download_from,images_array)
185 path="#{@env.processing_path.processing}/external_document/image"
186 FileUtils::mkdir_p(path) unless FileTest.directory?(path)
187 images_array.each do |i|
188 image="#{path}/#{i}"
189 unless FileTest.exists?(image)
190 imagefile=File.new(image,'w+')
191 open("#{download_from}/#{i}") do |g|
192 imagefile << g.read
193 end
194 imagefile.close
195 end
196 end
197 end
198 def insertion(fni,insert_array)
199 file={ prepared: [], images: [] }
200 rgx_image=/(?:^|[^_\\])\{\s*(\S+?\.(?:png|jpg|gif))/
201 file[:prepared] << "\n% |#{fni}|@|^|>>ok\n\n"
202 @code_flag=false
203 insert_array.each do |i|
204 @code_flag=if i =~/^code\{/ then true
205 elsif i =~/^\}code/ then false
206 else @code_flag
207 end
208 if not @code_flag \
209 and i !~/^%+\s/
210 i=i.
211 gsub(/^([123]|:?[ABCD])~\? /,
212 '% [conditional heading:] \1~ ') #off conditional heading (consider syntax)
213 if i =~/^@\S+?:/
214 i=i.gsub(/\n/m,"\n% ").
215 gsub(/\n%\s+$/m,'').
216 gsub(/^@\S+?:/m,"\n% [imported header:] ") #off imported headers
217 end
218 end
219 file[:prepared] << i
220 if i !~/^%+\s/ \
221 and i =~rgx_image
222 file[:images] << i.scan(rgx_image).uniq
223 end
224 end
225 file[:prepared] << "\n% end import" << "\n\n"
226 if file[:images].length > 0
227 file[:images]=file[:images].flatten.uniq
228 file[:images].delete_if {|x| x =~/https?:\/\// }
229 end
230 file
231 end
232 end
233 class CompositeFileList
234 def initialize(opt)
235 @opt=opt
236 @env=SiSU_Env::InfoEnv.new
237 end
238 def read
239 begin
240 @opt.fns=@opt.fns.gsub(/\.ssm\.sst$/,'.ssm') #FIX earlier, hub
241 fns_array=IO.readlines(@opt.fns,'')
242 insertions?(fns_array)
243 rescue
244 SiSU_Errors::Rescued.new($!,$@,@opt.selections.str,@opt.fns).location do
245 __LINE__.to_s + ':' + __FILE__
246 end
247 ensure
248 end
249 end
250 def insertions?(fns_array)
251 tuned_file=[]
252 SiSU_Screen::Ansi.new(
253 @opt.act[:color_state][:set],
254 'Composite Document',
255 @opt.fno
256 ).grey_title_hi unless @opt.act[:quiet][:set]==:on
257 @ssm=[@opt.fns]
258 fns_array.each do |para|
259 if para =~/^<<\s+(\S+?\.ss[it])$/
260 loadfilename=$1.strip
261 if (@opt.act[:verbose][:set]==:on \
262 || @opt.act[:verbose_plus][:set]==:on \
263 || @opt.act[:maintenance][:set]==:on)
264 SiSU_Screen::Ansi.new(
265 @opt.act[:color_state][:set],
266 'loading:',
267 loadfilename,
268 ).txt_grey
269 end
270 tuned_file << if loadfilename =~ /(?:https?|file):\/\/\S+?\.ss[it]$/
271 @ssm << loadfilename
272 elsif loadfilename =~ /\.ss[it]$/ \
273 and FileTest.file?(loadfilename)
274 @ssm << loadfilename
275 else
276 STDERR.puts %{SKIPPED processing file: [#{@opt.lng}] "#{@opt.fns}" it requires an invalid or non-existent file: "#{loadfilename}"}
277 $process_document = :skip; break #remove this line to continue processing documents that have missing include files
278 para
279 end
280 end
281 end
282 @ssm
283 end
284 end
285 end
286 __END__