aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/sisu/v6/ao_numbering.rb
blob: cd3f366a9c48f9dbc9c250beffcd155c5e6b8a2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
# encoding: utf-8
=begin

* Name: SiSU

** Description: documents, structuring, processing, publishing, search
*** system environment, resource control and configuration details

** Author: Ralph Amissah
  <ralph@amissah.com>
  <ralph.amissah@gmail.com>

** Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
  2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Ralph Amissah,
  All Rights Reserved.

** License: GPL 3 or later:

  SiSU, a framework for document structuring, publishing and search

  Copyright (C) Ralph Amissah

  This program is free software: you can redistribute it and/or modify it
  under the terms of the GNU General Public License as published by the Free
  Software Foundation, either version 3 of the License, or (at your option)
  any later version.

  This program is distributed in the hope that it will be useful, but WITHOUT
  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  more details.

  You should have received a copy of the GNU General Public License along with
  this program. If not, see <http://www.gnu.org/licenses/>.

  If you have Internet connection, the latest version of the GPL should be
  available at these locations:
  <http://www.fsf.org/licensing/licenses/gpl.html>
  <http://www.gnu.org/licenses/gpl.html>

  <http://www.sisudoc.org/sisu/en/manifest/gpl.fsf.html>

** SiSU uses:
  * Standard SiSU markup syntax,
  * Standard SiSU meta-markup syntax, and the
  * Standard SiSU object citation numbering and system

** Hompages:
  <http://www.jus.uio.no/sisu>
  <http://www.sisudoc.org>

** Git
  <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=summary>
  <http://git.sisudoc.org/gitweb/?p=code/sisu.git;a=blob;f=lib/sisu/v6/ao_numbering.rb;hb=HEAD>

