class Dyndoc::Document

Attributes

cfg[RW]

gather in @cfg optionnal parameters @content really matters to be included in @cfg!

content[RW]

gather in @cfg optionnal parameters @content really matters to be included in @cfg!

inputs[RW]

gather in @cfg optionnal parameters @content really matters to be included in @cfg!

tmpl_doc[RW]

gather in @cfg optionnal parameters @content really matters to be included in @cfg!

Public Class Methods

cfg() click to toggle source
# File lib/dyndoc/document.rb, line 351
def Document.cfg
  @@cfg.dup
end
new(key_doc,tmpl_doc) click to toggle source
# File lib/dyndoc/document.rb, line 362
def initialize(key_doc,tmpl_doc)
  @tmpl_doc=tmpl_doc #to be aware of the cfg of tmpl_doc!
  ##p [:tmpl_doc,@tmpl_doc]
  @cfg=Document.cfg
  @content=""
  @cfg[:key_doc]=key_doc #just to record the key of this document
  # update @cfg
  ## cmd
  @cfg[:cmd].uniq!
  @cfg[:cmd] = [:save] if @cfg[:model_doc] and @cfg[:model_doc] != "default"
  append_cfg(@tmpl_doc.cfg[:docs][key_doc])
  ## from command line
  read_cmdline
  # update cmd
  # p @tmpl_doc.cfg[:cmd];p @cfg[:cmd]
  @cfg[:cmd]=@tmpl_doc.cfg[:cmd] unless @tmpl_doc.cfg[:cmd].empty?
  ## TODO: MODE MULTIDOC => maybe to correct if options differ for each document
  @cfg[:cmd_pandoc_options]=@tmpl_doc.cfg[:cmd_pandoc_options] unless @tmpl_doc.cfg[:cmd_pandoc_options].empty?
  ##p [:pandoc_cfg,@tmpl_doc.cfg[:pandoc_filter]]
  @cfg[:pandoc_filter]=@tmpl_doc.cfg[:pandoc_filter] unless @tmpl_doc.cfg[:pandoc_filter].empty?
  # debug mode
  p @tmpl_doc.cfg if @tmpl_doc.cfg[:debug]
  p [:cfg,@cfg] if @tmpl_doc.cfg[:debug]
  # autocomplete the document filename if necessary!
  filename_completion
  @filename = @cfg[:filename_doc]
  @dirname = @tmpl_doc.dirname_orig
end

Public Instance Methods

cd_new() click to toggle source
# File lib/dyndoc/document.rb, line 543
def cd_new
  Dir.chdir(@dirname)
end
cd_old() click to toggle source
# File lib/dyndoc/document.rb, line 547
def cd_old
  Dir.chdir(@curdir)
end
close_log() click to toggle source
# File lib/dyndoc/document.rb, line 463
def close_log
  $dyn_logger.close if $dyn_logger
end
filename_completion() click to toggle source

# do we have to save the content to some file def to_be_saved?

@cfg[:filename_doc]!=:no

end

# File lib/dyndoc/document.rb, line 405
    def filename_completion
#p @tmpl_doc.basename_orig
#p @cfg[:append_doc]
#p Dyndoc.docExt(@cfg[:format_doc])
      ext_mode=nil
      if @tmpl_doc.basename_orig =~ /\_(html|tex|c|rb|txtl|md|txt|raw)$/
        @cfg[:cmd] += [:make_content,:save]
        ext_mode=$1.to_sym
        @cfg[:format_doc]=@cfg[:mode_doc]=@cfg[:format_output]=(ext_mode == :raw ? :txt : ext_mode )
        last=-(2 + $1.length)
      else
        last=-1
      end
      ##DEBUG:
      p [@tmpl_doc.basename_orig[0..last],@tmpl_doc.cfg[:append],@cfg[:append_doc],@cfg[:format_doc],Dyndoc.docExt(ext_mode || @cfg[:format_doc])]
      @cfg[:filename_doc]=@tmpl_doc.basename_orig[0..last]+@tmpl_doc.cfg[:append]+@cfg[:append_doc]+Dyndoc.docExt(ext_mode || @cfg[:format_doc]) if @cfg[:filename_doc].empty?
      ##p [:filename_completion,@filename,@cfg[:filename_doc] ]
    end
init_doc() click to toggle source
# File lib/dyndoc/document.rb, line 509
    def init_doc
      Dyndoc.mode=out=@cfg[:format_doc]
      unless @tmpl_doc.cfg[:raw_mode]
        if ( tmp=Dyndoc.doc_filename("Dyn/.preload",[""],nil))
          @cfg[:pre_doc] += File.read(tmp).split("\n").map{|l| l.split(",")}.flatten.map{|e| e.strip}
        end

        if (tmp=Dyndoc.doc_filename("Dyn/.postload",[""],nil))
          @cfg[:post_doc] += File.read(tmp).split("\n").map{|l| l.split(",")}.flatten.map{|e| e.strip}
        end
        if out
