sitepress-server 0.1.23 → 0.1.24

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: b9c87dd590485def5851a81c610ee882c6e5233f
4
- data.tar.gz: 31cc3e65dc7295406f5118f65b52d9d6a9fe3018
3
+ metadata.gz: 5c84e7696249f2cd29204723fb03cec97a558e08
4
+ data.tar.gz: e62564accecf300020a29f8371c015ade27e391b
5
5
  SHA512:
6
- metadata.gz: 185986c8942b10c4061baa6b3cf9b02e34bb48fb0e0fe630b806f592ac191aee499e24ad99a80af3d035ebc4fe7c60be3ef91bdeb151a4efb9839efffc174291
7
- data.tar.gz: 37440265df7a5c835f8fcb4dbe4ef379a3ad8cf3842d4c9c67cf4f3d7cae7a988870bb8e3cb694e9fcf761653d6766ee5367ff3c851a4e5f73f7295d800b052c
6
+ metadata.gz: 18784fd05ed9658103543acbcd052a8ce36a9f59e4c311c53118fd4a639918af85481fabca29ae24ab13a69d747fb66dfc0f02e6a4736fcb33ae102c2b008e3a
7
+ data.tar.gz: 80679da18d651d2e172abd60a3421265d1d534da321a1240dae711cd3184a5e2cbd9994e0c12579bc1810b11d5e889069df261ba9155f6a1299308f73f386e2f
@@ -0,0 +1,46 @@
1
+ require "tilt"
2
+
3
+ module Sitepress
4
+ # Since we use Frontmatter, we have to do some parsing to
5
+ # get a Tilt template working properly.
6
+ class AssetTemplate
7
+ attr_reader :asset
8
+
9
+ def initialize(asset)
10
+ @asset = ceorce_asset(asset)
11
+ end
12
+
13
+ def render(*args, &block)
14
+ if renderable_resource?
15
+ template.render(*args, &block)
16
+ else
17
+ asset.body
18
+ end
19
+ end
20
+
21
+ def template
22
+ @_template ||= engine.new{ asset.body }
23
+ end
24
+
25
+ private
26
+ def renderable_resource?
27
+ asset.template_extensions.any?
28
+ end
29
+
30
+ def engine
31
+ @_engine ||= Tilt[asset.path]
32
+ end
33
+
34
+ # TODO: Replace this with a "fuzzy asset finder"
35
+ def ceorce_asset(assetish)
36
+ case assetish
37
+ when String
38
+ Asset.new(path: assetish)
39
+ when Asset
40
+ assetish
41
+ else
42
+ raise RuntimeError, "#{assetish.inspect} cannot be coerce into Sitepress::Asset"
43
+ end
44
+ end
45
+ end
46
+ end
@@ -10,24 +10,22 @@ module Sitepress
10
10
  @paths = Array(paths)
11
11
  end
12
12
 
13
- def context(locals: {})
13
+ def extend_instance(object, locals: {})
14
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
15
+ # Locals of rendering context that are accessible from the
16
+ # helper modules.
17
+ locals.each do |name, value|
18
+ object.define_singleton_method(name) { value }
19
+ end
20
+ # Include the helper modules of the rendering context.
21
+ modules.constants.each do |module_name|
22
+ object.send(:extend, modules.const_get(module_name))
25
23
  end
26
24
  end
27
25
 
28
26
  private
29
27
  def helpers
30
- Module.new.tap do |m|
28
+ @_helpers ||= Module.new.tap do |m|
31
29
  @paths.each do |path|
32
30
  m.module_eval File.read(path)
33
31
  end
