inert 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9fa03de85cd5a1d823843221a17b25ba4a48608c
4
- data.tar.gz: 3d5c640a2cf87564991504c84435820016a6eaab
3
+ metadata.gz: f79a5379e8bc3c2efef1112aa3594a0d21c5ebd2
4
+ data.tar.gz: dac79be86b3228b75c50344d97a1af2131efa962
5
5
  SHA512:
6
- metadata.gz: e9c3978a0924899c9d3cb7f326d727a07364b0ca6814b07ea931f46a8ea3cfd5660bb537de3e8c2b5fe35ede51fca105aa06f6cc53da71eb129e9a1a6f3db1da
7
- data.tar.gz: 3b57e1472757407d5a3b00725f7aedc1f089055eb674425c6e43a43449ab44d1dbb127fc2d4c06e2cbf3542502c53ead4ed227fdea6358a254d930262c1e4ac1
6
+ metadata.gz: 958d318d85847d81d45b76d7e2a42f2e6b8d437b0dc1fa896676ddd0e4ae4603fa9aac2bdf575fd53cc02f2751276d5374bb3aebfdbaae83165c9c79ef4a3194
7
+ data.tar.gz: 96b3d288ead98c0e282eddec337bcd01de90d32a8c1eff0ad11b86ea09d5860c6dd28fe01b4eb621e47b6c2b6cd24ffe0e472248a254e82102d0b5761bcf5552
data/README.md CHANGED
@@ -8,6 +8,105 @@ An experimental static site builder with unambitious goals.
8
8
  - Allow reading of YAML frontmatter
9
9
  - Allow queueing of manual URLs for preservation
10
10
 
11
+
12
+ ## Usage
13
+
14
+ Install the gem:
15
+
16
+ gem install inert
17
+
18
+ Create a few folders:
19
+
20
+ mkdir static views
21
+
22
+ And a layout file:
23
+
24
+ echo "<%= yield %>" > views/layout.erb
25
+
26
+ And then start the Inert server:
27
+
28
+ inert
29
+
30
+ When you're ready to deploy, build the site:
31
+
32
+ inert build
33
+
34
+ ## Configuring
35
+
36
+ By default, Inert expects a few things, and should work without configuration.
37
+ Because it's built on top of Roda, it's very easily extendable, and Inert
38
+ provides hooks to do this via the `Inertfile` it will read from your project
39
+ root at runtime.
40
+
41
+ ```ruby
42
+ # Inertfile
43
+ Inert.config do |inert|
44
+ inert.helpers do
45
+ def generator
46
+ "Inert v#{Inert::VERSION}"
47
+ end
48
+ end
49
+
50
+ inert.app do
51
+ plugin :h
52
+ end
53
+
54
+ inert.routes do |r|
55
+ r.on "employees.html" do
56
+ @employees = [] # Read in actual data here
57
+ view("employees.html.erb")
58
+ end
59
+ end
60
+ end
61
+ ```
62
+
63
+ ## Asset Fingerprinting
64
+
65
+ Use asset fingerprinting to enable max-cache times on all your static assets.
66
+
67
+ ```ruby
68
+ # Inertfile
69
+ Inert.config do |inert|
70
+ inert.app do
71
+ plugin :timestamp_public
72
+ end
73
+
74
+ inert.routes do |r|
75
+ r.timestamp_public
76
+ end
77
+ end
78
+ ```
79
+
80
+ And inside your views, use the `timestamp_path` helper with the name of the file
81
+ in `static/` wherever you'd just use the filename itself:
82
+
83
+ ```ruby
84
+ <img src="<%= timestamp_path "some_image_in_static_folder.png" %>">
85
+ ```
86
+
87
+ ## Live Reloads
88
+
89
+ Use the `roda-live_reload` gem to enable live reloads:
90
+
91
+
92
+ ```ruby
93
+ # Gemfile
94
+ gem "roda-live_reload"
95
+ gem "puma" # Webrick wont' currently work with roda-live_reload
96
+
97
+
98
+ # Inertfile
99
+ Inert.config do |inert|
100
+ inert.app do
101
+ plugin :live_reload if ENV["RACK_ENV"] == "development"
102
+ end
103
+
104
+ inert.routes do |r|
105
+ r.live_reload if ENV["RACK_ENV"] == "development"
106
+ end
107
+ end
108
+ ```
109
+
11
110
  ## Contributing