## default preload
                out=:tex if out==:ttm
          outDir=out.to_s.capitalize
          if (tmp=Dyndoc.doc_filename("#{outDir}/.preload",[""],nil))
            @cfg[:pre_doc] += File.read(tmp).split("\n").map{|l| l.split(",")}.flatten.map{|e| e.strip}.map{|t| File.join(Dyndoc.cfg_dir[:tmpl_path][out],t)}
          end
## default postload
          if (tmp=Dyndoc.doc_filename("#{outDir}/.postload",[""],nil))
            @cfg[:post_doc] += File.read(tmp).split("\n").map{|l| l.split(",")}.flatten.map{|e| e.strip}.map{|t| File.join(Dyndoc.cfg_dir[:tmpl_path][out],t)}
          end
        end
      end
#p @cfg[:pre_doc]
#p @cfg[:post_doc]
      #model_doc
      @cfg[:model_doc]=nil unless @tmpl_doc.cfg[:model_tmpl]
      @cfg[:model_doc]=@tmpl_doc.cfg[:model_doc] if @tmpl_doc.cfg[:model_doc]
      @cfg[:cmd] -= [:png,:pdf,:view] if @cfg[:model_doc]=="content"
      # TO REMOVE: Dyndoc.mode=(out)
      # prepare the initialization of the TemplateManager
      @tmpl_doc.tmpl_mngr.init_doc(@cfg)
    end
make_all() click to toggle source

start ##################################################

# File lib/dyndoc/document.rb, line 425
    def make_all
#puts "make_all";p @cfg[:cmd]
      make_prelim
#puts "make_all";p @cfg[:cmd]
      cd_new
      open_log

      # First, save or rm (both is useless) the old file
      make_old(:save) if @cfg[:cmd].include? :save_old
      make_old(:rm)   if @cfg[:cmd].include? :rm_old

      # make content
      make_content if @cfg[:cmd].include? :make_content
      ##OBSOLETE## @content=make_ttm if @cfg[:format_doc]==:ttm
      @cfg[:cmd] -= [:save] if Dyndoc.cfg_dyn[:exec_mode]=="yes"
      #puts "make_all";p [@cfg[:cmd],Dyndoc.cfg_dyn[:exec_mode]]
      make_save unless (@cfg[:cmd] & [:save,:save!]).empty?
      ##OBSOLETE## make_pandoc if @cfg[:cmd].include? :pandoc
      ##OBSOLETE## make_backup if @cfg[:cmd].include? :backup
      ##OBSOLETE## make_cat if @cfg[:cmd].include? :cat
      # added for docker mode when latex is done via another docker
      ##OBSOLETE## make_docker if Dyndoc.cfg_dyn[:docker_mode] and @cfg[:format_doc]==:tex
      make_task_pdflatex if @cfg[:cmd].include? :pdf ##OBSOLETE## and !Dyndoc.cfg_dyn[:docker_mode]
      make_task_pandoc if @cfg[:cmd].include? :make_task_pandoc
      ##OBSOLETE## make_png if @cfg[:cmd].include? :png ##OBSOLETE## and !Dyndoc.cfg_dyn[:docker_mode]
      ##OBSOLETE## make_view if @cfg[:cmd].include? :view # added for docker mode when latex is done via another docker
      close_log
      cd_old
    end
make_content() click to toggle source

make ########################################### make content

# File lib/dyndoc/document.rb, line 565
def make_content
   ## if true
   if @tmpl_doc.cfg[:debug] or Settings["cfg_dyn.debug"]
     @tmpl_doc.tmpl_mngr.echo=0
     @tmpl_doc.tmpl_mngr.doc=self
     ## p [:make_content,@tmpl_doc.content]
     @content=@tmpl_doc.tmpl_mngr.output(@tmpl_doc.content)
     print "\nmake content for #{@basename} in #{@dirname} -> ok\n"
   else
     print "\nmake content for #{@basename} in #{@dirname}\n"
     begin
       @tmpl_doc.tmpl_mngr.echo=0
       @tmpl_doc.tmpl_mngr.doc=self
       @content=@tmpl_doc.tmpl_mngr.output(@tmpl_doc.content)
       ##puts "@content";p @content
       print " -> ok\n"
     rescue
       ok=false
       @content=nil ## added to know if one need to restaure
       print " -> NO, NO and NO!!\n"
     end
   end
 end
make_odt_automatic_styles() click to toggle source
# File lib/dyndoc/document.rb, line 613
    def make_odt_automatic_styles
#puts "make_odt_automatic_styles:@inputs"; p @inputs
      return if !@inputs or @inputs.empty?
      autostyles = @content_xml.root.elements['office:automatic-styles']
