aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib/sisu/v0/texpdf.rb
blob: 989899cf45ef72f02d0730feb96d0895a83e06c2 (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
=begin

 * Name: SiSU

 * Description: a framework for document structuring, publishing and search

 * Author: Ralph Amissah

 * Copyright: (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
   2007 Ralph Amissah All Rights Reserved.

 * License: GPL 3 or later:

   SiSU, a framework for document structuring, publishing and search

   Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
   2007 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/licenses/gpl.html>
   <http://www.gnu.org/copyleft/gpl.html>
   <http://www.jus.uio.no/sisu/gpl.fsf>

 * 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>

 * Download:
   <http://www.jus.uio.no/sisu/SiSU/download.html>

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

 ** Description: LaTeX generation

=end
module SiSU_TeX
  require 'pstore'
  require "#{SiSU_lib}/defaults"
  require "#{SiSU_lib}/param"
  include SiSU_Param
  include SiSU_Viz
  require "#{SiSU_lib}/texpdf_format"
  include SiSU_TeX_Pdf
  @tex_file=@@tex_footnote_array=@@tex_col_w=[]
  @@tex_backslash ||="\\\\"
  @@tilde='\\\\\\~' #?? debug crazy
  @@tabular="{tabular}"
  @@column_instruct=@@squigle_close=@@tex_line_mode=@@tex_word_mode=@@line_mode=''
  @@tex_debug_counter=@@table_pagebreak_counter=@@tex_footnote_call_counter=@@tex_table_flag=@@tex_counter=@@tex_column=@@tex_columns=@@tex_columns=@@counting=0
  @@tex_pattern_margin_number="\\\\begin\\\{tiny\\\}~\\\\end\\\{tiny\\\}\\\{\\\\marginpar.+?\s+"
  #@@tex_pattern_margin_number="\\\\marginpar.+?\s+"
  @@n=@@tableheader=@@rights=nil
  @@date ||=SiSU_Env::Info_date.new
  class Source #Songsheet #<SiSU_Param::MyFiles
    require 'pstore'
    require "#{SiSU_lib}/sysenv"
    require "#{SiSU_lib}/param"
    include SiSU_Env
    include SiSU_Param
    include SiSU_Viz
    require "#{SiSU_lib}/dal"
    include SiSU_DAL
    include SiSU_TeX
    def initialize(opt)
      @opt=opt
      @md=SiSU_Param::Parameters.new(@opt).get
      @env=SiSU_Env::Info_env.new(@opt.fns)
    end
    def directories
      begin
        @dir_out_root=@env.path.output
        case @opt.fns
        when /\.[_-]?sst$/
          SiSU_Env::SiSU_file.new(@md).mkdir
          Dir.mkdir(@env.path.tex) unless FileTest.directory?(@env.path.tex)
        end
      rescue; SiSU_Errors::Info_error.new($!,$@,@opt.cmd,@opt.fns).error
      ensure
      end
    end
    def read
      songsheet
    end
    def songsheet
      begin
        @md=SiSU_Param::Parameters.new(@opt).get
        tell=SiSU_Screen::Ansi.new(@opt.cmd,'LaTeX/PDF')
        tell.green_title_hi unless @opt.cmd =~/q/
        path=@env.url.output_tell
        tell=SiSU_Screen::Ansi.new(@opt.cmd,@opt.fns,"#{@env.program.pdf_viewer} #{path}/#{@md.fnb}/#{@md.fn[:pdf_l]}")
        tell.flow if @opt.cmd =~/[MVv]/
        tell=SiSU_Screen::Ansi.new(@opt.cmd,@opt.fns,"#{@env.program.pdf_viewer} #{path}/#{@md.fnb}/#{@md.fn[:pdf_p]}")
        tell.flow if @opt.cmd =~/[MVv]/
        @md=SiSU_Param::Parameters.new(@opt).get
        my_make=SiSU_Env::Create_file.new(@opt.cmd,@opt.fns)
        if @md.flag_pdf
          $flag=@md.cmd                                                         #introduced to pass 0 for no object citation numbers... to texpdf_format
          @dir_o="#{@env.path.output}/#{@opt.fnb}"
          directories
          filename_index="#{@md.txt_path}/pdf"
          newfilename=filename_index
                                                                               #% 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
          dal_array=''
          pwd=Dir.pwd
          SiSU_TeX::Source::LaTeX_to_pdf.new(@md).latexrun_selective
          Dir.chdir(pwd)
        else
          tell=SiSU_Screen::Ansi.new(@opt.cmd,'instructed NOT to produce LaTeX/PDF')
          tell.warn unless @opt.cmd =~/q/
        end
      rescue; SiSU_Errors::Info_error.new($!,$@,@opt.cmd,@opt.fns).error
      ensure
        unless @opt.cmd =~/[MV]/ #check maintenance flag
          texfiles=Dir["#{@env.path.tex}/#{@opt.fns}*"]
          texfiles.each do |f|
            if FileTest.file?(f)
              File.unlink(f)
            end
          end
        end
        @tex_file=@@tex_footnote_array=[]
        @@column_instruct=''
        @@squigle_close=@@tex_line_mode=@@tex_word_mode=@@line_mode=''
        @@tex_debug_counter=@@table_pagebreak_counter=@@tex_footnote_call_counter=@@tex_table_flag=@@tex_counter=@@tex_column=@@tex_columns=@@tex_columns=@@counting=0
        @@tex_col_w=[]
        @@n=@@tableheader=@@rights=nil
        @@date=SiSU_Env::Info_date.new
        @@flag={}
        $flag=1 #remove at some stage
        SiSU_Env::Create_file.new(@opt.cmd,@opt.fns).param_instantiate
      end
    end
    private
    class LaTeX_to_pdf #<Songsheet
      require 'fileutils'
      include FileUtils #::Verbose
      @@n_lpdf||=0 #change
      def initialize(md)
        @md=md
        @env=SiSU_Env::Info_env.new(@md.fns)
      end
      def latex_do(texfilename)
        @texfilename=texfilename
        @@n_lpdf=@@n_lpdf+1
        tell=SiSU_Screen::Ansi.new(@md.cmd,'portrait ->')
        tell.dark_grey_title_hi if @md.cmd =~/[MVv]/
        tex_fn_base=@texfilename.gsub(/\.tex$/,'')
        cmd=SiSU_Env::System_call.new("#{tex_fn_base}.tex",'',@md.cmd)
        tell=SiSU_Screen::Ansi.new(@md.cmd)
        tell.grey_open unless @md.cmd =~/q/
        2.times { |i| cmd.latex2pdf unless ("#{tex_fn_base}.tex" !~/\w+/) }
        tell.p_off unless @md.cmd =~/q/
        tell=SiSU_Screen::Ansi.new(@md.cmd,'landscape ->')
        tell.dark_grey_title_hi if @md.cmd =~/[MVv]/
        cmd=SiSU_Env::System_call.new("#{tex_fn_base}.landscape.tex",'',@md.cmd)
        tell.grey_open
        2.times { |i| cmd.latex2pdf unless ("#{tex_fn_base}.landscape.tex" !~/\w+/) }
        tell.p_off unless @md.cmd =~/q/
        pwd=Dir.pwd
        portrait_pdf="#{pwd}/#{tex_fn_base}.pdf"
        landscape_pdf="#{pwd}/#{tex_fn_base}.landscape.pdf"
        if FileTest.file?(portrait_pdf)
          File.install(portrait_pdf,"#@dir_sisu/#{@md.fnb}/#{@md.fn[:pdf_p]}")
          File.unlink(portrait_pdf)
        end
        if FileTest.file?(landscape_pdf)
          File.install(landscape_pdf,"#@dir_sisu/#{@md.fnb}/#{@md.fn[:pdf_l]}")
          File.unlink(landscape_pdf)
        end
        tell=SiSU_Screen::Ansi.new(@md.cmd,@@n_lpdf,'processed (SiSU LaTeX to pdf - using pdfetex aka. pdftex or pdflatex)')
        tell.generic_number unless @md.cmd =~/q/
      end
      def latexrun_selective
        begin
          pwd=Dir.pwd
          Dir.chdir(pwd) #watch
          @tex_f_no=0
          info={}
          if FileTest.file?(@env.source_file_with_path)
            if @md.fns =~/\.[_-]?sst$/
              @dirout=SiSU_Env::Info_env.new(@md.fns)
              case @md.fns
              when /\.[_-]?sst$/
                if FileTest.directory?(@env.path.tex)==true
                  Dir.chdir(@env.path.tex)
                  @dir_sisu=@dirout.path.output
                  texfile=@md.fns.gsub(/$/,'.tex')
                  texfile=texfile.gsub(/~/,'-')
                  if File.exist?(texfile) and File.size(texfile) > 0
                    @tex_f_no+=1
                    latex_do(texfile)
                  else
                    puts "\tzero file size #{@env.path.tex}/#{@md.fns}"
                  end
                end
              end
            end
          else
            tell=SiSU_Screen::Ansi.new(@md.cmd,"FILE NOT FOUND: << #{@md.fns} >> - requested latex system processing skipped")
            tell.warn
          end
          lst=Dir["*.{aux,log,out}"]
          lst.each {|file| File.unlink(file)} if lst
          #touch("#{@dir_pdf}index.html") #correct @dir_pdf appears to contain slash / and should not
        rescue; SiSU_Errors::Info_error.new($!,$@,@md.fns,@md.cmd).error
        end
      end
    end
    class LaTeX_create
      include SiSU_Param
      @@tex_backslash ||="\\\\"
      @@tilde='\\\\\\~' #?? debug crazy
      @@tex_head_portrait,@@tex_head_landscape=nil,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
        @env=SiSU_Env::Info_env.new(@md.fns)
        @st={ :tex=>{} }
        @tex=SiSU_TeX_Pdf::Use_TeX.new(@md,@orientation)
        @vz=SiSU_Env::Get_init.instance.skin
        @dp=@@dp ||=SiSU_Env::Info_env.new.digest.pattern
      end
      def songsheet
        begin
          data=@data
          @@tex_footnote_array=[]
          @@rights=nil
          tell=SiSU_Screen::Ansi.new(@md.cmd,"pdfTex #@orientation")
          tell.txt_grey unless @md.cmd =~/q/
          if @md.dc_rights
            use=@md.dc_rights.dup #dup is necessary, else contents of :rights changed
            do_mono=SiSU_TeX_Pdf::Format_text_object.new(@md,use)
            copymark=if @md.creator_copymark; '{\\begin{small}\\raisebox{1ex}{\\copyright}\\end{small}} '
            else ''
            end
            copymark='Copyright {\\begin{small}\\raisebox{1ex}{\\copyright}\\end{small}} '
            copyright=do_mono.special_characters_safe.gsub(/^\s*Copyright \(C\)/, copymark)
            @@rights||="\n #{@@tex_backslash*2}[3]\\ \\linebreak #{copyright}"
          end
          if @md.prefix_b
            do_mono=SiSU_TeX_Pdf::Format_text_object.new(@md,@md.prefix_b)
            prefix_b=do_mono.special_characters_safe
            @@prefix_b="\n #{@@tex_backslash*2}[3]\\ \\linebreak \\ #{prefix_b}\n" unless @@prefix_b
          end
          if @md.markup.to_s !~/url_png/
            data=pre(data)
            data=footnote(data)
            if @md.flag_tables
             data=tables(data)
            end
            data=number_paras(data) if @md.markup !~/not_to/ #check
            data=markup(data)
            output(data)
          end
        rescue; SiSU_Errors::Info_error.new($!,$@,@md.fns,@md.cmd).error
        ensure
        end
      end
    protected
      def pre(data)
        @tex_file=[]
        data.each do |para|
          # DEBUG 2003w16 this is a kludge, because i could not get parameters
          # from param, Sort out ... revert to more elegant solution
          # even more of a kludge as had to insert newlines where code is used not satisfactory, think about
          para=if para =~/<:br>|\n/; para.split(/<:br>|\n/)
          else para
          end
          if para.class == String
            @md.flag_tables=true if para =~/<!Th?¡\s+c/
            do_mono=SiSU_TeX_Pdf::Format_text_object.new(@md,para)
            @tex_file << do_mono.special_characters
          elsif para.class == Array
            para.each do |grp|
              @md.flag_tables=true if grp =~/<!Th?¡\s+c/
              do_mono=SiSU_TeX_Pdf::Format_text_object.new(@md,grp)
              @tex_file << do_mono.special_characters
            end
          end
        end
        data=@tex_file.delete_if {|x| x =~/^\s*0#{@@tilde}/}
        @tex_file
      end
      def footnote(data)
        @tex_file=[]
        data.each do |para|
          # EMBEDDED FOOTNOTES / ENDNOTES should be straightforward but not quite a synch.
          footnote=[]
          if para =~/~\\\{[\d*+]+\s|\\\~\[([*+]\d+)\s/                                                # note escape not necessary in front of ~ has implications for many other matches #debug note
            para.gsub!(/\\~\\\{(\d+)\s+(.+?)<#@dp>\\\}\\~/m,"\\footnote[\\1]{%\n \\2} ") #removed space before \\footnote 2004w21, watch
            para.gsub!(/\\~\[([*+]\d+)\s+(.+?)<#@dp>\]\\~/m,"\\FootnoteA{\\1}{%\n \\2} ") #work on asterisk footnotes
            para.gsub!(/\\~\\\{([*+]+)\s+(.+?)<#@dp>\\\}\\~/m,"\\FootnoteA{\\1}{%\n \\2} ") #work on asterisk footnotes
          end
          @tex_file << para
        end
        @tex_file
      end
      def tables(data)
        @tex_file=[]
        @@tableheader=0
        data.each do |para|
          if para =~/¡|<!T/
            do_mono=SiSU_TeX_Pdf::Format_text_object.new(@md,para)
            case @orientation                                                   # using longtable latex package
            when /landscape/; para=do_mono.longtable_landscape
            when /portrait/; para=do_mono.longtable_portrait
            end
          end
          @tex_file << para
        end
      end
      def enclose(para,type='')
        para.strip!
        if type =~/code/; para
        elsif para !~/(\\begin\{tabular\}.*|\\end\{tabular\}|&|#{@@tex_backslash*2})\s*$/ #check
          para.gsub!(/(.+)/m,"\n#{@tex.skip_small} \\1 #{@tex.skip_small}\n")
        else para
        end
      end
      def markup(data)
        @tex_file=[]
        md={}
        #% document headers watch special characters in title & subtitle: glark "\{\{~(sub)?title.+?[$&%#_\{\}]" *.(lm|er)??
        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
        @tex_file <<<<WOK
#{@tex.header}#{@tex.footer}
\\tolerance=500
\\makeatother
\\makeatother
\\begin{document}
WOK
        sisu_rc_footnote=if @md.sc_info; @tex.doc_sc_info_footnote_full
        else                             @tex.doc_sc_info_footnote_brief
        end
        #@copymark=if @md.creator_copymark; '^\copyright'
        #else                               ''
        #end
        @copymark='' #check and remove as now is superflous
        if @orientation =~/landscape/ # using longtable latex package
          @tex_file << SiSU_TeX_Pdf::Format_text_object.new(@md,@md.title,@md.subtitle).title_landscape
          if @md.dc_creator
            do_mono=SiSU_TeX_Pdf::Format_text_object.new(@md,@md.dc_creator)
            creator=do_mono.special_characters
            @tex_file << if @md.creator_home
              <<WOK

\\author{\\href{#{@md.creator_home}}{#@copymark \\textnormal{#{creator}}}}
WOK
            else "\n\\author{#@copymark \\textnormal{#{creator}}}"
            end
          end
          @tex_file << unless @md.fnb =~/^mail\s*$/
            "\n\\date{\\begin{footnotesize} copy @ #{@tex.site} \\end{footnotesize} #{sisu_rc_footnote}}"
          else "\\date"
          end
        else #portrait
          @tex_file << SiSU_TeX_Pdf::Format_text_object.new(@md,@md.title,@md.subtitle).title_portrait
          if @md.dc_creator
            creator=@md.dc_creator
            @tex_file << if @md.creator_home
              <<WOK

\\author{\\href{#{@md.creator_home}}{#@copymark \\textnormal{#{creator}}}}
WOK
            else "\n\\author{#@copymark \\textnormal{#{creator}}}"
            end
           end
          @tex_file << unless @md.fnb =~/^mail\s*$/; "\n\\date{\\begin{footnotesize} copy @ #{@tex.site} \\end{footnotesize} #{sisu_rc_footnote}}"
          else                                       "\\date"
          end
        end
        @tex_file <<<<WOK
\\pagenumbering{roman}\\maketitle
\\pagestyle{fancy}
WOK
        #@tex_file << @tex.newpage #newpage clearpage depending on portrait or landscape
        @tex_file << "\\newpage\n"
        @tex_file << @@rights if @md.dc_rights #if @orientation =~/landscape/
        @tex_file << @@prefix_b if @md.prefix_b
        @tex_file << @tex.sisu_rights
        @tex_file << if @md.sc_info; @tex.doc_sc_info
        else                         @tex.doc_no_sc_info
        end
        @tex_file << @tex.manifest_info
        @tex_file << @tex.newpage
        @tex_file <<<<WOK
\\pagestyle{fancy}
\\tableofcontents
#{@tex.newpage}
\\pagenumbering{arabic}
#{@tex.paraskip_normal}
WOK
        @tex_file << @tex.newpage
        @group_collect=[]
        data.each do |para|                                                      #% case follows with levels 1-6 indents & graphics
          mono=SiSU_TeX_Pdf::Format_text_object.new(@md,para)
          if para =~/<:(code|alt|verse|group)>/ 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
          end
        end
        @tex_file << "\n\\newpage\n" # was \\pagebreak\n
        @md.subtitle_tex=@md.subtitle.dup if @md.subtitle
        # kludge ... look again later
        @tex_file << if @md.doc_skin !~/skin_mail/; @tex.doc_tail
        else                                        @tex.mail_tail
        end
        if defined? @md.lnk and @md.lnk
          @md.lnk.each do |l|
            if l[:say]
              url=%<#{l[:url]}>
              url.gsub!(/(?:\\)*([$&~%_#}{^])/,"\\\\\\1")                         #latex special chars
              s_lnk=l[:say]
              s_lnk.gsub!(/(<br>|<br \/>|<p>|<p \/>)/,'')
              s_lnk.gsub!(/(?:\\)*([$&~%_#}{^])/,"\\\\\\1")                       #latex special chars
              if url !~/^\.(\.)?\//
                s_lnk_url=%<\\begin{scriptsize}\\href{#{url}}{#{url}}\\end{scriptsize}>  # note this bit of dereferencing magic
              else
                url.gsub!(/\.\.\//,'')
                s_lnk_url="(#{@tex.site}) \\\\\n" + ' ' +
                  "\\begin{scriptsize}" +
                  %<\\href\{#{@vz.url_root_http}/#{url}\}\{#{@vz.url_root_http}/#{url}\}> + # note this bit of dereferencing magic
                  "\\end{scriptsize}"
              end
              @tex_file << " #{s_lnk} \\\\\n #{s_lnk_url} \n" unless  @md.doc_skin =~/skin_mail/
              s_lnk=s_lnk_url=nil
            end
          end
        end
        @tex_file << " #{@tex.sitename} home: \\\\
\\begin{bfseries}#{@tex.site}\\end{bfseries}
"
        #Stamp.stamp #removed 200408 but watch
        #% code for inclusion of addresses of promulgating authority
        @st[:tex][:stmp]||=@md.stmpd
        stamp=@st[:tex][:stmp] if @st[:tex][:stmp]
        if stamp
          use=stamp.gsub(/\n/,"#{@@tex_backslash*2}\n")
          @tex_file << "\n\\newpage\n"
          @tex_file << "\\section*" +
            "{#{@tex.owner_chapter}}\n" +
            "\\addcontentsline{toc}" +
            "{section}{#{@tex.owner_chapter}}\n"
          @tex_file << "#{use}\n"
          @tex_file << @@rights if @@rights
        end
        @tex_file << "\n\\end{document}"
      end
      def number_paras(data)
        @tex_file=[]
        data.each do |para|
          if para =~/<\\~(\d+);(?:[oh]|[0-6]:)\d+;\w\d+><#@dp:#@dp>$/ and para !~/\\end\{longtable\}|<EOF>/ #catch <!TZ!>
            m=/(.+?)<\\~(\d+);(?:[oh]|[0-6]:)\d+;\w\d+><#@dp:#@dp>$/m
            parablock=para[m,1]
            paranum=para[m,2]
            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 =~/<!T.+?!~\d+?>/
          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 <!TZ!>
            para.gsub!(/<\\~\d+>|<\\~(\d+);(?:[ohm]|[0-6]:)\d+;\w\d+><#@dp:#@dp>/,'')
            para.gsub!(/<\\~\d+>|<\\~(\d+);(?:[ohm]|[0-6]:)\d+;\w\d+>/,'') #extra
          end
          @tex_file << para
        end
        @tex_file
      end
      def output(data)
        data.flatten!
        data.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|
            para.gsub!(/^\s+/,'')
            filename_tex_portrait.puts para,"\n" if para !~/\A\s*\Z/
          end
          filename_tex_portrait.close
          @@tex_head_portrait=[]
        end
        data=[]
      end
    end
  end
end
__END__