sitepress-server 0.1.20 → 0.1.21

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d91517d56e59f48bf4c8951db24fc55055a71a71
4
- data.tar.gz: 40f13195eea362e060027892e43cc7c77f6d35a2
3
+ metadata.gz: 1ea1399d159fbe7092f1437667333bd5c4c8ceb2
4
+ data.tar.gz: e5d09019b47a63bd1a3259f07450983432374872
5
5
  SHA512:
6
- metadata.gz: b036b8dd7ddf03ec25c15a76964bba410e5a83e8ed404b01814b4d2f2f70b4d815b6a257b441d6f8ab61329ecd06b950140c86a11515e12c665894afd49bf3c3
7
- data.tar.gz: 6fd62160834758c17291ab0b7bf41a99843ee2be8fee1088e165353f184e6acf2b701d8f145fd909a01f47f5cd784f5763e4d6ed40efa9be797d9049f79c0a8c
6
+ metadata.gz: 3d742e3d1a6e299ba9ed30836223307a97a897986e04a831e2a5104dbdf86a490d73c36584ebf41a84a53f725e6920de68c60942e936ca995716f4094b252b7c
7
+ data.tar.gz: 371cbf55936e26d057062e05d395f1ed115db2b2d3ffe360df6a11336a87cb02d49f549c6602b0ef5caae1632ca95bc6dd7e3b751f44a4cc7792b0d96101785b
@@ -0,0 +1,37 @@
1
+ module Sitepress
2
+ # Loads modules into an isolated namespace that will be
3
+ # used for the rendering context. This loader is designed to
4
+ # be immutable so that it throws away the constants and modules
5
+ # on each load.
6
+ #
7
+ # TODO: Rename this to a RenderingContext, or something.
8
+ class HelperLoader
9
+ def initialize(paths:)
10
+ @paths = Array(paths)
11
+ end
12
+
13
+ def context(locals: {})
14
+ modules = helpers
15
+ Object.new.tap do |object|
16
+ # Locals of rendering context that are accessible from the
17
+ # helper modules.
18
+ locals.each do |name, value|
19
+ object.define_singleton_method(name) { value }
20
+ end
21
+ # Include the helper modules of the rendering context.
22
+ modules.constants.each do |module_name|
23
+ object.send(:extend, modules.const_get(module_name))
24
+ end
25
+ end
26
+ end
27
+
28
+ private
29
+ def helpers
30
+ Module.new.tap do |m|
31
+ @paths.each do |path|
32
+ m.module_eval File.read(path)
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,31 @@
1
+ module Sitepress
2
+ # Run a Sitepress site as a rack app.
3
+ class Server
4
+ def initialize(site: )
5
+ @site = site
6
+ # TODO: This is in the wrong place. Needs to be configurable by
7
+ # Sitepress::Site.
8
+ @helper_paths = Dir.glob(@site.root_path.join("helpers/**.rb"))
9
+ end
10
+
11
+ def call(env)
12
+ req = Rack::Request.new(env)
13
+ resource = @site.get req.path
14
+
15
+ if resource
16
+ # TODO: Lets slim this down a bit.
17
+ helpers = HelperLoader.new paths: @helper_paths
18
+ context = helpers.context locals: {
19
+ current_page: resource, site: @site }
20
+ renderer = ResourceRenderer.new resource: resource
21
+
22
+ mime_type = resource.mime_type.to_s
23
+ body = renderer.render context: context
24
+
25
+ [ 200, {"Content-Type" => mime_type}, Array(body) ]
26
+ else
27
+ [ 404, {"Content-Type" => "text/plain"}, ["Not Found"]]
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,52 +1,66 @@
1
- require "sitepress"
1
+ require "sitepress-core"
2
2
  require "tilt"
3
- require "pathname"
4
3
 
5
4
  module Sitepress
6
- class TiltResourceRenderer
7
- def initialize(resource)
8
- @resource = resource
5
+ # TODO: These renderers are confusing. They need to render
6
+ # a resource, be able to wrap a resource with another resource,
7
+ # and have rendering partial from within the context of the resource redender.
8
+ class AssetRenderer
9
+ def initialize(asset)
10
+ @asset = asset
9
11
  end
10
12
 