#puts "autostyles";p autostyles
#p @automatic_styles
      # add the automatic styles from input template
      if autostyles
        @inputs.values.each do |input|
          input.automatic_styles.each_element do |e|
            autostyles << e
          end
        end
      end
    end
make_odt_content_xml() click to toggle source
# File lib/dyndoc/document.rb, line 608
    def make_odt_content_xml
      @content_xml=REXML::Document.new(@content)
#p @content_xml.to_s
    end
make_odt_ressources() click to toggle source
# File lib/dyndoc/document.rb, line 629
def make_odt_ressources
  return if !@inputs or @inputs.empty?
  # create directories if necessary
  @ar.find_entry('ObjectReplacements') || @ar.mkdir('ObjectReplacements')
  @ar.find_entry('Pictures') || @ar.mkdir('Pictures')
  # create the necessary files
  @inputs.values.each do |input|
        input.xlink_href.keys.each do |key|
      if (entry=input.ar.find_entry(key))
        @ar.get_output_stream(input.xlink_href[key]) do |f|
            f.write input.ar.read(entry)
        end
      end
    end
  end
end
make_old(mode=:rm) click to toggle source

As soon as possible when using dropbox or tools

# File lib/dyndoc/document.rb, line 664
def make_old(mode=:rm) #mode=:rm or :save
  ## After introduction of dyntask, the default is to save the old file if existing
  if File.exist? @filename
    case mode
    when :save
      FileUtils.mkdir_p(File.join(File.dirname(@filename),".save"))
      FileUtils.mv(@filename,@filename_old=File.join(File.dirname(@filename),".save",File.basename(@filename)))
    when :rm
      FileUtils.rm(@filename)
    end
  end
end
make_prelim() click to toggle source
# File lib/dyndoc/document.rb, line 467
    def make_prelim
      init_doc
      @cfg[:created_docs]=[]
      ##p [:make_prelim,@cfg]
      #update @dirname if @cfg[:dirname_doc] or @tmpl_doc.cfg[:dirname_docs] is fixed!
      if @dirname.empty? and @cfg[:dirname_doc] and !@cfg[:dirname_doc].empty? and File.exist? @cfg[:dirname_doc]
              @dirname= @cfg[:dirname_doc]
      elsif @dirname.empty? and @tmpl_doc.cfg[:dirname_docs] and !@tmpl_doc.cfg[:dirname_docs].empty? and File.exist? @tmpl_doc.cfg[:dirname_docs]
              @dirname= @tmpl_doc.cfg[:dirname_docs]
      end
      #rsrc!
      $dyn_rsrc=File.join("rsrc",@filename)
      @basename=File.basename(@filename,".*")
      Dyndoc.cfg_dir[:file]=File.expand_path(@dirname)
      @curdir=Dir.pwd
=begin
      # read current path if it exists
      cur_path=File.join(@dirname,".dyn_path")
      Dyndoc.setRootDoc(@cfg[:rootDoc],File.read(cur_path).chomp,true) if File.exist? cur_path
      Dyndoc.make_append unless Dyndoc.appendVar
=end
    #p "ici";p @cfg
    ##p [:pandoc_filter,@cfg[:pandoc_filter]]
      require "dyndoc/common/init"
      #p PANDOC_CMDS
      if @basename =~ /\_(md|tex)2(odt|docx|beamer|s5|dzslides|slideous|slidy|revealjs)$/ or (pandoc_cmd=PANDOC_CMDS.include? "--"+@cfg[:pandoc_filter])
        #p [@basename,$1,$2,pandoc_cmd]
        if pandoc_cmd
          @cfg[:pandoc_filter] =~ /(md|tex)2(odt|docx|beamer|s5|dzslides|slideous|slidy|revealjs)$/
        else
          @basename = @basename[0..(@basename.length-$1.length-$2.length-3)] unless pandoc_cmd
        end
        #p @basename
        @cfg[:cmd] << :make_content << :make_task_pandoc
        @cfg[:cmd] -= [:save]
        @cfg[:cmd].uniq!
        @cfg[:format_doc]=@cfg[:mode_doc]=$1.to_sym
        @cfg[:format_output]=$2.to_sym

      end
    end
