dyndoc-ruby-doc 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8973b5e9a90ab0745a7cac9429e9e840a2cbea74
4
+ data.tar.gz: c59310d22bb29f4aeddd9c9c19cc4d9bbf188f5d
5
+ SHA512:
6
+ metadata.gz: 5e47f6c9ac9d89057cf07b035296441a91ac8d5203333ad0bebe99fc99a962d00688f8748cbe8d925b05a7991ac24174574be9c5a1bbafb4088001b165d79502
7
+ data.tar.gz: 493a011ea9176bab86823e8e6bd85c7af2000bd2e4d9e4be6cbdce88a49ab1e9fdc08cd14d0034913d15e7ac82ba08897f8be6babd5b9739c861e7d90d61dfd7
@@ -0,0 +1,66 @@
1
+ module Dyndoc
2
+
3
+ class TemplateContentOnly
4
+
5
+ @@cfg={
6
+ :version=>:V3,
7
+ :format_doc => :tex, #instead of :output
8
+ :format_output => :tex, ## :tex or :html or :tm or :odt
9
+ :mode_doc => :tex, #execution mode
10
+ :rootDoc=>"",
11
+ #:model_doc=>"default", #NO MODEL in this mode
12
+ :pre_doc=>[],
13
+ :post_doc=>[],
14
+ :cmd=> [], # :save , :cat, :pdf or :png, :view behaving differently depending on the format_doc
15
+ :cmd_pandoc_options => [],
16
+ :enc => "utf8"
17
+ }
18
+
19
+
20
+ attr_accessor :cfg, :tmpl_cfg, :content
21
+
22
+ def initialize(cfg={})
23
+ @content=""
24
+ @cfg=cfg
25
+ @tmpl_cfg=@@cfg.dup
26
+ @tmplMngr=Dyndoc.tmpl_mngr
27
+ end
28
+
29
+ def init_tmpl
30
+ @tmplMngr.init_doc(@tmpl_cfg)
31
+ @tmplMngr.init_model(@content) #no model since @cfg[:model_doc] is nil! But, default libraries are loaded!
32
+ @tmplMngr.parse(File.read(Dyndoc.doc_filename("Dyn/Minimum"))+"\n",@tmplMngr.filterGlobal)
33
+ end
34
+
35
+ alias init_doc init_tmpl
36
+
37
+ def require_dyndoc_libs(libs)
38
+ libs="{#require]\n"+libs.split("\n").map{|lib| lib.split(",")}.flatten.uniq.join("\n")+"\n[#}\n"
39
+ @tmplMngr.parse(libs,@tmplMngr.filterGlobal)
40
+ end
41
+
42
+ def prepare_content
43
+ #Dyndoc.warn "prepare_content",@content
44
+ out=@tmplMngr.parse(@content)
45
+ return out
46
+ end
47
+
48
+ def make_content(content=nil)
49
+ @content=content if content
50
+ ##@tmplMngr.cfg[:debug]=true
51
+ if @tmplMngr.cfg[:debug]
52
+ ##puts "@content";p @content
53
+ return prepare_content
54
+ else
55
+ begin
56
+ return prepare_content
57
+ # rescue
58
+ # print "WARNING: fail to eval content #{@content} !!\n"
59
+ # return ""
60
+ end
61
+ end
62
+ end
63
+
64
+ end
65
+
66
+ end
@@ -0,0 +1,909 @@
1
+ # encoding: UTF-8
2
+ require 'dyndoc-software'
3
+ require 'dyndoc-core'
4
+
5
+ module Dyndoc
6
+
7
+
8
+ =begin ##OBSOLETE## replaced by system like dyndoc-ruby-task
9
+ # used for Docker mode
10
+
11
+ module Docker
12
+
13
+ ## TO TEST: @@task_file = "/Users/remy/DOCKER_TASK_FILE"
14
+ @@task_file = "/dyndoc-proj/.cache/task_latex_file"
15
+ @@tasks=nil
16
+
17
+ def Docker.init_task_file
18
+ FileUtils.rm_f(@@task_file)
19
+ @@tasks=[]
20
+ end
21
+
22
+ def Docker.add_task(task)
23
+ @@tasks << task
24
+ end
25
+
26
+ def Docker.save_task_file
27
+ File.open(@@task_file,"w") do |f|
28
+ f << @@tasks.join(",")
29
+ end
30
+ end
31
+
32
+ end
33
+ =end
34
+
35
+ module DynConfig
36
+
37
+ def init_cfg(cfg=nil)
38
+ @cfg=@@cfg.dup
39
+ read_cfg(cfg) if cfg
40
+ end
41
+
42
+ def read_cfg(cfg)
43
+ cfg.each_key do |k|
44
+ @cfg[k]=cfg[k]
45
+ end
46
+ end
47
+
48
+ # append with partial match
49
+ def append_cfg(cfg)
50
+ return unless cfg.respond_to? "[]"
51
+ keys=@cfg.keys.map{|e| e.to_s}.sort
52
+ cfg.each_key do |k|
53
+ #like R, partial match of the parameter names
54
+ if k2=keys.find{|e| e=~/^#{k}/}
55
+ @cfg[k2.to_sym]=cfg[k]
56
+ end
57
+ end
58
+ end
59
+
60
+ def [](key)
61
+ @cfg[key]
62
+ end
63
+
64
+ def []=(key,value)
65
+ @cfg[key]=value
66
+ return self
67
+ end
68
+
69
+ end
70
+
71
+ EMPTY_ODT=File.join(ENV["HOME"],"dyndoc","share","odt","2004","empty.odt") if File.exists? File.join(ENV["HOME"],"dyndoc","share","odt","2004","empty.odt")
72
+ EMPTY_ODT=File.join($dyn_gem_root,"share","odt","2004","emptyTex4Ht.odt") if $dyn_gem_root and File.exists? File.join($dyn_gem_root,"share","odt","2004","empty.odt")
73
+ EMPTY_ODT=File.join("/export/prjCqls","share","rsrc","dyndoc","odt","2004","empty.odt") if File.exists? File.join("/export/prjCqls","share","rsrc","dyndoc","odt","2004","empty.odt")
74
+
75
+ #just for a shortcut
76
+ TexDoc={
77
+ :docs=>{
78
+ "main"=>{:cmd=>[:save]} #,:pdf,:view]},
79
+ }
80
+ }
81
+
82
+ OdtDoc={
83
+ :docs=>{
84
+ "main"=>{:cmd=>[:save],:format_doc=>:odt},
85
+ }
86
+ }
87
+
88
+ TtmDoc={
89
+ :docs=>{
90
+ "main"=>{:cmd=>[:save],:format_doc=>:ttm},
91
+ }
92
+ }
93
+
94
+ HtmlDoc={
95
+ :docs=>{
96
+ "main"=>{:cmd=>[:save],:format_doc=>:html},
97
+ }
98
+ }
99
+
100
+ class TemplateDocument
101
+
102
+ # GOAL: to deal with a master document which may generate many output with different formats
103
+ # RMK: no attempt to deal with template in odt format which is a next objective.
104
+ # RMK2: later on, one could imagine to propose a text language easily convertible to the expected formats
105
+
106
+ @@cfg={
107
+ :working_dir => "", #directory where dyndoc is processed
108
+ :dyndoc_mode => :normal, #default mode, alternative modes :local_server, :remote_server
109
+ ## OBSOLETE: :docker_mode => false,
110
+ :filename_tmpl=>"", #be specified later
111
+ :filename_tmpl_orig=>"", #be specified later
112
+ :lib_dyn_content=>"",
113
+ :dirname_docs=>"", #default directory unless specified by doc!
114
+ :rootDoc=>"",
115
+ :user_input=>[],
116
+ :tag_tmpl=>[], #old part_tag!
117
+ :keys_tmpl=>[],
118
+ :docs=>[], #this one is introduced from version V3 to deal with multidocuments
119
+ :doc_list=>[], #list of documents if nonempty
120
+ :cmd=>[], #list of commands if nonempty for every document
121
+ :cmd_pandoc_options => [], #pandoc options
122
+ :pandoc_filter => "",
123
+ :options => {}, # added for example to compile twice latex
124
+ :dtag=>:dtag,
125
+ :dtags=>[:dtag],
126
+ :raw_mode=>false,
127
+ :model_tmpl=>"default",
128
+ :model_doc=>"default",
129
+ :append => "",
130
+ :verbose => false,
131
+ :debug => false
132
+ }
133
+
134
+
135
+ include DynConfig
136
+
137
+ attr_accessor :tmpl_mngr, :docs, :cfg, :basename, :dirname, :basename_orig, :dirname_orig, :content
138
+
139
+ def initialize(name) #docs is a hash containing all the files
140
+ ## complete with default extension if not provided
141
+ name += ".dyn" if File.basename(name) == File.basename(name,".*")
142
+ @name=name
143
+ ## read config from name
144
+ @cfg=@@cfg.dup
145
+ read_cfg(@name)
146
+ ## the template manager
147
+ Dyndoc.tmpl_mngr=@tmpl_mngr = Dyndoc::Ruby::TemplateManager.new(@cfg)
148
+ ## the documents
149
+ @docs={}
150
+ make_doc_list
151
+ if @cfg[:content] #a non file based document is a Hash cfg with :content inside (give a basename used for generated files)
152
+ @content=@cfg[:content]
153
+ else
154
+ # find the basename of the template
155
+ @basename=basename_tmpl
156
+ @dirname=File.dirname(@basename)
157
+ @basename=File.basename(@basename)
158
+ #p @basename
159
+ @basename_orig=basename_tmpl_orig
160
+ @dirname_orig=File.dirname(@basename_orig)
161
+ @basename_orig=File.basename(@basename_orig)
162
+ #p @basename_orig
163
+ # read content of the template
164
+ @content=File.read(@cfg[:filename_tmpl])
165
+ end
166
+ # list of Document objects
167
+ #puts "@doc_list (init)";p @doc_list
168
+ @doc_list.each do |kdoc|
169
+ @docs[kdoc]=Document.new(kdoc,self)
170
+ end
171
+ end
172
+
173
+ def read_cfg(name,mode=:all)
174
+ # cfg_dyn is the options given inside the master template
175
+ cfg_dyn=nil
176
+
177
+
178
+ name_tmpl=Dyndoc.name_tmpl(name,mode)
179
+ #Dyndoc.warn "read_cfg:name_tmpl",[name,name_tmpl]
180
+ if name_tmpl
181
+ name_tmpl2=Dyndoc.directory_tmpl? name_tmpl
182
+ if name_tmpl2
183
+ # test if [#cfg] block exists inside _lib.dyn file
184
+ lib_dyn_content=lib_dyn_content_from(name_tmpl2)
185
+ tmp=cfg_dyn_from_lib(lib_dyn_content)
186
+ if tmp[:cfg]
187
+ #p :cfg_from_lib
188
+ cfg_dyn=cfg_dyn_from_code(tmp[:cfg])
189
+ lib_dyn_content=tmp[:lib_dyn_content]
190
+ else
191
+ cfg_dyn=cfg_dyn_from(name_tmpl2)
192
+ end
193
+ cfg_dyn[:filename_tmpl]=name_tmpl2
194
+ cfg_dyn[:lib_dyn_content]=lib_dyn_content
195
+ else
196
+ cfg_dyn=Dyndoc::TexDoc
197
+ end
198
+ cfg_dyn[:filename_tmpl_orig] = name_tmpl
199
+ end
200
+ #Dyndoc.warn "read_cfg:cfg_dyn",cfg_dyn
201
+
202
+ #otherwise it is the default version!
203
+ append_cfg(cfg_dyn) if cfg_dyn
204
+ #read the optional cfg
205
+ read_cmdline
206
+ end
207
+
208
+ def lib_dyn_content_from(tmpl=nil)
209
+ code,lib_file = "",nil
210
+ code=File.read(lib_file) if (lib_file=(Dyndoc::Utils.lib_file_exists? tmpl))
211
+ return code
212
+ end
213
+
214
+ def cfg_dyn_from_code(code)
215
+ if code and code.is_a? String
216
+ code="{\n"+code+"\n}" if code=~/\A\s*\:/m #to avoid at the beginning { and at the end }!
217
+ ##p [code,Object.class_eval(code)]
218
+ return Object.class_eval(code)
219
+ end
220
+ return nil
221
+ end
222
+
223
+ def cfg_dyn_from_lib(content)
224
+ return {:cfg=> nil, :lib_dyn_content => content} if content.empty?
225
+ tmp=content.force_encoding("utf-8").split(/\[\#/)
226
+ if tmp[0].empty? and tmp[1][0...4]=="cfg]"
227
+ return {:cfg=> tmp[1][4..-1], :lib_dyn_content => (tmp[0,1]+tmp[2..-1]).join('[#')}
228
+ else
229
+ return {:cfg=> nil, :lib_dyn_content => content}
230
+ end
231
+ end
232
+
233
+ def cfg_dyn_from(tmpl)
234
+ code,cfg_file=nil,nil
235
+ code=File.read(cfg_file) if (cfg_file=(Dyndoc::Utils.cfg_file_exists? tmpl))
236
+ ##puts "code";p code;p cfg_file
237
+ Utils.clean_bom_utf8!(code) if code
238
+ code="Dyndoc::TexDoc" unless code
239
+ return cfg_dyn_from_code(code)
240
+ end
241
+
242
+ def read_cmdline
243
+ ##p [:read_cmdline,Dyndoc.cfg_dyn]
244
+ cfg_cmdline={}
245
+ cfg_cmdline[:doc_list]=Dyndoc.cfg_dyn[:doc_list] unless Dyndoc.cfg_dyn[:doc_list].empty?
246
+ cfg_cmdline[:cmd]=Dyndoc.cfg_dyn[:cmd_doc] unless Dyndoc.cfg_dyn[:cmd_doc].empty?
247
+ cfg_cmdline[:model_doc]=Dyndoc.cfg_dyn[:model_doc] unless Dyndoc.cfg_dyn[:model_doc].empty?
248
+ cfg_cmdline[:tag_tmpl]=Dyndoc.cfg_dyn[:tag_tmpl] unless Dyndoc.cfg_dyn[:tag_tmpl].empty?
249
+ cfg_cmdline[:options]=Dyndoc.cfg_dyn[:options] unless Dyndoc.cfg_dyn[:options].empty?
250
+ ## select doc_list by reading "[#default]" useful in atom
251
+ if !@cfg[:content] and Dyndoc.cfg_dyn[:doc_list].empty? and Dyndoc.cfg_dyn[:tag_tmpl].empty?
252
+ if File.read(@cfg[:filename_tmpl]).force_encoding("utf-8") =~ /^\[\#default\]([^\[]*)\[\#/
253
+ cfg_cmdline[:doc_list]=$1.strip.split(",").map{|e| e.strip}
254
+ puts "Default document: "+cfg_cmdline[:doc_list].join(",")
255
+ end
256
+ end
257
+ append_cfg(cfg_cmdline)
258
+ end
259
+
260
+ def make_doc_list
261
+ doc_list=@cfg[:docs].keys
262
+ #puts "@cfg[doc_list]";p @cfg[:doc_list]
263
+ doc_list &= @cfg[:doc_list] unless @cfg[:doc_list].empty?
264
+ #p doc_list
265
+ #deal with aliases
266
+ doc_alias={}
267
+ @cfg[:docs].each_pair{|key,doc|
268
+ doc_alias[key]=doc if doc.is_a? Array
269
+ }
270
+ #puts "doc_alias";p doc_alias
271
+ doc_list=doc_list.map{|key| (doc_alias[key] ? doc_alias[key] : key )}.flatten until (doc_list & doc_alias.keys).empty?
272
+ @doc_list=doc_list
273
+ #puts "doc_list";p @doc_list
274
+ end
275
+
276
+ # document basename from template filename
277
+ def basename_tmpl
278
+ ##p @cfg[:filename_tmpl]
279
+ mode=Dyndoc.guess_mode(@cfg[:filename_tmpl])
280
+ ##p ["mode",mode,Dyndoc.tmplExt[mode]]
281
+ if mode
282
+ name,ext=@cfg[:filename_tmpl].scan(/^(.*)(?:#{Dyndoc.tmplExt[mode].join("|")})$/).flatten.compact
283
+ else
284
+ name,ext=@cfg[:filename_tmpl].scan(/^(.*)(?:_tmpl(\..*)|(\.dyn))$/).flatten.compact
285
+ end
286
+ #p [:name,@cfg[:filename_tmpl],name]
287
+ name
288
+ end
289
+
290
+ def basename_tmpl_orig
291
+ mode=Dyndoc.guess_mode(@cfg[:filename_tmpl_orig])
292
+ if mode
293
+ name,ext=@cfg[:filename_tmpl_orig].scan(/^(.*)(?:#{Dyndoc.tmplExt[mode].join("|")})$/).flatten.compact
294
+ else
295
+ name,ext=@cfg[:filename_tmpl_orig].scan(/^(.*)(?:_tmpl(\..*)|(\.dyn))$/).flatten.compact
296
+ end
297
+ name
298
+ end
299
+
300
+ def make_all
301
+ if @cfg[:cmd].include? :list
302
+ puts "document list: "+@cfg[:docs].keys.join(", ")
303
+ return
304
+ end
305
+
306
+ # Added for docker mode
307
+ ##OBSOLETE## Docker.init_task_file if Dyndoc.cfg_dyn[:docker_mode]
308
+
309
+ ##puts "@doc_list"; p @doc_list
310
+ @doc_list.each do |kdoc|
311
+ @docs[kdoc].make_all
312
+ end
313
+
314
+ ##OBSOLETE## Docker.save_task_file if Dyndoc.cfg_dyn[:docker_mode]
315
+
316
+ end
317
+
318
+ end
319
+
320
+ class Document ## or more explicitly, CreatedDocument from TemplateDocument
321
+ # each document has its own config parameters
322
+
323
+ @@cfg={
324
+ :key_doc=>"", #to specify in initialize
325
+ :format_doc => :tex, #instead of :output
326
+ :format_output => :tex,
327
+ :mode_doc => :tex, #execution mode
328
+ :rootDoc=>"",
329
+ :model_doc=>"default",
330
+ :pre_doc=>[],
331
+ :post_doc=>[],
332
+ :cmd=> [], # :save , :cat, :pdf or :png, :view behaving differently depending on the format_doc
333
+ :cmd_pandoc_options => [],
334
+ :pandoc_filter => "",
335
+ :filename_doc => "",
336
+ :created_docs => [],
337
+ :dirname_doc=>"",
338
+ :append_doc=>"",
339
+ :tag_doc => [],
340
+ :keys_doc=>[],
341
+ :enc => "utf8",
342
+ :options => {}, # added for example to compile twice latex
343
+ :input => [] # added
344
+ }
345
+
346
+ def Document.cfg
347
+ @@cfg.dup
348
+ end
349
+
350
+ include Dyndoc::DynConfig
351
+
352
+ # gather in @cfg optionnal parameters
353
+ # @content really matters to be included in @cfg!
354
+
355
+ attr_accessor :tmpl_doc, :cfg, :content, :inputs
356
+
357
+ def initialize(key_doc,tmpl_doc)
358
+ @tmpl_doc=tmpl_doc #to be aware of the cfg of tmpl_doc!
359
+ ##p [:tmpl_doc,@tmpl_doc]
360
+ @cfg=Document.cfg
361
+ @content=""
362
+ @cfg[:key_doc]=key_doc #just to record the key of this document
363
+ # update @cfg
364
+ ## cmd
365
+ @cfg[:cmd].uniq!
366
+ @cfg[:cmd] = [:save] if @cfg[:model_doc] and @cfg[:model_doc] != "default"
367
+ append_cfg(@tmpl_doc.cfg[:docs][key_doc])
368
+ ## from command line
369
+ read_cmdline
370
+ # update cmd
371
+ # p @tmpl_doc.cfg[:cmd];p @cfg[:cmd]
372
+ @cfg[:cmd]=@tmpl_doc.cfg[:cmd] unless @tmpl_doc.cfg[:cmd].empty?
373
+ ## TODO: MODE MULTIDOC => maybe to correct if options differ for each document
374
+ @cfg[:cmd_pandoc_options]=@tmpl_doc.cfg[:cmd_pandoc_options] unless @tmpl_doc.cfg[:cmd_pandoc_options].empty?
375
+ ##p [:pandoc_cfg,@tmpl_doc.cfg[:pandoc_filter]]
376
+ @cfg[:pandoc_filter]=@tmpl_doc.cfg[:pandoc_filter] unless @tmpl_doc.cfg[:pandoc_filter].empty?
377
+ # debug mode
378
+ p @tmpl_doc.cfg if @tmpl_doc.cfg[:debug]
379
+ p [:cfg,@cfg] if @tmpl_doc.cfg[:debug]
380
+ # autocomplete the document filename if necessary!
381
+ filename_completion
382
+ @filename = @cfg[:filename_doc]
383
+ @dirname = @tmpl_doc.dirname_orig
384
+ end
385
+
386
+ def read_cmdline
387
+ cfg_cmdline={}
388
+ cfg_cmdline[:format_doc]=Dyndoc.cfg_dyn[:format_doc] unless Dyndoc.cfg_dyn[:format_doc].empty?
389
+ cfg_cmdline[:pandoc_filter]=Dyndoc.cfg_dyn[:pandoc_filter] unless Dyndoc.cfg_dyn[:pandoc_filter].empty?
390
+ append_cfg(cfg_cmdline)
391
+ end
392
+
393
+ =begin
394
+ # do we have to save the content to some file
395
+ def to_be_saved?
396
+ @cfg[:filename_doc]!=:no
397
+ end
398
+ =end
399
+
400
+ def filename_completion
401
+ #p @tmpl_doc.basename_orig
402
+ #p @cfg[:append_doc]
403
+ #p Dyndoc.docExt(@cfg[:format_doc])
404
+ ext_mode=nil
405
+ if @tmpl_doc.basename_orig =~ /\_(html|tex|c|rb|txtl|md|txt|raw)$/
406
+ @cfg[:cmd] += [:make_content,:save]
407
+ ext_mode=$1.to_sym
408
+ @cfg[:format_doc]=@cfg[:mode_doc]=@cfg[:format_output]=(ext_mode == :raw ? :txt : ext_mode )
409
+ last=-(2 + $1.length)
410
+ else
411
+ last=-1
412
+ end
413
+ ##DEBUG:
414
+ p [@tmpl_doc.basename_orig[0..last],@tmpl_doc.cfg[:append],@cfg[:append_doc],@cfg[:format_doc],Dyndoc.docExt(ext_mode || @cfg[:format_doc])]
415
+ @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?
416
+ ##p [:filename_completion,@filename,@cfg[:filename_doc] ]
417
+ end
418
+
419
+ # start ##################################################
420
+ def make_all
421
+ #puts "make_all";p @cfg[:cmd]
422
+ make_prelim
423
+ #
424
+ puts "make_all";p @cfg[:cmd]
425
+ cd_new
426
+ open_log
427
+
428
+ # First, save or rm (both is useless) the old file
429
+ make_old(:save) if @cfg[:cmd].include? :save_old
430
+ make_old(:rm) if @cfg[:cmd].include? :rm_old
431
+
432
+ # make content
433
+ make_content if @cfg[:cmd].include? :make_content
434
+ ##OBSOLETE## @content=make_ttm if @cfg[:format_doc]==:ttm
435
+ #puts "make_all";p @cfg[:cmd]
436
+ make_save unless (@cfg[:cmd] & [:save,:save!]).empty?
437
+ ##OBSOLETE## make_pandoc if @cfg[:cmd].include? :pandoc
438
+ ##OBSOLETE## make_backup if @cfg[:cmd].include? :backup
439
+ ##OBSOLETE## make_cat if @cfg[:cmd].include? :cat
440
+ # added for docker mode when latex is done via another docker
441
+ ##OBSOLETE## make_docker if Dyndoc.cfg_dyn[:docker_mode] and @cfg[:format_doc]==:tex
442
+ make_task_pdflatex if @cfg[:cmd].include? :pdf ##OBSOLETE## and !Dyndoc.cfg_dyn[:docker_mode]
443
+ make_task_pandoc if @cfg[:cmd].include? :make_task_pandoc
444
+ ##OBSOLETE## make_png if @cfg[:cmd].include? :png ##OBSOLETE## and !Dyndoc.cfg_dyn[:docker_mode]
445
+ ##OBSOLETE## make_view if @cfg[:cmd].include? :view # added for docker mode when latex is done via another docker
446
+ close_log
447
+ cd_old
448
+ end
449
+
450
+ def open_log
451
+ #p [@tmpl_doc.basename_orig,@tmpl_doc.basename]
452
+ # logfile=File.join(@dirname,@tmpl_doc.basename_orig+".dyn_log")
453
+ # #p logfile
454
+ # $dyn_logger=File.new(logfile,"w")
455
+ @cfg[:created_docs] << @basename+".dyn_log"
456
+ end
457
+
458
+ def close_log
459
+ $dyn_logger.close if $dyn_logger
460
+ end
461
+
462
+ def make_prelim
463
+ init_doc
464
+ @cfg[:created_docs]=[]
465
+ ##p [:make_prelim,@cfg]
466
+ #update @dirname if @cfg[:dirname_doc] or @tmpl_doc.cfg[:dirname_docs] is fixed!
467
+ if @dirname.empty? and @cfg[:dirname_doc] and !@cfg[:dirname_doc].empty? and File.exist? @cfg[:dirname_doc]
468
+ @dirname= @cfg[:dirname_doc]
469
+ elsif @dirname.empty? and @tmpl_doc.cfg[:dirname_docs] and !@tmpl_doc.cfg[:dirname_docs].empty? and File.exist? @tmpl_doc.cfg[:dirname_docs]
470
+ @dirname= @tmpl_doc.cfg[:dirname_docs]
471
+ end
472
+ #rsrc!
473
+ $dyn_rsrc=File.join("rsrc",@filename)
474
+ @basename=File.basename(@filename,".*")
475
+ Dyndoc.cfg_dir[:file]=File.expand_path(@dirname)
476
+ @curdir=Dir.pwd
477
+ =begin
478
+ # read current path if it exists
479
+ cur_path=File.join(@dirname,".dyn_path")
480
+ Dyndoc.setRootDoc(@cfg[:rootDoc],File.read(cur_path).chomp,true) if File.exists? cur_path
481
+ Dyndoc.make_append unless Dyndoc.appendVar
482
+ =end
483
+ #p "ici";p @cfg
484
+ ##p [:pandoc_filter,@cfg[:pandoc_filter]]
485
+ require "dyndoc/common/init"
486
+ #p PANDOC_CMDS
487
+ if @basename =~ /\_(md|tex)2(odt|docx|beamer|s5|dzslides|slideous|slidy|revealjs)$/ or (pandoc_cmd=PANDOC_CMDS.include? "--"+@cfg[:pandoc_filter])
488
+ #p [@basename,$1,$2,pandoc_cmd]
489
+ if pandoc_cmd
490
+ @cfg[:pandoc_filter] =~ /(md|tex)2(odt|docx|beamer|s5|dzslides|slideous|slidy|revealjs)$/
491
+ else
492
+ @basename = @basename[0..(@basename.length-$1.length-$2.length-3)] unless pandoc_cmd
493
+ end
494
+ #p @basename
495
+ @cfg[:cmd] << :make_content << :make_task_pandoc
496
+ @cfg[:cmd] -= [:save]
497
+ @cfg[:cmd].uniq!
498
+ @cfg[:format_doc]=@cfg[:mode_doc]=$1.to_sym
499
+ @cfg[:format_output]=$2.to_sym
500
+
501
+ end
502
+ end
503
+
504
+ def init_doc
505
+ Dyndoc.mode=out=@cfg[:format_doc]
506
+ unless @tmpl_doc.cfg[:raw_mode]
507
+ if ( tmp=Dyndoc.doc_filename("Dyn/.preload",[""],nil))
508
+ @cfg[:pre_doc] += File.read(tmp).split("\n").map{|l| l.split(",")}.flatten.map{|e| e.strip}
509
+ end
510
+
511
+ if (tmp=Dyndoc.doc_filename("Dyn/.postload",[""],nil))
512
+ @cfg[:post_doc] += File.read(tmp).split("\n").map{|l| l.split(",")}.flatten.map{|e| e.strip}
513
+ end
514
+ if out
515
+ ## default preload
516
+ out=:tex if out==:ttm
517
+ outDir=out.to_s.capitalize
518
+ if (tmp=Dyndoc.doc_filename("#{outDir}/.preload",[""],nil))
519
+ @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)}
520
+ end
521
+ ## default postload
522
+ if (tmp=Dyndoc.doc_filename("#{outDir}/.postload",[""],nil))
523
+ @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)}
524
+ end
525
+ end
526
+ end
527
+ #p @cfg[:pre_doc]
528
+ #p @cfg[:post_doc]
529
+ #model_doc
530
+ @cfg[:model_doc]=nil unless @tmpl_doc.cfg[:model_tmpl]
531
+ @cfg[:model_doc]=@tmpl_doc.cfg[:model_doc] if @tmpl_doc.cfg[:model_doc]
532
+ @cfg[:cmd] -= [:png,:pdf,:view] if @cfg[:model_doc]=="content"
533
+ # TO REMOVE: Dyndoc.mode=(out)
534
+ # prepare the initialization of the TemplateManager
535
+ @tmpl_doc.tmpl_mngr.init_doc(@cfg)
536
+ end
537
+
538
+ def cd_new
539
+ Dir.chdir(@dirname)
540
+ end
541
+
542
+ def cd_old
543
+ Dir.chdir(@curdir)
544
+ end
545
+
546
+ #like txt (see below) but for string!
547
+ def output(input,echo=0)
548
+ @cfg[:cmd]=:txt
549
+ @cfg[:output]=:txt if @cfg[:output]== :tex
550
+ @cfg[:raw_mode],@cfg[:model_tmpl]=false,nil
551
+ init(@cfg[:output])
552
+ @tmpl_doc.echo=echo
553
+ @tmpl_doc.reinit
554
+ @tmpl_doc.output input
555
+ end
556
+
557
+
558
+ # make ###########################################
559
+ # make content
560
+ def make_content
561
+ ## if true
562
+ if @tmpl_doc.cfg[:debug] or Settings["cfg_dyn.debug"]
563
+ @tmpl_doc.tmpl_mngr.echo=0
564
+ @tmpl_doc.tmpl_mngr.doc=self
565
+ ## p [:make_content,@tmpl_doc.content]
566
+ @content=@tmpl_doc.tmpl_mngr.output(@tmpl_doc.content)
567
+ print "\nmake content for #{@basename} in #{@dirname} -> ok\n"
568
+ else
569
+ print "\nmake content for #{@basename} in #{@dirname}\n"
570
+ begin
571
+ @tmpl_doc.tmpl_mngr.echo=0
572
+ @tmpl_doc.tmpl_mngr.doc=self
573
+ @content=@tmpl_doc.tmpl_mngr.output(@tmpl_doc.content)
574
+ ##puts "@content";p @content
575
+ print " -> ok\n"
576
+ rescue
577
+ ok=false
578
+ @content=nil ## added to know if one need to restaure
579
+ print " -> NO, NO and NO!!\n"
580
+ end
581
+ end
582
+ end
583
+
584
+ def make_task_pdflatex
585
+ echo = @cfg[:options][:echo_mode] || @tmpl_doc.cfg[:options][:echo_mode] || false
586
+ nb = @cfg[:options][:pdflatex_nb_pass] || @tmpl_doc.cfg[:options][:pdflatex_nb_pass] || 1
587
+ if @cfg[:format_doc]==:tex
588
+ require 'dyntask'
589
+ DynTask.add_task({cmd: :pdflatex, source: "%"+@basename+".tex", content: @content.force_encoding("utf-8"), nb_pass: nb, echo: echo})
590
+ DynTask.save_tasks(@basename)
591
+ end
592
+ end
593
+
594
+ def make_task_pandoc
595
+ filter=@cfg[:format_doc].to_s + "2" + @cfg[:format_output].to_s
596
+ require 'dyntask'
597
+ task={cmd: :pandoc, content: @content.force_encoding("utf-8"), filter: filter}
598
+ task[:source]="%"+@basename+".tex" if ["tex2docx","tex2odt"].include? filter
599
+ DynTask.add_task(task)
600
+ DynTask.save_tasks(@basename)
601
+ end
602
+
603
+ def make_odt_content_xml
604
+ @content_xml=REXML::Document.new(@content)
605
+ #p @content_xml.to_s
606
+ end
607
+
608
+ def make_odt_automatic_styles
609
+ #puts "make_odt_automatic_styles:@inputs"; p @inputs
610
+ return if !@inputs or @inputs.empty?
611
+ autostyles = @content_xml.root.elements['office:automatic-styles']
612
+ #puts "autostyles";p autostyles
613
+ #p @automatic_styles
614
+ # add the automatic styles from input template
615
+ if autostyles
616
+ @inputs.values.each do |input|
617
+ input.automatic_styles.each_element do |e|
618
+ autostyles << e
619
+ end
620
+ end
621
+ end
622
+ end
623
+
624
+ def make_odt_ressources
625
+ return if !@inputs or @inputs.empty?
626
+ # create directories if necessary
627
+ @ar.find_entry('ObjectReplacements') || @ar.mkdir('ObjectReplacements')
628
+ @ar.find_entry('Pictures') || @ar.mkdir('Pictures')
629
+ # create the necessary files
630
+ @inputs.values.each do |input|
631
+ input.xlink_href.keys.each do |key|
632
+ if (entry=input.ar.find_entry(key))
633
+ @ar.get_output_stream(input.xlink_href[key]) do |f|
634
+ f.write input.ar.read(entry)
635
+ end
636
+ end
637
+ end
638
+ end
639
+ end
640
+
641
+ =begin ##OBSOLETE##
642
+ def make_pandoc
643
+ opts=@cfg[:cmd_pandoc_options]+["-o",@cfg[:pandoc_file_output]]
644
+ ## p ["make_pandoc",@content]
645
+ if @cfg[:pandoc_file_input]
646
+ opts << @cfg[:pandoc_file_input]
647
+ p [:make_pandoc_input, opts.join(" ")]
648
+ Converter.pandoc(nil,opts.join(" "))
649
+ else
650
+ p [:make_pandoc_content, opts.join(" "),@content]
651
+ Converter.pandoc(@content,opts.join(" "))
652
+ end
653
+ @cfg[:created_docs] << @cfg[:pandoc_file_output]
654
+ end
655
+ =end
656
+
657
+
658
+ # As soon as possible when using dropbox or tools
659
+ def make_old(mode=:rm) #mode=:rm or :save
660
+ ## After introduction of dyntask, the default is to save the old file if existing
661
+ if File.exists? @filename
662
+ case mode
663
+ when :save
664
+ FileUtils.mkdir_p(File.join(File.dirname(@filename),".save"))
665
+ FileUtils.mv(@filename,@filename_old=File.join(File.dirname(@filename),".save",File.basename(@filename)))
666
+ when :rm
667
+ FileUtils.rm(@filename)
668
+ end
669
+ end
670
+ end
671
+
672
+ def make_save
673
+ case @cfg[:format_doc]
674
+ when :odt
675
+ #before saving: make automatic styles!
676
+ make_odt_content_xml
677
+ make_odt_automatic_styles
678
+ FileUtils.cp(EMPTY_ODT,@cfg[:filename_doc]) unless File.exists? @cfg[:filename_doc]
679
+ require 'zip'
680
+ @ar=Zip::ZipFile.open(@cfg[:filename_doc])
681
+ @ar.get_output_stream('content.xml') do |f|
682
+ f.write @content_xml.to_s
683
+ end
684
+
685
+ make_odt_ressources
686
+ @ar.close
687
+ else
688
+ ##p [:cfg,Dir.pwd,@dirname]
689
+ print "\nsave content in #{@cfg[:filename_doc]} or #{@filename}"
690
+ ##p [:make_save,@cfg[:cmd]]
691
+ FileUtils.mkdir_p(File.dirname(@cfg[:filename_doc])) if @cfg[:cmd].include? :save!
692
+
693
+ ## if @content is nil => bad execution
694
+ if !@content
695
+ FileUtils.mv(@filename_old,@filename) if @filename_old and File.exists? @filename_old
696
+ else
697
+ ## Save new
698
+ File.open(@cfg[:filename_doc],"w") do |f|
699
+ f << @content
700
+ end
701
+ print " -> ok\n"
702
+ @cfg[:created_docs] << @filename #( @dirname.empty? ? "" : @dirname+"/" ) + @filename
703
+ end
704
+ end
705
+ end
706
+
707
+
708
+ =begin ##OBSOLETE##
709
+
710
+ def make_cat
711
+ puts @content
712
+ end
713
+
714
+
715
+ # make pdf
716
+
717
+ def make_pdf
718
+ nb = @cfg[:options][:pdflatex_nb_pass] || @tmpl_doc.cfg[:options][:pdflatex_nb_pass] || 1
719
+ echo_mode=@cfg[:options][:pdflatex_echo] || @tmpl_doc.cfg[:options][:pdflatex_echo] || false
720
+ nb.times {|i| make_pdflatex(echo_mode) } if @cfg[:format_doc]==:tex
721
+ end
722
+
723
+ def make_docker
724
+ nb = @cfg[:options][:pdflatex_nb_pass] || @tmpl_doc.cfg[:options][:pdflatex_nb_pass] || 1
725
+ if @cfg[:format_doc]==:tex
726
+ Docker.add_task @dirname+";"+@basename+";"+nb.to_s
727
+ end
728
+ end
729
+
730
+
731
+
732
+ # make prj-tex
733
+ def make_prj_tex
734
+ system "prj-tex #{@basename}"
735
+ print "\nprj-tex #{@basename} in #{@dirname} -> ok\n"
736
+ end
737
+
738
+ # make pdflatex
739
+ def make_pdflatex(echo_mode=false)
740
+ if File.read(@basename+".tex").empty?
741
+ msg="No pdflatex #{@basename} in #{@dirname} since empty file!"
742
+ print "\n==> "+msg
743
+ $dyn_logger.write("ERROR pdflatex: "+msg+"\n") unless Dyndoc.cfg_dyn[:dyndoc_mode]==:normal
744
+ return ""
745
+ end
746
+ print "\n==> #{Dyndoc.pdflatex} #{@basename} in #{@dirname}"
747
+ # NEW: Not to be devkit dependent!!!
748
+ # if RUBY_PLATFORM =~ /(win|w)32$/
749
+ # unless SOFTWARE[:taskkill]
750
+ # cmd = `which taskkill`.strip
751
+ # SOFTWARE[:taskkill]=cmd unless cmd.empty?
752
+ # end
753
+ # if SOFTWARE[:taskkill]
754
+ # system(SOFTWARE[:taskkill]+" /FI \"windowtitle eq "+@basename+".pdf*\"")
755
+ # end
756
+ # end
757
+
758
+ out=`#{Dyndoc.pdflatex} -halt-on-error -file-line-error -interaction=nonstopmode #{@basename}`
759
+ out=out.b if RUBY_VERSION >= "1.9" #because out is not necessarily utf8 encoded
760
+ out=out.split("\n")
761
+ puts out if echo_mode
762
+ if out[-2].include? "Fatal error"
763
+ if Dyndoc.cfg_dyn[:dyndoc_mode]==:normal
764
+ print " -> NOT OKAY!!!\n==> "
765
+ puts out[-4...-1]
766
+ raise SystemExit
767
+ else
768
+ # File.open("#{@dirname}/#{@basename}.dyn_log","w") do |f|
769
+ # f << out[-4...-1]
770
+ # end
771
+ #p out[-4...-1]
772
+ $dyn_logger.write("ERROR pdflatex: "+out[-4...-1].to_s+"\n") if $dyn_logger
773
+ @cfg[:created_docs] << @basename+".log"
774
+ end
775
+ else
776
+ print " -> OKAY!!!\n"
777
+ @cfg[:created_docs] << @basename+".pdf" #( @dirname.empty? ? "" : @dirname+"/" ) + @basename+".pdf"
778
+ end
779
+ end
780
+
781
+ # make png
782
+
783
+ def make_png
784
+ make_dvipng if @cfg[:format_doc]==:tex
785
+ end
786
+
787
+ # make latex and dvipng
788
+ def make_dvipng
789
+ system "latex #{@basename}.tex"
790
+ print "\nlatex #{@basename}.tex -> ok\n"
791
+ system "dvipng --nogssafer #{@basename}.dvi -o #{@basename}.png"
792
+ print "\ndvipng --nogssafer #{@basename}.dvi -o #{@basename}.png -> ok\n"
793
+ end
794
+
795
+ # make ttm
796
+ def make_ttm
797
+ #puts "make_ttm:begin"
798
+ Dyndoc::Converter.ttm(@content)
799
+ end
800
+
801
+
802
+ # make view
803
+ def make_view
804
+ make_viewpdf if @cfg[:cmd].include? :pdf
805
+ make_viewpng if @cfg[:cmd].include? :png
806
+ end
807
+
808
+
809
+ # make view pdf
810
+ def make_viewpdf
811
+ if RUBY_PLATFORM =~ /(win|w)32$/
812
+ unless SOFTWARE[:pdfviewer]
813
+ SOFTWARE[:pdfviewer]="start"
814
+ end
815
+ elsif RUBY_PLATFORM =~ /darwin/
816
+ unless SOFTWARE[:pdfviewer]
817
+ SOFTWARE[:pdfviewer]="open"
818
+ end
819
+ else
820
+ if @tmpl_doc.cfg[:pdfviewer]=="xpdf"
821
+ SOFTWARE[:pdfviewer]="xpdf"
822
+ else
823
+ cmd = `which #{@tmpl_doc.cfg[:pdfviewer]}`.strip
824
+ SOFTWARE[:pdfviewer]=cmd unless cmd.empty?
825
+ end
826
+ end
827
+ if SOFTWARE[:pdfviewer]
828
+ if SOFTWARE[:pdfviewer]=="xpdf"
829
+ ##test xpdf is already open
830
+ if `ps aux`.scan("xpdf-#{@basename}").length>0
831
+ system "xpdf -remote xpdf-#{@basename} -reload"
832
+ else
833
+ system "xpdf -remote xpdf-#{@basename} #{@basename}.pdf&"
834
+ end
835
+ print "\n==> xpdf #{@cfg[:filename_doc]}.pdf -> OKAY!!!\n"
836
+ else
837
+ if RUBY_PLATFORM =~ /(win|w)32$/
838
+ `start /B #{@basename}.pdf`
839
+ elsif RUBY_PLATFORM =~ /darwin/
840
+ `open #{@basename}.pdf`
841
+ else
842
+ `#{SOFTWARE[:pdfviewer]} #{@basename}.pdf&`
843
+ end
844
+ end
845
+ end
846
+ end
847
+
848
+ # make view png
849
+ def make_viewpng
850
+ system "#{@tmpl_doc.cfg[:pngviewer]} #{@basename}.png&"
851
+ end
852
+
853
+ =end
854
+
855
+ # TODO: TO UPDATE!!!! file ############################################
856
+ # file tex
857
+ def tex(name)
858
+ @name=name
859
+ start
860
+ cd_new
861
+ make_tex
862
+ make_backup
863
+ cd_old
864
+ end
865
+
866
+ # file pdf
867
+ def tex_pdf(name)
868
+ @cfg[:cmd]=:pdf
869
+ @name=name
870
+ start
871
+ cd_new
872
+ make_tex
873
+ make_backup
874
+ make_pdflatex
875
+ cd_old
876
+ end
877
+
878
+ # file pdf+viewer
879
+ def tex_xpdf(name)
880
+ tex_pdf(name)
881
+ make_viewpdf
882
+ end
883
+
884
+ # file png
885
+ def tex_png(name)
886
+ @cfg[:cmd]=:png
887
+ @name=name
888
+ start
889
+ cd_new
890
+ make_tex
891
+ make_dvipng
892
+ cd_old
893
+ make_viewpng
894
+ end
895
+
896
+ #file txt
897
+ def txt(name)
898
+ @cfg[:cmd]=:txt
899
+ @cfg[:output]=:txt if @cfg[:output]== :tex
900
+ @cfg[:raw_mode],@cfg[:model_tmpl]=false,nil
901
+ @name=name
902
+ start
903
+ cd_new
904
+ make_txt
905
+ cd_old
906
+ end
907
+
908
+ end
909
+ end
metadata ADDED
@@ -0,0 +1,48 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dyndoc-ruby-doc
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - CQLS
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-02-21 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: |2
14
+ Provide templating in text document.
15
+ email: rdrouilh@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - lib/dyndoc/content_only.rb
21
+ - lib/dyndoc/document.rb
22
+ homepage: http://cqls.upmf-grenoble.fr
23
+ licenses:
24
+ - MIT
25
+ - GPL-2
26
+ metadata: {}
27
+ post_install_message:
28
+ rdoc_options: []
29
+ require_paths:
30
+ - lib
31
+ required_ruby_version: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '0'
36
+ required_rubygems_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ requirements:
42
+ - none
43
+ rubyforge_project:
44
+ rubygems_version: 2.0.14
45
+ signing_key:
46
+ specification_version: 4
47
+ summary: dyndoc document
48
+ test_files: []