class Serum::Site

Attributes

baseurl[RW]
config[RW]
exclude[RW]
include[RW]
posts[RW]
slugs_to_posts[RW]
source[RW]
static_files[RW]
time[RW]

Public Class Methods

new(config) click to toggle source

Public: Initialize a new Site.

config - A Hash containing site configuration details.

# 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

Public Instance Methods

filter_entries(entries) click to toggle source

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.

# 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) click to toggle source

Looks up a post by its slug.

slug - A String denoting the slug for the post.

Returns the found Post or nil.

# File lib/serum/site.rb, line 40
def find_by_slug(slug)
  self.slugs_to_posts[slug]
end
get_entries(dir, subfolder) click to toggle source

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

# 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
inspect() click to toggle source
# File lib/serum/site.rb, line 123
def inspect
  "<Site: #{source}>"
end
read_directories(dir = '') click to toggle source

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.

# 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) click to toggle source

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.

# 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
reset() click to toggle source

Reset Site details.

Returns nothing

# 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