class Zenweb::Site

Holder for the entire website. Everything gets driven from here.

TODO: describe expected filesystem layout and dependency mgmt.

Attributes

configs[R]

Returns all configs found via scan

layouts[R]

Returns all known layouts found via scan

pages[R]

Returns all pages found via scan

Public Class Methods

binary_files() click to toggle source
# File lib/zenweb/site.rb, line 222
def self.binary_files
  @binary_files ||= %w[png jpg gif eot svg ttf woff2? ico pdf m4a t?gz]
end
load_plugins() click to toggle source

Loads all files matching “zenweb/plugins/*.rb”.

# File lib/zenweb/site.rb, line 36
def self.load_plugins
  seen = {}
  Gem.find_files("zenweb/plugins/*.rb").each do |path|
    file = File.basename path
    next if seen[file]
    seen[file] = true
    require path
  end
end
text_files() click to toggle source
# File lib/zenweb/site.rb, line 226
def self.text_files
  @text_files ||= %w[txt html css js]
end

Public Instance Methods

categories() click to toggle source

Returns a magic hash that groups up all the pages by category (directory). The hash has extra accessor methods on it to make grabbing what you want a bit cleaner. eg:

site.categories.blog # => [Page[blog/...], ...]
# File lib/zenweb/site.rb, line 61
def categories
  @categories ||=
    begin
      h = Hash.new { |h2,k| h2[k] = [] }

      def h.method_missing msg, *args
        if self.has_key? msg.to_s then
          self[msg.to_s]
        else
          super
        end
      end

      time_prune
      fix_subpages

      pages.each do |url, page|
        dir = url.split(/\//).first
        next unless File.directory? dir and dir !~ /^_/
        next if url =~ /index.html/ or url !~ /html/
        h[dir] << page
      end

      h.keys.each do |dir|
        h[dir] = h[dir].sort_by { |p| [-p.date.to_i, p.title ] }
      end

      h
    end
end
config() click to toggle source

Return the top level config.

# File lib/zenweb/site.rb, line 95
def config
  configs["_config.yml"]
end
generate() click to toggle source

Generates the website by invoking the ‘site’ task.

# File lib/zenweb/site.rb, line 102
def generate
  task(:site).invoke
end
html_page_map(&map) click to toggle source

Return a list of all pages my applying map to all html_pages and cleaning up the results.

# File lib/zenweb/site.rb, line 110
def html_page_map &map
  html_pages.map(&map).flatten.uniq.compact
end
html_pages() click to toggle source

Returns a list of all known html pages.

# File lib/zenweb/site.rb, line 117
def html_pages
  self.pages.values.select { |p| p.url_path =~ /\.html/ }
end
layout(name) click to toggle source

Returns a layout named name.

# File lib/zenweb/site.rb, line 128
def layout name
  name and (@layouts[name] or raise "unknown layout #{name.inspect}")
end
method_missing(msg, *_args) click to toggle source

Proxy object for the config. Returns a config item at msg.

# File lib/zenweb/site.rb, line 143
def method_missing msg, *_args
  k = msg.to_s
  config.key?(k) ? config[k] : warn("#{self.inspect} does not define #{k}")
end
pages_by_date() click to toggle source

Returns all pages (with titles) sorted by date.

# File lib/zenweb/site.rb, line 151
def pages_by_date
  # page.config["title"] avoids the warning
  html_pages.select {|page| page.config["title"] }.
    sort_by { |page| [-page.date.to_i, page.title] }
end
pages_by_url() click to toggle source

Returns a hash mapping page url to page.

# File lib/zenweb/site.rb, line 160
def pages_by_url
  unless defined? @pages_by_url then
    h = {}
    pages.each do |_,p|
      h[p.url] = p
    end
    @pages_by_url = h
  end
  @pages_by_url
end
scan() click to toggle source

Scans the directory tree and finds all relevant pages, configs, layouts, etc.

TODO: talk about expected directory structure and extra naming enhancements.

# File lib/zenweb/site.rb, line 178
def scan
  excludes = %w[~ Rakefile] + Array(config["exclude"])

  top = Dir["*"] - excludes
  files = top.select { |path| File.file? path }
  files += Dir["{#{top.join(",")}}/**/*"].reject { |f| not File.file? f }
  files.reject! { |f| f.include? "/_" }

  renderers_re = Page.renderers_re

  files.each do |path|
    case path
    when /(?:#{excludes.join '|'})$/
      # ignore
    when /^_layout/ then
      name = File.basename(path).sub(/\..+$/, '')
      @layouts[name] = Page.new self, path
    when /^_/ then
      next
    when /\.yml$/ then
      @configs[path] = Config.new self, path
    when /\.(?:#{self.class.binary_files.join("|")})$/ then
      @pages[path] = Page.new self, path, self.config
    when /\.(?:#{self.class.text_files.join("|")})$/, renderers_re then
      @pages[path] = Page.new self, path
    else
      warn "unknown file type: #{path}" if Rake.application.options.trace
    end
  end

  $website = self # HACK
  task(:virtual_pages).invoke

  time_prune

  fix_subpages
end
stale?() click to toggle source
# File lib/zenweb/site.rb, line 234
def stale?
  not stale_pages.empty?
end
stale_pages() click to toggle source
# File lib/zenweb/site.rb, line 230
def stale_pages
  pages.values.find_all(&:stale?)
end
time_prune() click to toggle source
# File lib/zenweb/site.rb, line 216
def time_prune
  t = Time.now
  @pages.reject! { |path, page| page.date && page.date > t } unless
    ENV["ALL"]
end
wire() click to toggle source

Wire up all the configs and pages. Invokes :extra_wirings to allow you to add extra manual dependencies.

# File lib/zenweb/site.rb, line 275
def wire
  directory ".site"
  task :site => ".site"

  configs.each do |path, config|
    config.wire
  end

  pages.each do |path, page|
    page.wire
  end

  $website = self # HACK
  task(:extra_wirings).invoke
end