sitepress-rails 3.2.2 → 4.0.0.beta1

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
  SHA256:
3
- metadata.gz: e9bc76c95c24124962b58d764b8560ff1d1fdddbe0d883d33594374b2b267c14
4
- data.tar.gz: 61bcad1d8c4c483f7ffe8cde092d486208c7b8e2e96f974208c8af0089acf5e0
3
+ metadata.gz: c12aa71c76b0e53a1ddf4ca1b6be9e0e097218bc76f801038b35d390bf509d3c
4
+ data.tar.gz: '02858bb77cd06f142c9a19cc9c8a072d11491b97175757ad0f8daf8859c230a8'
5
5
  SHA512:
6
- metadata.gz: 43bc1c26a2166171605414278eae64cb1cc4a2e7d455f96a0399470ecf70b72406fab6927b1ae492638ece8915460ef6869127886e6e518937bff2b7725a22bc
7
- data.tar.gz: 630721e4a95dff8f27eb774400edc308b5f5f0dabc28df6a97b99173f724d199946126650f5bc228800435065be233cd3d70e69a1eaa9d9d7f33e927a7c3021c
6
+ metadata.gz: a31246d3285899bea7595cde279624bc2e3894a211a274c491e834054a9d00ffbd53389a5fcd6e8c7d9c7539405df785289a4438cd7c5ca09a879c005f763185
7
+ data.tar.gz: a11e604a709e8dbea31410df7f6be34690bd61dc05aa22cee2c6d8dcc57008a75d1c0a02ce2f07a0483dc5b8b2ed3177f54cf27c90aca4f20cc19861488fb24b
@@ -2,67 +2,117 @@ require "pathname"
2
2
  require "fileutils"
3
3
 
4
4
  module Sitepress
5
- # Compile all resources from a Sitepress site into static pages.
6
- class Compiler
7
- include FileUtils
8
-
9
- attr_reader :site, :root_path
10
-
11
- # If a resource can't render, it will raise an exception and stop the compiler. Sometimes
12
- # its useful to turn off errors so you can get through a full compilation and see how many
13
- # errors you encounter along the way. To do that, you'd set `raise_exception_on_error` to
14
- # `false` and the compile will get through all the resources.
15
- attr_accessor :raise_exception_on_error
16
-
17
- def initialize(site:, root_path:, stdout: $stdout, raise_exception_on_error: true)
18
- @site = site
19
- @stdout = stdout
20
- @root_path = Pathname.new(root_path)
21
- @raise_exception_on_error = raise_exception_on_error
5
+ module Compiler
6
+ class Abstract
7
+ include Enumerable
8
+
9
+ attr_reader :site, :failed, :succeeded
10
+
11
+ # If a resource can't render, it will raise an exception and stop the compiler. Sometimes
12
+ # its useful to turn off errors so you can get through a full compilation and see how many
13
+ # errors you encounter along the way. To do that, you'd set `fail_on_error` to
14
+ # `false` and the compile will get through all the resources.
15
+ attr_accessor :fail_on_error
16
+
17
+ def initialize(site:, stdout: $stdout, fail_on_error: false)
18
+ @site = site
19
+ @stdout = stdout
20
+ @fail_on_error = fail_on_error
21
+ @failed = []
22
+ @succeeded = []
23
+ end
24
+
25
+ # Iterates through all pages and writes them to disk
26
+ def compile
27
+ before_compile
28
+ each do |resource, *args, **kwargs|
29
+ if resource.renderable?
30
+ render_resource(resource, *args, **kwargs)
31
+ else
32
+ copy_resource(resource, *args, **kwargs)
33
+ end
34
+ @succeeded << resource
35
+ rescue
36
+ status "Error building #{resource.inspect}"
37
+ @failed << resource
38
+ raise if fail_on_error
39
+ end
40
+ after_compile
41
+ end
42
+
43
+ def each(&block)
44
+ site.resources.each(&block)
45
+ end
46
+
47
+ protected
48
+ def copy_resource(resource, *args, **kwargs)
49
+ raise NotImplementedError
50
+ end
51
+
52
+ def render_resource(resource, *args, **kwargs)
53
+ raise NotImplementedError
54
+ end
55
+
56
+ def before_compile
57
+ status "Building #{site.root_path.expand_path}"
58
+ end
59
+
60
+ def after_compile
61
+ status "Built #{site.root_path.expand_path}"
62
+ end
63
+
64
+ def render(resource)
65
+ Renderers::Server.new(resource).render
66
+ end
67
+
68
+ def status(message)
69
+ @stdout.puts message
70
+ end
22
71
  end
23
72
 
24
- # Iterates through all pages and writes them to disk
25
- def compile
26
- status "Building #{site.root_path.expand_path} to #{root_path.expand_path}"
27
- resources.each do |resource, path|
28
- if resource.renderable?
73
+ # Compile all resources from a Sitepress site into static pages.
74
+ class Files < Abstract
75
+ include FileUtils
76
+
77
+ attr_reader :root_path
78
+
79
+ def initialize(*args, root_path:, **kwargs, &block)
80
+ super(*args, **kwargs, &block)
81
+ @root_path = Pathname.new(root_path)
82
+ end
83
+
84
+ protected
85
+ def render_resource(resource, path)
29
86
  status "Rendering #{path}"
