Class: Docgenerator::DocumentTemplate

Inherits:
Object
  • Object
show all
Defined in:
lib/docgenerator/templates/docgenerator_template.rb

Overview

Definition of templates.

This templates are used for document types.

All templates will include their encoding. Default is UTF-8.

Constant Summary collapse

@@templates =
{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key, target, template, encoding = Encoding.find('UTF-8')) ⇒ DocumentTemplate

Define a template.

A template gets a key, a target, the corresponding source and a encoding.

Valid targets are: -:latex -:html -:creole -:context -:text -:wiki (obsolete)

The source should contain the following place holder:

  • <<prefix>> Contains later some admin data like time of creation…

  • <<head>> Some header definitions

  • <<body>> The main text

  • <<classoptions>> Class options (LaTeX)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/docgenerator/templates/docgenerator_template.rb', line 36

def initialize( key, target, template, encoding = Encoding.find('UTF-8'))
  @key     = key
  @target = target
  @encoding = encoding
  @template = template.gsub(/^\t*/, '').encode(@encoding)
  case target
  when :text
  when :wiki
  when :creole
  when :latex
  when :context
  when :html
  else
    DOCGENERATOR_LOGGER.error("DocumentTemplate: Undefined target #{target.inspect}") if DOCGENERATOR_LOGGER.error?
  end
  
  if @@templates[key]
    DOCGENERATOR_LOGGER.warn("DocumentTemplate: Double definition of template #{key.inspect}") if DOCGENERATOR_LOGGER.warn?
  end
  @@templates[key] = self
  
end

Instance Attribute Details

#encodingObject (readonly)

Target encoding of the template



72
73
74
# File 'lib/docgenerator/templates/docgenerator_template.rb', line 72

def encoding
  @encoding
end

#keyObject (readonly)

Returns the value of attribute key.



73
74
75
# File 'lib/docgenerator/templates/docgenerator_template.rb', line 73

def key
  @key
end

#targetObject (readonly)

Target format of the Template (:html, :latex…)



70
71
72
# File 'lib/docgenerator/templates/docgenerator_template.rb', line 70

def target
  @target
end

Class Method Details

.[](key) ⇒ Object



81
82
83
84
85
86
87
88
# File 'lib/docgenerator/templates/docgenerator_template.rb', line 81

def []( key )
  if @@templates[key]
    return @@templates[key]
  else
    DOCGENERATOR_LOGGER.error("Template unknown: #{key}") if DOCGENERATOR_LOGGER.error?
    return nil
  end
end

.keysObject

Returns array with all defined template keys.



90
91
92
# File 'lib/docgenerator/templates/docgenerator_template.rb', line 90

def keys()
  return @@templates.keys
end

.load(yamlfile) ⇒ Object

To add new templates via an external yaml-definition file. See ‘docgenerator_template.yaml’ as an example.

You can use DocumentTemplate.to_yaml() to build your file.



99
100
101
102
103
104
105
106
107
108
# File 'lib/docgenerator/templates/docgenerator_template.rb', line 99

def load( yamlfile )
  File.open(yamlfile){|yaml|
    YAML.load(yaml).each{|key, data|
      DocumentTemplate.new( key, 
                        data[:target], data[:source], 
                        Encoding.find( data[:encoding] || 'UTF-8')
                  )
  }}
  
end

.to_yamlObject

Litte helper to build yaml-files



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/docgenerator/templates/docgenerator_template.rb', line 110

def to_yaml()
  yaml = {}
  @@templates.each{|key, template|
    yaml[key] = {
      #~ :key => template.key,
      :source => template.template,
      :target => template.target,
    }
  }
  puts yaml.to_yaml
end

Instance Method Details

#inspectObject



75
76
77
# File 'lib/docgenerator/templates/docgenerator_template.rb', line 75

def inspect()
  "<#{self.class} #{@key} for #{@target.inspect}>"
end

#templateObject

Template. Base for the document to be created.

  • <<head>> will be replaced by the header data

  • <<body>> will be replaced by the body data



65
66
67
68
# File 'lib/docgenerator/templates/docgenerator_template.rb', line 65

def template()
  #Return a dup. Else the template may be changed
  return @template.dup
end