Class: Ecstatic::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/ecstatic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(templatefile = nil, datafiles = [], layoutfile = nil) ⇒ Page

Returns a new instance of Page.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ecstatic.rb', line 12

def initialize(templatefile = nil, datafiles = [], layoutfile = nil)
  @templatefile = templatefile
  @layoutfile = layoutfile
  # get context from data files
  @contexthash = {}
  datafiles.each do |file|
    yamltext = File.open(file).read
    yaml = YAML::load(yamltext)
    # if YAML is not a hash, make a hash with file's basename as key:
    unless yaml.class == Hash
      yaml = {File.basename(file, File.extname(file)) => yaml}
    end
    yaml.each_pair do |key,val|
      model = key.singularize.capitalize
      begin
        @contexthash[key] = Object.const_get(model).from_array(val)
      rescue
        $stderr.puts("Unable to initialize " + key + " from model " + model)
        @contexthash[key] = val
      end
    end
  end
end

Instance Attribute Details

#contexthashObject

Returns the value of attribute contexthash.



10
11
12
# File 'lib/ecstatic.rb', line 10

def contexthash
  @contexthash
end

#layoutfileObject

Returns the value of attribute layoutfile.



10
11
12
# File 'lib/ecstatic.rb', line 10

def layoutfile
  @layoutfile
end

#templatefileObject

Returns the value of attribute templatefile.



10
11
12
# File 'lib/ecstatic.rb', line 10

def templatefile
  @templatefile
end

Instance Method Details

#escapefun(format) ⇒ Object



36
37
38
39
40
41
42
43
44
45
# File 'lib/ecstatic.rb', line 36

def escapefun(format)
 case
 when format == :html
   return 'Ecstatic.markdown_to_compact_html'
 when format == :latex
   return 'Ecstatic.markdown_to_latex'
 else
   return 'escape'
 end
end

#to_format(format) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ecstatic.rb', line 47

def to_format(format)
  engine = Tenjin::Engine.new(:cache => false, :escapefunc => escapefun(format))
  
  if File.extname(self.templatefile) == '.markdown'
    contents = File.open(self.templatefile).read
    output = case
             when format == :html
                markdown_to_compact_html(contents)
             when format == :latex
                markdown_to_latex(contents)
             else
                escape(contents)
             end
  else
    context = Tenjin::Context.new(self.contexthash)
    output = engine.render(self.templatefile, context)
  end
 
  if self.layoutfile
    supercontext = Tenjin::Context.new({'_contents' => output})
    superoutput = engine.render(self.layoutfile, {'_contents' => output})
    return superoutput
  else
    return output
  end
end

#to_htmlObject



74
75
76
# File 'lib/ecstatic.rb', line 74

def to_html
  self.to_format(:html)
end

#to_latexObject



78
79
80
# File 'lib/ecstatic.rb', line 78

def to_latex
  self.to_format(:latex)
end

#to_plainObject



82
83
84
# File 'lib/ecstatic.rb', line 82

def to_plain
  self.to_format(:plain)
end