aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/sisu/v6/ao_numbering.rb
diff options
context:
space:
mode:
authorRalph Amissah <ralph@amissah.com>2014-07-01 00:54:37 -0400
committerRalph Amissah <ralph@amissah.com>2014-07-01 00:54:39 -0400
commitf74de438d030629eec66eeb1f7275ed62bc5c370 (patch)
tree2762210469f80149d12e2185677d2e4911523f68 /lib/sisu/v6/ao_numbering.rb
parentv5 v6: ao, doc objects, layout group open & close, identify, is_for (diff)
v5 v6: ao, segment numbering, fixed width, leading zeros
* alphanumeric sorting
Diffstat (limited to 'lib/sisu/v6/ao_numbering.rb')
-rw-r--r--lib/sisu/v6/ao_numbering.rb55
1 files changed, 48 insertions, 7 deletions
diff --git a/lib/sisu/v6/ao_numbering.rb b/lib/sisu/v6/ao_numbering.rb
index 791f5bfc..d6faf3a6 100644
--- a/lib/sisu/v6/ao_numbering.rb
+++ b/lib/sisu/v6/ao_numbering.rb
@@ -63,10 +63,23 @@
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
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
data=@data
data=number_plaintext_para(data)
@@ -152,7 +165,6 @@ module SiSU_AO_Numbering
data=data.compact
data.each do |dob| #@md.seg_names << [additions to segment names]
title_no=nil
- dob=SiSU_AO_DocumentStructureExtract::Structure.new(@md,dob).structure_markup #must happen earlier, node info etc. require
if dob.is ==:heading \
&& dob.autonum_ \
and defined? @md.make.num_top \
@@ -280,6 +292,33 @@ module SiSU_AO_Numbering
end
@tuned_file=@tuned_file.flatten
end
+ def leading_zeros_fixed_width_number(possible_seg_name)
+ if possible_seg_name =~/^([\d]+?\.|[\d]+)$/m #!~/[.,:-]+/
+ possible_seg_name=possible_seg_name.
+ 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 \
+ ? Mx[:auto_seg_prefix] +
+ possible_seg_name.to_s
+ : Mx[:auto_seg_prefix] +
+ zero*zero_width +
+ possible_seg_name.to_s
+ end
+ end
+ def auto_seg_name(possible_seg_name)
+ if possible_seg_name =~/^([\d]+?\.|[\d]+)$/m #!~/[.,:-]+/
+ leading_zeros_fixed_width_number(possible_seg_name)
+ elsif possible_seg_name =~/^[\d.,:-]+$/m
+ possible_seg_name=possible_seg_name.
+ gsub(/(?:[:,-]|\W)/,'.').
+ gsub(/\.$/,'')
+ #Mx[:auto_seg_prefix] + possible_seg_name
+ else possible_seg_name
+ end
+ end
def name_para_seg_filename(data) #segment naming, remaining
# paragraph name/numbering rules
# manual naming overrides, manual naming may be
@@ -314,8 +353,10 @@ module SiSU_AO_Numbering
@md.set_heading_seg=true
end
if dob.name !~/^\S+/ \
- and dob.obj =~/^\s*(?:\S+\s+)?([\d.,:-]+)/m #heading starts with a recognised numeric or word followed by a recognised numerical construct, use that as name
+ and dob.obj =~/^\s*(?:\S+\s+)?([\d.,:-]+)/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)
possible_seg_name=possible_seg_name.
gsub(/(?:[:,-]|\W)/,'.').
gsub(/\.$/,'')
@@ -339,13 +380,13 @@ module SiSU_AO_Numbering
end
if dob.ln==4 \
and not dob.name #if still no segment name, provide a numerical one
- pf='_' #pg='' #may use e.g. '' or '~' or '_'
- segn_auto="#{pf}#{art_filename_auto.to_s}"
+ possible_seg_name=
+ auto_seg_name(art_filename_auto)
if @md.seg_names.is_a?(Array) \
- and not @md.seg_names.include?(segn_auto)
- dob.name=segn_auto
+ 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 << segn_auto
+ @md.seg_names << possible_seg_name
else puts 'segment name (numbering) error'
end
art_filename_auto+=1