Class: Dyndoc::Document

Inherits:
Object
  • Object
show all
Includes:
DynConfig
Defined in:
lib/dyndoc/document.rb

Overview

or more explicitly, CreatedDocument from TemplateDocument

Constant Summary collapse

@@cfg =

each document has its own config parameters

{
  :key_doc=>"", #to specify in initialize
  :format_doc => :tex, #instead of :output
  :format_output => :tex,
  :mode_doc => :tex, #execution mode
  :rootDoc=>"",
  :model_doc=>"default",
  :exec_mode=>"no",
  :pre_doc=>[],
  :post_doc=>[],
  :cmd=> [], # :save , :cat, :pdf or :png, :view behaving differently depending on the format_doc
  :cmd_pandoc_options => [],
  :pandoc_filter => "",
  :filename_doc => "",
  :created_docs => [],
  :dirname_doc=>"",
  :append_doc=>"",
  :tag_doc => [],
  :keys_doc=>[],
  :enc => "utf8",
  :options => {}, # added for example to compile twice latex
  :input => [] # added
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DynConfig

#[], #[]=, #append_cfg, #init_cfg, #read_cfg

Constructor Details

#initialize(key_doc, tmpl_doc) ⇒ Document

Returns a new instance of Document.



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

Instance Attribute Details

#cfgObject

gather in @cfg optionnal parameters



360
361
362
# File 'lib/dyndoc/document.rb', line 360

def cfg
  @cfg
end

#contentObject

gather in @cfg optionnal parameters



360
361
362
# File 'lib/dyndoc/document.rb', line 360

def content
  @content
end

#inputsObject

gather in @cfg optionnal parameters



360
361
362
# File 'lib/dyndoc/document.rb', line 360

def inputs
  @inputs
end

#tmpl_docObject

gather in @cfg optionnal parameters



360
361
362
# File 'lib/dyndoc/document.rb', line 360

def tmpl_doc
  @tmpl_doc
end

Class Method Details

.cfgObject



351
352
353
# File 'lib/dyndoc/document.rb', line 351

def Document.cfg
  @@cfg.dup
end

Instance Method Details

#cd_newObject



543
544
545
# File 'lib/dyndoc/document.rb', line 543

def cd_new
  Dir.chdir(@dirname)
end

#cd_oldObject



547
548
549
# File 'lib/dyndoc/document.rb', line 547

def cd_old
  Dir.chdir(@curdir)
end

#close_logObject



463
464
465
# File 'lib/dyndoc/document.rb', line 463

def close_log
  $dyn_logger.close if $dyn_logger
end

#filename_completionObject

# do we have to save the content to some file

def to_be_saved?
  @cfg[:filename_doc]!=:no
end


405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# 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_docObject



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
# 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_allObject

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



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
# 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_contentObject

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



565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
# 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_stylesObject



613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# 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_xmlObject



608
609
610
611
# 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_ressourcesObject



629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
# 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) ⇒ Object

As soon as possible when using dropbox or tools



664
665
666
667
668
669
670
671
672
673
674
675
# 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_prelimObject



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
# 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_saveObject



677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
# 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_pandocObject



599
600
601
602
603
604
605
606
# 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_pdflatexObject



589
590
591
592
593
594
595
596
597
# 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_logObject



455
456
457
458
459
460
461
# 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) ⇒ Object

like txt (see below) but for string!



552
553
554
555
556
557
558
559
560
# 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_cmdlineObject



391
392
393
394
395
396
# 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) ⇒ Object

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



862
863
864
865
866
867
868
869
# 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) ⇒ Object

file pdf



872
873
874
875
876
877
878
879
880
881
# 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) ⇒ Object

file png



890
891
892
893
894
895
896
897
898
899
# 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) ⇒ Object

file pdf+viewer



884
885
886
887
# File 'lib/dyndoc/document.rb', line 884

def tex_xpdf(name)
  tex_pdf(name)
  make_viewpdf
end

#txt(name) ⇒ Object

file txt



902
903
904
905
906
907
908
909
910
911
# 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