# coding: utf-8
=begin

 * Name: SiSU

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

 * Author: Ralph Amissah

 * Copyright: (C) 1997 - 2010, 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.jus.uio.no/sisu/gpl.fsf/toc.html>
   <http://www.jus.uio.no/sisu/gpl.fsf/doc.html>
   <http://www.jus.uio.no/sisu/gpl.fsf/plain.txt>

 * 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: texinfo processing

=end
module SiSU_TexInfo
  require "#{SiSU_lib}/html"                               # html.rb
  require "#{SiSU_lib}/param"                              # param.rb
    include SiSU_Param
  include SiSU_Viz
  #include Stamp ... needed removed arbitrarily 2005w05/1 (warnings about undefined flags)
  require "#{SiSU_lib}/texinfo_format"                     # texinfo_format.rb
  include SiSU_Texinfo_format
  @tex_file=[]
  @@tabular="{tabular}"
  @@table_pagebreak_counter,@@tex_endnote_call_counter,@@tex_table_flag,@@tex_counter,@@tex_column,@@tex_columns,@@counting=0,0,0,0,0,0,0
  @@column_instruct,@@tex_line_mode,@@tex_word_mode,@@start_table,@@line_mode='','','','',''
  @@n,@@copyright,@@tableheader=nil,nil,nil
  @@tex_col_w=[]
  @@tex_pattern_margin_number="\\\\marginpar.+?\s+"
  class Source
    include SiSU_Param
    include SiSU_Viz
    include SiSU_TexInfo
    def initialize(opt)
      @opt=opt
      @env=SiSU_Env::Info_env.new(@opt.fns)
      @vz=SiSU_Env::Get_init.instance.skin
    end
    def directories
      begin
        case @opt.fns
        when /\.(?:-|ssm\.)?sst$/
          Dir.mkdir(@env.path.output) unless FileTest.directory?("#{@env.path.output}")
          Dir.mkdir(@env.path.texi) unless FileTest.directory?(@env.path.tex)
        end
      rescue; STDERR.puts SiSU_Screen::Ansi.new(@opt.cmd,$!,$@).rescue
      ensure
      end
    end
    def read
      song
    end
    def song
      begin
        unless @opt.cmd =~/q/
          tell=SiSU_Screen::Ansi.new(@opt.cmd,'TexInfo')
          tell.green_title_hi
        end
        @md=SiSU_Param::Parameters.new(@opt).get
        puts "\t#{@@cX.grey}TexInfo#{@@cX.off}"
        my_make=SiSU_Env::Create_file.new(@opt.cmd,@opt.fns)
        directories
        @marshalfile=my_make.dal_content
        if FileTest.file?(@marshalfile)==true
          File.open(@marshalfile) { |f| @@tuned_file=Marshal.load(f)}
          tell.meta_verse_skipped unless @opt.cmd =~/q/
        else
          tex_array=IO.readlines(@opt.fns,'')
          SiSU_Metaverse.songsheet(tex_array)
        end
        tex_array=@@tuned_file
        Texinfo_make.new(@md,tex_array).songsheet
        tex_array=''
      rescue; STDERR.puts SiSU_Screen::Ansi.new(@opt.cmd,$!,$@).rescue
      ensure
      end
    end
  end
  class Texinfo_make
    include SiSU_Param
    include SiSU_Texinfo_format
    @@tex_1='(?:.+?)+~' #?? debug
    @@tabular="{tabular}"
    @@tex_pattern_margin_number="\\\\marginpar.+?\s+"
    def initialize(md,data)
      @md,@data=md,data
      @env=SiSU_Env::Info_env.new(@md.fns)
      @vz=SiSU_Env::Get_init.instance.skin
    end
    def songsheet
      begin
        @data=pre
        @data=endnote
        @data=markup
        @data=tail
        output
        makeinfo #KEEP reinstate when fixed #%
      rescue; STDERR.puts SiSU_Screen::Ansi.new(@md.cmd,$!,$@).rescue
      ensure
      end
    end
    def pre
      data=@data
      data.each do |dob|
        # DEBUG 2003w16 this is a kludge, because i could not get parameters
        # from param, Sort out ... revert to more elegant solution
        if dob.is =='table'
          @@flag['tables']='y' # KLUDGE get from param
        end
        dob.obj.gsub!(/<:p[bn]>/,'')
        dob.obj.gsub!(/#{Mx[:lnk_o]}(.+?)#{Mx[:lnk_c]}#{Mx[:url_o]}(\S+?)#{Mx[:url_c]}/,'\1(\2 [linked to:] \3)')
        dob.obj.gsub!(/(^|#{Mx[:gl_c]}|\s)\{(.+?)\}((?:https?|file):\/\/\S+)/,'\1(\2 [linked to:] \3)')
        do_mono=SiSU_Texinfo_format::Texinfo.new(@md,dob)
        dob.obj=do_mono.spec_char(dob.obj)
      end
      data
    end
    def endnote
      data=@data
      data.each do |dob|
        if dob.of=~/para|group/
          dob.obj.gsub!(/\s*#{Mx[:en_a_o]}(?:\d+)\s+(.+?)#{Mx[:en_a_c]}/m,' @footnote{ \1} ')
          dob.obj.gsub!(/\s*#{Mx[:en_a_o]}(\*+)\s+(.+?)#{Mx[:en_a_c]}/m,' @footnote{ \1} ')
        end
        dob
      end
      data
    end
    def poem
      data=@data
      @tex_file=[]
      @@counting=0
      data.each do |dob|
        if dob.is =='code'
          @@flag['code']=true
          @@counting=1
        end
        if dob.is =='verse'
          @@flag['poem']=1
        end
        if @@flag['code']
          if @@flag['code'] \
          and dob.obj =~ /#{Mx[:gr_o]}code[-_](?:end|close)#{Mx[:gr_c]}/ #watch change not tested 200501 #fix
            @@flag['code']=false
          end
          if @@flag['code'] \
          and dob.obj =~ /\S/
            sub_array=dob.obj.dup
            @@line_mode=sub_array.scan(/.+/)
            Tune.code_lines(@@line_mode)
            dob.obj=@@line_mode.join
          end
        elsif @@flag['poem']==1
          if @@flag['poem']==1 \
          and dob.obj =~ /#{Mx[:gr_o]}verse[-_](?:end|close)#{Mx[:gr_c]}/ #watch change not tested 200501 #fix
            @@flag['poem']=0
          end
          if @@flag['poem']==1 \
          and dob.obj =~ /\S/
            sub_array=dob.obj.dup
            @@line_mode=sub_array.scan(/.+/)
            Tune.code_lines(@@line_mode)
            dob.obj=@@line_mode.join
          end
        end
        @tex_file << dob.obj
      end
    end
    def code_lines
      data=@data
      data.each do |line|
        if line =~ /\S/ \
        and line !~ /#{Mx[:gr_o]}(code|verse).+/ #fix
          if @@flag['code']
            line.gsub!(/^\s*(.+)/m,"\\noindent \\marginpar\[left-text\]{\\begin{tiny}#{@@counting}\\end{tiny}}\\1\\")
            @@counting+=1 if @@flag['code']
          else line.gsub!(/(.+)/m,'\noindent\1')
          end
        end
      end
    end
    def tables
      data=@data
      @tex_file=[]
      @@tableheader=0
      data.each do |dob|
        if dob.obj =~ /#{Mx[:tc_p]}|#{Mx[:gr_o]}T/ui #fix
          do_mono=SiSU_Texinfo_format::Texinfo.new(@md,dob)
          dob.obj=do_mono.longtable # using longtable latex package
        end
        @tex_file << dob.obj
      end
    end
    def markup
      data=@data
      @tex_file=[]
      @row_break='\\\\\\'
      @break_page="#{@row_break}\n#{@row_break} \n"
      md={}
      @tex_file << SiSU_Texinfo_format::Texinfo.new(@md).head
      mono=SiSU_Texinfo_format::Texinfo.new(@md)
      @tex_file << mono.topnode(@md.title.full)
      texinfo_menu=[]
      n_menu,n_submenu=0,0
      @submenu,@subsubmenu={},{}
      data.each do |dob|
        if dob.is =='heading' \
        and dob.ln.to_s =~ /^[1-3]$/
          toc=SiSU_Texinfo_format::Texinfo.new(@md,dob)
          texinfo_menu << toc.menu
        elsif dob.is =='heading' \
        and dob.ln.to_s =~ /^[4-6]$/
          toc=SiSU_Texinfo_format::Texinfo.new(@md,dob)
          texinfo_menu << toc.menu
          case dob.ln
          when 4
            n_menu+=1
            @submenu[n_menu]=[]
          when 5
            n_submenu+=1
            @subsubmenu[n_menu]=[]
            @submenu[n_menu] << toc.menu
          when 6
            n_submenu+=1
            @subsubmenu[n_submenu]=[]
            @subsubmenu[n_submenu] << toc.menu
          end
        else
          dob.obj.gsub!(/\s*(?:<:?br>|<br \/>)\s*/,"\n\n")
        end
      end
      texinfo_menu.compact!
      texinfo_menu << "* Dublin Core::"
      @tex_file << texinfo_menu
      @tex_file << "* Index::\n" +
        "@end menu\n\n" +
        "@c %% 5\n\n"
      n_menu,n_submenu=0,0
      @@do_submenu,@@do_subsubmenu=1,1
      data.each do |dob|
        mono=SiSU_Texinfo_format::Texinfo.new(@md,dob)
        if dob.is=='heading'
          case dob.ln
          when 1; mono.level1
          when 2; mono.level2
          when 3; mono.level3
          when 4;
            mono.level4
            n_menu+=1
            @@do_submenu,@@do_subsubmenu=1,1
          when 5;
            n_submenu+=1
            @@do_subsubmenu=1
            if @@do_submenu==1
              menu=SiSU_Texinfo_format::Texinfo_txt.new(@md,dob,@submenu[n_menu])
              dob.obj="#{menu.submenu}#{mono.level5.obj}"
              @@do_submenu=0
            else mono.level5
            end
          when 6;
            if @@do_submenu==1
              menu=SiSU_Texinfo_format::Texinfo_txt.new(@md,dob,@submenu[n_menu])
              dob.obj="#{menu.subsubmenu}#{mono.level6.obj}"
              @@do_subsubmenu=0
            else
              mono.level6
            end
          end
        else
          if dob.obj !~/\S/
            dob.obj=nil
          else
            if dob.is=='para' \
            and dob.obj !~/##{dob.ocn}/
              dob.obj="#{dob.obj} ##{dob.ocn}"
            end
          end
        end
        #%case with endnotes
        dob.obj.gsub!(/\s*[0-8]\\+(\S+)?\s+/,' ') if dob.obj
        @tex_file << dob.obj if dob.obj and dob.is !~/structure|comment/ #sort exceptions
      end
      data=@tex_file
    end
    def number_titles
      data=@data
      @tex_file=[]
      input=%{#{@md.markup}}[/(num_top\s*=\s*(\d?))?/m,2] # else default usually 4  # this was a bit of a trick required to pass nil to input if nothing matched... #puts input
      num_top=input.to_i
      t_no1=0;  t_no2=0;  t_no3=0;  t_no4=0;
      no1=num_top; no2=(num_top + 1); no3=(num_top + 2);  no4=(num_top + 3);
      data.each do |dob|
        if @md.markup =~ /num_top/i \
        and dob.obj !~ /#{Rx[:meta]}/
          if dob.obj =~ /^[1-6]\\+(?:~\S+)?\s*<!h-.+?-!>/ \
          and dob.obj !~ /<:\d-endnotes>/
            header=dob.obj[/<!h-(.+?)-!>/m, 1].gsub!(/-/m,'.')
            dob.obj.gsub!(/^(?:[1-6]\\+(?:~\S+)|<:([12356]|4-.+?-)>)\s*<!h-.+?-!>/,
              "\\1 #{header} ")
          end
        elsif dob.obj=~ /<!h!>|<!h\d!>|<!h.+?!>|<!!h.+?!>/
          if dob.obj=~ /<!h-.+?-!>/
            dob.obj.gsub!(/<!h-(.+?)-!>/,'\1 ')
          end
        end
        @tex_file << dob.obj
      end
      data=@tex_file
    end
    def tail
      data=@data
      tex=SiSU_Texinfo_format::Texinfo.new(@md)
      data << tex.dublincore
      data << tex.tail
    end
    def output
      data=@data
      data.compact!
      filename=%{#{@env.path.texi}/#{@md.fnb}.texinfo}
      filename_texinfo=File.new(filename,'w+')
      puts filename if @md.cmd =~/M/
      data.each {|s| (filename_texinfo.puts s,"\n") if s}
      filename_texinfo.close
    end
    def makeinfo
      if @md.fns =~/\.(?:-|ssm\.)?sst$/
        m=/(.+?)\.([_-]?sst)$/.match(@md.fns)
        fnb,sfx=m[1],m[2]
        pwd=Dir.pwd
        case sfx
        when /(?:-|ssm\.)?sst$/
          @env=SiSU_Env::Info_env.new(@md.fns,@md.cmd)
          Dir.chdir(@env.path.texi)
          texinfo=SiSU_Env::System_call.new("#{fnb}.texinfo")
          texinfo.makeinfo
        end
        Dir.chdir(pwd)
      end
    end
  end
end
__END__