12
111
 
13
112
  Bug reports and pull requests are welcome on GitHub at https://github.com/adam12/inert.
data/exe/inert CHANGED
@@ -1,11 +1,18 @@
1
1
  #!/usr/bin/env ruby
2
- require "inert"
2
+ require "inert/config"
3
+
4
+ inertfile = File.join(Dir.pwd, "Inertfile")
5
+ if File.exist?(inertfile)
6
+ load inertfile
7
+ end
3
8
 
4
9
  case ARGV[0]
5
10
  when "build"
6
11
  ENV["RACK_ENV"] = "production"
12
+ require "inert"
7
13
  Inert.build
8
14
  else
9
15
  ENV["RACK_ENV"] = "development"
16
+ require "inert"
10
17
  Inert.start
11
18
  end
data/lib/inert.rb CHANGED
@@ -1,16 +1,17 @@
1
1
  # frozen_string_literal: true
2
- require_relative "inert/page"
3
- require_relative "inert/builder"
4
- require_relative "inert/middleware"
2
+ require_relative "inert/config"
5
3
 
6
4
  module Inert
7
5
  module_function
8
6
 
9
7
  def start
10
- app = Inert::Middleware
8
+ app = Rack::Builder.app do
9
+ if ENV["RACK_ENV"] != "production"
10
+ use Rack::ShowExceptions
11
+ use Rack::CommonLogger
12
+ end
11
13
 
12
- if ENV["RACK_ENV"] != "production"
13
- app = Rack::ShowExceptions.new(app)
14
+ run Inert::Middleware
14
15
  end
15
16
 
16
17
  Rack::Server.start(app: app)
@@ -19,4 +20,12 @@ module Inert
19
20
  def build
20
21
  Inert::Builder.new(Inert::Middleware).call("/")
21
22
  end
23
+
24
+ def view_path
25
+ File.expand_path(Inert.config.views, Dir.pwd)
26
+ end
22
27
  end
28
+
29
+ require_relative "inert/page"
30
+ require_relative "inert/builder"
31
+ require_relative "inert/middleware"
data/lib/inert/builder.rb CHANGED
@@ -2,6 +2,7 @@
2
2
  require "fileutils"
3
3
  require "set"
4
4
  require "oga"
5
+ require "uri"
5
6
 
6
7
  module Inert
7
8
  class Builder
@@ -30,7 +31,7 @@ module Inert
30
31
  warn "Saving #{url}"
31
32
  request = Rack::MockRequest.new(app)
32
33
 
33
- dest = url.dup
34
+ dest = URI(url.dup).path
34
35
  dest << "index.html" if dest.end_with?("/")
35
36
  dest << ".html" unless dest =~ /\.\w+$/
36
37
  dest = File.expand_path(dest[1..-1], build_path)
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+ module Inert
3
+ # Configure your Inert build by creating an Inertfile in the project root,
4
+ # and call Inert.config with a block which receives the inert config instance.
5
+ #
6
+ # This config instance allows you to access the Roda application underneath,
7
+ # configuring plugins, helpers, and adding routes that run prior to the catch-all.
8
+ #
9
+ # Inert.config do |inert|
10
+ # inert.helpers do
11
+ # def generator
12
+ # "Inert v#{Inert::VERSION}"
13
+ # end
14
+ # end
15
+ #
16
+ # inert.app do
17
+ # plugin :h
18
+ # end
19
+ #
20
+ # inert.routes do |r|
21
+ # r.on "employees.html" do
22
+ # @employees = [] # Read in actual data here
23
+ # view("employees.html.erb")
24
+ # end
25
+ # end
26
+ # end
27
+ class Config
28
+ # Folder containing your pages or view files.
29
+ attr_accessor :views
30
+
31
+ def initialize
32
+ @app = proc{}
33
+ @helpers = proc{}
34
+ @routes = proc{}
35
+ @views = "views"
36
+ end
37
+
38
+ # Helpers to make available in your views.
39
+ # Pass a module to be included, or a block to be evaluates.
40
+ def helpers(value=nil, &block)
41
+ @helpers = ->{ include value } if value
42
+ @helpers = block if block_given?
43
+ @helpers
44
+ end
45
+
46
+ # Configuration changes on the underlying Roda app.
47
+ # Pass a block which will be class evaled.
48
+ def app(&block)
49
+ return @app unless block_given?
50
+ @app = block
51
+ end
52
+
53
+ # Extra routes that run before the catch-all route (but after the assets route).
54
+ # Pass a block which will be instance evaluated.
55
+ def routes(&block)
56
+ return @routes unless block_given?
57
+ @routes = block
58
+ end
59
+ end
60
+
61
+ module_function
62
+ def config
63
+ @config ||= Config.new
64
+ yield @config if block_given?
65
+ @config
66
+ end
67
+ end
@@ -3,31 +3,41 @@ require "roda"
3
3
 
