lifer 0.10.2 → 0.12.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
  SHA256:
3
- metadata.gz: '0628d28de38f509eb51cc2d2dd65cb794fe3e4e46239bbf11c82decfd28f1d5d'
4
- data.tar.gz: dcb12aba6fa318f57c3224fed97a6a28233b847ca7399f321c855a739a44543b
3
+ metadata.gz: 5f018de3a46e9674b5d0da11bb7d0dd5e111c7f8119d4c901fa2fb5d2458a9ab
4
+ data.tar.gz: a17f6d9a91115594caa0f786f4bbecb01b4f3afc2925864ed62cf8ead11de457
5
5
  SHA512:
6
- metadata.gz: 0d74bf3c194492fec78a17ca70045dd107a40ddf33cf154243b33bb0a9d13876b1b0bdc4ea6bbf40c15984bf6a2a86dc898ed55939c9e222cf86d4cadd73b8d3
7
- data.tar.gz: af6d75bd7b875807c8fc74c69b04c5849d0d5e787a4ae6516f8af5189eb22e668d249660f741802b90209d9c2697909ae69400f364fc80a7dfe703a69490bb78
6
+ metadata.gz: 5d2f01b4f70b947cba613f7bf23be66d777f23d9188d055a453586b1bca8ef46ef818faccb108d909ce2d809c9a4e15a162c0822f6a6e0d340682bbd525e436e
7
+ data.tar.gz: d3cfc16e8436ff73cfc0c01deb2e5f78574f202b8160b2dca64288980f1ea0c75699176150c87acaf24010cdedaddfa7f98871dadacad5fa2f16293ecdfe3e7c
data/CHANGELOG.md CHANGED
@@ -1,5 +1,32 @@
1
1
  ## Next
2
2
 
3
+ ## v0.12.0
4
+
5
+ This release adds a new `#render` method that can be used in any ERB files being
6
+ built into HTML documents. It matches the functionality of Liquid's builtiin
7
+ `render` functionality. So:
8
+
9
+ {% render "my_partial.html.liquid" with local_variable: "some-value" %}
10
+
11
+ is equivalent to:
12
+
13
+ <%= render "my_partial.html.erb", local_variable: "some-value" %>
14
+
15
+ ## v0.11.0
16
+
17
+ This release adds new functionality to our URI strategies, adding
18
+ `URIStrategy#permalink`, to make support for pretty URI strategies (i.e. where
19
+ entries all end in `/index.ext`) better and more compatible with web servers
20
+ where `foo` is the "same thing" as `foo/index.html`.
21
+
22
+ Example output filename:
23
+
24
+ /my/filename/index.html
25
+
26
+ Expected permalink for this file using a pretty URI strategy:
27
+
28
+ /my/filename
29
+
3
30
  ## v0.10.2
4
31
 
5
32
  This release resolves another bug in `Entry#summary` causing entries without
@@ -21,7 +48,6 @@ trash.
21
48
  The summary is meant to be ideal for things like `<meta>` description tags,
22
49
  which should only contain plain text.
23
50
 
24
-
25
51
  ## v0.10.0
26
52
 
27
53
  This release lets all layout files (either Liquid or ERB files) provide a
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lifer (0.10.2)
4
+ lifer (0.12.0)
5
5
  i18n (< 2)
6
6
  kramdown (~> 2.4)
7
7
  liquid (~> 5.6, < 6)
@@ -16,7 +16,7 @@ GEM
16
16
  specs:
17
17
  addressable (2.8.7)
18
18
  public_suffix (>= 2.0.2, < 7.0)
19
- bigdecimal (3.1.9)
19
+ bigdecimal (3.2.2)
20
20
  bump (0.10.0)
21
21
  capybara (3.40.0)
22
22
  addressable
@@ -44,7 +44,7 @@ GEM
44
44
  kramdown (2.5.1)
45
45
  rexml (>= 3.3.9)
46
46
  language_server-protocol (3.17.0.3)
47
- liquid (5.8.6)
47
+ liquid (5.8.7)
48
48
  bigdecimal
49
49
  strscan (>= 3.1.1)
50
50
  listen (3.9.0)
@@ -10,7 +10,7 @@ class Lifer::Builder
10
10
  # @param entry [Lifer::Entry] The entry to be rendered.