@@ -0,0 +1,42 @@
1
+ module Sitepress
2
+ # TODO: We're starting to get too many rendering contexts ... and this
3
+ # won't quite fit in with the Tilt rendering context. We'll want to merge
4
+ # this and support `capture` so that we can get `wrap_layout` working.
5
+ class RenderingContext
6
+ attr_reader :resource, :site
7
+ alias :current_page :resource
8
+
9
+ def initialize(resource:, site:)
10
+ @resource = resource
11
+ @site = site
12
+ # TODO: Remove this from RenderingContext ... it should build
13
+ load_helpers
14
+ end
15
+
16
+ def render(layout: nil, locals: {}, &block)
17
+ layout ||= resource.data["layout"]
18
+ render_with_layout(layout) { renderer.render(self, **locals, &block) }
19
+ end
20
+
21
+ private
22
+ def render_with_layout(path, &block)
23
+ if path
24
+ template = AssetTemplate.new(path)
25
+ template.render(self, &block)
26
+ else
27
+ block.call
28
+ end
29
+ end
30
+
31
+ def renderer
32
+ @_renderer ||= AssetTemplate.new(resource.asset)
33
+ end
34
+
35
+ # TODO: This might be accessible from the rendering scope, which wouldn't be good.
36
+ # Figure out if this needs to be removed.
37
+ def load_helpers
38
+ paths = Dir.glob @site.helpers_path.join("**.rb")
39
+ HelperLoader.new(paths: paths).extend_instance(self)
40
+ end
41
+ end
42
+ end
@@ -3,9 +3,6 @@ module Sitepress
3
3
  class Server
4
4
  def initialize(site: )
5
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
6
  end
10
7
 
11
8
  def call(env)
@@ -13,14 +10,9 @@ module Sitepress
13
10
  resource = @site.get req.path
14
11
 
15
12
  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
13
  mime_type = resource.mime_type.to_s
23
- body = renderer.render context: context
14
+ context = RenderingContext.new(resource: resource, site: @site)
15
+ body = context.render
24
16
 
25
17
  [ 200, {"Content-Type" => mime_type}, Array(body) ]
26
18
  else
@@ -1,66 +1,8 @@
1
1
  require "sitepress-core"
2
- require "tilt"
3
2
 
4
3
  module Sitepress
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
11
- end
12
-
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
18
- end
19
-
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
-
30
- def engine
31
- Tilt[@asset.path]
32
- end
33
- end
34
-
35
- # Renders a resource
36
- class ResourceRenderer
37
- def initialize(resource:)
38
- @resource = resource
39
- end
40
-
41
- def render(context:)
42
- if renderable_resource?
43
- renderer.render layout: layout, context: context
44
- else
45
- @resource.body
46
- end
47
- end
48
-
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
54
-
55
- def renderer
56
- AssetRenderer.new(@resource.asset)
57
- end
58
-
59
- def renderable_resource?
60
- @resource.asset.template_extensions.any?
61
- end
62
- end
63
-
64
- autoload :HelperLoader, "sitepress/helper_loader"
65
- autoload :Server, "sitepress/server"
4
+ autoload :AssetTemplate, "sitepress/asset_template"
5
+ autoload :HelperLoader, "sitepress/helper_loader"
6
+ autoload :RenderingContext, "sitepress/rendering_context"
7
+ autoload :Server, "sitepress/server"
66
8
  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.23
4
+ version: 0.1.24
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-11-04 00:00:00.000000000 Z
11
+ date: 2016-11-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: haml
@@ -58,14 +58,14 @@ dependencies:
58
58
  requirements:
59
59
  - - '='
60
60
  - !ruby/object:Gem::Version
61
- version: 0.1.23
61
+ version: 0.1.24
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.23
68
+ version: 0.1.24
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: tilt
71
71
  requirement: !ruby/object:Gem::Requirement
@@ -88,7 +88,9 @@ extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
90
  - lib/sitepress-server.rb
91
+ - lib/sitepress/asset_template.rb
91
92
  - lib/sitepress/helper_loader.rb
93
+ - lib/sitepress/rendering_context.rb
92
94
  - lib/sitepress/server.rb
93
95
  - sitepress-server.gemspec
94
96
  homepage: https://github.com/sitepress/sitepress