c&d: small fixes
[software/sisu] / lib / sisu / develop / ao.rb
1 # encoding: utf-8
2 =begin
3
4 * Name: SiSU
5
6 ** Description: documents, structuring, processing, publishing, search
7 *** preprocessing, (document abstraction), data abstraction used in subsequent
8 processing
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.rb;hb=HEAD>
56
57 =end
58 module SiSU_AO
59 require_relative 'se' # se.rb
60 include SiSU_Env
61 require_relative 'dp' # dp.rb
62 include SiSU_Param
63 require_relative 'ao_doc_objects' # ao.rb
64 require_relative 'ao_syntax' # ao_syntax.rb
65 include SiSU_AO_Syntax
66 require_relative 'ao_doc_str' # ao_doc_str.rb
67 require_relative 'ao_idx' # ao_idx.rb
68 require_relative 'ao_numbering' # ao_numbering.rb
69 require_relative 'ao_hash_digest' # ao_hash_digest.rb
70 require_relative 'ao_endnotes' # ao_endnotes.rb
71 require_relative 'ao_images' # ao_images.rb
72 require_relative 'ao_metadata' # ao_metadata.rb
73 require_relative 'ao_character_check' # ao_character_check.rb
74 require_relative 'ao_misc_arrange' # ao_misc_arrange.rb
75 require_relative 'ao_expand_insertions' # ao_expand_insertions.rb
76 require_relative 'ao_persist' # ao_persist.rb
77 require_relative 'prog_text_translation' # prog_text_translation.rb
78 require_relative 'shared_sem' # shared_sem.rb
79 class Instantiate < SiSU_Param::Parameters::Instructions
80 def initialize
81 @@flag_vocab=0
82 @@line_mode=''
83 end
84 end
85 class Source <Instantiate
86 def initialize(opt,fnx=nil)
87 @opt,@fnx=opt,fnx
88 @per ||=SiSU_AO_Persist::Persist.new.persist_init
89 @per.fns ||=opt.fns
90 fn_use=if fnx \
91 and fnx =~/\.ss[tmi]$/
92 fnx
93 elsif opt.fns =~/\.ssm$/
94 opt.fns + '.sst'
95 else
96 opt.fns
97 end
98 @make_fns=SiSU_Env::InfoFile.new(fn_use)
99 @fnm=@make_fns.marshal.ao_metadata
100 @fnc=@make_fns.marshal.ao_content
101 @idx_sst=@make_fns.marshal.ao_idx_sst_rel_html_seg
102 @idx_raw=@make_fns.marshal.ao_idx_sst_rel
103 @idx_html=@make_fns.marshal.ao_idx_html
104 @idx_xhtml=@make_fns.marshal.ao_idx_xhtml
105 @map_nametags=@make_fns.marshal.ao_map_nametags
106 @map_ocn_htmlseg=@make_fns.marshal.ao_map_ocn_htmlseg
107 @env=SiSU_Env::InfoEnv.new
108 end
109 def read #creates ao
110 begin
111 @per=SiSU_AO_Persist::Persist.new
112 @per.ao_arr=[]
113 @per.fns=(@fnx && @fnx =~/\.ss[tmi]$/) \
114 ? @fnx
115 : @opt.fns
116 create_ao
117 rescue
118 SiSU_Errors::Rescued.new($!,$@,@opt.selections,@per.fns).location do
119 __LINE__.to_s + ':' + __FILE__
120 end
121 ensure
122 SiSU_AO_Persist::Persist.new.persist_init
123 SiSU_AO::Instantiate.new
124 end
125 end
126 def get #reads ao, unless does not exist then creates first
127 begin
128 ao=[]
129 unless @per.fns==@opt.fns \
130 or @per.fns==@fnx
131 @per.fns=(@fnx && @fnx =~/\.ss[tmi]$/) \
132 ? @fnx
133 : @opt.fns
134 @per.ao_arr=[]
135 end
136 ao=(@per.ao_arr.empty?) \
137 ? read_fnc
138 : @per.ao_arr.dup
139 rescue
140 SiSU_Errors::Rescued.new($!,$@,@opt.selections,@opt.fns).location do
141 __LINE__.to_s + ':' + __FILE__
142 end
143 ensure
144 SiSU_AO::Instantiate.new
145 end
146 end
147 def get_idx_sst #reads ao idx.sst, #unless does not exist then creates first
148 begin
149 ao=[]
150 unless @per.fns==@opt.fns \
151 or @per.fns==@fnx
152 @per.fns=(@fnx && @fnx =~/\.ss[tmi]$/) \
153 ? @fnx
154 : @opt.fns
155 @per.idx_arr_sst=[]
156 end
157 ao=(@per.idx_arr_sst.empty?) \
158 ? read_idx_sst
159 : @per.idx_arr_sst.dup #check
160 rescue
161 SiSU_Errors::Rescued.new($!,$@,@opt.selections,@opt.fns).location do
162 __LINE__.to_s + ':' + __FILE__
163 end
164 ensure
165 SiSU_AO::Instantiate.new
166 end
167 end
168 def get_idx_raw
169 begin
170 ao=[]
171 unless @per.fns==@opt.fns \
172 or @per.fns==@fnx
173 @per.fns=(@fnx && @fnx =~/\.ss[tmi]$/) \
174 ? @fnx
175 : @opt.fns
176 @per.idx_arr_tex=[]
177 end
178 ao=(@per.idx_arr_tex.empty?) \
179 ? read_idx_raw
180 : @per.idx_arr_tex.dup #check
181 rescue
182 SiSU_Errors::Rescued.new($!,$@,@opt.selections,@opt.fns).location do
183 __LINE__.to_s + ':' + __FILE__
184 end
185 ensure
186 SiSU_AO::Instantiate.new
187 end
188 end
189 def get_idx_html #reads ao idx.html, #unless does not exist then creates first
190 begin
191 ao=[]
192 unless @per.fns==@opt.fns \
193 or @per.fns==@fnx
194 @per.fns=(@fnx && @fnx =~/\.ss[tmi]$/) \
195 ? @fnx
196 : @opt.fns
197 @per.idx_arr_html=[]
198 end
199 ao=(@per.idx_arr_html.empty?) \
200 ? read_idx_html
201 : @per.idx_arr_html.dup
202 rescue
203 SiSU_Errors::Rescued.new($!,$@,@opt.selections,@opt.fns).location do
204 __LINE__.to_s + ':' + __FILE__
205 end
206 ensure
207 SiSU_AO::Instantiate.new
208 end
209 end
210 def get_idx_xhtml #reads ao idx.xhtml, #unless does not exist then creates first
211 begin
212 ao=[]
213 unless @per.fns==@opt.fns \
214 or @per.fns==@fnx
215 @per.fns=(@fnx && @fnx =~/\.ss[tmi]$/) \
216 ? @fnx
217 : @opt.fns
218 @per.idx_arr_xhtml=[] #...
219 end
220 ao=(@per.idx_arr_xhtml.empty?) \
221 ? read_idx_xhtml
222 : @per.idx_arr_xhtml.dup
223 rescue
224 SiSU_Errors::Rescued.new($!,$@,@opt.selections,@opt.fns).location do
225 __LINE__.to_s + ':' + __FILE__
226 end
227 ensure
228 SiSU_AO::Instantiate.new
229 end
230 end
231 def get_map_nametags #reads ao map.nametags, #unless does not exist then creates first
232 begin
233 ao=[]
234 unless @per.fns==@opt.fns \
235 or @per.fns==@fnx
236 @per.fns=(@fnx && @fnx =~/\.ss[tmi]$/) \
237 ? @fnx
238 : @opt.fns
239 @per.map_arr_nametags=[]
240 end
241 ao=(@per.map_arr_nametags.empty?) \
242 ? read_map_nametags
243 : @per.map_arr_nametags.dup
244 rescue
245 SiSU_Errors::Rescued.new($!,$@,@opt.selections,@opt.fns).location do
246 __LINE__.to_s + ':' + __FILE__
247 end
248 ensure
249 SiSU_AO::Instantiate.new
250 end
251 end
252 def get_map_ocn_htmlseg #reads ao map.ocn_htmlseg, #unless does not exist then creates first
253 begin
254 ao=[]
255 unless @per.fns==@opt.fns \
256 or @per.fns==@fnx
257 @per.fns=(@fnx && @fnx =~/\.ss[tmi]$/) \
258 ? @fnx
259 : @opt.fns
260 @per.map_arr_ocn_htmlseg=[]
261 end
262 ao=(@per.map_arr_ocn_htmlseg.empty?) \
263 ? read_map_ocn_htmlseg
264 : @per.map_arr_ocn_htmlseg.dup
265 rescue
266 SiSU_Errors::Rescued.new($!,$@,@opt.selections,@opt.fns).location do
267 __LINE__.to_s + ':' + __FILE__
268 end
269 ensure
270 SiSU_AO::Instantiate.new
271 end
272 end
273 protected
274 def create_ao
275 ao_array=[]
276 unless @opt.act[:quiet][:set]==:on
277 tell=(@opt.act[:verbose][:set]==:on \
278 || @opt.act[:verbose_plus][:set]==:on \
279 || @opt.act[:maintenance][:set]==:on) \
280 ? SiSU_Screen::Ansi.new(
281 @opt.act[:color_state][:set],
282 'Document Abstraction'
283 )
284 : SiSU_Screen::Ansi.new(
285 @opt.act[:color_state][:set],
286 'Document Abstraction',"[#{@opt.f_pth[:lng_is]}] #{@opt.fno}"
287 )
288 tell.blue_title_hi
289 end
290 fn=(@fnx && @fnx =~/\.ss[tmi]$/) \
291 ? @fnx
292 : @opt.fns
293 if @opt.fno =~/\.txz$/
294 Dir.chdir(@opt.f_pth[:pth])
295 end
296 meta=file_array=@env.source_file_processing_array(fn)
297 @md=SiSU_Param::Parameters::Instructions.new(meta,@opt).extract
298 meta=nil
299 ao=SiSU_AO::Make.new(fn,@md,file_array).song
300 if (@opt.act[:verbose][:set]==:on \
301 || @opt.act[:verbose_plus][:set]==:on \
302 || @opt.act[:maintenance][:set]==:on)
303 cf=SiSU_Env::CreateFile.new(fn)
304 if (@opt.act[:verbose][:set]==:on \
305 || @opt.act[:verbose_plus][:set]==:on)
306 SiSU_Screen::Ansi.new(
307 @opt.act[:color_state][:set],
308 @opt.fns,
309 "~meta/#{@opt.fns}.meta"
310 ).output
311 elsif @opt.act[:maintenance][:set]==:on
312 SiSU_Screen::Ansi.new(
313 @opt.act[:color_state][:set],
314 "ao -> #{cf.meta}"
315 ).txt_grey
316 end
317 end
318 ao.each {|s| ao_array << s}
319 if @opt.act[:maintenance][:set]==:on
320 ao_array.each do |obj|
321 if defined? obj.parent
322 if defined? obj.ln
323 if defined? obj.node
324 puts %{#{obj.ln}: #{obj.ocn} : #{obj.parent} : #{obj.node} - #{obj.lc}}
325 else
326 puts %{#{obj.ln}: #{obj.ocn} : #{obj.parent}}
327 end
328 else
329 if defined? obj.node
330 puts %{ #{obj.ocn} : #{obj.parent} : #{obj.node} - #{obj.lc}}
331 else
332 puts %{ #{obj.ocn} : #{obj.parent}}
333 end
334 end
335 end
336 end
337 end
338 ao_array
339 end
340 def read_fnm
341 ao=[]
342 ao=(FileTest.file?(@fnm)) \
343 ? (File.open(@fnm,'r:utf-8'){ |f| ao=Marshal.load(f)})
344 : SiSU_AO::Source.new(@opt).create_ao
345 end
346 def read_fnc
347 ao=[]
348 ao=(FileTest.file?(@fnc)) \
349 ? (File.open(@fnc,'r:utf-8'){ |f| ao=Marshal.load(f)})
350 : SiSU_AO::Source.new(@opt).create_ao
351 end
352 def read_idx_sst
353 m=[]
354 m=(FileTest.file?(@idx_sst)) \
355 ? (File.open(@idx_sst,'r:utf-8'){ |f| m=Marshal.load(f)})
356 : nil
357 end
358 def read_idx_raw
359 m=[]
360 m=(FileTest.file?(@idx_raw)) \
361 ? (File.open(@idx_raw,'r:utf-8'){ |f| m=Marshal.load(f)})
362 : nil
363 end
364 def read_idx_html
365 m=[]
366 m=(FileTest.file?(@idx_html)) \
367 ? (File.open(@idx_html,'r:utf-8'){ |f| m=Marshal.load(f)})
368 : nil
369 end
370 def read_idx_xhtml
371 m=[]
372 m=(FileTest.file?(@idx_xhtml)) \
373 ? (File.open(@idx_xhtml,'r:utf-8'){ |f| m=Marshal.load(f)})
374 : nil
375 end
376 def read_map_nametags
377 m=[]
378 m=(FileTest.file?(@map_nametags)) \
379 ? (File.open(@map_nametags,'r:utf-8'){ |f| m=Marshal.load(f)})
380 : nil
381 end
382 def read_map_ocn_htmlseg
383 m=[]
384 m=(FileTest.file?(@map_ocn_htmlseg)) \
385 ? (File.open(@map_ocn_htmlseg,'r:utf-8'){ |f| m=Marshal.load(f)})
386 : nil
387 end
388 end
389 class Output
390 def initialize(fn,md,data)
391 @fn,@md,@data=fn,md,data
392 @cf=SiSU_Env::CreateFile.new(@fn)
393 @make=SiSU_Env::InfoFile.new(@fn)
394 @dir=SiSU_Env::InfoEnv.new(@fn)
395 end
396 def screen_dump(o)
397 if defined? o.of
398 print %{OF: #{o.of}; }
399 end
400 if defined? o.is
401 print %{IS: #{o.is.to_s}; }
402 end
403 if defined? o.ocn
404 print %{OCN: #{o.ocn}; }
405 end
406 if defined? o.node
407 print %{NODE: #{o.node}; }
408 end
409 if defined? o.parent
410 print %{Parent: #{o.parent}; }
411 end
412 if defined? o.obj and not o.obj.empty?
413 puts %{\n#{o.obj}; }
414 else "\n"
415 end
416 end
417 def screen_print(t_o)
418 if defined? t_o
419 print ' ' + t_o.to_s
420 end
421 end
422 def screen_output(data)
423 data.each do |o|
424 print o.class
425 screen_print(o.ocn)
426 screen_print(o.obj)
427 puts "\n"
428 end
429 end
430 def hard_output
431 if @md.opt.act[:maintenance][:set]==:on
432 filename_meta=@cf.metaverse.file_meta
433 @data.each {|o| filename_meta.puts o.inspect.sub(/:0x[0-9a-f]{8}\s/,': ')} #to make diffing easier
434 filename_txt=@cf.metaverse.file_txt
435 @data.each do |o|
436 if defined? o.ocn
437 filename_txt.puts case o.is
438 when :heading
439 "[#{o.is.to_s} #{o.lv}~#{o.name} [#{o.ocn}]] #{o.obj}"
440 else "[#{o.is.to_s} [#{o.ocn}]] #{o.obj}"
441 end
442 else
443 filename_txt.puts case o.is
444 when :meta
445 "[m~#{o.tag}] #{o.obj}"
446 else "[#{o.is.to_s}] #{o.obj}"
447 end
448 end
449 end
450 filename_debug=@cf.file_debug
451 @data.each do |o|
452 if defined? o.ocn
453 case o.is
454 when :heading
455 filename_debug.puts
456 "#{o.is.to_s} #{o.lv}~#{o.name} odv=#{o.odv} osp=#{o.osp} [#{o.ocn}] -->\n\t#{o.obj}"
457 end
458 end
459 end
460 else
461 hard="#{@dir.processing_path.ao}/#{@md.fns}.meta"
462 File.unlink(hard) if FileTest.file?(hard)
463 hard="#{@dir.processing_path.ao}/#{@md.fns}.txt"
464 File.unlink(hard) if FileTest.file?(hard)
465 hard="#{@dir.processing_path.ao}/#{@md.fns}.debug.txt"
466 File.unlink(hard) if FileTest.file?(hard)
467 end
468 end
469 def make_marshal_content
470 marshal_ao=@make.marshal.ao_content
471 File.open(marshal_ao,'w'){|f| Marshal.dump(@data,f)} if @data.is_a?(Array)
472 end
473 def make_marshal_metadata
474 marshal_ao=@make.marshal.ao_metadata
475 File.open(marshal_ao,'w'){|f| Marshal.dump(@data,f)} if @data.is_a?(Array)
476 end
477 def idx_html_hard_output
478 if @md.book_idx \
479 and @md.opt.act[:maintenance][:set]==:on
480 filename_meta=@cf.file_meta_idx_html
481 if @data.is_a?(Array)
482 @data.each {|s| p s.inspect + "\n" unless s.is_a?(String)}
483 @data.each {|s| filename_meta.puts s.strip + "\n" unless s.strip.empty?}
484 end
485 else
486 hard_idx_html="#{@dir.processing_path.ao}/#{@md.fns}.idx.html"
487 File.unlink(hard_idx_html) if FileTest.file?(hard_idx_html)
488 end
489 end
490 def make_marshal_idx_sst_html_seg
491 marshal_ao=@make.marshal.ao_idx_sst_rel_html_seg
492 File.open(marshal_ao,'w'){|f| Marshal.dump(@data,f)} \
493 if @data.is_a?(Array)
494 end
495 def make_marshal_idx_sst_rel
496 marshal_ao=@make.marshal.ao_idx_sst_rel
497 File.open(marshal_ao,'w'){|f| Marshal.dump(@data,f)} \
498 if @data.is_a?(Array)
499 end
500 def make_marshal_idx_html
501 marshal_ao=@make.marshal.ao_idx_html
502 File.open(marshal_ao,'w'){|f| Marshal.dump(@data,f)} \
503 if @data.is_a?(Array)
504 end
505 def make_marshal_idx_xhtml
506 marshal_ao=@make.marshal.ao_idx_xhtml
507 File.open(marshal_ao,'w'){|f| Marshal.dump(@data,f)} \
508 if @data.is_a?(Array)
509 end
510 def make_marshal_map_nametags
511 marshal_ao=@make.marshal.ao_map_nametags
512 File.open(marshal_ao,'w'){|f| Marshal.dump(@data,f)} \
513 if @data.is_a?(Hash)
514 end
515 def make_marshal_map_name_ocn_htmlseg
516 marshal_ao=@make.marshal.ao_map_ocn_htmlseg
517 File.open(marshal_ao,'w'){|f| Marshal.dump(@data,f)} \
518 if @data.is_a?(Hash)
519 end
520 end
521 class Make
522 def initialize(fn,md,data)
523 @fn,@md,@data=fn,md,data
524 @env=SiSU_Env::InfoEnv.new(@md.fns)
525 end
526 def reset
527 @@flag_vocab=0
528 @@line_mode=''
529 end
530 def song
531 reset
532 data_txt=@data
533 data_txt=
534 SiSU_AO_Insertions::Insertions.new(@md,data_txt). # ao_expand_insertions.rb
535 expand_insertions?
536 data_txt=
537 SiSU_AO_MiscArrangeText::SI.new(@md,data_txt). # ao_misc_arrange.rb
538 prepare_text
539 data_obj,
540 metadata=
541 SiSU_AO_DocumentStructureExtract::Build.new(@md,data_txt). # ao_doc_str.rb
542 identify_parts
543 data_obj=
544 SiSU_AO_Syntax::Markup.new(@md,data_obj).songsheet # ao_syntax.rb
545 data_obj,
546 endnote_array=
547 SiSU_AO_CharacterCheck::Check.new(data_obj). # ao_character_check.rb
548 character_check_and_oldstyle_endnote_array
549 data_obj=
550 SiSU_AO_Images::Images.new(@md,data_obj).images # ao_images.rb
551 data_obj,
552 tags_map,
553 ocn_html_seg_map=
554 SiSU_AO_Numbering::Numbering.new(@md,data_obj). # ao_numbering.rb
555 numbering_song
556 data_obj,
557 book_index_rel,
558 book_index_rel_html_seg,
559 html_idx,xhtml_idx=
560 SiSU_AO_BookIndex::BookIndex.new(@md,data_obj,@env). # ao_idx.rb
561 indexing_song if @md.book_idx
562 data_obj=
563 SiSU_AO_Endnotes::Endnotes.new(@md,data_obj,endnote_array). # ao_endnotes.rb
564 endnotes
565 outputdata=data_obj
566 if (@md.opt.act[:ao][:set]==:on \
567 || @md.opt.act[:maintenance][:set]==:on)
568 SiSU_AO::Output.new(@fn,@md,outputdata).hard_output
569 SiSU_AO::Output.new(@fn,@md,outputdata).make_marshal_content
570 SiSU_AO::Output.new(@fn,@md,metadata).make_marshal_metadata
571 SiSU_AO::Output.new(@fn,@md,html_idx).idx_html_hard_output
572 SiSU_AO::Output.new(@fn,@md,book_index_rel_html_seg).make_marshal_idx_sst_html_seg
573 SiSU_AO::Output.new(@fn,@md,book_index_rel).make_marshal_idx_sst_rel
574 SiSU_AO::Output.new(@fn,@md,html_idx).make_marshal_idx_html
575 SiSU_AO::Output.new(@fn,@md,xhtml_idx).make_marshal_idx_xhtml
576 SiSU_AO::Output.new(@fn,@md,tags_map).make_marshal_map_nametags
577 SiSU_AO::Output.new(@fn,@md,ocn_html_seg_map).make_marshal_map_name_ocn_htmlseg
578 end
579 reset
580 outputdata
581 end
582 protected
583 end
584 end
585 __END__