11
11
  # @return [String] The rendered entry.
12
12
  def build(entry:)
13
- new(entry: entry).render
13
+ new(entry: entry).build
14
14
  end
15
15
  end
16
16
 
@@ -11,6 +11,8 @@ class Lifer::Builder::HTML
11
11
  # </head>
12
12
  #
13
13
  # <body>
14
+ # <%= partial "_layouts/header.html.erb" %>
15
+ #
14
16
  # <h1><%= my_collection.name %></h1>
15
17
  #
16
18
  # <% my_collection.entries.each do |entry| %>
@@ -20,22 +22,31 @@ class Lifer::Builder::HTML
20
22
  # <a href="<%= entry.permalink %>">Read more</a>
21
23
  # </section>
22
24
  # <% end %>
25
+ #
26
+ # <%= partial "_layouts/footer.html.erb" %>
23
27
  # </body>
24
28
  # </html>
25
29
  #
26
30
  class FromERB < FromAny
27
31
  # Reads the entry as ERB, given our renderer context (see the documentation
28
- # for `#build_binding_context`) and renders the production-ready entry.
32
+ # for `#build_binding_context`) and builds the production-ready entry.
29
33
  #
30
- # @return [String] The rendered entry.
31
- def render
34
+ # @return [String] The resulting HTML entry.
35
+ def build
32
36
  document = ERB.new(layout_file_contents).result context
33
37
 
34
38
  return document unless (relative_layout_path = frontmatter[:layout])
35
39
 
36
40
  document_binding = binding.tap { |binding|
41
+ context.local_variables.each do |variable|
42
+ next if variable == :content
43
+
44
+ binding.local_variable_set variable,
45
+ context.local_variable_get(variable)
46
+ end
37
47
  binding.local_variable_set :content, document
38
48
  }
49
+
39
50
  layout_path = "%s/%s" % [Lifer.root, relative_layout_path]
40
51
  ERB.new(File.read layout_path).result(document_binding)
41
52
  end
@@ -95,6 +106,11 @@ class Lifer::Builder::HTML
95
106
  binding.local_variable_set :tags, tags
96
107
  binding.local_variable_set :content,
97
108
  ERB.new(entry.to_html).result(binding)
109
+
110
+ define_singleton_method :render,
111
+ -> (relative_path_to_template, locals = {}) {
112
+ partial_render_method relative_path_to_template, locals
113
+ }
98
114
  }
99
115
  end
100
116
 
@@ -105,5 +121,38 @@ class Lifer::Builder::HTML
105
121
  def tag_context_class
106
122
  @tag_context_class ||= Class.new(Array) do end
107
123
  end
124
+
125
+ # @private
126
+ # If the end user should want to render a partial from an entry or a layout
127
+ # file, this method provides the functionality for the `#partial` method
128
+ # provided to the ERB template context, complete with all of the information
129
+ # one might want want about the entry, project collections, and so on, that's
130
+ # available from entry and layout templates.
131
+ #
132
+ # @example Usage
133
+ # <%= partial "_layouts/my_partial.html.erb", id: "123" %>
134
+ # @param relative_path_to_template [String] The path, from the Lifer root,
135
+ # to the partial layout file.
136
+ # @param locals [Hash] Additional data that should be passed along for
137
+ # rendering the partial.
138
+ # @return [String] The rendered partial document.
139
+ def partial_render_method(relative_path_to_template, locals)
140
+ template_path = File.join(Lifer.root, relative_path_to_template)
141
+
142
+ partial_binding = binding.tap { |binding|
143
+ context.local_variables.each do |variable|
144
+ next if variable == :content
145
+
146
+ binding.local_variable_set variable,
147
+ context.local_variable_get(variable)
148
+ end
149
+
150
+ locals.each do |key, value|
151
+ binding.local_variable_set key.to_sym, value
152
+ end
153
+ }
154
+
155
+ ERB.new(File.read template_path).result(partial_binding)
156
+ end
108
157
  end
109
158
  end
@@ -28,11 +28,11 @@ class Lifer::Builder::HTML
28
28
  require_relative "from_liquid/filters"
29
29
  require_relative "from_liquid/liquid_env"
30
30
 
