Class: Dyndoc::TemplateDocument

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

Constant Summary collapse

@@cfg =

GOAL: to deal with a master document which may generate many output with different formats RMK: no attempt to deal with template in odt format which is a next objective. RMK2: later on, one could imagine to propose a text language easily convertible to the expected formats

{
  :working_dir => "", #directory where dyndoc is processed
  :dyndoc_mode => :normal, #default mode, alternative modes :local_server, :remote_server
  ## OBSOLETE: :docker_mode => false,
  :filename_tmpl=>"", #be specified later
  :filename_tmpl_orig=>"", #be specified later
  :lib_dyn_content=>"",
  :dirname_docs=>"", #default directory unless specified by doc!
  :rootDoc=>"",
  :user_input=>[],
  :tag_tmpl=>[], #old part_tag!
  :keys_tmpl=>[],
  :docs=>[], #this one is introduced from version V3 to deal with multidocuments
  :doc_list=>[], #list of documents if nonempty
  :cmd=>[], #list of commands if nonempty for every document
  :cmd_pandoc_options => [], #pandoc options
  :pandoc_filter => "",
  :options => {}, # added for example to compile twice latex
  :dtag=>:dtag,
  :dtags=>[:dtag],
  :raw_mode=>false,
  :model_tmpl=>"default",
  :model_doc=>"default",
  :append => "",
  :verbose => false,
  :debug => false
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DynConfig

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

Constructor Details

#initialize(name) ⇒ TemplateDocument

docs is a hash containing all the files



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
# File 'lib/dyndoc/document.rb', line 139

def initialize(name) #docs is a hash containing all the files
  ## complete with default extension if not provided
  name += ".dyn" if File.basename(name) == File.basename(name,".*")
  @name=name
  ## read config from name
  @cfg=@@cfg.dup
  read_cfg(@name)
  ## the template manager
  Dyndoc.tmpl_mngr=@tmpl_mngr = Dyndoc::Ruby::TemplateManager.new(@cfg)
  ## the documents
  @docs={}
  make_doc_list
  if @cfg[:content] #a non file based document is a Hash cfg with :content inside (give a basename used for generated files)
    @content=@cfg[:content]
  else
    # find the basename of the template
    @basename=basename_tmpl
    @dirname=File.dirname(@basename)
    @basename=File.basename(@basename)
  #p @basename
    @basename_orig=basename_tmpl_orig
    @dirname_orig=File.dirname(@basename_orig)
    @basename_orig=File.basename(@basename_orig)
  #p @basename_orig
    # read content of the template
    @content=File.read(@cfg[:filename_tmpl])
  end
  # list of Document objects
#puts "@doc_list (init)";p @doc_list
  @doc_list.each do |kdoc|
    @docs[kdoc]=Document.new(kdoc,self)
  end
end

Instance Attribute Details

#basenameObject

Returns the value of attribute basename.



137
138
139
# File 'lib/dyndoc/document.rb', line 137

def basename
  @basename
end

#basename_origObject

Returns the value of attribute basename_orig.



137
138
139
# File 'lib/dyndoc/document.rb', line 137

def basename_orig
  @basename_orig
end

#cfgObject

Returns the value of attribute cfg.



137
138
139
# File 'lib/dyndoc/document.rb', line 137

def cfg
  @cfg
end

#contentObject

Returns the value of attribute content.



137
138
139
# File 'lib/dyndoc/document.rb', line 137

def content
  @content
end

#dirnameObject

Returns the value of attribute dirname.



137
138
139
# File 'lib/dyndoc/document.rb', line 137

def dirname
  @dirname
end

#dirname_origObject

Returns the value of attribute dirname_orig.



137
138
139
# File 'lib/dyndoc/document.rb', line 137

def dirname_orig
  @dirname_orig
end

#docsObject

Returns the value of attribute docs.



137
138
139
# File 'lib/dyndoc/document.rb', line 137

def docs
  @docs
end

#tmpl_mngrObject

Returns the value of attribute tmpl_mngr.



137
138
139
# File 'lib/dyndoc/document.rb', line 137

def tmpl_mngr
  @tmpl_mngr
end

Instance Method Details

#basename_tmplObject

document basename from template filename



277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/dyndoc/document.rb', line 277

def basename_tmpl
  ##p @cfg[:filename_tmpl]
  mode=Dyndoc.guess_mode(@cfg[:filename_tmpl])
  ##p ["mode",mode,Dyndoc.tmplExt[mode]]
  if mode
    name,ext=@cfg[:filename_tmpl].scan(/^(.*)(?:#{Dyndoc.tmplExt[mode].join("|")})$/).flatten.compact
  else
    name,ext=@cfg[:filename_tmpl].scan(/^(.*)(?:_tmpl(\..*)|(\.dyn))$/).flatten.compact
  end
  #p [:name,@cfg[:filename_tmpl],name]
  name
end

#basename_tmpl_origObject



290
291
292
293
294
295
296
297
298
# File 'lib/dyndoc/document.rb', line 290

def basename_tmpl_orig
  mode=Dyndoc.guess_mode(@cfg[:filename_tmpl_orig])
  if mode
    name,ext=@cfg[:filename_tmpl_orig].scan(/^(.*)(?:#{Dyndoc.tmplExt[mode].join("|")})$/).flatten.compact
  else
    name,ext=@cfg[:filename_tmpl_orig].scan(/^(.*)(?:_tmpl(\..*)|(\.dyn))$/).flatten.compact
  end
  name
end

#cfg_dyn_from(tmpl) ⇒ Object



233
234
235
236
237
238
239
240
# File 'lib/dyndoc/document.rb', line 233

def cfg_dyn_from(tmpl)
  code,cfg_file=nil,nil
  code=File.read(cfg_file) if (cfg_file=(Dyndoc::Utils.cfg_file_exists? tmpl))
  ##puts "code";p code;p cfg_file
  Utils.clean_bom_utf8!(code) if code
  code="Dyndoc::TexDoc" unless code
  return cfg_dyn_from_code(code)
end

#cfg_dyn_from_code(code) ⇒ Object



214
215
216
217
218
219
220
221
# File 'lib/dyndoc/document.rb', line 214

def cfg_dyn_from_code(code)
  if code and code.is_a? String
      code="{\n"+code+"\n}" if code=~/\A\s*\:/m #to avoid at the beginning { and at the end }!
      ##p [code,Object.class_eval(code)]
      return Object.class_eval(code)
  end
  return nil
end

#cfg_dyn_from_lib(content) ⇒ Object



223
224
225
226
227
228
229
230
231
# File 'lib/dyndoc/document.rb', line 223

def cfg_dyn_from_lib(content)
    return {:cfg=> nil, :lib_dyn_content => content} if content.empty?
    tmp=content.force_encoding("utf-8").split(/\[\#/)
    if tmp[0].empty? and tmp[1][0...4]=="cfg]"
      return {:cfg=> tmp[1][4..-1], :lib_dyn_content => (tmp[0,1]+tmp[2..-1]).join('[#')}
    else
      return {:cfg=> nil, :lib_dyn_content => content}
    end
end

#lib_dyn_content_from(tmpl = nil) ⇒ Object



208
209
210
211
212
# File 'lib/dyndoc/document.rb', line 208

def lib_dyn_content_from(tmpl=nil)
  code,lib_file = "",nil
  code=File.read(lib_file) if (lib_file=(Dyndoc::Utils.lib_file_exists? tmpl))
  return code
end

#make_allObject



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# File 'lib/dyndoc/document.rb', line 300

def make_all
  if @cfg[:cmd].include? :list
    puts "document list: "+@cfg[:docs].keys.join(", ")
    return
  end

  # Added for docker mode
  ##OBSOLETE## Docker.init_task_file if Dyndoc.cfg_dyn[:docker_mode]

  ##puts "@doc_list"; p @doc_list
  @doc_list.each do |kdoc|
   @docs[kdoc].make_all
  end

  ##OBSOLETE## Docker.save_task_file if Dyndoc.cfg_dyn[:docker_mode]

end

#make_doc_listObject



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/dyndoc/document.rb', line 260

def make_doc_list
  doc_list=@cfg[:docs].keys
#puts "@cfg[doc_list]";p @cfg[:doc_list]
  doc_list &= @cfg[:doc_list] unless @cfg[:doc_list].empty?
#p doc_list
  #deal with aliases
  doc_alias={}
  @cfg[:docs].each_pair{|key,doc|
  	doc_alias[key]=doc if doc.is_a? Array
  }
#puts "doc_alias";p doc_alias
  doc_list=doc_list.map{|key| (doc_alias[key] ?  doc_alias[key] : key )}.flatten until (doc_list &  doc_alias.keys).empty?
  @doc_list=doc_list
#puts "doc_list";p @doc_list
end

#read_cfg(name, mode = :all) ⇒ Object



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
# File 'lib/dyndoc/document.rb', line 173

def read_cfg(name,mode=:all)
  # cfg_dyn is the options given inside the master template
  cfg_dyn=nil


  name_tmpl=Dyndoc.name_tmpl(name,mode)
#Dyndoc.warn "read_cfg:name_tmpl",[name,name_tmpl]
  if name_tmpl
    name_tmpl2=Dyndoc.directory_tmpl? name_tmpl
    if name_tmpl2
      # test if [#cfg] block exists inside _lib.dyn file
      lib_dyn_content=lib_dyn_content_from(name_tmpl2)
      tmp=cfg_dyn_from_lib(lib_dyn_content)
      if tmp[:cfg]
        #p :cfg_from_lib
        cfg_dyn=cfg_dyn_from_code(tmp[:cfg])
        lib_dyn_content=tmp[:lib_dyn_content]
      else
        cfg_dyn=cfg_dyn_from(name_tmpl2)
      end
      cfg_dyn[:filename_tmpl]=name_tmpl2
      cfg_dyn[:lib_dyn_content]=lib_dyn_content
    else
      cfg_dyn=Dyndoc::TexDoc
    end
    cfg_dyn[:filename_tmpl_orig] = name_tmpl
  end
#Dyndoc.warn "read_cfg:cfg_dyn",cfg_dyn

  #otherwise it is the default version!
  append_cfg(cfg_dyn) if cfg_dyn
  #read the optional cfg
  read_cmdline
end

#read_cmdlineObject



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
# File 'lib/dyndoc/document.rb', line 242

def read_cmdline
  ##p [:read_cmdline,Dyndoc.cfg_dyn]
  cfg_cmdline={}
  cfg_cmdline[:doc_list]=Dyndoc.cfg_dyn[:doc_list] unless Dyndoc.cfg_dyn[:doc_list].empty?
  cfg_cmdline[:cmd]=Dyndoc.cfg_dyn[:cmd_doc] unless Dyndoc.cfg_dyn[:cmd_doc].empty?
  cfg_cmdline[:model_doc]=Dyndoc.cfg_dyn[:model_doc] unless Dyndoc.cfg_dyn[:model_doc].empty?
  cfg_cmdline[:tag_tmpl]=Dyndoc.cfg_dyn[:tag_tmpl] unless Dyndoc.cfg_dyn[:tag_tmpl].empty?
  cfg_cmdline[:options]=Dyndoc.cfg_dyn[:options] unless Dyndoc.cfg_dyn[:options].empty?
  ## select doc_list by reading "[#default]" useful in atom
  if !@cfg[:content] and Dyndoc.cfg_dyn[:doc_list].empty? and Dyndoc.cfg_dyn[:tag_tmpl].empty?
    if File.read(@cfg[:filename_tmpl]).force_encoding("utf-8") =~ /^\[\#default\]([^\[]*)\[\#/
      cfg_cmdline[:doc_list]=$1.strip.split(",").map{|e| e.strip}
      puts "Default document: "+cfg_cmdline[:doc_list].join(",")
    end
  end
  append_cfg(cfg_cmdline)
end