Class: Spandex::Page

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#extensionObject (readonly)

Returns the value of attribute extension.



9
10
11
# File 'lib/spandex/page.rb', line 9

def extension
  @extension
end

#mtimeObject (readonly)

Returns the value of attribute mtime.



9
10
11
# File 'lib/spandex/page.rb', line 9

def mtime
  @mtime
end

#pathObject (readonly)

Returns the value of attribute path.



9
10
11
# File 'lib/spandex/page.rb', line 9

def path
  @path
end

#render_optionsObject (readonly)

Returns the value of attribute render_options.



9
10
11
# File 'lib/spandex/page.rb', line 9

def render_options
  @render_options
end

Class Method Details

.from_filename(filename, base_path, render_options = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/spandex/page.rb', line 11

def self.from_filename(filename, base_path, render_options = {})
  pathname = Pathname.new(filename)
  return nil unless pathname.exist?

  path = path_from_file(pathname, base_path)
  , content = parse_file(filename)

  Page.new(path, content, pathname.extname, , render_options)
end

.metadata?(text) ⇒ Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/spandex/page.rb', line 89

def self.metadata?(text)
  text.split("\n").first =~ /^[\w ]+:/
end

.path_from_file(pathname, base_path) ⇒ Object



21
22
23
24
# File 'lib/spandex/page.rb', line 21

def self.path_from_file(pathname, base_path)
  pathname = pathify pathname
  pathify pathname.relative_path_from(pathify(base_path)).sub_ext('')
end

.registered?(pathname) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
# File 'lib/spandex/page.rb', line 26

def self.registered?(pathname)
  pathname = pathify(pathname)
  Tilt.registered?(pathname.extname.sub(/^./, ''))
end

Instance Method Details

#bodyObject



47
48
49
# File 'lib/spandex/page.rb', line 47

def body
  @rendered_body ||= Tilt[@extension].new(nil, 1, @render_options){@content}.render
end

#dateObject



43
44
45
# File 'lib/spandex/page.rb', line 43

def date
  @date ||= (:date) ? DateTime.parse((:date)) : nil
end

#draft?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'lib/spandex/page.rb', line 55

def draft?
  (:draft) ? true : false
end

#metadata(*keys) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/spandex/page.rb', line 31

def (*keys)
  keys.each do |key|
    key = key.to_s
    return @metadata[key] if @metadata.has_key? key
  end
  nil
end

#tagsObject



51
52
53
# File 'lib/spandex/page.rb', line 51

def tags
  @tags ||= (:tags, :categories) ? (:tags, :categories).split(",").map{|tag| tag.strip} : []
end

#titleObject



39
40
41
# File 'lib/spandex/page.rb', line 39

def title
  (:title) || "(Unknown Title)"
end

#to_atom_entry(root) ⇒ Object



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

def to_atom_entry(root)
  unless date
    raise "Must have a date"
  end

  Atom::Entry.new do |entry|
    entry.title = title
    entry.updated = date
    entry.id = "#{root},#{@path}"
    entry.links << Atom::Link.new(:href => "http://#{root}/#{@path}")
    entry.content = Atom::Content::Html.new(body)
  end
end