Class: Milksteak::YmlContent

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

Direct Known Subclasses

Fragment, Layout, Page

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentObject

Returns the value of attribute content.



3
4
5
# File 'lib/models/yml_content.rb', line 3

def content
  @content
end

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/models/yml_content.rb', line 3

def data
  @data
end

#fileObject

Returns the value of attribute file.



3
4
5
# File 'lib/models/yml_content.rb', line 3

def file
  @file
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/models/yml_content.rb', line 3

def name
  @name
end

#outputObject

Returns the value of attribute output.



3
4
5
# File 'lib/models/yml_content.rb', line 3

def output
  @output
end

Class Method Details

.listObject



5
6
7
8
9
10
11
# File 'lib/models/yml_content.rb', line 5

def self.list
  milk_root = Milksteak::Admin.milk_root
  page_dir = File.join(Milksteak::Admin.milk_root, folder)
  FileUtils.mkdir(milk_root) unless File.exist? milk_root
  FileUtils.mkdir(page_dir) unless File.exist? page_dir
  Dir.glob("#{page_dir}/*.yml")
end

.load(name, mode = "r") ⇒ Object

loads a page from yaml, sets data and content attributes, returns a Milksteak::Page



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/models/yml_content.rb', line 14

def self.load(name, mode = "r")
  milk_root = Milksteak::Admin.milk_root
  page_dir = File.join(Milksteak::Admin.milk_root, folder)
  FileUtils.mkdir(milk_root) unless File.exist? milk_root
  FileUtils.mkdir(page_dir) unless File.exist? page_dir

  page_dir = File.join(page_dir, "#{name}.yml")
  f = File.exist?(page_dir) ? File.open(page_dir, mode) : File.new(page_dir, mode)

  p = self.new
  p.name = name
  p.file = f
  p.read_yaml
  p
end

.render(name, params = {}) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/models/yml_content.rb', line 40

def self.render(name, params = {})
  begin
    p = self.load(name, "r")
    p.render(params)
  rescue Errno::ENOENT => e
    ""
  end
end

.write(name, params = {}, content) ⇒ Object

writes to a page. Replaces params and content, doesn’t merge.



31
32
33
34
35
36
37
38
# File 'lib/models/yml_content.rb', line 31

def self.write(name, params = {}, content)
  p = self.load(name, "w+")
  p.data = params
  p.content = content
  p.write_yaml
  p.file.close
  p
end

Instance Method Details

#read_yamlObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/models/yml_content.rb', line 59

def read_yaml
  self.content = self.file.read
  begin
    if self.content =~ /^(---\s*\n.*?\n?)^(---\s*$\n?)(.*)/m
      self.content = $3
      self.data = YAML.load($1)
    end
  rescue => e
    puts "YAML Exception reading #{name}: #{e.message}"
  end
  self.data ||= {}
end

#render(params = {}) ⇒ Object

todo: test



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/models/yml_content.rb', line 73

def render(params = {})
  rendered = Liquid::Template.parse(self.content).render(self.data.merge("params" => params))

  if self.data["format"]
    if self.data["format"] == "markdown"
      if !self.data["allow_html"].nil? and self.data["allow_html"] == false
        rendered = RDiscount.new(rendered, :filter_html, :smart).to_html 
      else
        rendered = RDiscount.new(rendered, :smart).to_html 
      end
    end
  end

  if self.data["layout"]
    layout = Milksteak::Layout.load(self.data["layout"])
    data = layout.data.merge("yield" => rendered, "params" => params)
    rendered = Liquid::Template.parse(layout.content).render(data)
  end

  return rendered
end

#write_yamlObject



49
50
51
52
53
54
55
56
57
# File 'lib/models/yml_content.rb', line 49

def write_yaml
  if self.data.empty?
    self.file.write("---\n")
  else
    self.file.write(YAML.dump(self.data))
  end
  self.file.write("---\n\n")
  self.file.write(self.content)
end