Class: Serum::Site

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Site

Public: Initialize a new Site.

config - A Hash containing site configuration details.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/serum/site.rb', line 9

def initialize(config)
  self.config          = config.clone

  self.source          = File.expand_path(config['source'])
  self.baseurl         = config['baseurl']
  self.exclude         = config['exclude'] || []
  self.include         = config['include'] || []

  self.reset
  self.read_directories
end

Instance Attribute Details

#baseurlObject

Returns the value of attribute baseurl.



4
5
6
# File 'lib/serum/site.rb', line 4

def baseurl
  @baseurl
end

#configObject

Returns the value of attribute config.



3
4
5
# File 'lib/serum/site.rb', line 3

def config
  @config
end

#excludeObject

Returns the value of attribute exclude.



3
4
5
# File 'lib/serum/site.rb', line 3

def exclude
  @exclude
end

#includeObject

Returns the value of attribute include.



3
4
5
# File 'lib/serum/site.rb', line 3

def include
  @include
end

#postsObject

Returns the value of attribute posts.



3
4
5
# File 'lib/serum/site.rb', line 3

def posts
  @posts
end

#slugs_to_postsObject

Returns the value of attribute slugs_to_posts.



4
5
6
# File 'lib/serum/site.rb', line 4

def slugs_to_posts
  @slugs_to_posts
end

#sourceObject

Returns the value of attribute source.



3
4
5
# File 'lib/serum/site.rb', line 3

def source
  @source
end

#static_filesObject

Returns the value of attribute static_files.



3
4
5
# File 'lib/serum/site.rb', line 3

def static_files
  @static_files
end

#timeObject

Returns the value of attribute time.



4
5
6
# File 'lib/serum/site.rb', line 4

def time
  @time
end

Instance Method Details

#filter_entries(entries) ⇒ Object

Filter out any files/directories that are hidden or backup files (start with “.” or “#” or end with “~”), or contain site content (start with “_”), or are excluded in the site configuration, unless they are web server files such as ‘.htaccess’.

entries - The Array of String file/directory entries to filter.

Returns the Array of filtered entries.



100
101
102
103
104
105
106
107
108
109
# File 'lib/serum/site.rb', line 100

def filter_entries(entries)
  entries.reject do |e|
    unless self.include.glob_include?(e)
      ['.', '_', '#'].include?(e[0..0]) ||
      e[-1..-1] == '~' ||
      self.exclude.glob_include?(e) ||
      (File.symlink?(e) && self.safe)
    end
  end
end

#find_by_slug(slug) ⇒ Object

Looks up a post by its slug.

slug - A String denoting the slug for the post.

Returns the found Post or nil.



40
41
42
# File 'lib/serum/site.rb', line 40

def find_by_slug(slug)
  self.slugs_to_posts[slug]
end

#get_entries(dir, subfolder) ⇒ Object

Read the entries from a particular directory for processing

dir - The String relative path of the directory to read subfolder - The String directory to read

Returns the list of entries to process



117
118
119
120
121
# File 'lib/serum/site.rb', line 117

def get_entries(dir, subfolder)
  base = File.join(self.source, dir, subfolder)
  return [] unless File.exists?(base)
  entries = Dir.chdir(base) { filter_entries(Dir['**/*']) }
end

#inspectObject



123
124
125
# File 'lib/serum/site.rb', line 123

def inspect
  "<Site: #{source}>"
end

#read_directories(dir = '') ⇒ Object

Recursively traverse directories to find posts and static files that will become part of the site according to the rules in filter_entries.

dir - The String relative path of the directory to read. Default: ”.

Returns nothing.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/serum/site.rb', line 51

def read_directories(dir = '')
  self.reset
  base = File.join(self.source, dir)
  entries = Dir.chdir(base) { filter_entries(Dir.entries('.')) }

  self.read_posts(dir)
  self.posts.sort!

  entries.each do |f|
    f_abs = File.join(base, f)
    f_rel = File.join(dir, f)
    if File.directory?(f_abs)
      read_directories(f_rel)
    elsif !File.symlink?(f_abs)
      static_files << StaticFile.new(self, self.source, dir, f)
    end
  end
end

#read_posts(dir) ⇒ Object

Read all the files in <source>/<dir> and create a new Post object with each one.

dir - The String relative path of the directory to read.

Returns nothing.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/serum/site.rb', line 76

def read_posts(dir)
  entries = get_entries(dir, '')

  # first pass processes, but does not yet render post content
  entries.each do |f|
    if Post.valid?(f)
      post = Post.new(self, self.source, dir, f)

      if post.published && post.date <= self.time
        self.posts << post
        self.slugs_to_posts[post.slug] = post
      end
    end
  end
end

#resetObject

Reset Site details.

Returns nothing



24
25
26
27
28
29
30
31
32
33
# File 'lib/serum/site.rb', line 24

def reset
  self.time            = if self.config['time']
                           Time.parse(self.config['time'].to_s)
                         else
                           Time.now
                         end
  self.posts           = []
  self.static_files    = []
  self.slugs_to_posts  = {}
end