Class: Rack::Bunto

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/bunto.rb,
lib/rack/bunto/utils.rb,
lib/rack/bunto/version.rb,
lib/rack/bunto/filehandler.rb

Defined Under Namespace

Modules: Utils Classes: FileHandler

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Bunto

Initializes a new Rack::Bunto site.

Options:

:config

use given config file (default: “_config.yml”)

:force_build

whether to always generate the site at startup, even when the destination path is not empty (default: false)

:auto

whether to watch for changes and rebuild (default: false)

Other options are passed on to Bunto::Site.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/rack/bunto.rb', line 25

def initialize(options = {})
  overrides = options.dup
  @compiling = false
  @force_build = overrides.fetch(:force_build, false)
  @auto        = overrides.fetch(:auto, false)

  overrides.delete(:force_build)
  overrides.delete(:auto)
  @config = ::Bunto.configuration(overrides)

  @destination = @config["destination"]
  @source      = @config["source"]

  @files = FileHandler.new(@destination)
  @site = ::Bunto::Site.new(@config)

  if @files.empty? || @force_build
    process("Generating site: #{@source} -> #{@destination}")
  end

  if @auto
    require 'listen'
    require 'listen/version'
    require 'pathname'

    puts "Auto-regeneration enabled: #{@source} -> #{@destination}"

    source_abs = ::File.expand_path(@source)
    dest_abs   = ::File.expand_path(@destination)
    relative_path_to_dest = Pathname.new(dest_abs)
                            .relative_path_from(Pathname.new(source_abs))
                            .to_path
    ignore_pattern = %r{#{Regexp.escape(relative_path_to_dest)}}

    listener = Listen.to(@source, :ignore => ignore_pattern) do |modified, added, removed|
      t = Time.now.strftime("%Y-%m-%d %H:%M:%S")
      n = modified.length + added.length + removed.length
      process("[#{t}] Regenerating: #{n} file(s) changed")
    end
    listener.start  unless Listen::VERSION =~ /\A[0-1]\./
  end
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



11
12
13
# File 'lib/rack/bunto.rb', line 11

def config
  @config
end

#destinationObject (readonly)

Returns the value of attribute destination.



11
12
13
# File 'lib/rack/bunto.rb', line 11

def destination
  @destination
end

Class Method Details

.versionObject



3
4
5
# File 'lib/rack/bunto/version.rb', line 3

def self.version
  '2.0.0'
end

Instance Method Details

#call(env) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/rack/bunto.rb', line 68

def call(env)
  while @compiling
    sleep 0.1
  end

  request = Rack::Request.new(env)

  filename = @files.get_filename(request.path_info)

  if filename
    media_type = Utils.media_type(filename)

    file = Utils.file_info(filename)
    body = file[:body]
    time = file[:time]
    hdrs = { "Last-Modified" => time }

    if time == request.env["HTTP_IF_MODIFIED_SINCE"]
      response = [304, hdrs, []]
    else
      hdrs.update({ "Content-Length" => body.bytesize.to_s,
                    "Content-Type"   => media_type })
      response = [200, hdrs, [body]]
    end

  else
    body = not_found_message
    hdrs = { "Content-Length" => body.bytesize.to_s,
             "Content-Type"   => "text/html" }
    response = [404, hdrs, [body]]
  end

  request.head? ? remove_body(response) : response
end