31
- # Reads the entry as Liquid, given our document context, and renders
32
- # an entry.
31
+ # Reads the Liquid entry, given our document context, and builds an HTML
32
+ # document.
33
33
  #
34
- # @return [String] The rendered entry.
35
- def render
34
+ # @return [String] The resulting HTML entry.
35
+ def build
36
36
  document_context = context.merge!(
37
37
  "content" => Liquid::Template
38
38
  .parse(entry.to_html, **parse_options)
@@ -7,9 +7,22 @@ require "fileutils"
7
7
  # ERB[1] or Liquid[2] template files. The layout file yields entry contents
8
8
  # via a `content` call that is parsed by ERB or Liquid.
9
9
  #
10
- # Layout files can also include other contextual information about the current
11
- # Lifer project to provide "normal website features" like navigation links,
12
- # indexes, and so on. Context is provided via:
10
+ # It is normal to have templates that render other templates. So normal, in
11
+ # fact, that Liquid provides a built-in `render` function that allows us to render
12
+ # partials from other Liquid files:
13
+ #
14
+ # {% render "path/to/my/partial.html.liquid" with local_variable: "value" %}
15
+ #
16
+ # And we've ensured a similar `render` function exists for our ERB HTML builder:
17
+ #
18
+ # <%= render "path/to/my/partial.html.erb", local_variable: "value" %>
19
+ #
20
+ # The entry being built will include contextual data about the current project,
21
+ # so that the user can build out normal website features like navigation links,
22
+ # indexes, and so on. Note that with the current Liquid implementation, you may
23
+ # need to explicitly pass context from layouts to the partials that require it via
24
+ # `with`. Meanwhile, the ERB implementation provides a bunch of context globally.
25
+ # Context includes:
13
26
  #
14
27
  # - `my_collection_name`: Or, any collection by name.
15
28
  #
data/lib/lifer/entry.rb CHANGED
@@ -194,9 +194,10 @@ module Lifer
194
194
  cached_permalink_variable,
195
195
  File.join(
196
196
  host,
197
- Lifer::URIStrategy.find(collection.setting :uri_strategy)
197
+ Lifer::URIStrategy
198
+ .find(collection.setting :uri_strategy)
198
199
  .new(root: Lifer.root)
199
- .output_file(self)
200
+ .permalink(self)
200
201
  )
201
202
  )
202
203
  end
@@ -14,8 +14,15 @@ class Lifer::URIStrategy::Pretty < Lifer::URIStrategy
14
14
  basename = File.basename entry.file,
15
15
  Lifer::Utilities.file_extension(entry.file)
16
16
 
