c&d: po4a (& git) revisited, revisit
[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,process=:complete)
87 @opt,@fnx,@process=opt,fnx,process
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 fnp = @fnx ? "#{@opt.fno} #{@fnx}" : @opt.fno
277 unless @opt.act[:quiet][:set]==:on
278 tell=(@opt.act[:verbose][:set]==:on \
279 || @opt.act[:verbose_plus][:set]==:on \
280 || @opt.act[:maintenance][:set]==:on) \
281 ? SiSU_Screen::Ansi.new(
282 @opt.act[:color_state][:set],
283 'Document Abstraction'
284 )
285 : SiSU_Screen::Ansi.new(
286 @opt.act[:color_state][:set],
287 'Document Abstraction',
288 "[#{@opt.f_pth[:lng_is]}] #{fnp}"
289 )
290 tell.blue_title_hi
291 end
292 fn=(@fnx && @fnx =~/\.ss[tmi]$/) \
293 ? @fnx
294 : @opt.fns
295 if @opt.fno =~/\.txz$/
296 Dir.chdir(@opt.f_pth[:pth])
297 end
298 meta=file_array=@env.source_file_processing_array(fn)
299 @md=SiSU_Param::Parameters::Instructions.new(meta,@opt).extract
300 meta=nil
301 ao=SiSU_AO::Make.new(fn,@md,file_array,@fnx,@process).song
302 if (@opt.act[:verbose][:set]==:on \
303 || @opt.act[:verbose_plus][:set]==:on \
304 || @opt.act[:maintenance][:set]==:on)
305 cf=SiSU_Env::CreateFile.new(fn)
306 if (@opt.act[:verbose][:set]==:on \
307 || @opt.act[:verbose_plus][:set]==:on)
308 SiSU_Screen::Ansi.new(
309 @opt.act[:color_state][:set],
310 @opt.fns,
311 "~meta/#{@opt.fns}.meta"
312 ).output
313 elsif @opt.act[:maintenance][:set]==:on
314 SiSU_Screen::Ansi.new(
315 @opt.act[:color_state][:set],
316 "ao -> #{cf.meta}"
317 ).txt_grey
318 end
319 end
320 ao.each {|s| ao_array << s}
321 if @opt.act[:maintenance][:set]==:on
322 ao_array.each do |obj|
323 if defined? obj.parent
324 if defined? obj.ln
325 if defined? obj.node
326 puts %{#{obj.ln}: #{obj.ocn} : #{obj.parent} : #{obj.node} - #{obj.lc}}
327 else
328 puts %{#{obj.ln}: #{obj.ocn} : #{obj.parent}}
329 end
330 else
331 if defined? obj.node
332 puts %{ #{obj.ocn} : #{obj.parent} : #{obj.node} - #{obj.lc}}
333 else
334 puts %{ #{obj.ocn} : #{obj.parent}}
335 end
336 end
337 end
338 end
339 end
340 ao_array
341 end
342 def read_fnm
343 ao=[]
344 ao=(FileTest.file?(@fnm)) \
345 ? (File.open(@fnm,'r:utf-8'){ |f| ao=Marshal.load(f)})
346 : SiSU_AO::Source.new(@opt).create_ao
347 end
348 def read_fnc
349 ao=[]
350 ao=(FileTest.file?(@fnc)) \
351 ? (File.open(@fnc,'r:utf-8'){ |f| ao=Marshal.load(f)})
352 : SiSU_AO::Source.new(@opt,@fnx,@process).create_ao
353 end
354 def read_idx_sst
355 m=[]
356 m=(FileTest.file?(@idx_sst)) \
357 ? (File.open(@idx_sst,'r:utf-8'){ |f| m=Marshal.load(f)})
358 : nil
359 end
360 def read_idx_raw
361 m=[]
362 m=(FileTest.file?(@idx_raw)) \
363 ? (File.open(@idx_raw,'r:utf-8'){ |f| m=Marshal.load(f)})
364 : nil
365 end
366 def read_idx_html
367 m=[]
368 m=(FileTest.file?(@idx_html)) \
369 ? (File.open(@idx_html,'r:utf-8'){ |f| m=Marshal.load(f)})
370 : nil
371 end
372 def read_idx_xhtml
373 m=[]
374 m=(FileTest.file?(@idx_xhtml)) \
375 ? (File.open(@idx_xhtml,'r:utf-8'){ |f| m=Marshal.load(f)})
376 : nil
377 end
378 def read_map_nametags
379 m=[]
380 m=(FileTest.file?(@map_nametags)) \
381 ? (File.open(@map_nametags,'r:utf-8'){ |f| m=Marshal.load(f)})
382 : nil
383 end
384 def read_map_ocn_htmlseg
385 m=[]
386 m=(FileTest.file?(@map_ocn_htmlseg)) \
387 ? (File.open(@map_ocn_htmlseg,'r:utf-8'){ |f| m=Marshal.load(f)})
388 : nil
389 end
390 end
391 class Output
392 def initialize(fn,md,data)
393 @fn,@md,@data=fn,md,data
394 @cf=SiSU_Env::CreateFile.new(@fn)
395 @make=SiSU_Env::InfoFile.new(@fn)
396 @dir=SiSU_Env::InfoEnv.new(@fn)
397 end
398 def screen_dump(o)
399 if defined? o.of
400 print %{OF: #{o.of}; }
401 end
402 if defined? o.is
403 print %{IS: #{o.is.to_s}; }
404 end
405 if defined? o.ocn
406 print %{OCN: #{o.ocn}; }
407 end
408 if defined? o.node
409 print %{NODE: #{o.node}; }
410 end
411 if defined? o.parent
412 print %{Parent: #{o.parent}; }
413 end
414 if defined? o.obj and not o.obj.empty?
415 puts %{\n#{o.obj}; }
416 else "\n"
417 end
418 end
419 def screen_print(t_o)
420 if defined? t_o
421 print ' ' + t_o.to_s
422 end
423 end
424 def screen_output(data)
425 data.each do |o|
426 print o.class
427 screen_print(o.ocn)
428 screen_print(o.obj)
429 puts "\n"
430 end
431 end
432 def hard_output
433 if @md.opt.act[:maintenance][:set]==:on
434 filename_meta=@cf.metaverse.file_meta
435 @data.each {|o| filename_meta.puts o.inspect.sub(/:0x[0-9a-f]{8}\s/,': ')} #to make diffing easier
436 filename_txt=@cf.metaverse.file_txt
437 @data.each do |o|
438 if defined? o.ocn
439 filename_txt.puts case o.is
440 when :heading
441 "[#{o.is.to_s} #{o.lv}~#{o.name} [#{o.ocn}]] #{o.obj}"
442 else "[#{o.is.to_s} [#{o.ocn}]] #{o.obj}"
443 end
444 else
445 filename_txt.puts case o.is
446 when :meta
447 "[m~#{o.tag}] #{o.obj}"
448 else "[#{o.is.to_s}] #{o.obj}"
449 end
450 end
451 end
452 filename_debug=@cf.file_debug
453 @data.each do |o|
454 if defined? o.ocn
455 case o.is
456 when :heading
457 filename_debug.puts
458 "#{o.is.to_s} #{o.lv}~#{o.name} odv=#{o.odv} osp=#{o.osp} [#{o.ocn}] -->\n\t#{o.obj}"
459 end
460 end
461 end
462 else
463 hard="#{@dir.processing_path.ao}/#{@md.fns}.meta"
464 File.unlink(hard) if FileTest.file?(hard)
465 hard="#{@dir.processing_path.ao}/#{@md.fns}.txt"
466 File.unlink(hard) if FileTest.file?(hard)
467 hard="#{@dir.processing_path.ao}/#{@md.fns}.debug.txt"
468 File.unlink(hard) if FileTest.file?(hard)
469 end
470 end
471 def make_marshal_content
472 marshal_ao=@make.marshal.ao_content
473 File.open(marshal_ao,'w'){|f| Marshal.dump(@data,f)} if @data.is_a?(Array)
474 end
475 def make_marshal_metadata
476 marshal_ao=@make.marshal.ao_metadata
477 File.open(marshal_ao,'w'){|f| Marshal.dump(@data,f)} if @data.is_a?(Array)
478 end
479 def idx_html_hard_output
480 if @md.book_idx \
481 and @md.opt.act[:maintenance][:set]==:on
482 filename_meta=@cf.file_meta_idx_html
483 if @data.is_a?(Array)
484 @data.each {|s| p s.inspect + "\n" unless s.is_a?(String)}
485 @data.each {|s| filename_meta.puts s.strip + "\n" unless s.strip.empty?}
486 end
487 else
488 hard_idx_html="#{@dir.processing_path.ao}/#{@md.fns}.idx.html"
489 File.unlink(hard_idx_html) if FileTest.file?(hard_idx_html)
490 end
491 end
492 def make_marshal_idx_sst_html_seg
493 marshal_ao=@make.marshal.ao_idx_sst_rel_html_seg
494 File.open(marshal_ao,'w'){|f| Marshal.dump(@data,f)} \
495 if @data.is_a?(Array)
496 end
497 def make_marshal_idx_sst_rel
498 marshal_ao=@make.marshal.ao_idx_sst_rel
499 File.open(marshal_ao,'w'){|f| Marshal.dump(@data,f)} \
500 if @data.is_a?(Array)
501 end
502 def make_marshal_idx_html
503 marshal_ao=@make.marshal.ao_idx_html
504 File.open(marshal_ao,'w'){|f| Marshal.dump(@data,f)} \
505 if @data.is_a?(Array)
506 end
507 def make_marshal_idx_xhtml
508 marshal_ao=@make.marshal.ao_idx_xhtml
509 File.open(marshal_ao,'w'){|f| Marshal.dump(@data,f)} \
510 if @data.is_a?(Array)
511 end
512 def make_marshal_map_nametags
513 marshal_ao=@make.marshal.ao_map_nametags
514 File.open(marshal_ao,'w'){|f| Marshal.dump(@data,f)} \
515 if @data.is_a?(Hash)
516 end
517 def make_marshal_map_name_ocn_htmlseg
518 marshal_ao=@make.marshal.ao_map_ocn_htmlseg
519 File.open(marshal_ao,'w'){|f| Marshal.dump(@data,f)} \
520 if @data.is_a?(Hash)
521 end
522 end
523 class Make
524 def initialize(fn,md,data,fnx,process)
525 @fn,@md,@data,@fnx,@process=fn,md,data,fnx,process
526 @env=SiSU_Env::InfoEnv.new(@md.fns)
527 end
528 def reset
529 @@flag_vocab=0
530 @@line_mode=''
531 end
532 def song
533 reset
534 data_txt=@data
535 data_txt=
536 SiSU_AO_Insertions::Insertions.new(@md,data_txt). # ao_expand_insertions.rb
537 expand_insertions?
538 data_txt=
539 SiSU_AO_MiscArrangeText::SI.new(@md,data_txt). # ao_misc_arrange.rb
540 prepare_text
541 data_obj,
542 metadata=
543 SiSU_AO_DocumentStructureExtract::Build.new(@md,data_txt). # ao_doc_str.rb
544 identify_parts
545 data_obj=
546 SiSU_AO_Syntax::Markup.new(@md,data_obj).songsheet # ao_syntax.rb
547 data_obj,
548 endnote_array=
549 SiSU_AO_CharacterCheck::Check.new(data_obj). # ao_character_check.rb
550 character_check_and_oldstyle_endnote_array
551 data_obj=
552 SiSU_AO_Images::Images.new(@md,data_obj).images # ao_images.rb
553 data_obj,
554 tags_map,
555 ocn_html_seg_map=
556 SiSU_AO_Numbering::Numbering.new(@md,data_obj,@fnx,@process). # ao_numbering.rb
557 numbering_song
558 data_obj,
559 book_index_rel,
560 book_index_rel_html_seg,
561 html_idx,xhtml_idx=
562 SiSU_AO_BookIndex::BookIndex.new(@md,data_obj,@env). # ao_idx.rb
563 indexing_song if @md.book_idx
564 data_obj=
565 SiSU_AO_Endnotes::Endnotes.new(@md,data_obj,endnote_array). # ao_endnotes.rb
566 endnotes
567 outputdata=data_obj
568 if (@md.opt.act[:ao][:set]==:on \
569 || @md.opt.act[:maintenance][:set]==:on)
570 SiSU_AO::Output.new(@fn,@md,outputdata).hard_output
571 SiSU_AO::Output.new(@fn,@md,outputdata).make_marshal_content
572 SiSU_AO::Output.new(@fn,@md,metadata).make_marshal_metadata
573 SiSU_AO::Output.new(@fn,@md,html_idx).idx_html_hard_output
574 SiSU_AO::Output.new(@fn,@md,book_index_rel_html_seg).make_marshal_idx_sst_html_seg
575 SiSU_AO::Output.new(@fn,@md,book_index_rel).make_marshal_idx_sst_rel
576 SiSU_AO::Output.new(@fn,@md,html_idx).make_marshal_idx_html
577 SiSU_AO::Output.new(@fn,@md,xhtml_idx).make_marshal_idx_xhtml
578 SiSU_AO::Output.new(@fn,@md,tags_map).make_marshal_map_nametags
579 SiSU_AO::Output.new(@fn,@md,ocn_html_seg_map).make_marshal_map_name_ocn_htmlseg
580 end
581 reset
582 outputdata
583 end
584 protected
585 end
586 end
587 __END__