11
- def render(locals: {}, layout: "layout")
12
- template = engine.new { @resource.body }
13
- template.render(Object.new, **locals.merge(resource: @resource))
13
+ def render(locals: {}, layout: nil, context: , &block)
14
+ template = engine.new { @asset.body }
15
+ with_layout layout: layout, context: context do
16
+ template.render(context, **locals, &block)
17
+ end
14
18
  end
15
19
 
16
20
  private
21
+ def with_layout(layout: , **args, &block)
22
+ if layout
23
+ layout_renderer = AssetRenderer.new(layout)
24
+ layout_renderer.render **args, &block
25
+ else
26
+ block.call
27
+ end
28
+ end
29
+
17
30
  def engine
18
- Tilt[@resource.asset.path]
31
+ Tilt[@asset.path]
19
32
  end
20
33
  end
21
34
 
22
- # Mount inside of a config.ru file to run this as a server.
23
- class Server
24
- ROOT_PATH = Pathname.new("/")
35
+ # Renders a resource
36
+ class ResourceRenderer
37
+ def initialize(resource:)
38
+ @resource = resource
39
+ end
25
40
 
26
- def initialize(site: , relative_to: "/")
27
- @relative_to = Pathname.new(relative_to)
28
- @site = site
41
+ def render(context:)
42
+ if renderable_resource?
43
+ renderer.render layout: layout, context: context
44
+ else
45
+ @resource.body
46
+ end
29
47
  end
30
48
 
31
- def call(env)
32
- req = Rack::Request.new(env)
33
- resource = @site.get req.path
34
- # TODO: Memoize this per request and between requests eventually.
35
- resources = @site.root
49
+ private
50
+ # TODO: Add layout_path to Site#layout_path.
51
+ def layout
52
+ @resource.data.has_key?("layout") ? Asset.new(path: @resource.data["layout"]) : nil
53
+ end
36
54
 
37
- if resource
38
- body = if resource.asset.template_extensions.empty?
39
- # TODO: This is not efficient for huge files. Research how Rack::File
40
- # serves this up (or just take that, maybe mount it as a cascading middleware.)
41
- resource.body
42
- else
43
- TiltResourceRenderer.new(resource).render(locals: {resources: resources})
44
- end
55
+ def renderer
56
+ AssetRenderer.new(@resource.asset)
57
+ end
45
58
 
46
- [ 200, {"Content-Type" => resource.mime_type.to_s}, Array(body) ]
47
- else
48
- [ 404, {"Content-Type" => "text/plain"}, ["Not Found"]]
49
- end
59
+ def renderable_resource?
60
+ @resource.asset.template_extensions.any?
50
61
  end
51
62
  end
63
+
64
+ autoload :HelperLoader, "sitepress/helper_loader"
65
+ autoload :Server, "sitepress/server"
52
66
  end
@@ -21,6 +21,6 @@ Gem::Specification.new do |spec|
21
21
  spec.add_development_dependency "rack-test"
22
22
  spec.add_development_dependency "rack"
23
23
 
24
- spec.add_runtime_dependency "sitepress", spec.version
25
- spec.add_runtime_dependency "tilt"
24
+ spec.add_runtime_dependency "sitepress-core", spec.version
25
+ spec.add_runtime_dependency "tilt", "~> 2.0"
26
26
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sitepress-server
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.20
4
+ version: 0.1.21
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brad Gessler
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-10-12 00:00:00.000000000 Z
11
+ date: 2016-10-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: haml
@@ -53,33 +53,33 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: sitepress
56
+ name: sitepress-core
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
59
  - - '='
60
60
  - !ruby/object:Gem::Version
61
- version: 0.1.20
61
+ version: 0.1.21
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
66
  - - '='
67
67
  - !ruby/object:Gem::Version
68
- version: 0.1.20
68
+ version: 0.1.21
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: tilt
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ">="
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '0'
75
+ version: '2.0'
76
76
  type: :runtime
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ">="
80
+ - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '0'
82
+ version: '2.0'
83
83
  description:
84
84
  email:
85
85
  - bradgessler@gmail.com
@@ -88,6 +88,8 @@ extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
90
  - lib/sitepress-server.rb
91
+ - lib/sitepress/helper_loader.rb
92
+ - lib/sitepress/server.rb
91
93
  - sitepress-server.gemspec
92
94
  homepage: https://github.com/sitepress/sitepress
93
95
  licenses: []