30
87
  File.open(path.expand_path, "w"){ |f| f.write render resource }
31
- else
88
+ end
89
+
90
+ def copy_resource(resource, path)
32
91
  status "Copying #{path}"
33
92
  cp resource.asset.path, path.expand_path
34
93
  end
35
- rescue
36
- status "Error building #{resource.inspect}"
37
- raise if raise_exception_on_error
38
- end
39
- status "Successful build to #{root_path.expand_path}"
40
- end
41
94
 
42
- private
43
- def resources
44
- Enumerator.new do |y|
95
+ def before_compile
45
96
  mkdir_p root_path
97
+ status "Building #{site.root_path.expand_path} to #{root_path.expand_path}"
98
+ end
46
99
 
100
+ def after_compile
101
+ status "Build at #{root_path.expand_path}"
102
+ end
103
+
104
+ def each
47
105
  site.resources.each do |resource|
48
106
  path = build_path resource
49
107
  mkdir_p path.dirname
50
- y << [resource, path]
108
+ yield resource, path
51
109
  end
52
110
  end
53
- end
54
-
55
- def build_path(resource)
56
- path_builder = resource.node.root? ? BuildPaths::RootPath : BuildPaths::DirectoryIndexPath
57
- root_path.join path_builder.new(resource).path
58
- end
59
111
 
60
- def render(resource)
61
- Renderers::Server.new(resource).render
62
- end
63
-
64
- def status(message)
65
- @stdout.puts message
66
- end
112
+ def build_path(resource)
113
+ path_builder = resource.node.root? ? BuildPaths::RootPath : BuildPaths::DirectoryIndexPath
114
+ root_path.join path_builder.new(resource).path
115
+ end
116
+ end
67
117
  end
68
118
  end
@@ -3,9 +3,9 @@ module Sitepress
3
3
  # lets us keep the functions in the controller more functional, which makes them
4
4
  # easier to override by the end users.
5
5
  class Rendition
6
- attr_accessor :resource, :output, :controller_layout
6
+ attr_accessor :resource, :output, :layout
7
7
 
8
- def initialize(resource)
8
+ def initialize(resource, layout: nil)
9
9
  @resource = resource
10
10
  end
11
11
 
@@ -22,7 +22,7 @@ module Sitepress
22
22
  end
23
23
 
24
24
  def layout
25
- resource.data.fetch("layout", controller_layout)
25
+ resource.data.fetch("layout", @layout)
26
26
  end
27
27
  end
28
28
  end
@@ -17,7 +17,7 @@ module Sitepress
17
17
  included do
18
18
  rescue_from Sitepress::ResourceNotFound, with: :resource_not_found
19
19
  helper Sitepress::Engine.helpers
20
- helper_method :current_page, :site
20
+ helper_method :current_page, :site, :page_rendition
21
21
  before_action :append_relative_partial_path, only: :show
22
22
  around_action :ensure_site_reload, only: :show
23
23
  end
@@ -41,16 +41,25 @@ module Sitepress
41
41
  end
42
42
  end
43
43
 
44
+ # Renders the markup within a resource that can be rendered.
45
+ def page_rendition(resource, layout: nil)
46
+ Rendition.new(resource).tap do |rendition|
47
+ rendition.layout = layout
48
+ pre_render rendition
49
+ end
50
+ end
51
+
44
52
  # If a resource has a handler (e.g. erb, haml, etc.) we use the Rails renderer to
45
53
  # process templates, layouts, partials, etc. To keep the whole rendering process
46
54
  # contained in a way that the end user can override, we coupled the resource, source
47
55
  # and output within a `Rendition` object so that it may be processed via hooks.
48
56
  def render_resource_with_handler(resource)
49
- rendition = Rendition.new(resource)
50
- rendition.controller_layout = controller_layout
57
+ rendition = page_rendition(resource, layout: controller_layout)
51
58
 
52
- pre_render rendition
59
+ # Fire a callback in the controller in case anybody needs it.
53
60
  process_rendition rendition
61
+
62
+ # Now we finally render the output of the processed rendition to the client.
54
63
  post_render rendition
55
64
  end
56
65
 
@@ -162,13 +171,13 @@ module Sitepress
162
171
  # and the current page's node formats. If nothing intersects, HTML is returned
163
172
  # as a default.
164
173
  def current_resource_rails_formats
165
- extensions = current_resource.node.formats.extensions
166
- supported_extensions = extensions & Mime::EXTENSION_LOOKUP.keys
174
+ node_formats = current_resource.node.formats
175
+ supported_formats = node_formats & Mime::EXTENSION_LOOKUP.keys
167
176
 
168
- if supported_extensions.empty?
177
+ if supported_formats.empty?
169
178
  DEFAULT_PAGE_RAILS_FORMATS
170
179
  else
171
- supported_extensions.map?(&:to_sym)
180
+ supported_formats.map?(&:to_sym)
172
181
  end
173
182
  end
174
183