4
4
  module Inert
5
5
  class Middleware < Roda
6
- plugin :render, cache: ENV["RACK_ENV"] == "production"
6
+ plugin :render,
7
+ cache: ENV["RACK_ENV"] == "production",
8
+ views: Inert.config.views
9
+
7
10
  plugin :partials
8
11
  plugin :public, root: "static"
12
+ plugin :content_for
13
+
14
+ class_exec &Inert.config.app
9
15
 
10
16
  attr_reader :page
11
17
 
12
18
  route do |r|
13
19
  r.public
14
20
 
21
+ Inert.config.routes.call(r)
22
+
15
23
  view_file = r.remaining_path.dup
16
24
  view_file << "index.html" if view_file.end_with?("/")
17
25
 
18
26
  begin
19
- page = Page.load_from_file("./views#{view_file}.erb")
27
+ page = Page.load_from_file(File.join(Inert.view_path, view_file))
20
28
  rescue Errno::ENOENT
21
29
  r.halt([404, {}, ["Not found"]])
22
30
  end
23
31
 
24
32
  @page = page.frontmatter
25
33
 
26
- view(inline: page.body, layout: page.layout)
34
+ view(inline: page.body, layout: page.layout, template_class: Tilt[page.filename])
27
35
  end
28
36
 
37
+ class_exec &Inert.config.helpers
38
+
29
39
  def inline(file)
30
- File.read("./views/#{file}")
40
+ File.read(File.join(Inert.view_path, file))
31
41
  end
32
42
  end
33
43
  end
data/lib/inert/page.rb CHANGED
@@ -4,17 +4,22 @@ require "ostruct"
4
4
 
5
5
  module Inert
6
6
  class Page
7
- attr_reader :body, :frontmatter, :layout
7
+ attr_reader :body, :frontmatter, :layout, :filename
8
8
 
9
- def initialize(body: "", frontmatter: {})
9
+ def initialize(body: "", frontmatter: {}, filename: nil)
10
10
  @body = body
11
11
  @frontmatter = frontmatter
12
- @layout = @frontmatter.delete(:layout) || "layout"
12
+ @layout = @frontmatter.delete("layout") || "layout"
13
+ @filename = filename
13
14
 
14
15
  @frontmatter = OpenStruct.new(@frontmatter)
15
16
  end
16
17
 
17
18
  def self.load_from_file(filename)
19
+ filename = find_file(filename)
20
+
21
+ raise Errno::ENOENT if filename.nil?
22
+
18
23
  body = File.read(filename)
19
24
  frontmatter = {}
20
25
 
@@ -23,7 +28,11 @@ module Inert
23
28
  ""
24
29
  end.lstrip
25
30
 
26
- new(body: body, frontmatter: frontmatter)
31
+ new(body: body, frontmatter: frontmatter, filename: filename)
32
+ end
33
+
34
+ def self.find_file(filename)
35
+ Dir[filename + ".*"].first
27
36
  end
28
37
  end
29
38
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inert
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Daniels
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-08 00:00:00.000000000 Z
11
+ date: 2017-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: roda
@@ -81,6 +81,7 @@ files:
81
81
  - exe/inert
82
82
  - lib/inert.rb
83
83
  - lib/inert/builder.rb
84
+ - lib/inert/config.rb
84
85
  - lib/inert/middleware.rb
85
86
  - lib/inert/page.rb
86
87
  homepage: https://github.com/adam12/inert