From 58c13f49d81e3a6448a0f4dabf44b3bfb52ea84c Mon Sep 17 00:00:00 2001 From: Ralph Amissah Date: Mon, 1 Oct 2012 15:27:56 -0400 Subject: v3: odf, internal links/bookmarks, toc, book index --- data/doc/sisu/CHANGELOG_v3 | 2 + lib/sisu/v3/dal.rb | 14 ++-- lib/sisu/v3/odf.rb | 170 +++++++++++++++++++++++++-------------------- lib/sisu/v3/odf_format.rb | 60 ++++++++++++++-- lib/sisu/v3/particulars.rb | 10 +-- lib/sisu/v3/sysenv.rb | 4 +- lib/sisu/v3/texpdf.rb | 2 +- 7 files changed, 166 insertions(+), 96 deletions(-) diff --git a/data/doc/sisu/CHANGELOG_v3 b/data/doc/sisu/CHANGELOG_v3 index fdbe9c0b..3b0f7126 100644 --- a/data/doc/sisu/CHANGELOG_v3 +++ b/data/doc/sisu/CHANGELOG_v3 @@ -48,6 +48,8 @@ http://www.jus.uio.no/sisu/pkg/src/sisu_3.3.3.orig.tar.xz * v3: sisupod source, paths fix +* v3: odf, internal links/bookmarks, toc, book index + * v3: pdflatex, fixes * landscape cover page was botched, came out as portrait diff --git a/lib/sisu/v3/dal.rb b/lib/sisu/v3/dal.rb index 6c7fef79..a039545f 100644 --- a/lib/sisu/v3/dal.rb +++ b/lib/sisu/v3/dal.rb @@ -100,7 +100,7 @@ module SiSU_DAL @fnm=@make_fns.marshal.dal_metadata @fnc=@make_fns.marshal.dal_content @idx_sst=@make_fns.marshal.dal_idx_sst_rel_html_seg - @idx_tex=@make_fns.marshal.dal_idx_sst_rel + @idx_raw=@make_fns.marshal.dal_idx_sst_rel @idx_html=@make_fns.marshal.dal_idx_html @idx_xhtml=@make_fns.marshal.dal_idx_xhtml @map_nametags=@make_fns.marshal.dal_map_nametags @@ -163,7 +163,7 @@ module SiSU_DAL SiSU_DAL::Instantiate.new end end - def get_idx_tex #reads dal idx.tex, #unless does not exist then creates first + def get_idx_raw begin dal=[] unless @@fns==@opt.fns \ @@ -173,7 +173,7 @@ module SiSU_DAL : @opt.fns @@idx_arr[:tex]=[] end - dal=(@@idx_arr[:tex].empty?) ? read_idx_tex : @@idx_arr[:tex].dup #check + dal=(@@idx_arr[:tex].empty?) ? read_idx_raw : @@idx_arr[:tex].dup #check rescue SiSU_Errors::InfoError.new($!,$@,@opt.cmd,@opt.fns).error do __LINE__.to_s + ':' + __FILE__ @@ -318,12 +318,12 @@ module SiSU_DAL else nil end end - def read_idx_tex + def read_idx_raw m=[] - m=if FileTest.file?(@idx_tex) + m=if FileTest.file?(@idx_raw) (RUBY_VERSION < '1.9') \ - ? (File.open(@idx_tex){ |f| m=Marshal.load(f)}) - : (File.open(@idx_tex,'r:utf-8'){ |f| m=Marshal.load(f)}) + ? (File.open(@idx_raw){ |f| m=Marshal.load(f)}) + : (File.open(@idx_raw,'r:utf-8'){ |f| m=Marshal.load(f)}) else nil end end diff --git a/lib/sisu/v3/odf.rb b/lib/sisu/v3/odf.rb index 4985b3dd..2830b24d 100644 --- a/lib/sisu/v3/odf.rb +++ b/lib/sisu/v3/odf.rb @@ -101,7 +101,7 @@ module SiSU_ODF require_relative 'defaults' # defaults.rb require_relative 'shared_txt' # shared_txt.rb @@img_count=0 - @@odf={ body: [], open: [], close: [], head: [], metadata: [], tail: [], endnotes: [] } + @@odf={ body: [], head: [], toc: [], metadata: [], tail: [], book_idx: [], endnotes: [] } @@docstart=true @@fns=nil def initialize(particulars) @@ -144,6 +144,19 @@ module SiSU_ODF end end end + def odf_book_idx + if @md.book_idx + idx_arr,idx_raw=[],SiSU_Particulars::CombinedSingleton.instance.get_idx_raw(@md.opt).raw_idx + idx_raw.each do |x| + x=if x.is_a?(String) + SiSU_ODF_Format::FormatBookIndex.new(x).book_idx_bookmark + else nil + end + idx_arr << x.strip if x.is_a?(String) + end + @@odf[:book_idx]=idx_arr.join + end + end def odf_metadata @@odf[:metadata]=SiSU_Metadata::Summary.new(@md).odf.metadata end @@ -162,8 +175,10 @@ module SiSU_ODF @@odf[:tail] << %{\nSiSU: <www.jus.uio.no/sisu> and <www.sisudoc.org>} @@odf[:tail] << "\n" end - def heading(dob,p_num='') - dob.obj=dob.obj.gsub(/#{Mx[:tag_o]}\S+?#{Mx[:tag_c]}/,'') #check + def set_bookmark_tag(dob) + tags=SiSU_ODF_Format::Tags.new.set_bookmark_tag(dob) + end + def heading(dob,p_num) m=/#{$1}/ breakpage='' if @md.fns \ @@ -180,7 +195,19 @@ module SiSU_ODF end end @@docstart=false - dob.obj=%{#{breakpage}#{dob.obj}#{p_num}} + dob.tmp=dob.obj + dob.obj=%{#{breakpage}#{p_num[:set_ref]}#{set_bookmark_tag(dob)}#{dob.obj}#{p_num[:display]}} + dob + end + def toc(dob,p_num) + m=/#{$1}/ + hardspace=(dob.lv =~/[A-C]/i) \ + ? '' + : '' + toc_heading=dob.ocn \ + ? %{#{dob.tmp}} + : dob.tmp + dob.obj=%{#{toc_heading}#{hardspace}} dob end def image_src(i) @@ -282,6 +309,10 @@ module SiSU_ODF end t end + def text_link_odf_bookmark(txt,url,trail) + map_nametags=SiSU_Particulars::CombinedSingleton.instance.get_map_nametags(@md).nametags_map + %{#{txt.strip}#{trail}} + end def text_link(dob) m=dob.obj.scan(/(#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:url_o]}(\S+?)#{Mx[:url_c]})/) #sort if m @@ -305,6 +336,19 @@ module SiSU_ODF txt=txt.gsub(/([)(\]\[])/,"\\\\\\1"). gsub(/([+?*])/,"\\\\\\1") # problems with + url=url.gsub(/([+?])/,"\\\\\\1") # problems with + + dob.obj=dob.obj.gsub(/#{Mx[:lnk_o]}[ ]*#{txt}#{Mx[:lnk_c]}#{Mx[:rel_o]}#{url}#{Mx[:rel_c]}/m,text_link_odf_bookmark(txt,url,trail)). #make sure trailing ']' are not caught in url + gsub(/\\([)(\]\[?])/,'\1') #clumsy fix + end + m=nil + end + dob + end + def text_link_relative_(dob) + m=dob.obj.scan(/(#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:rel_o]}(\S+?)#{Mx[:rel_c]})/) #sort + if m + m.each do |i| + txt,url,trail=i[1],i[2] + txt=txt.gsub(/([)(\]\[])/,"\\\\\\1") dob.obj=dob.obj.gsub(/#{Mx[:lnk_o]}[ ]*#{txt}#{Mx[:lnk_c]}#{Mx[:rel_o]}#{url}#{Mx[:rel_c]}/m,text_link_odf(txt,url,trail)). #make sure trailing ']' are not caught in url gsub(/\\([)(\]\[?])/,'\1') #clumsy fix end @@ -312,7 +356,7 @@ module SiSU_ODF end dob end - def normal(dob,p_num='') #P1 - P3 + def normal(dob,p_num) #P1 - P3 dob.obj=dob.obj.gsub(/#{Mx[:url_o]}_(\S+?)#{Mx[:url_c]}/, '\1'). #http ftp matches escaped, no decoration gsub(/#{Mx[:url_o]}([a-zA-Z0-9._-]+\@\S+?\.[a-zA-Z0-9._-]+)#{Mx[:url_c]}/, @@ -322,12 +366,12 @@ module SiSU_ODF dob.obj= if dob.is==:para \ and dob.indent.to_s =~/[0-9]/ \ and dob.indent == dob.hang - %{#{dob.obj}#{p_num}} + %{#{p_num[:set_ref]}#{set_bookmark_tag(dob)}#{dob.obj}#{p_num[:display]}} elsif dob.is==:para \ and dob.hang.to_s =~/[0-9]/ \ and dob.indent != dob.hang - %{#{dob.obj}#{p_num}} - else %{#{dob.obj}#{p_num}} + %{#{p_num[:set_ref]}#{set_bookmark_tag(dob)}#{dob.obj}#{p_num[:display]}} + else %{#{p_num[:set_ref]}#{set_bookmark_tag(dob)}#{dob.obj}#{p_num[:display]}} end dob end @@ -390,12 +434,13 @@ module SiSU_ODF end def poem(dob,p_num) #P4 #same as group parray=[] - dob.obj.split(/#{Mx[:br_line]}|#{Mx[:br_nl]}/).each do |parablock| + dob.obj.split(/#{Mx[:br_line]}|#{Mx[:br_nl]}/).each_with_index do |parablock,i| + set_ref=(i==0) ? "#{p_num[:set_ref]}#{set_bookmark_tag(dob)}" : '' parablock=group_clean(parablock) - parray << %{#{parablock}} if parablock =~/\S+/ + parray << %{#{set_ref}#{parablock}} if parablock =~/\S+/ end dob.obj=parray.join \ - + %{#{p_num}} \ + + %{#{p_num[:display]}} \ + '' dob end @@ -407,7 +452,8 @@ module SiSU_ODF %{#{@brace_url.xml_open}\\1#{@brace_url.xml_close}}). gsub(/#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/, %{#{@brace_url.xml_open}\\1#{@brace_url.xml_close}}) #http ftp matches with decoration - dob.obj.split(/#{Mx[:br_line]}|#{Mx[:br_nl]}/).each do |parablock| + dob.obj.split(/#{Mx[:br_line]}|#{Mx[:br_nl]}/).each_with_index do |parablock,i| + set_ref=(i==0) ? "#{p_num[:set_ref]}#{set_bookmark_tag(dob)}" : '' parablock=group_clean(parablock) parablock=parablock.gsub(/<text:a xlink:type="simple" xlink:href="(.+?)">/m,''). gsub(/<(\/text:a)>/,'<\1>'). @@ -415,10 +461,10 @@ module SiSU_ODF gsub(/<(text:p text:style-name="Footnote")>/,'<\1>'). gsub(/<(\/?text:(?:note-citation|note-body|note|p))>/,'<\1>') parablock=footnote(parablock) - parray << %{#{parablock}} if parablock =~/\S+/ + parray << %{#{set_ref}#{parablock}} if parablock =~/\S+/ end dob.obj=parray.join \ - + %{#{p_num}} \ + + %{#{p_num[:display]}} \ + '' dob end @@ -430,7 +476,8 @@ module SiSU_ODF %{#{@brace_url.xml_open}\\1#{@brace_url.xml_close}}). gsub(/#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/, %{#{@brace_url.xml_open}\\1#{@brace_url.xml_close}}) #http ftp matches with decoration - dob.obj.split(/#{Mx[:br_line]}|#{Mx[:br_nl]}/).each do |parablock| + dob.obj.split(/#{Mx[:br_line]}|#{Mx[:br_nl]}/).each_with_index do |parablock,i| + set_ref=(i==0) ? "#{p_num[:set_ref]}#{set_bookmark_tag(dob)}" : '' parablock=group_clean(parablock) parablock=parablock.gsub(/<text:a xlink:type="simple" xlink:href="(.+?)">/m,''). gsub(/<(\/text:a)>/,'<\1>'). @@ -438,10 +485,10 @@ module SiSU_ODF gsub(/<(text:p text:style-name="Footnote")>/,'<\1>'). gsub(/<(\/?text:(?:note-citation|note-body|note|p))>/,'<\1>') parablock=footnote(parablock) - parray << %{#{parablock}} if parablock =~/\S+/ + parray << %{#{set_ref}#{parablock}} if parablock =~/\S+/ end dob.obj=parray.join \ - + %{#{p_num}} \ + + %{#{p_num[:display]}} \ + '' dob end @@ -449,15 +496,16 @@ module SiSU_ODF if dob.is==:code dob.obj=dob.obj.gsub(/\s\s/,'  ') parray=[] - dob.obj.split(/#{Mx[:br_line]}|#{Mx[:br_nl]}/).each do |parablock| + dob.obj.split(/#{Mx[:br_line]}|#{Mx[:br_nl]}/).each_with_index do |parablock,i| + set_ref=(i==0) ? "#{p_num[:set_ref]}#{set_bookmark_tag(dob)}" : '' parablock=group_clean(parablock) parablock=parablock.gsub(/^\s*$/,'
'). gsub(/#{Mx[:url_o]}_(\S+?)#{Mx[:url_c]}/, '\1') #http ftp matches escaped, no decoration - parray << %{#{parablock}} if parablock =~/\S+/ + parray << %{#{set_ref}#{parablock}} if parablock =~/\S+/ end dob.obj=parray.join \ - + %{#{p_num}} \ + + %{#{p_num[:display]}} \ + '' end dob @@ -490,18 +538,22 @@ module SiSU_ODF dob else dob end - p_num='' - if @env.odt_ocn? - if dob.is !~/(^#{Rx[:meta]}|#{Mx[:br_eof]}|#{Mx[:br_endnotes]})/ - if defined? dob.ocn \ - and not dob.ocn.nil? - p_num=SiSU_ODF_Format::ParagraphNumber.new(dob.ocn).display - end + p_num={ display: '', set_ref: '' } + if dob.is !~/(^#{Rx[:meta]}|#{Mx[:br_eof]}|#{Mx[:br_endnotes]})/ + if defined? dob.ocn \ + and dob.ocn.is_a?(Fixnum) + p_num=SiSU_ODF_Format::ParagraphNumber.new(dob.ocn).set_bookmark_and_display + #p_num=SiSU_ODF_Format::ParagraphNumber.new(dob.ocn).set_ref_and_display end end dob=footnote(dob) if dob.is==:heading @@odf[:body] << heading(dob,p_num).obj << @br*2 + if SiSU_Env::ProcessingSettings.new(md).build.toc? + if dob.lv =~/[A-C1]/i + @@odf[:toc] << toc(dob,p_num).obj + end + end elsif dob.is ==:verse @@odf[:body] << poem(dob,p_num).obj << @br*2 elsif dob.is==:group @@ -539,6 +591,7 @@ module SiSU_ODF else STDERR.puts %{\t*WARN* did not find image - "#{bullet}/bullet_09.png" [#{__FILE__}:#{__LINE__}]} end end + odf_book_idx odf_metadata data.each do |dob| #p dob.obj if dob.obj =~safe_characters and @md.opt.cmd =~/V/ #KEEP @@ -573,7 +626,7 @@ module SiSU_ODF dob.obj=dob.obj.gsub(/#{Mx[:fa_strike_o]}(.+?)#{Mx[:fa_strike_c]}/,'\1'). gsub(/#{Mx[:fa_insert_o]}(.+?)#{Mx[:fa_insert_c]}/,'\1'). gsub(/#{Mx[:fa_cite_o]}(.+?)#{Mx[:fa_cite_c]}/,'\1'). - gsub(/#{Mx[:tag_o]}\S+?#{Mx[:tag_c]}/,''). #check + gsub(/#{Mx[:tag_o]}\S+?#{Mx[:tag_c]}/,''). #check gsub(/#{Mx[:mk_o]}#([a-zA-Z]+)#{Mx[:mk_c]}/,'&\1;'). gsub(/#{Mx[:mk_o]}(#[0-9]+)#{Mx[:mk_c]}/,'&\1;'). gsub(/#{Mx[:mk_o]}[~-]##{Mx[:mk_c]}/,'') @@ -693,14 +746,19 @@ WOK def publish divider='=' content=[] + br_pg=' ' data=@data - content << @@odf[:open] - content << @@odf[:head] - content << @@odf[:body] - content << @@odf[:metadata] - content << @@odf[:tail] + content << + @@odf[:head] << + @@odf[:toc] << + br_pg << + @@odf[:body] << + @@odf[:book_idx] << + br_pg << + @@odf[:metadata] << + @@odf[:tail] SiSU_ODF::Source::Output.new(content,@md,@env).odf - @@odf[:head],@@odf[:body],@@odf[:tail],@@odf[:metadata]=[],[],[],[] + @@odf={ head: [], toc: [], body: [], tail: [], book_idx: [], metadata: [] } end end class Output ● #bullet -dob.obj.gsub!(/^(#{Mx[:pa_o]}:i[1-9]#{Mx[:pa_c]})\s*#{Mx[:gl_bullet]}/,'\1 · ') #bullet -dob.obj.gsub!(/^#{Mx[:gl_bullet]}/,'· ') #bullet diff --git a/lib/sisu/v3/odf_format.rb b/lib/sisu/v3/odf_format.rb index ae5c4aec..cd998f43 100644 --- a/lib/sisu/v3/odf_format.rb +++ b/lib/sisu/v3/odf_format.rb @@ -63,8 +63,15 @@ module SiSU_ODF_Format def initialize(paranum) @paranum=/(\d+)/m.match(paranum.to_s)[1] end - def display - @paranum.gsub(/(\d+)/,' [\1]') + def set_ref_and_display + set_ref=@paranum.gsub(/(\d+)/,' ') + disp=@paranum.gsub(/(\d+)/,' [\1]') + ocn={ display: disp, set_ref: set_ref } + end + def set_bookmark_and_display + set_ref=@paranum.gsub(/(\d+)/,' ') + disp=@paranum.gsub(/(\d+)/,' [\1]') + ocn={ display: disp, set_ref: set_ref } end def name @paranum.gsub(/(\d+)/,'') @@ -73,6 +80,50 @@ module SiSU_ODF_Format @paranum.gsub(/(\d+)/,'') end end + class FormatBookIndex + def initialize(idx_str) + @idx_str=idx_str + end + def book_idx_bookmark + map_nametags=SiSU_Particulars::CombinedSingleton.instance.get_map_nametags(@md).nametags_map #p map_nametags + rgx_bookmark=/#{Mx[:lnk_o]}.+?#{Mx[:lnk_c]}#{Mx[:rel_o]}#?\S+?#{Mx[:rel_c]}/m + while @idx_str =~/#{Mx[:lnk_o]}([^#{Mx[:lnk_o]}#{Mx[:lnk_c]}]+)#{Mx[:lnk_c]}#{Mx[:rel_o]}#?(\S+?)#{Mx[:rel_c]}/m + link,url=$1,$2 + link,url=link.strip,url.strip + @idx_str=@idx_str.gsub(/&/m,"&") + ocn_lnk=if map_nametags[url] \ + and map_nametags[url][:ocn] + map_nametags[url][:ocn] + else nil + end + ocn_lnk=(url=~/^\d+$/ ? url : ocn_lnk) + if ocn_lnk and not ocn_lnk.empty? + @idx_str=@idx_str.sub(/#{Mx[:fa_bold_o]}(.+?)#{Mx[:fa_bold_c]}/, + '\1'). + sub(rgx_bookmark, + %{#{link.strip}}) + else + puts %{name tag: "#{url}" not found} + @idx_str.sub!(rgx_bookmark,"#{link}") + end + end + @idx_str=@idx_str.gsub(/#{Xx[:protect]}/m,''). + sub(/,\s*$/m,''). + gsub(/\n/,'') + @idx_str='' + @idx_str + '' + end + end + class Tags + def set_bookmark_tag(dob) + tags='' + if dob.tags.length > 0 + dob.tags.each do |tag| + tags +=%{ } + end + end + tags + end + end class FormatTextObject def initialize(md,t_o) @md,@t_o=md,t_o @@ -136,12 +187,13 @@ module SiSU_ODF_Format when 14; 'N' else 'D' end - %{#{@br}} + + tag=SiSU_ODF_Format::Tags.new.set_bookmark_tag(@dob) + %{#{@p_num[:set_ref]}#{tag}#{@br}} + %{#{@br}} end def table_close(tablefoot='') '' \ - + %{#{@p_num}} + + %{#{@p_num[:display]}} end def table_tag_cell(str,i) txt_name_cell=if i==0 \ diff --git a/lib/sisu/v3/particulars.rb b/lib/sisu/v3/particulars.rb index b95ba52a..97a3da3e 100644 --- a/lib/sisu/v3/particulars.rb +++ b/lib/sisu/v3/particulars.rb @@ -97,9 +97,9 @@ module SiSU_Particulars @opt=opt set_sst_idx end - def get_idx_tex(opt) + def get_idx_raw(opt) @opt=opt - set_tex_idx + set_raw_idx end def get_idx_html(opt) @opt=opt @@ -126,7 +126,7 @@ module SiSU_Particulars @opt=opt set_ocn_htmlseg_map end - attr_accessor :opt,:md,:sst_idx,:tex_idx,:html_idx,:xhtml_idx + attr_accessor :opt,:md,:sst_idx,:raw_idx,:html_idx,:xhtml_idx def set_md begin @md=SiSU_Param::Parameters.new(@opt).get @@ -180,9 +180,9 @@ module SiSU_Particulars end end end - def set_tex_idx + def set_raw_idx begin - @tex_idx=SiSU_DAL::Source.new(@opt).get_idx_tex + @raw_idx=SiSU_DAL::Source.new(@opt).get_idx_raw self rescue SiSU_Errors::InfoError.new($!,$@,@opt.cmd,@opt.fns).error do diff --git a/lib/sisu/v3/sysenv.rb b/lib/sisu/v3/sysenv.rb index be13c4fc..0786119c 100644 --- a/lib/sisu/v3/sysenv.rb +++ b/lib/sisu/v3/sysenv.rb @@ -3465,8 +3465,8 @@ WOK def dal_idx_sst_rel_html_seg "#{@env.processing_path.dal}/#{@fns}.idx_sst.rbm" end - def dal_idx_sst_rel - "#{@env.processing_path.dal}/#{@fns}.idx_tex.rbm" + def dal_idx_sst_rel #used by tex & odf + "#{@env.processing_path.dal}/#{@fns}.idx_raw.rbm" end def dal_idx_html "#{@env.processing_path.dal}/#{@fns}.idx_html.rbm" diff --git a/lib/sisu/v3/texpdf.rb b/lib/sisu/v3/texpdf.rb index 618c891e..aba92c3e 100644 --- a/lib/sisu/v3/texpdf.rb +++ b/lib/sisu/v3/texpdf.rb @@ -535,7 +535,7 @@ module SiSU_TeX h=tst.heading_major heading="\\clearpage\n" + h.tmp idx_arr=[] - idx=SiSU_Particulars::CombinedSingleton.instance.get_idx_tex(@md.opt).tex_idx + idx=SiSU_Particulars::CombinedSingleton.instance.get_idx_raw(@md.opt).raw_idx idx.each do |x| x=if x.class==String x=SiSU_TeX_Pdf::SpecialCharacters.new(@md,x).special_characters -- cgit v1.2.3