make_save() click to toggle source
# File lib/dyndoc/document.rb, line 677
def make_save
        case @cfg[:format_doc]
        when :odt
          #before saving: make automatic styles!
          make_odt_content_xml
          make_odt_automatic_styles
          FileUtils.cp(EMPTY_ODT,@cfg[:filename_doc]) unless File.exist? @cfg[:filename_doc]
    require  'zip'
    @ar=Zip::ZipFile.open(@cfg[:filename_doc])
    @ar.get_output_stream('content.xml') do |f|
            f.write @content_xml.to_s
          end

          make_odt_ressources
          @ar.close
        else
    ##p [:cfg,Dir.pwd,@dirname]
          print "\nsave content in #{@cfg[:filename_doc]} or #{@filename}"
    ##p [:make_save,@cfg[:cmd]]
          FileUtils.mkdir_p(File.dirname(@cfg[:filename_doc])) if @cfg[:cmd].include? :save!

    ## if @content is nil => bad execution
    if !@content
      FileUtils.mv(@filename_old,@filename) if @filename_old and File.exist? @filename_old
    else
      ## Save new
      File.open(@cfg[:filename_doc],"w") do |f|
            f << @content
          end
          print " -> ok\n"
          @cfg[:created_docs] << @filename #( @dirname.empty? ? "" : @dirname+"/" ) + @filename
    end
  end
end
make_task_pandoc() click to toggle source
# File lib/dyndoc/document.rb, line 599
def make_task_pandoc
  filter=@cfg[:format_doc].to_s + "2" + @cfg[:format_output].to_s
  require 'dyntask'
  task={cmd: :pandoc, content: @content.force_encoding("utf-8"), filter: filter}
  task[:source]="%"+@basename+".tex" if ["tex2docx","tex2odt"].include? filter
  DynTask.add_task(task)
  DynTask.save_tasks(@basename)
end
make_task_pdflatex() click to toggle source
# File lib/dyndoc/document.rb, line 589
def make_task_pdflatex
  echo = @cfg[:options][:echo_mode] || @tmpl_doc.cfg[:options][:echo_mode] || false
  nb = @cfg[:options][:pdflatex_nb_pass] || @tmpl_doc.cfg[:options][:pdflatex_nb_pass] || 1
  if @cfg[:format_doc]==:tex
    require 'dyntask'
    DynTask.add_task({cmd: :pdflatex, source: "%"+@basename+".tex", content: @content.force_encoding("utf-8"), nb_pass: nb, echo: echo})
    DynTask.save_tasks(@basename)
  end
end
open_log() click to toggle source
# File lib/dyndoc/document.rb, line 455
def open_log
  #p [@tmpl_doc.basename_orig,@tmpl_doc.basename]
  # logfile=File.join(@dirname,@tmpl_doc.basename_orig+".dyn_log")
  # #p logfile
  # $dyn_logger=File.new(logfile,"w")
  @cfg[:created_docs] << @basename+".dyn_log"
end
output(input,echo=0) click to toggle source

like txt (see below) but for string!

# File lib/dyndoc/document.rb, line 552
def output(input,echo=0)
  @cfg[:cmd]=:txt
  @cfg[:output]=:txt if @cfg[:output]== :tex
  @cfg[:raw_mode],@cfg[:model_tmpl]=false,nil
  init(@cfg[:output])
  @tmpl_doc.echo=echo
  @tmpl_doc.reinit
  @tmpl_doc.output input
end
read_cmdline() click to toggle source
# File lib/dyndoc/document.rb, line 391
def read_cmdline
  cfg_cmdline={}
  cfg_cmdline[:format_doc]=Dyndoc.cfg_dyn[:format_doc] unless Dyndoc.cfg_dyn[:format_doc].empty?
  cfg_cmdline[:pandoc_filter]=Dyndoc.cfg_dyn[:pandoc_filter] unless Dyndoc.cfg_dyn[:pandoc_filter].empty?
  append_cfg(cfg_cmdline)
end
tex(name) click to toggle source

TODO: TO UPDATE!!!! file ############################################ file tex

# File lib/dyndoc/document.rb, line 862
def tex(name)
  @name=name
  start
  cd_new
  make_tex
  make_backup
  cd_old
end
tex_pdf(name) click to toggle source

file pdf

# File lib/dyndoc/document.rb, line 872
def tex_pdf(name)
  @cfg[:cmd]=:pdf
  @name=name
  start
  cd_new
  make_tex
  make_backup
  make_pdflatex
  cd_old
end
tex_png(name) click to toggle source

file png

# File lib/dyndoc/document.rb, line 890
def tex_png(name)
  @cfg[:cmd]=:png
  @name=name
  start
  cd_new
  make_tex
  make_dvipng
  cd_old
  make_viewpng
end
tex_xpdf(name) click to toggle source

file pdf+viewer

# File lib/dyndoc/document.rb, line 884
def tex_xpdf(name)
  tex_pdf(name)
  make_viewpdf
end
txt(name) click to toggle source

file txt

# File lib/dyndoc/document.rb, line 902
def txt(name)
  @cfg[:cmd]=:txt
  @cfg[:output]=:txt if @cfg[:output]== :tex
  @cfg[:raw_mode],@cfg[:model_tmpl]=false,nil
  @name=name
  start
  cd_new
  make_txt
  cd_old
end