=end
module SiSU_AO_Numbering
  class Numbering
    attr_accessor :obj,:osp,:ocn,:lv,:name,:index,:comment
    @@segments_count=0
    def initialize(md,data)
      @md,@data=md,data
      @obj=@type=@ocn=@lv=@name=@index=@comment=nil
      @chosen_seg_names=[]
    end
    def chosen_seg_names(chosen,chosen_seg_name,dob,md,type)
      @chosen_seg_names=if chosen.compact.uniq.length \
      == chosen.compact.length
        chosen
      else
        if md.opt.act[:maintenance][:set]==:on
          SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:green).
            mark(
              "duplicated auto segment name: #{type} #{chosen}\n" \
              + "#{chosen}\n" \
              + " manually name level 1 segments '1~given_name'\n" \
              + 'filename: ' + md.fns + "\n" \
              + 'heading text: "' + dob.obj + '"' + "\n" \
              + 'duplication: "' + chosen_seg_name + '" (level: ' + dob.lv + '; numbering type: ' + type.to_s + ')'
            )
        end
        chosen=chosen[0..-2]
        chosen_seg_name=auto_numbering_exceptions(chosen,md,dob)
        chosen << chosen_seg_name
      end
    end
    def number_of_segments?
      if @@segments_count==0
        @data.each do |dob|
          if dob.is ==  :heading \
          and dob.lv == '1'
            @@segments_count += 1
          end
        end
        @@segments_count
      else @@segments_count
      end
    end
    def numbering_song
      begin
        data=@data
        data=number_plaintext_para(data)
        data=auto_number_heading_ie_title(data.compact) #tr issue
        data=ocn(data.compact) #watch
        data=xml(data.compact)
        data=minor_numbering(data.compact)
        data,tags_map,ocn_html_seg_map=name_para_seg_filename(data)
        data=set_heading_top(data) unless @md.set_heading_top
        [data,tags_map,ocn_html_seg_map]
      ensure
        @@segments_count=0
      end
    end
    def set_tags(tags,tag)
      tags=if not tag.empty? \
      and tag !~/^\d+$/
        tag=tag.gsub(/[^a-z0-9._-]/,'')
        [tag,tags].flatten
      else tags
      end
    end
    def number_plaintext_para(data)
      @tuned_file=[]
      data.each do |dob|
        if (dob.of !=:block \
        && dob.of !=:comment \
        && dob.of !=:layout) \
        && dob.ocn_ #and dob.obj !~ /#{Mx[:gr_o]}Th|#{Mx[:tc_o]}#{Mx[:tc_p]}#{Mx[:tc_p]}/ #FIX
          dob.obj=dob.obj.gsub(/(.+)\n/,'\1 ') #messy, but idea is that tables should retain breaks
        end
        unless dob.obj.is_a?(Array)
          dob.obj=dob.obj.gsub(/^\s+/,'').
            gsub(/\s$/,"\n")
        end
        @tuned_file << dob
      end
      @tuned_file=@tuned_file.flatten
    end
    def number_sub_heading(dob,num,title_no)
      unless dob.obj =~/\d+\.|(?:chapter|article|section|clause)\s+\d+/i #name selection arbitrary, fix
        dob.obj=case dob.name
        when /-/          then dob.obj.gsub(/^/,"#{title_no} ")
        when /^#/         then dob.obj.gsub(/^/,"#{title_no} ")
        when /^[a-z_\.]+/ then dob.obj.gsub(/^/,"#{title_no} ")
        else
          dob.name=title_no if dob.name=~/^$/ #where title contains title number
          dob.obj.gsub(/^/,"#{title_no} ") if title_no =~/\d+/ #main, where title number is to be provided #watch changed placement
        end
        if @md.toc_lev_limit \
        and @md.toc_lev_limit < num
          dob.obj=dob.obj.gsub(/^/,'!_ ') #bold line, watch
        end
      end
      dob
    end
    def heading_tag_clean(heading_tag)
      heading_tag=heading_tag.
        gsub(/[ ]+/,'_').
        gsub(/["']/,'').
        gsub(/[\/]/,'-').
        gsub(/#{Mx[:fa_bold_o]}|#{Mx[:fa_bold_c]}/,'').
        gsub(/#{Mx[:fa_italics_o]}|#{Mx[:fa_italics_c]}/,'').
        gsub(/#{Mx[:fa_underscore_o]}|#{Mx[:fa_underscore_c]}/,'').
        gsub(/#{Mx[:fa_cite_o]}|#{Mx[:fa_cite_c]}/,'').
        gsub(/#{Mx[:fa_insert_o]}|#{Mx[:fa_insert_c]}/,'').
        gsub(/#{Mx[:fa_strike_o]}|#{Mx[:fa_strike_c]}/,'').
        gsub(/#{Mx[:fa_superscript_o]}|#{Mx[:fa_superscript_c]}/,'').
        gsub(/#{Mx[:fa_subscript_o]}|#{Mx[:fa_subscript_c]}/,'').
        gsub(/#{Mx[:fa_hilite_o]}|#{Mx[:fa_hilite_c]}/,'').
        gsub(/#{Mx[:gl_bullet]}/,'')
    end
    def auto_number_heading_ie_title(data)                                             #also does some segment naming
      @tuned_file=[]
      if defined? @md.make.num_top \
      and @md.make.num_top \
      and @md.make.num_top !~/^$/
        input||=@md.make.num_top
      end
      num_top=(input ? input.to_i : nil)
      t_no1=t_no2=t_no3=0
      if num_top
        no1=num_top; no2=(num_top + 1); no3=(num_top + 2)
      end
      chapter_number_counter=0
      data=data.compact
      data.each do |dob| #@md.seg_names << [additions to segment names]
        title_no=nil
        if dob.is ==:heading \
        && dob.autonum_ \
        and defined? @md.make.num_top \
        and @md.make.num_top !~/^$/
          if  dob.lv=='1' \
          and dob.obj =~/^#\s|\s#(?:\s|$)/
            chapter_number_counter +=1
            dob.obj=dob.obj.gsub(/^#\s/,"#{chapter_number_counter} ").
              gsub(/#([:,]?\s|[.]?$)/,"#{chapter_number_counter}\\1")
          end
          if dob.ln==no1
            @subnumber=1
            @subnumber=0 if dob.ln==no1
          end
          if dob.ln.to_s =~/^[0-6]/ \
          and not dob.use_ ==:dummy \
          and dob.obj !~/#{Mx[:fa_o]}(?:~#|-#)#{Mx[:fa_c]}/ # <-- fix
            if dob.ln==no1
              t_no1+=1; t_no2=0; t_no3=0
              title_no="#{t_no1}"
              if @md.seg_names.is_a?(Array) \
              and not @md.seg_names.include?(title_no)
                if dob.ln==no1
                  dob.name="#{title_no}" if not dob.name
                  dob.tags=set_tags(dob.tags,title_no)
                  tag=dob.obj.
                    gsub(/(Article|Clause|Section|Chapter)\s+/,
                      "\\1_#{title_no}").
                    downcase
                  tag=heading_tag_clean(tag)
                  dob.tags=set_tags(dob.tags,tag)
                  dob.obj=(dob.obj =~/(Article|Clause|Section)\s+/) \
                  ? (dob.obj.gsub(/(Article|Clause|Section)\s+/,"\\1 #{title_no} "))
                  : (dob.obj.gsub(/^/,"#{title_no}. ")) #fix stop later
                end
                if dob.ln !=no1 \
                and dob.obj =~/^[\d.]+\s/ #fix -> if the title starts with a numbering scheme, do not auto-number, review
                  dob.name ="#{title_no}" if not dob.name
                  dob.tags=set_tags(dob.tags,title_no)
                  dob.obj=dob.obj.gsub(/^/,"#{title_no}. ")
                end
                @md.seg_names << title_no
              end
              if dob.ln!=no1 \
              and dob.name!~/^[a-z_\.]+$/ \
              and dob.obj !~/[A-Z]\.?\s/ #bug -> tmp fix, excludes A. B. C. lettering, but not roman numerals, is arbitrary, review required # not fixed, work on
                dob.tags=set_tags(dob.tags,title_no)
                dob.obj=dob.obj.gsub(/^/i,"#{title_no}. ")
              end
            end
            if dob.ln==no1         #watch because here you change dob.name
              dob.tags=set_tags(dob.tags,"h#{title_no}")
            end
            if dob.ln==no2         #watch because here you change dob.name
              t_no2+=1; t_no3=0
              title_no="#{t_no1}.#{t_no2}"
              dob.tags=set_tags(dob.tags,"h#{title_no}")
              dob=number_sub_heading(dob,no2,title_no)
            end
            if dob.ln==no3         #watch because here you change dob.name
              t_no3+=1
              title_no="#{t_no1}.#{t_no2}.#{t_no3}"
              dob.tags=set_tags(dob.tags,"h#{title_no}")
              dob=number_sub_heading(dob,no3,title_no)
            end
          elsif dob.ln.to_s =~/^[0-6]/ \
          and dob.name =~ /^[\w-]+-/ # endnotes, watch2005# endnotes, watch2005
            dob.tags=set_tags(dob.tags,dob.name)
            dob.name.gsub(/^([a-z_\.]+)-$/,'\1')
          end
        elsif dob.is ==:heading \
        and dob.autonum_ \
        and @md.markup =~/num_extract/ #AS DANGEROUS force enable with document, note already does this type of numbering for cisg, locate and coordinate logic, is currently misplaced in code, chengwei inspired 2004w23/4
          #here lies a bug, as is nil when run from -Dv --update, FIX
          if (dob.name.nil? or dob.name.empty?) \
          and dob.ln.to_s =~/^[0-9]/ \
          and dob.obj =~ /^([\d\.]+)/ #risky (must be unique) consider output to 4~~\d instead of 4~\d
            dob.name=$1
            dob.tags=set_tags(dob.tags,dob.name)
          end
          if @md.toc_lev_limit
          end
        elsif defined? dob.name \
        and  dob.name
          dob.tags=set_tags(dob.tags,dob.name)
        end
        dob.tags=dob.tags.uniq if defined? dob.tags
        @tuned_file << dob
      end
      @tuned_file=@tuned_file.flatten
    end
    def ocn(data)                                                                      #and auto segment numbering increment
      @tuned_file=SiSU_AO_DocumentStructureExtract::OCN.new(@md,data).ocn
      @tuned_file
    end
    def xml(data)
      @tuned_file=SiSU_AO_DocumentStructureExtract::XML.new(@md,data).dom
      @tuned_file
    end
    def minor_numbering(data)                                                          #and auto segment numbering increment
      @tuned_file=[]
      number_small,letter_small=0,0
      letter=%w( a b c d e f g h i j k l m n o p q r s t u v w x y z )
      data.each do |dob|
        if dob.of ==:heading \
        || dob.of ==:heading_insert \
        || dob.of ==:para \
        || dob.of ==:block
          if dob.is ==:heading \
          and dob.ln.to_s=~/^[0-9]/                                                    #% sub-number system, (baby numbering) reset with any change of major number (more obviously should be placed in number titles, but that is conditionally executed, check and move later)
            number_small,letter_small=0,0
          elsif dob.is ==:para
            if dob.obj =~/^#[ 1]/ \
            and dob.obj !~/^#\s+(?:~#)?$/
              letter_small=0
              number_small=0 if dob.obj =~ /^#1/
              number_small+=1
              dob.obj=dob.obj.gsub(/^#[ 1]/,"#{number_small}. ")
            end
            if dob.obj =~/^_# /
              dob.obj=dob.obj.gsub(/^_# /,"#{letter[letter_small]}. ")
              dob.indent='1'
              letter_small+=1
            end
          end
        end
        @tuned_file << dob
      end
      @tuned_file=@tuned_file.flatten
    end
    def leading_zeros_fixed_width_number(possible_seg_name)
      if possible_seg_name.to_s =~/^([0-9]+?\.|[0-9]+)$/m       #!~/[.,:-]+/
        possible_seg_name=possible_seg_name.to_s.
          gsub(/\.$/,'')
        nl=possible_seg_name.to_s.length
        zero='0'
        zeros_fixed_width=number_of_segments?.to_s.length
        zero_width=(zeros_fixed_width - nl)
        zero_width == 0 \
        ? possible_seg_name.to_s
        : zero*zero_width +
          possible_seg_name.to_s
      end
    end
    def auto_numbering_exceptions(chosen_seg_names_,md,dob)
      number_make=case dob.lv.to_i
      when 1
        @num_exc={
          t1: @num_exc[:t1] += 1,
          t2: 0,
          t3: 0,
          t4: 0
        }
        Mx[:segname_prefix_auto_num_other] + '_' \
        + @num_exc[:t1].to_s
      when 2
        @num_exc={
          t1: @num_exc[:t1],
          t2: @num_exc[:t2] += 1,
          t3: 0,
          t4: 0
        }
        Mx[:segname_prefix_auto_num_other] + '_' \
        + @num_exc[:t1].to_s + '_' \
        + @num_exc[:t2].to_s
      when 3
        @num_exc={
          t1: @num_exc[:t1],
          t2: @num_exc[:t2],
          t3: @num_exc[:t3] += 1,
          t4: 0
        }
        Mx[:segname_prefix_auto_num_other] + '_' \
        + @num_exc[:t1].to_s + '_' \
        + @num_exc[:t2].to_s + '_' \
        + @num_exc[:t3].to_s
      when 4
        @num_exc[:t4] += 1
        @num_exc={
          t1: @num_exc[:t1],
          t2: @num_exc[:t2],
          t3: @num_exc[:t3],
          t4: @num_exc[:t4] += 1
        }
        Mx[:segname_prefix_auto_num_other] + '_' \
        + @num_exc[:t1].to_s + '_' \
        + @num_exc[:t2].to_s + '_' \
        + @num_exc[:t3].to_s + '_' \
        + @num_exc[:t4].to_s
      end
    end
    def check_that_seg_names_are_unique(chosen_seg_names_,chosen_seg_name,type,md,dob)
      begin
        chosen_seg_names_ << chosen_seg_name
        chosen_seg_names_=chosen_seg_names(chosen_seg_names_,chosen_seg_name,dob,md,type)
        if chosen_seg_names_.compact.uniq.length \
        == chosen_seg_names_.compact.length
          #check that all auto given seg names are unique
          chosen_seg_names_=chosen_seg_names(chosen_seg_names_,chosen_seg_name,dob,md,type)
          chosen_seg_name
        else
          SiSU_Utils::CodeMarker.new(__LINE__,__FILE__,:green).
            mark(
              "duplicated auto segment name: #{type} #{chosen_seg_name}\n" \
              + "#{chosen_seg_names_}\n" \
              + " manually name level 1 segments '1~given_name'\n" \
              + 'filename: ' + md.fns + "\n" \
              + 'heading text: "' + dob.obj + '"' + "\n" \
              + 'duplication: "' + chosen_seg_name + '" (level: ' + dob.lv + '; numbering type: ' + type.to_s + ')'
            )
          chosen_seg_name=auto_numbering_exceptions(chosen_seg_names_,md,dob)
          check_that_seg_names_are_unique(chosen_seg_names_,chosen_seg_name,:exception,md,dob)
        end
      rescue
      end
    end
    def auto_seg_name(possible_seg_name,heading_num_is,dob,type)
      prefix=case type
      when :auto    then Mx[:segname_prefix_auto_num_provide]
      when :extract then Mx[:segname_prefix_auto_num_extract]
      else               '_'*dob.lv.to_i #should not occur
      end
      if possible_seg_name =~/^[0-9]+?\.$/m                #!~/[.,:-]+/
        possible_seg_name=possible_seg_name.
          gsub(/\.$/,'')
      end
      @chosen_seg_name=
      if dob.lv=='4' \
      and possible_seg_name.to_s =~/^[0-9]+(?:[.,:-][0-9]){3}/m
        possible_seg_name=possible_seg_name.to_s.
          gsub(/(?:[:,-]|\W)/,'.').
          gsub(/\.$/,'')
        prefix + possible_seg_name
      elsif dob.lv=='3' \
      and possible_seg_name.to_s =~/^[0-9]+(?:[.,:-][0-9]){2}/m
        possible_seg_name=possible_seg_name.to_s.
          gsub(/(?:[:,-]|\W)/,'.').
          gsub(/\.$/,'')
        prefix + possible_seg_name
      elsif dob.lv=='2' \
      and possible_seg_name.to_s =~/^[0-9]+(?:[.,:-][0-9]){1}/m
        possible_seg_name=possible_seg_name.to_s.
          gsub(/(?:[:,-]|\W)/,'.').
          gsub(/\.$/,'')
        prefix + possible_seg_name
      elsif dob.lv=='1' \
      and possible_seg_name.to_s =~/^[0-9]+[:,-]?$/m
        if possible_seg_name.to_i <= heading_num_is.to_i
          prefix + leading_zeros_fixed_width_number(possible_seg_name)
        else
          possible_seg_name=possible_seg_name.to_s.
            gsub(/(?:[:,-]|\W)/,'.').
            gsub(/\.$/,'')
          prefix + possible_seg_name
        end
      else
        @chosen_seg_name=auto_numbering_exceptions(@chosen_seg_names,md,dob)
      end
      check_that_seg_names_are_unique(@chosen_seg_names,@chosen_seg_name,type,@md,dob)
    end
    def set_name_and_tags(dob,possible_seg_name)
      if @md.seg_names.is_a?(Array) \
      and not @md.seg_names.include?(possible_seg_name)
        dob.name=possible_seg_name
        dob.tags=set_tags(dob.tags,dob.name)
        @md.seg_names << possible_seg_name
      elsif (@md.opt.act[:verbose_plus][:set]==:on \
      or @md.opt.act[:maintenance][:set]==:on)
        puts 'warn, there may be a conflicting numbering scheme'
      end
    end
    def name_para_seg_filename(data)                                                   #segment naming, remaining
      # paragraph name/numbering rules
      # manual naming overrides, manual naming may be
      #   alpha-numeric characters mixed,
      #   numeric only (a number), if
      #     all segments have been named,
      #     the numbers used are over 1000 or
      #     it is not minded that auto-numbering uses a funny scheme for naming segments (not yet implemented)
      #       [for now a warning is printed for such documents on use of maintenance or very-verbose flag]
      # auto-naming takes the form of giving numbers to segments
      # the rules for which are as follows
      #   if the title/heading text starts with a numeric, then that is used (1 3.1 3rd etc.)
      #   otherwise the level 4 segment number from the embedded document structure info is used
      #   if there is none a sequential number is designated, preceded by an underscore
      @tuned_file,@unique_auto_name=[],[]
      tags={}
      @art_filename_auto=0
      @counter=1
      if not @md.seg_autoname_safe \
      and (@md.opt.act[:verbose_plus][:set]==:on \
      || @md.opt.act[:maintenance][:set]==:on)
        puts 'manual segment names, numbers used as names, risk warning (segmented html)'
      end
      ocn_html_seg=[]
      @num_exc={ t1: 0, t2: 0, t3: 0, t4: 0 }
      data.each do |dob|
        if dob.is==:heading \
        && dob.ln \
        and dob.ln.to_s =~/^[4-7]/
          heading_num_is=/^\d+:(\d+);\d/m.match(dob.node)[1]
          if dob.ln==4 \
          and not dob.name \
          and not @md.set_heading_seg
            @md.set_heading_seg=true
          end
          if dob.name !~/^\S+/ \
          and dob.ln.to_s =~/^[5-7]/ \
          and dob.obj =~/^\s*(?:\S+\s+)?([0-9]+(?:[.,:-][0-9])+)/m
            #heading starts with a recognised numeric
            #or word followed by a recognised numeric construct,
            #use that as name
            if dob.ln==7 \
            and dob.obj =~/^\s*(?:\S+\s+)?([0-9]+(?:[.,:-][0-9]){3})/m
              possible_seg_name=$1.
                gsub(/(?:[:,-]|\W)/,'.').
                gsub(/\.$/,'')
              possible_seg_name=
                auto_seg_name(possible_seg_name,heading_num_is,dob,:extract)
              set_name_and_tags(dob,possible_seg_name)
            elsif dob.ln==6 \
            and dob.obj =~/^\s*(?:\S+\s+)?([0-9]+(?:[.,:-][0-9]){2})/m
              possible_seg_name=$1.
                gsub(/(?:[:,-]|\W)/,'.').
                gsub(/\.$/,'')
              possible_seg_name=
                auto_seg_name(possible_seg_name,heading_num_is,dob,:extract)
              set_name_and_tags(dob,possible_seg_name)
            elsif dob.ln==5 \
            and dob.obj =~/^\s*(?:\S+\s+)?([0-9]+(?:[.,:-][0-9]){1})/m
              possible_seg_name=$1.
                gsub(/(?:[:,-]|\W)/,'.').
                gsub(/\.$/,'')
              possible_seg_name=
                auto_seg_name(possible_seg_name,heading_num_is,dob,:extract)
              set_name_and_tags(dob,possible_seg_name)
            end
          end
          if dob.ln==4
            if dob.name !~/^\S+/ \
            and dob.obj =~/^\s*(?:\S+\s+)?([0-9]+)/m
              #heading starts with a recognised numeric
              #or word followed by a recognised numeric construct,
              #use that as name
              possible_seg_name=$1
              possible_seg_name=
                auto_seg_name(possible_seg_name,heading_num_is,dob,:extract)
              set_name_and_tags(dob,possible_seg_name)
            end
            if dob.name
              #extract segment name from embedded document structure info
              if @md.seg_names.is_a?(Array) \
              and not @md.seg_names.include?(dob.name)
                dob.tags=set_tags(dob.tags,dob.name)
                @md.seg_names << dob.name
              end
            else
              #if no segment name,
              #provide a numerical one
              @art_filename_auto+=1
              possible_seg_name=
                auto_seg_name(@art_filename_auto,heading_num_is,dob,:auto)
              if @md.seg_names.is_a?(Array) \
              and not @md.seg_names.include?(possible_seg_name)
               dob.name=possible_seg_name
               dob.tags=set_tags(dob.tags,dob.name)
                @md.seg_names << possible_seg_name
              else puts 'segment name (numbering) error'
              end
            end
            if not dob.name #should not occur
              puts "e r r o r -\t#{__FILE__}::#{__LINE__}\n#{dob.inspect}"
            end
          end
        end
        if (dob.is ==:heading \
        || dob.is ==:heading_insert) \
        && dob.ln==4
          @seg=dob.name
        end
        @tuned_file << if dob.is==:heading \
        && (@md.pagenew || @md.pagebreak || @md.pageline)
          m=dob.ln.to_s
          dob_tmp=[]
          if @md.pagenew.inspect =~/#{m}/
            dob_tmp <<
              SiSU_AO_DocumentStructure::ObjectLayout.new.break(Hx[:br_page_new]) <<
              dob
          elsif @md.pagebreak.inspect =~/#{m}/
            dob_tmp <<
              SiSU_AO_DocumentStructure::ObjectLayout.new.break(Hx[:br_page]) <<
              dob
          elsif @md.pageline.inspect =~/#{m}/
            dob_tmp <<
              SiSU_AO_DocumentStructure::ObjectLayout.new.break(Hx[:br_page_line]) <<
              dob
          end
          unless dob_tmp.length > 0; dob
          else                       dob_tmp
          end
        else dob
        end
        if defined? dob.ocn \
        and dob.ocn
          @segname=((dob.is==:heading || dob.is==:heading_insert) && dob.ln==4 && (defined? dob.name)) \
          ? (dob.name)
          : @segname
          tags["#{dob.ocn}"]={ segname: @segname }
          ocn_html_seg[dob.ocn]=if (dob.is==:heading || dob.is==:heading_insert)
            if dob.ln =~/[0-3]/
              {
                seg: nil,
                level: dob.ln,
              }
            #elsif dob.ln =~/[4-6]/
            else
              {
                seg: @seg,
                level: dob.ln,
              }
            end
          else
            {
              seg: @seg,
              level: nil,
            }
          end
        end
        dob.tags=dob.tags.uniq if defined? dob.tags
        if defined? dob.tags \
        and dob.tags.length > 0
          #@segname=((dob.is=='heading'|| dob.is=='heading_insert') && dob.ln==4 && (defined? dob.name)) \
          #? (dob.name) \
          #: @segname
          dob.tags.each do |y|
            tags[y]={ ocn: dob.ocn.to_s, segname: @segname }
          end
        end
        dob
      end
      ocn_html_seg.each_with_index do |ocn,i|
        if ocn \
        and ocn[:level].to_s=~/[1-3]/
          (1..4).each do |x|
            if ocn_html_seg[i+x] \
            and ocn_html_seg[i+x][:level]==4
              ocn[:seg]=ocn_html_seg[i+x][:seg]
            end
          end
        end
      end
      if @md.seg_names.length > 0
        @md.set_heading_seg=true
      end
      tuned_file=@tuned_file.flatten
      [tuned_file,tags,ocn_html_seg]
    end
    def set_heading_top(data)                                                          #% make sure no false positives
      unless @md.set_heading_top
        if (@md.opt.act[:verbose_plus][:set]==:on \
        or @md.opt.act[:maintenance][:set]==:on)
          puts "\tdocument contains no top level heading, (will have to manufacture one)"
        end
        @tuned_file=[]
        data.each do |t_o|
          unless @md.set_heading_top
            if t_o !~/^(?:#{Rx[:meta]}|@\S+:)\s/m \
            and t_o !~/\A\s*\Z/m
              @md.set_heading_top=true
              if defined? @md.title \
              and @md.title \
              and defined? @md.title.full \
              and defined? @md.creator \
              and @md.creator
                head=@md.title.main \
                ? ([@lv='1',@obj=@md.title.main])
                : ([@lv='1',@obj='[no title provided]'])
                @tuned_file << head
              end
            end
          end
          @tuned_file << t_o
        end
        @tuned_file=@tuned_file.flatten
      end
    end
    def set_heading_seg(data)                                                          #% make sure no false positives
      unless @md.set_heading_seg
        if (@md.opt.act[:verbose_plus][:set]==:on \
        or @md.opt.act[:maintenance][:set]==:on)
          puts "\tdocument contains no segment level, (will have to manufacture one)"
        end
        @tuned_file=[]
        data.each do |dob|
          unless @md.set_heading_seg
            if defined? dob.ln and dob.ln.to_s !~/^[0-3]/m \
            and dob.obj !~/\A\s*\Z/m \
            and dob.is !=:layout
              @md.set_heading_seg=true
              head=@md.title.main \
              ? (dob.ln,dob.name,dob.obj=4,'seg',@md.title.main)
              : (dob.ln,dob.name,dob.obj=4,'seg','[segment]')
              @tuned_file << head
            end
          end
          @tuned_file << dob
        end
        @tuned_file=@tuned_file.flatten
      end
    end
    def set_header_title(data)                                                         #% make sure no false positives
      unless @md.set_header_title
        if (@md.opt.act[:verbose_plus][:set]==:on \
        or @md.opt.act[:maintenance][:set]==:on)
          puts "\t no document title provided, (will have to manufacture one)"
        end
        @tuned_file=[]
        data.each do |t_o|
          unless @md.set_header_title
            if t_o !~/^%{1,2}\s/m \
            and t_o !~/\A\s*\Z/m
              @tuned_file <<
                "#{Mx[:meta_o]}title#{Mx[:meta_c]} #{@md.heading_seg_first}"
              @md.title.main=@md.heading_seg_first
              @md.set_header_title=true
            end
          end
          @tuned_file << t_o
        end
        @tuned_file=@tuned_file.flatten
      end
    end
  end
end
__END__