From a9d166c65f9d56b1d4de20fbdfae6e940681c535 Mon Sep 17 00:00:00 2001 From: Ralph Amissah Date: Mon, 17 Dec 2007 00:07:08 +0000 Subject: texpdf, generate sisu latex output in a single pass implications if additional modules added e.g. provide dvi file without latex pdf headers or generate an A6 document by default for e-readers capable of reading pdf (this would otherwise require a separate pass/loop of content for each type to be generated) current shortcoming, images are sized for smaller landscape output by default, image dimension adjustments not yet made (for different orientation) --- lib/sisu/v0/texpdf.rb | 406 +++++++++++++++++++++++-------------------- lib/sisu/v0/texpdf_format.rb | 95 ++++++---- 2 files changed, 282 insertions(+), 219 deletions(-) diff --git a/lib/sisu/v0/texpdf.rb b/lib/sisu/v0/texpdf.rb index b22b462b..ead5752b 100644 --- a/lib/sisu/v0/texpdf.rb +++ b/lib/sisu/v0/texpdf.rb @@ -125,8 +125,8 @@ module SiSU_TeX #% needed needs to be reprogrammed !!! SiSU_Env::Info_skin.new(@md).select dal_array=SiSU_DAL::Source.new(@opt).get # dal file drawn here - SiSU_TeX::Source::LaTeX_create.new(dal_array,@md,'landscape').songsheet - SiSU_TeX::Source::LaTeX_create.new(dal_array,@md,'portrait').songsheet + SiSU_TeX::Source::LaTeX_create.new(dal_array,@md).songsheet + #SiSU_TeX::Source::LaTeX_create.new(dal_array,@md,'portrait').songsheet dal_array='' pwd=Dir.pwd SiSU_TeX::Source::LaTeX_to_pdf.new(@md).latexrun_selective @@ -234,14 +234,14 @@ module SiSU_TeX include SiSU_Param @@tex_backslash ||="\\\\" @@tilde='\\\\\\~' #?? debug crazy - @@tex_head_portrait,@@tex_head_landscape=nil,nil + @@tex_head={ :p => nil, :l => nil } @@flag_alt,@@flag_group,@@flag_code=false,false,false @@dp,@@prefix_b=nil,nil - def initialize(data,md,orientation) - @data,@md,@orientation=data,md,orientation + def initialize(data,md) + @data,@md=data,md @env=SiSU_Env::Info_env.new(@md.fns) @st={ :tex=>{} } - @tex=SiSU_TeX_Pdf::Use_TeX.new(@md,@orientation) + @tex=SiSU_TeX_Pdf::Use_TeX.new(@md) @vz=SiSU_Env::Get_init.instance.skin @dp=@@dp ||=SiSU_Env::Info_env.new.digest.pattern vz=SiSU_Env::Get_init.instance.skin @@ -256,7 +256,7 @@ module SiSU_TeX data=@data @@tex_footnote_array=[] @@rights=nil - tell=SiSU_Screen::Ansi.new(@md.cmd,"pdfTex #@orientation") + tell=SiSU_Screen::Ansi.new(@md.cmd,"pdfTex portrait & landscape") tell.txt_grey unless @md.cmd =~/q/ if @md.dc_rights use=@md.dc_rights.dup #dup is necessary, else contents of :rights changed @@ -286,7 +286,6 @@ module SiSU_TeX end data=number_paras(data,ocn) data=markup(data) - #data=markup(data).flatten #watch output(data) rescue; SiSU_Errors::Info_error.new($!,$@,@md.cmd,@md.fns).error ensure @@ -331,19 +330,25 @@ module SiSU_TeX end @tex_file end + def tables_hash(md,para) + para_p=para.dup #visit + do_mono_l=SiSU_TeX_Pdf::Format_text_object.new(md,para) + do_mono_p=SiSU_TeX_Pdf::Format_text_object.new(md,para_p) + block={ + :l => do_mono_l.longtable_landscape, + :p => do_mono_p.longtable_portrait + } + end def tables(data) + @@tableheader={ :p => 0, :l => 0 } @tex_file=[] - @@tableheader=0 data.each do |para| - if para =~/¡|/ \ + or @@flag_alt + if para =~/<:(?:code|alt|verse|group)>/ + @lineone=case para + when /<:(?:alt|verse|group)>/; para + when /<:code>/; "#{@tex.paraskip_small} \\begin{scriptsize} " + para + else 'error' #should never occur + end + end + if para =~/<=curly/ #takes care of escaped curly braces, expand + do_mono=SiSU_TeX_Pdf::Format_text_object.new(@md,para) + para=do_mono.special_characters_curly(para) + end + regx=/<:((?:code|alt|verse|group)(?:-end)?)>/m + x=regx.match(para)[1] if para =~regx + x=$1 + para.gsub!(/\n<:(?:code|alt|verse|group)>\n/m,'') + para=enclose(para,'code') unless para =~/^$/ + if x =~/(?:alt|verse|group)/; @@flag_alt=true + if x =~/group/; @@flag_group=true + end + elsif x =~/code/; @@flag_alt,@@flag_code=true,true + elsif @@flag_alt; + if para =~ /\}(?:https?|file|ftp)/m + para=mono.http #(@orientation) + end + @group_collect << para #<< "\n\n" + end + if x =~/(?:code|alt|verse|group)-end/m + regx=/(\\+marginpar\{\\+begin\{tiny\}\d+\\+end\{tiny\}\})/ + y=if para =~regx + regx.match(para)[1] + else '' + end + para.gsub!(regx,'') + #@group_collect.each{ |x| x.gsub!(/(<:\S+>||)/,' ') } + group_collect=[] + group_collect << '\begin{footnotesize} ' unless @@flag_code + @group_collect.each do |x| + x.gsub!(/(<:\S+>||)/,' ') + x=x.split(/ \\\\ /) + group_collect << x + end + group_collect << ' \end{footnotesize} ' unless @@flag_code + @group_collect=group_collect.flatten + @lineone.gsub!(/(<:\S+>||)/,' ') + #@group_collect.each{ |x| x.gsub!(/(.#{@@tilde}\S*\s*|<:\S+>||)/,' ') } + #@lineone.gsub!(/(.#{@@tilde}\S*\s*|<:\S+>||)/,' ') + insert=[] + if para =~/<:code-end>/m + insert << y + @lineone << @group_collect << ' \end{scriptsize}' << " #{@tex.paraskip_normal}" + else insert << y + @lineone << @group_collect + end + @@flag_alt,@@flag_group,@@flag_code=false,false,false + @group_collect=[] + para.gsub!(/(<:\S+>||)/,' ') + #para.gsub!(/(.#{@@tilde}\S*\s*|<:\S+>||)/,' ') + #@tex_file << insert.flatten + tex_f = insert.flatten + end + else + if para =~ /\}(?:https?|file|ftp)/ + para=mono.http #(@orientation) + end + case para + when /^1#{@@tilde}/; mono.level1 + when /^2#{@@tilde}/; mono.level2 + when /^3#{@@tilde}/; mono.level3 + when /^4#{@@tilde}/; mono.level4 + when /^5#{@@tilde}/; mono.level5 + when /^6#{@@tilde}/; mono.level6 + when /^<:i([1-9])>/; mono.indent($1) + when /<:=/; mono.symbol_graphic #watch + when /^\s*<:image\s+/; mono.image + when /\}image/; mono.png + else + para.strip! + para=enclose(para) unless para =~/^$/ + end + para.gsub!(/(\.#{@@tilde}\S*\s*|<:\S+>||)/,' ') #% tread with care + #para.gsub!(/(.#{@@tilde}(?:\\~\S+)?\s*|<:\S+>||)/,' ') #KEEP reference, problem escaping open curly braces \{ + if para =~/<=curly/ #takes care of escaped curly braces, expand + do_mono=SiSU_TeX_Pdf::Format_text_object.new(@md,para) + para=do_mono.special_characters_curly(para) + end + tex_f = para + end + tex_f + end def markup(data) @tex_file=[] md={} @@ -360,12 +457,10 @@ module SiSU_TeX home=@vz.txt_home.gsub(/<(br|p)>|<\/\s*(br|p)>|<(br|p)\s*\/>|\\\\/,' - ') #no line splitting in heading neither html nor latex title=@md.title.gsub(/<(br|p)>|<\/\s*(br|p)>|<(br|p)\s*\/>|\\\\/,' - ') #no line splitting in heading neither html nor latex subtitle=@md.subtitle.gsub(/<(br|p)>|<\/\s*(br|p)>|<(br|p)\s*\/>|\\\\/,' - ') if @md.subtitle #no line splitting in heading neither html nor latex - orient=SiSU_TeX_Pdf::Format_text_object.new(@md,@orientation,"#{home}: - #{title} #{subtitle}") #.new - if @orientation =~/portrait/ - @@tex_head_portrait=orient.document_head_with_orientation - elsif @orientation =~/landscape/ - @@tex_head_landscape=orient.document_head_with_orientation - end + orient_portrait=SiSU_TeX_Pdf::Format_text_object.new(@md,'portrait',"#{home}: - #{title} #{subtitle}") + orient_landscape=SiSU_TeX_Pdf::Format_text_object.new(@md,'landscape',"#{home}: - #{title} #{subtitle}") + @@tex_head[:p]=orient_portrait.document_head_with_orientation + @@tex_head[:l]=orient_landscape.document_head_with_orientation @tex_file <<</ \ - or @@flag_alt - if para =~/<:(?:code|alt|verse|group)>/ - @lineone=case para - when /<:(?:alt|verse|group)>/; para - when /<:code>/; "#{@tex.paraskip_small} \\begin{scriptsize} " + para - else 'error' #should never occur - end - end - if para =~/<=curly/ #takes care of escaped curly braces, expand - do_mono=SiSU_TeX_Pdf::Format_text_object.new(@md,para) - para=do_mono.special_characters_curly(para) - end - regx=/<:((?:code|alt|verse|group)(?:-end)?)>/m - x=regx.match(para)[1] if para =~regx - x=$1 - para.gsub!(/\n<:(?:code|alt|verse|group)>\n/m,'') - para=enclose(para,'code') unless para =~/^$/ - if x =~/(?:alt|verse|group)/; @@flag_alt=true - if x =~/group/; @@flag_group=true - end - elsif x =~/code/; @@flag_alt,@@flag_code=true,true - elsif @@flag_alt; - if para =~ /\}(?:https?|file|ftp)/m - para=mono.http(@orientation) - end - @group_collect << para #<< "\n\n" - end - if x =~/(?:code|alt|verse|group)-end/m - regx=/(\\+marginpar\{\\+begin\{tiny\}\d+\\+end\{tiny\}\})/ - y=if para =~regx - regx.match(para)[1] - else '' - end - para.gsub!(regx,'') - #@group_collect.each{ |x| x.gsub!(/(<:\S+>||)/,' ') } - group_collect=[] - group_collect << '\begin{footnotesize} ' unless @@flag_code - @group_collect.each do |x| - x.gsub!(/(<:\S+>||)/,' ') - x=x.split(/ \\\\ /) - group_collect << x - end - group_collect << ' \end{footnotesize} ' unless @@flag_code - @group_collect=group_collect.flatten - @lineone.gsub!(/(<:\S+>||)/,' ') - #@group_collect.each{ |x| x.gsub!(/(.#{@@tilde}\S*\s*|<:\S+>||)/,' ') } - #@lineone.gsub!(/(.#{@@tilde}\S*\s*|<:\S+>||)/,' ') - insert=[] - if para =~/<:code-end>/m - insert << y + @lineone << @group_collect << ' \end{scriptsize}' << " #{@tex.paraskip_normal}" - else insert << y + @lineone << @group_collect - end - @@flag_alt,@@flag_group,@@flag_code=false,false,false - @group_collect=[] - para.gsub!(/(<:\S+>||)/,' ') - #para.gsub!(/(.#{@@tilde}\S*\s*|<:\S+>||)/,' ') - @tex_file << insert.flatten - end - else - if para =~ /\}(?:https?|file|ftp)/ - para=mono.http(@orientation) - end - case para - when /^1#{@@tilde}/; mono.level1 - when /^2#{@@tilde}/; mono.level2 - when /^3#{@@tilde}/; mono.level3 - when /^4#{@@tilde}/; mono.level4 - when /^5#{@@tilde}/; mono.level5 - when /^6#{@@tilde}/; mono.level6 - when /^<:i([1-9])>/; mono.indent($1) - when /<:=/; mono.symbol_graphic #watch - when /^\s*<:image\s+/; mono.image - when /\}image/; mono.png - else - para.strip! - para=enclose(para) unless para =~/^$/ - end - para.gsub!(/(\.#{@@tilde}\S*\s*|<:\S+>||)/,' ') #% tread with care - #para.gsub!(/(.#{@@tilde}(?:\\~\S+)?\s*|<:\S+>||)/,' ') #KEEP reference, problem escaping open curly braces \{ - if para =~/<=curly/ #takes care of escaped curly braces, expand - do_mono=SiSU_TeX_Pdf::Format_text_object.new(@md,para) - para=do_mono.special_characters_curly(para) - end - @tex_file << para + if para.class == String + para=markup_common(para) + elsif para.class == Hash + para={ + :p => markup_common(para[:p]), + :l => markup_common(para[:l]) + } end + @tex_file << para end @tex_file << "\n\\newpage\n" # was \\pagebreak\n @md.subtitle_tex=@md.subtitle.dup if @md.subtitle @@ -573,62 +588,79 @@ WOK end @tex_file << "\n\\end{document}" end + def number_paras_numbering(para,ocn) + if para =~/<\\~(\d+);(?:[oh]|[0-6]:)\d+;\w\d+><#@dp:#@dp>$/ \ + and para !~/\\end\{longtable\}|/ #catch + m=/(.+?)<\\~(\d+);(?:[oh]|[0-6]:)\d+;\w\d+><#@dp:#@dp>$/m + parablock=para[m,1] + paranum=if ocn; para[m,2] + else '' + end + do_duo=SiSU_TeX_Pdf::Format_text_object.new(@md,parablock,paranum) + para=do_duo.para_num if parablock + elsif para =~/^<~\d+;(?:[oh]|[0-6]:)\d+;\w\d+><#@dp:#@dp>$/ #2005 this is added for tables, rationalise + m=/<~(\d+);(?:[oh]|[0-6]:)\d+;\w\d+><#@dp:#@dp>$/m + paranum=para[m,1] + para.gsub!(/<~\d+;(?:[oh]|[0-6]:)\d+;\w\d+><#@dp:#@dp>/,'') + para="\\marginpar{\\begin{tiny}#{paranum}\\end{tiny}}" + para + elsif para =~/^<~\d+;(?:[oh]|[0-6]:)\d+;\w\d+>/ #extra 2005 this is added for tables, rationalise + m=/<~(\d+);[oh]\d+;\w\d+>/m + paranum=para[m,1] + para.gsub!(/<~\d+;(?:[oh]|[0-6]:)\d+;\w\d+>/,'') + para="\\marginpar{\\begin{tiny}#{paranum}\\end{tiny}}" + para + elsif para =~/\\end\{longtable\}/ #catch + para.gsub!(/<\\~\d+>|<\\~(\d+);(?:[ohm]|[0-6]:)\d+;\w\d+><#@dp:#@dp>/,'') + para.gsub!(/<\\~\d+>|<\\~(\d+);(?:[ohm]|[0-6]:)\d+;\w\d+>/,'') #extra + end + para + end def number_paras(data,ocn) - @tex_file=[] + tex_file=[] data.each do |para| - if para =~/<\\~(\d+);(?:[oh]|[0-6]:)\d+;\w\d+><#@dp:#@dp>$/ \ - and para !~/\\end\{longtable\}|/ #catch - m=/(.+?)<\\~(\d+);(?:[oh]|[0-6]:)\d+;\w\d+><#@dp:#@dp>$/m - parablock=para[m,1] - paranum=if ocn; para[m,2] - else '' - end - do_duo=SiSU_TeX_Pdf::Format_text_object.new(@md,parablock,paranum) - para=do_duo.para_num if parablock - elsif para =~/^<~\d+;(?:[oh]|[0-6]:)\d+;\w\d+><#@dp:#@dp>$/ #2005 this is added for tables, rationalise - m=/<~(\d+);(?:[oh]|[0-6]:)\d+;\w\d+><#@dp:#@dp>$/m - paranum=para[m,1] - para.gsub!(/<~\d+;(?:[oh]|[0-6]:)\d+;\w\d+><#@dp:#@dp>/,'') - para="\\marginpar{\\begin{tiny}#{paranum}\\end{tiny}}" + para - #para="\\marginpar{\\begin{tiny}#{paranum}\\end{tiny}}" - #elsif para =~// - elsif para =~/^<~\d+;(?:[oh]|[0-6]:)\d+;\w\d+>/ #extra 2005 this is added for tables, rationalise - m=/<~(\d+);[oh]\d+;\w\d+>/m - paranum=para[m,1] - para.gsub!(/<~\d+;(?:[oh]|[0-6]:)\d+;\w\d+>/,'') - para="\\marginpar{\\begin{tiny}#{paranum}\\end{tiny}}" + para - elsif para =~/\\end\{longtable\}/ #catch - para.gsub!(/<\\~\d+>|<\\~(\d+);(?:[ohm]|[0-6]:)\d+;\w\d+><#@dp:#@dp>/,'') - para.gsub!(/<\\~\d+>|<\\~(\d+);(?:[ohm]|[0-6]:)\d+;\w\d+>/,'') #extra + para=if para.class == Hash \ + and (para[:p] and para[:l]) + para = { + :p => number_paras_numbering(para[:p],ocn), + :l => number_paras_numbering(para[:l],ocn) + } + else #elsif para.class == String + number_paras_numbering(para,ocn) end - @tex_file << para + tex_file << para end - @tex_file + tex_file end - def output(data) - data.flatten! - data.compact! + def output(array) + array.flatten! + array.compact! fns_l=@md.fns.gsub(/~/,'-') #this is a sorry fix, but necessary as it appears latex programs like not ~ - if @orientation =~/landscape/ - filename_tex_landscape=File.new("#{@env.path.tex}/#{fns_l}.landscape.tex",'w+') - filename_tex_landscape << @@tex_head_landscape - data.each do |para| - para.gsub!(/^\s+/,'') - filename_tex_landscape.puts para,"\n" if para !~/\A\s*\Z/ - end - filename_tex_landscape.close - @@tex_head_landscape=[] - elsif @orientation =~/portrait/ - filename_tex_portrait=File.new("#{@env.path.tex}/#{fns_l}.tex",'w+') - filename_tex_portrait << @@tex_head_portrait - data.each do |para| + filename_tex_landscape=File.new("#{@env.path.tex}/#{fns_l}.landscape.tex",'w+') + filename_tex_portrait=File.new("#{@env.path.tex}/#{fns_l}.tex",'w+') + filename_tex_portrait << @@tex_head[:p] + filename_tex_landscape << @@tex_head[:l] + array.each do |para| + case para + when String para.gsub!(/^\s+/,'') - filename_tex_portrait.puts para,"\n" if para !~/\A\s*\Z/ + if para !~/\A\s*\Z/ + filename_tex_portrait.puts para,"\n" + filename_tex_landscape.puts para,"\n" + end + when Hash + para[:p].gsub!(/^\s+/,'') + para[:l].gsub!(/^\s+/,'') + if para[:p] !~/\A\s*\Z/ + filename_tex_portrait.puts para[:p],"\n" + end + if para[:l] !~/\A\s*\Z/ + filename_tex_landscape.puts para[:l],"\n" + end end - filename_tex_portrait.close - @@tex_head_portrait=[] end - data=[] + filename_tex_portrait.close + filename_tex_landscape.close + @@tex_head={} #@@tex_head={ :p => nil, :l => nil } + array=[] end end end diff --git a/lib/sisu/v0/texpdf_format.rb b/lib/sisu/v0/texpdf_format.rb index f7d2177a..9506e17b 100644 --- a/lib/sisu/v0/texpdf_format.rb +++ b/lib/sisu/v0/texpdf_format.rb @@ -64,8 +64,8 @@ module SiSU_TeX_Pdf include SiSU_Viz class Use_TeX attr_accessor :url,:txt,:date - def initialize(md,orientation='') - @md,@orientation=md,orientation + def initialize(md) + @md=md @vz=SiSU_Env::Get_init.instance.skin @date=SiSU_Env::Info_date.new # #{@date.year} @copymark='{\\begin{footnotesize}\\raisebox{1ex}{\\copyright}\\end{footnotesize}}' @@ -122,8 +122,8 @@ module SiSU_TeX_Pdf "Contact Details for Original Promulgating Authority" end #BOOK standard dimensions - 229x156 - def newpage - case @orientation + def newpage(orientation) + case orientation when /landscape/ # using longtable latex package < 0, :l => 0 } @@sys=SiSU_Env::System_call.new @@flag_code=false @@dp=nil @@ -298,12 +298,12 @@ WOK else @tx.a4.landscape.w #default currently A4 end textwidth=(tw.to_i/2) - 24 - @@tableheader=1 if @string =~//," #@end_table\n\\end{tiny}") end @string.gsub!(//,1] @string.gsub!(/\/,'') @string.gsub!(/¡\d+?¡(.+?)(?:¡|!)/,'\bfseries \1&') @string.gsub!(/&>\s*$/," #@row_break \\hline\\endhead #@row_break") @string="#@string \\multicolumn{#{@@number_of_cols}}{l}{\\tiny #{tablefoot}} \\\\ \\hline\n\\endfoot\n\\hline\n" if tablefoot - @@tableheader,@@number_of_cols=0,0 + @@tableheader[:l],@@number_of_cols=0,0 end else if @string =~/¡\d+?¡(.+?)(?:¡|!)/ @@ -334,7 +334,9 @@ WOK @string.gsub!(/&>\s*$/," #@row_break") end end - @string="<~#{ocn}>" + @string + @string=if ocn; "<~#{ocn}>" + @string + else @string + end end def longtable_portrait @end_table='\end{longtable}' @@ -350,12 +352,12 @@ WOK else @tx.a4.portrait.w #default currently A4 end textwidth=tw.to_i - 20 - @@tableheader=1 if @string =~//," #@end_table\n\\end{tiny}") end @string.gsub!(//,1] @string.gsub!(/\/,'') @string.gsub!(/¡\d+?¡(.+?)(?:¡|!)/,'\bfseries \1&') @string.gsub!(/&>\s*$/," #@row_break \\hline\\endhead #@row_break") @string="#@string \\multicolumn{#{@@number_of_cols}}{l}{\\tiny #{tablefoot}} \\\\ \\hline\n\\endfoot\n\\hline\n" if tablefoot - @@tableheader,@@number_of_cols=0,0 + @@tableheader[:p],@@number_of_cols=0,0 end else if @string =~/¡\d+?¡(.+?)(?:¡|!)/ @@ -387,7 +389,9 @@ WOK @string.gsub!(/&>\s*$/," #@row_break") end end - @string="<~#{ocn}>" + @string + @string=if ocn; "<~#{ocn}>" + @string + else @string + end end def scopedtable # some features related to headers have been incorporated in longtable that are not included yet here, @@ -799,7 +803,7 @@ WOK @string.gsub!(/\\\{\S+\.(png|jpg|gif).+?\}(image|png)/,'') # fragile match operator\\ fragile ! end end - def http_word_mode(orientation='') + def http_word_mode #(orientation='') # clean up ! - work area, testing dir=SiSU_Env::Info_env.new(@md.fns) @words=[] @@ -831,16 +835,9 @@ WOK when /a5/; @tx.a5 else @tx.a4 end - width=if orientation =~/portrait/ #value is not currently passed - width=if width.to_i > dm.portrait.img_px - dm.portrait.img_px - else width - end - else - width=if width.to_i > dm.landscape.img_px - dm.landscape.img_px - else width - end + width=if width.to_i > dm.landscape.img_px + dm.landscape.img_px + else width end c=z[/``(.+?)''/m,1] hsp="\n{\\color{mywhite} .}&~\n" # ~ character for hardspace @@ -887,9 +884,9 @@ WOK @words=@words.join @words end - def http(orientation) + def http wm=@string.dup.scan(/\\\{.+?\\\}(?:(?:https?|file|ftp):\S+|image)|\w+\s*|./m) - @string=SiSU_TeX_Pdf::Format_text_object.new(@md,wm).http_word_mode(orientation) + @string=SiSU_TeX_Pdf::Format_text_object.new(@md,wm).http_word_mode #(orientation) end def language @lang=if @md.dc_language[:code] @@ -998,6 +995,18 @@ WOK \\setlength{\\columnsep}{#{d[:columnsep]}} \\setlength{\\marginparsep}{#{d[:marginparsep]}} \\setlength{\\marginparwidth}{#{d[:marginparwidth]}} +WOK + end + def tex_head_paper_portrait_dvi(d) + <