d: composite documents (.ssm), extract insert files list
[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 require_relative 'utils_composite' # utils_composite.rb
61 class RemoteImage
62 def initialize
63 @env=SiSU_Env::InfoEnv.new
64 end
65 def image(dir)
66 images=[]
67 images[0]=dir
68 images
69 end
70 def download_images(images_info)
71 path="#{@env.processing_path.processing}/external_document/image"
72 FileUtils::mkdir_p(path) \
73 unless FileTest.directory?(path)
74 download_from=images_info.shift
75 images_info.each do |i|
76 image="#{path}/#{i}"
77 imagefile=File.new(image,'w+')
78 open("#{download_from}/#{i}") do |g|
79 imagefile << g.read
80 end
81 imagefile.close
82 end
83 output_path="#{@env.path.webserv}/#{@env.path.base_markup_dir_stub}/_sisu/image_external"
84 FileUtils::mkdir_p(output_path) \
85 unless FileTest.directory?(output_path)
86 SiSU_Env::SystemCall.new("#{path}/*",output_path,'q').rsync
87 end
88 end
89 class Composite
90 include SiSU_Composite_Doc_Utils # composite doc, .ssm, extract all related insert files, array of filenames test
91 def initialize(opt)
92 @opt=opt
93 @env=SiSU_Env::InfoEnv.new
94 end
95 def read
96 begin
97 pwd=Dir.pwd
98 Dir.chdir(@opt.f_pth[:pth])
99 if @opt.fno =~/\S+?\.ssm$/
100 SiSU_Screen::Ansi.new(
101 @opt.act[:color_state][:set],
102 'Composite Document',
103 "[#{@opt.f_pth[:lng_is]}] #{@opt.fno}",
104 ).grey_title_hi unless @opt.act[:quiet][:set]==:on
105 composite_and_imported_filenames_array(@opt.fno) # composite doc, .ssm, extract all related insert files, array of filenames test
106 assembled=loadfile(@opt.fno)
107 write(assembled)
108 end
109 Dir.chdir(pwd)
110 rescue
111 SiSU_Errors::Rescued.new($!,$@,@opt.selections.str,@opt.fns).
112 location do
113 __LINE__.to_s + ':' + __FILE__
114 end
115 ensure
116 end
117 end
118 def insert?(para)
119 if para =~ /^<<\s+((?:https?|file):\/\/\S+?\.ss[it])$/ # and NetTest
120 url($1.strip)
121 elsif para =~/^<<\s+(\S+?\.ss[it])$/
122 loadfilename=$1.strip
123 insert_array=loadfile(loadfilename)
124 file=insertion(loadfilename,insert_array)
125 file[:prepared]
126 else para
127 end
128 end
129 def loadfile(loadfilename)
130 tuned_file=[]
131 begin
132 if FileTest.file?(loadfilename)
133 insert_array=IO.readlines(loadfilename,'')
134 if loadfilename =~/\S+?\.ss[itm]$/
135 if (@opt.act[:verbose][:set]==:on \
136 || @opt.act[:verbose_plus][:set]==:on \
137 || @opt.act[:maintenance][:set]==:on)
138 SiSU_Screen::Ansi.new(
139 @opt.act[:color_state][:set],
140 'loading:',
141 loadfilename,
142 ).txt_grey
143 end
144 if loadfilename =~/\S+?\.ss[im]$/
145 insert_array.each do |para|
146 tuned_file << insert?(para)
147 end
148 elsif loadfilename =~/\S+?\.sst$/
149 insert_array.each do |para|
150 tuned_file << para
151 end
152 end
153 end
154 end
155 tuned_file=tuned_file.flatten.compact
156 rescue
157 SiSU_Errors::Rescued.new($!,$@,@opt.selections.str,@opt.fns).location do
158 __LINE__.to_s + ':' + __FILE__
159 end
160 ensure
161 end
162 end
163 def url(loadfilename)
164 if loadfilename =~ /((?:https?|file):\/\/\S+?\.ss[it])$/ # and NetTest
165 loadfilename=$1
166 begin
167 require 'uri'
168 require 'open-uri'
169 require 'pp'
170 rescue LoadError
171 SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:fuchsia).
172 error('uri, open-uri or pp NOT FOUND (LoadError)')
173 end
174 insert=open(loadfilename)
175 insert_array=insert.dup
176 insert.close
177 file=insertion(loadfilename,insert_array)
178 file[:prepared]
179 end
180 end
181 def write(assembled)
182 assembled_file=File.new("#{@env.processing_path.composite_file}/#{@opt.fnb}.ssm.sst",'w+')
183 assembled.each {|a| assembled_file << a }
184 assembled_file.close
185 end
186 def download_images(download_from,images_array)
187 path="#{@env.processing_path.processing}/external_document/image"
188 FileUtils::mkdir_p(path) unless FileTest.directory?(path)
189 images_array.each do |i|
190 image="#{path}/#{i}"
191 unless FileTest.exists?(image)
192 imagefile=File.new(image,'w+')
193 open("#{download_from}/#{i}") do |g|
194 imagefile << g.read
195 end
196 imagefile.close
197 end
198 end
199 end
200 def insertion(fni,insert_array)
201 file={ prepared: [], images: [] }
202 rgx_image=/(?:^|[^_\\])\{\s*(\S+?\.(?:png|jpg|gif))/
203 file[:prepared] << "\n% |#{fni}|@|^|>>ok\n\n"
204 @code_flag=false
205 insert_array.each do |i|
206 @code_flag=if i =~/^code\{/ then true
207 elsif i =~/^\}code/ then false
208 else @code_flag
209 end
210 if not @code_flag \
211 and i !~/^%+\s/
212 i=i.
213 gsub(/^([123]|:?[ABCD])~\? /,
214 '% [conditional heading:] \1~ ') #off conditional heading (consider syntax)
215 if i =~/^@\S+?:/
216 i=i.gsub(/\n/m,"\n% ").
217 gsub(/\n%\s+$/m,'').
218 gsub(/^@\S+?:/m,"\n% [imported header:] ") #off imported headers
219 end
220 end
221 file[:prepared] << i
222 if i !~/^%+\s/ \
223 and i =~rgx_image
224 file[:images] << i.scan(rgx_image).uniq
225 end
226 end
227 file[:prepared] << "\n% end import" << "\n\n"
228 if file[:images].length > 0
229 file[:images]=file[:images].flatten.uniq
230 file[:images].delete_if {|x| x =~/https?:\/\// }
231 end
232 file
233 end
234 end
235 class CompositeFileList
236 def initialize(opt)
237 @opt=opt
238 @env=SiSU_Env::InfoEnv.new
239 end
240 def read
241 begin
242 @opt.fns=@opt.fns.gsub(/\.ssm\.sst$/,'.ssm') #FIX earlier, hub
243 fns_array=IO.readlines(@opt.fns,'')
244 insertions?(fns_array)
245 rescue
246 SiSU_Errors::Rescued.new($!,$@,@opt.selections.str,@opt.fns).location do
247 __LINE__.to_s + ':' + __FILE__
248 end
249 ensure
250 end
251 end
252 def insertions?(fns_array)
253 tuned_file=[]
254 SiSU_Screen::Ansi.new(
255 @opt.act[:color_state][:set],
256 'Composite Document',
257 @opt.fno
258 ).grey_title_hi unless @opt.act[:quiet][:set]==:on
259 @ssm=[@opt.fns]
260 fns_array.each do |para|
261 if para =~/^<<\s+(\S+?\.ss[it])$/
262 loadfilename=$1.strip
263 if (@opt.act[:verbose][:set]==:on \
264 || @opt.act[:verbose_plus][:set]==:on \
265 || @opt.act[:maintenance][:set]==:on)
266 SiSU_Screen::Ansi.new(
267 @opt.act[:color_state][:set],
268 'loading:',
269 loadfilename,
270 ).txt_grey
271 end
272 tuned_file << if loadfilename =~ /(?:https?|file):\/\/\S+?\.ss[it]$/
273 @ssm << loadfilename
274 elsif loadfilename =~ /\.ss[it]$/ \
275 and FileTest.file?(loadfilename)
276 @ssm << loadfilename
277 else
278 STDERR.puts %{SKIPPED processing file: [#{@opt.lng}] "#{@opt.fns}" it requires an invalid or non-existent file: "#{loadfilename}"}
279 $process_document = :skip; break #remove this line to continue processing documents that have missing include files
280 para
281 end
282 end
283 end
284 @ssm
285 end
286 end
287 end
288 __END__