module Ruhoh::Program

Public Class Methods

compile(target=nil) click to toggle source

Public: A program for compiling to a static website. The compile environment should always be ‘production’ in order to properly omit drafts and other development-only settings.

# File lib/ruhoh/programs/compile.rb, line 6
def self.compile(target=nil)
  ruhoh = Ruhoh.new
  ruhoh.env = 'production'
  ruhoh.setup_plugins

  if target
    ruhoh.config['compiled_path'] = File.expand_path(target)
  end

  ruhoh.compile

  ruhoh
end
preview(opts={}) click to toggle source

Public: A program for running ruhoh as a rack application which renders singular pages via their URL.

Examples

In config.ru:

 require 'ruhoh'
 run Ruhoh::Program.preview

Returns: A new Rack builder object which should work inside config.ru

# File lib/ruhoh/programs/preview.rb, line 16
def self.preview(opts={})
  opts[:watch] ||= true
  opts[:env] ||= 'development'
  
  ruhoh = Ruhoh.new
  ruhoh.env = opts[:env]
  ruhoh.setup_plugins unless opts[:enable_plugins] == false

  # initialize the routes dictionary for all page resources.
  ruhoh.routes.all

  Ruhoh::Program.watch(ruhoh) if opts[:watch]
  Rack::Builder.new {
    use Rack::Lint
    use Rack::ShowExceptions

    # Url endpoints as registered by the resources.
    # The urls are mapped to the resource's individual rack-compatable Previewer class.
    # Note page-like resources (posts, pages) don't render uniform url endpoints,
    # since presumably they define customized permalinks per singular resource.
    # Page-like resources are handled the root mapping below.
    ruhoh.collections.url_endpoints_sorted.each do |h|
      # Omit theme because they are special use-cases.
      next if %w{ theme }.include?(h["name"])
      map h["url"] do
        collection = ruhoh.collection(h["name"])
        if collection.previewer?
          run collection.load_previewer
        else
          run Rack::Cascade.new(
            collection.paths.reverse.map { |path|
              Rack::File.new(path)
            }
          )
        end
      end
    end

    map '/dash' do
      run Ruhoh::UI::Dashboard.new(ruhoh)
    end

    # The generic Page::Previewer is used to render any/all page-like resources,
    # since they likely have arbitrary urls based on permalink settings.
    map '/' do
      run Ruhoh::Resources::Pages::Previewer.new(ruhoh)
    end
  }
end
watch(ruhoh) click to toggle source

Internal: Watch website source directory for file changes. The observer triggers data regeneration as files change in order to keep the data up to date in real time.

# File lib/ruhoh/programs/watch.rb, line 9
def self.watch(ruhoh)
  Ruhoh::Friend.say {
    cyan "=> Start watching: #{ruhoh.cascade.base}"
  }
  dw = DirectoryWatcher.new(ruhoh.cascade.base, {
    :glob => "**/*", 
    :pre_load => true
  })
  dw.interval = 1
  dw.add_observer do |*args| 
    args.each do |event|
      ruhoh.cache.delete(event['path'])

      path = event['path'].gsub(ruhoh.cascade.base + '/', '')

      Ruhoh::Friend.say {
        yellow "Watch [#{Time.now.strftime("%H:%M:%S")}] [Update #{path}] : #{args.size} files changed"
      }

      if %w{ config.json config.yml config.yaml }.include?(path)
        ruhoh.config.touch
      else
        separator = File::ALT_SEPARATOR ?
                    %r{#{ File::SEPARATOR }|#{ File::ALT_SEPARATOR }} :
                    File::SEPARATOR
        resource = path.split(separator)[0]

        ruhoh.cache.delete(ruhoh.collection(resource).files_cache_key)
        ruhoh.cache.delete("#{ resource }-all")

        ruhoh.collection(resource).load_watcher.update(path)
      end
    end
  end

  dw.start
end