Class: Jenner::Item

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, site) ⇒ Item

Returns a new instance of Item.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/jenner/item.rb', line 5

def initialize(path, site)
  @path     = path
  @site     = site

  @body = File.read(File.join(@site.root,'_site',@path))

  if @body =~ /\A(---\s*\n.*?\n?)^(---\s*$\n?)/m
    @body   = $'
    @header = YAML.load($1)
  end

  begin
    @title         = @header.delete("title")
    @date          = @header.delete("date")
    @template_path = @header.delete("template")
    @tags          = @header.delete("tags") || []
    @url_format    = @header.delete("url_format")
    @data          = @header
  rescue Exception => e
    raise "Invalid header data on item at path: #{@path}"
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/jenner/item.rb', line 4

def data
  @data
end

#dateObject (readonly)

Returns the value of attribute date.



4
5
6
# File 'lib/jenner/item.rb', line 4

def date
  @date
end

#tagsObject (readonly)

Returns the value of attribute tags.



4
5
6
# File 'lib/jenner/item.rb', line 4

def tags
  @tags
end

#template_pathObject (readonly)

Returns the value of attribute template_path.



4
5
6
# File 'lib/jenner/item.rb', line 4

def template_path
  @template_path
end

#titleObject (readonly)

Returns the value of attribute title.



4
5
6
# File 'lib/jenner/item.rb', line 4

def title
  @title
end

Instance Method Details

#bodyObject



105
106
107
108
109
110
111
112
113
# File 'lib/jenner/item.rb', line 105

def body
  if haml?
    Haml::Engine.new(liquid_body).render(Jenner.deep_struct(self.to_liquid_without_body), :site => Jenner.deep_struct(@site.to_liquid))
  elsif markdown?
    Maruku.new(liquid_body).to_html
  else
    liquid_body
  end
end

#formatted_urlObject



67
68
69
# File 'lib/jenner/item.rb', line 67

def formatted_url
  "/#{relative_path}#{@date.strftime(@url_format).gsub(":title", underscored_title)}"
end

#generate!Object



125
126
127
128
129
130
# File 'lib/jenner/item.rb', line 125

def generate!
  return if File.basename(public_path)[0] == "_"

  FileUtils.mkdir_p(File.dirname(public_path))
  File.write(public_path,render)
end

#haml?Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/jenner/item.rb', line 97

def haml?
  File.extname(@path) == ".haml"
end

#input_filenameObject



55
56
57
# File 'lib/jenner/item.rb', line 55

def input_filename
  File.extname(@path)
end

#liquid_bodyObject



101
102
103
# File 'lib/jenner/item.rb', line 101

def liquid_body
  Liquid::Template.parse(@body).render({'self' => self.to_liquid_without_body}, registers: { site: @site })
end

#local_pathObject



35
36
37
# File 'lib/jenner/item.rb', line 35

def local_path
  @path
end

#markdown?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/jenner/item.rb', line 93

def markdown?
  File.extname(@path) == ".markdown"
end

#output_filenameObject



49
50
51
52
53
# File 'lib/jenner/item.rb', line 49

def output_filename
  return "index.html" if File.extname(url) == ""

  File.basename(@path).sub(".markdown",".html")
end

#pathObject



39
40
41
42
43
44
45
46
47
# File 'lib/jenner/item.rb', line 39

def path
  return @path if @url_format.nil?

  if File.extname(url) == ""
    "#{url}/index.html"
  else
    url
  end
end

#public_pathObject



121
122
123
# File 'lib/jenner/item.rb', line 121

def public_path
  File.join(@site.root,'public',path.sub('.markdown','.html').sub(".haml",".html"))
end

#relative_pathObject



28
29
30
31
32
33
# File 'lib/jenner/item.rb', line 28

def relative_path
  path = File.dirname(@path)
  return "" if path == "."

  "#{path}/"
end

#renderObject



115
116
117
118
119
# File 'lib/jenner/item.rb', line 115

def render
  template.render(
    'item' => self.to_liquid
  )
end

#templateObject



71
72
73
# File 'lib/jenner/item.rb', line 71

def template
  Jenner::Template.from_file(File.join(@site.root,'_templates',"#{@template_path}"), @site)
end

#to_liquidObject



87
88
89
90
91
# File 'lib/jenner/item.rb', line 87

def to_liquid
  to_liquid_without_body.merge(
    'body' => body
  )
end

#to_liquid_without_bodyObject



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/jenner/item.rb', line 75

def to_liquid_without_body
  {
    'title'         => @title,
    'date'          => @date,
    'template_path' => @template_path,
    'tags'          => @tags,
    'data'          => @data,
    'url'           => url,
    'site'          => @site
  }
end

#underscored_titleObject



63
64
65
# File 'lib/jenner/item.rb', line 63

def underscored_title
  @title.gsub(/[^\w]+/,"-").downcase
end

#urlObject



59
60
61
# File 'lib/jenner/item.rb', line 59

def url
  @url_format.nil? ? "/#{@path.sub(".markdown",".html")}" : formatted_url
end