17
- Pathname entry.file.to_s
17
+ entry.file.to_s
18
18
  .gsub(/#{root}[\/]{0,1}/, "")
19
- .gsub(/#{basename}(\..+)/, "#{basename}/index.#{file_extension(entry)}")
19
+ .gsub(/#{basename}(\..+)/, "#{basename}#{pretty_part entry}")
20
20
  end
21
+
22
+ # @see Lifer::UriStrategy#permalink
23
+ def permalink(entry) = output_file(entry).gsub pretty_part(entry), ""
24
+
25
+ private
26
+
27
+ def pretty_part(entry) = "/index.#{file_extension(entry)}"
21
28
  end
@@ -11,14 +11,28 @@ class Lifer::URIStrategy
11
11
 
12
12
  # @see Lifer::URIStrategy#output_file
13
13
  def output_file(entry)
14
- basename = File.basename entry.file,
15
- Lifer::Utilities.file_extension(entry.file)
14
+ if basename(entry) == "index"
15
+ "index.html"
16
+ else
17
+ "#{basename entry}#{pretty_part entry}"
18
+ end
19
+ end
16
20
 
17
- if basename == "index"
18
- Pathname "index.html"
21
+ # @see Lifer::UriStrategy#permalink
22
+ def permalink(entry)
23
+ if basename(entry) == "index"
24
+ "/"
19
25
  else
20
- Pathname "#{basename}/index.#{file_extension(entry)}"
26
+ output_file(entry).gsub pretty_part(entry), ""
21
27
  end
22
28
  end
29
+
30
+ private
31
+
32
+ def basename(entry)
33
+ File.basename entry.file, Lifer::Utilities.file_extension(entry.file)
34
+ end
35
+
36
+ def pretty_part(entry) = "/index.#{file_extension(entry)}"
23
37
  end
24
38
  end
@@ -24,9 +24,16 @@
24
24
  basename = File.basename entry.file,
25
25
  Lifer::Utilities.file_extension(entry.file)
26
26
 
27
- Pathname entry.file.to_s
27
+ entry.file.to_s
28
28
  .gsub(/#{root}[\/]{0,1}/, "")
29
- .gsub(/#{basename}(\..+)/, "#{basename}/index.#{file_extension(entry)}")
29
+ .gsub(/#{basename}(\..+)/, "#{basename}#{pretty_part entry}")
30
30
  .gsub(DATE_REGEXP, "")
31
31
  end
32
+
33
+ # @see Lifer::UriStrategy#permalink
34
+ def permalink(entry) = output_file(entry).gsub pretty_part(entry), ""
35
+
36
+ private
37
+
38
+ def pretty_part(entry) = "/index.#{file_extension(entry)}"
32
39
  end
@@ -11,7 +11,10 @@ class Lifer::URIStrategy
11
11
  basename = File.basename entry.file,
12
12
  Lifer::Utilities.file_extension(entry.file)
13
13
 
14
- Pathname "#{basename}.#{file_extension(entry)}"
14
+ "#{basename}.#{file_extension(entry)}"
15
15
  end
16
+
17
+ # @see Lifer::UriStrategy#permalink
18
+ def permalink(entry) = output_file(entry)
16
19
  end
17
20
  end
@@ -10,8 +10,11 @@ class Lifer::URIStrategy::Simple < Lifer::URIStrategy
10
10
  basename = File.basename entry.file,
11
11
  Lifer::Utilities.file_extension(entry.file)
12
12
 
13
- Pathname entry.file.to_s
13
+ entry.file.to_s
14
14
  .gsub(/#{root}[\/]{0,1}/, "")
15
15
  .gsub(/#{basename}(\..+)/, "#{basename}.#{file_extension(entry)}")
16
16
  end
17
+
18
+ # @see Lifer::UriStrategy#permalink
19
+ def permalink(entry) = output_file(entry)
17
20
  end
@@ -39,6 +39,24 @@ class Lifer::URIStrategy
39
39
  raise NotImplementedError, I18n.t("shared.not_implemented_method")
40
40
  end
41
41
 
42
+ # This method should sometimes return the path to the file in the format
43
+ # specified by the current URI strategy. Of course, this depends on what the URI
44
+ # stategy is. For "pretty" strategies, the permalink may differ from the output
45
+ # filename. For example, the output file may point to
46
+ #
47
+ # entry-name/index.html
48
+ #
49
+ # While the permalink like points to:
50
+ #
51
+ # entry-name
52
+ #
53
+ # @raise [NotImplementedError] This method must be implemented on each
54
+ # subclass.
55
+ # @return [String] The permalink to the built output file.
56
+ def permalink(entry)
57
+ raise NotImplementedError, I18n.t("shared.not_implemented_error")
58
+ end
59
+
42
60
  private
43
61
 
44
62
  def file_extension(entry) = entry.class.output_extension
data/lib/lifer/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Lifer
2
- VERSION = "0.10.2"
2
+ VERSION = "0.12.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lifer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.2
4
+ version: 0.12.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - benjamin wil
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-05-10 00:00:00.000000000 Z
11
+ date: 2025-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: i18n
@@ -210,8 +210,8 @@ licenses:
210
210
  - MIT
211
211
  metadata:
212
212
  allowed_push_host: https://rubygems.org
213
- homepage_uri: https://github.com/benjaminwil/lifer/blob/v0.10.2/README.md
214
- source_code_uri: https://github.com/benjaminwil/lifer/tree/v0.10.2
213
+ homepage_uri: https://github.com/benjaminwil/lifer/blob/v0.12.0/README.md
214
+ source_code_uri: https://github.com/benjaminwil/lifer/tree/v0.12.0
215
215
  changelog_uri: https://github.com/benjaminwil/lifer/blob/main/CHANGELOG.md
216
216
  post_install_message:
217
217
  rdoc_options: []