kiln 0.1.6 → 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
  SHA256:
3
- metadata.gz: 2f32e8bbaa7ee4f8dd14fa25c2303cb0c030e49a899bb73db1b746bdcb177f41
4
- data.tar.gz: b4adb00dfe11eca35cbb4297e5d48626869e5fa57b0aa16f660eb302b7f7658a
3
+ metadata.gz: 16e0dc4e5feadd3bf854b8bfec84c9973a97a899995aa4f28894e416672d845e
4
+ data.tar.gz: b1931b450833b2ca947b67aac41d9254c1ec45fe9e90d2b7b3658d3818e9f5a9
5
5
  SHA512:
6
- metadata.gz: 2a5d738c7589388db0f1714d2ba840936412c39b5f05c7a16ca4bc270fa0f27ae02652ed3f00dc343a0bf2cf9cd75e335bdaa141ac8bbbd9d53dd9ffcf388924
7
- data.tar.gz: 90852ee81ad4e6a60ae7084cf62bc9cdd9beaf0664755adacc713abcf8b6da47031e6bd532603958084863416058f0a31c06a9b3ebe221f333f8a8c910b5c2b7
6
+ metadata.gz: fe15eafda9dd8baa902fc6060326328d620dd2c2bd12fbcba337e34ba6e98e10e0454e58ddb7b7151e93e175288e4c9ba17372ae846e9b7d8d0b02f603c55369
7
+ data.tar.gz: 96b38b16497f0d43ca07ddd23d3fcd207d6eee5de3d90e274e31cbf1f6d7e1fa98754f621a06ebd34c857adc25e8e3108003322058cbec92006f804e911eb3ed
data/CHANGELOG.md CHANGED
@@ -1,5 +1,24 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.2.0 (2026-11-15)
4
+
5
+ Breaking:
6
+
7
+ - The `paths` option is removed, and every page URL has no trailing slash. `/about` is written to
8
+ `about.html` instead of `about/index.html`, route helpers return `/about`, and the sitemap lists
9
+ `/about`. Kiln no longer sets `trailing_slash` in `default_url_options`. Relative links between
10
+ pages resolve against the new URLs: from `/posts`, `hello` is `/hello`.
11
+ - An HTML response always gets `.html`, also when the last segment has a dot: `/posts/v1.2` is
12
+ written to `posts/v1.2.html` instead of `posts/v1.2`.
13
+ - `Kiln::Writer.new` no longer takes `mode:`.
14
+
15
+ Added:
16
+
17
+ - A link to a URL the host redirects, such as `/about/` or `/about.html` for `about.html`, fails the
18
+ build as a redirected link.
19
+ - `kiln:serve` redirects `/about/` and `/about.html` to `/about`, and `/docs` to `/docs/` when only
20
+ `docs/index.html` exists, with a 307, as Cloudflare does by default.
21
+
3
22
  ## 0.1.6 (2026-11-14)
4
23
 
5
24
  Fixed:
data/lib/kiln/build.rb CHANGED
@@ -58,7 +58,7 @@ module Kiln
58
58
  end
59
59
 
60
60
  def writer
61
- @writer ||= Writer.new(@stage, mode: config.paths, report:)
61
+ @writer ||= Writer.new(@stage, report:)
62
62
  end
63
63
 
64
64
  def render_pages
@@ -4,8 +4,6 @@ module Kiln
4
4
  attr_accessor :output
5
5
  # Public base URL ("https://davafons.com"). Sets the request host and enables sitemap.xml.
6
6
  attr_accessor :url
7
- # :directory ("/now" -> now/index.html) or :file ("/now" -> now.html).
8
- attr_accessor :paths
9
7
  # Writes a CNAME file with this domain when set. true takes the host from `url`.
10
8
  attr_accessor :cname
11
9
  # Path rendered to 404.html (expected to respond with 404 or 200), nil to skip.
@@ -19,7 +17,6 @@ module Kiln
19
17
  def initialize
20
18
  @output = "dist"
21
19
  @url = nil
22
- @paths = :directory
23
20
  @cname = nil
24
21
  @not_found = nil
25
22
  @verify_skip = []
data/lib/kiln/railtie.rb CHANGED
@@ -15,15 +15,6 @@ module Kiln
15
15
  hosts << Kiln.config.host if hosts.any? && !hosts.include?(Kiln.config.host)
16
16
  end
17
17
 
18
- # In :directory mode "/now" is written as now/index.html, which static hosts serve at "/now/" and
19
- # reach from "/now" with a redirect. Route helpers add the trailing slash so links point at the
20
- # served URL. Paths with a dot (feed.xml) are left alone, and an app that sets trailing_slash in
21
- # default_url_options keeps its own value.
22
- config.after_initialize do |app|
23
- options = app.routes.default_url_options
24
- options[:trailing_slash] = true if Kiln.config.paths == :directory && !options.key?(:trailing_slash)
25
- end
26
-
27
18
  rake_tasks do
28
19
  Dir[File.expand_path("../tasks/kiln/*.rake", __dir__)].sort.each { load it }
29
20
  end
data/lib/kiln/server.rb CHANGED
@@ -1,7 +1,8 @@
1
1
  require "rack/mime"
2
2
 
3
3
  module Kiln
4
- # Rack app that serves the built output the way a static host would.
4
+ # Rack app that serves the built output the way Cloudflare does: _redirects rules, then files, then
5
+ # 307 redirects to the URL a page is served at, then 404.html.
5
6
  class Server
6
7
  def self.start(root, port:, host: "localhost")
7
8
  require "rackup"
@@ -21,7 +22,10 @@ module Kiln
21
22
  [ redirect.status, { "location" => redirect.to }, [] ]
22
23
  elsif (file = @resolver.resolve(path))
23
24
  serve(200, file)
24
- elsif (not_found = @resolver.resolve("/404.html"))
25
+ elsif (location = @resolver.redirect(path))
26
+ query = env["QUERY_STRING"].presence
27
+ [ 307, { "location" => query ? "#{location}?#{query}" : location }, [] ]
28
+ elsif (not_found = @resolver.resolve("/404"))
25
29
  serve(404, not_found)
26
30
  else
27
31
  [ 404, { "content-type" => "text/plain" }, [ "Not found\n" ] ]
data/lib/kiln/sitemap.rb CHANGED
@@ -25,7 +25,7 @@ module Kiln
25
25
  urls = @manifest.entries.filter_map do |path, entry|
26
26
  next if entry[:noindex] || entry[:content_type] != "text/html"
27
27
 
28
- " <url><loc>#{ERB::Util.html_escape(@url + served_path(path, entry[:file]))}</loc></url>\n"
28
+ " <url><loc>#{ERB::Util.html_escape(@url + path)}</loc></url>\n"
29
29
  end
30
30
 
31
31
  writer.write_file(FILENAME, <<~XML)
@@ -34,10 +34,5 @@ module Kiln
34
34
  #{urls.join}</urlset>
35
35
  XML
36
36
  end
37
-
38
- private
39
- def served_path(path, file)
40
- File.basename(file.to_s) == "index.html" ? "/#{file.to_s.delete_suffix("index.html")}" : path
41
- end
42
37
  end
43
38
  end
@@ -1,28 +1,65 @@
1
1
  module Kiln
2
- # Maps a URL path to a file the way Cloudflare Pages and GitHub Pages do: exact file, then x.html,
3
- # then x/index.html. A trailing slash only matches x/index.html. Never returns a file outside root.
2
+ # Maps a URL path to a file the way Cloudflare static assets do by default (auto-trailing-slash).
3
+ # "/x" serves the file x or x.html, and "/x/" serves x/index.html. The other spellings of a page
4
+ # redirect to the URL it is served at: "/x/" and "/x.html" to "/x", "/x/index.html" and "/x" to
5
+ # "/x/" when only x/index.html exists. Never returns a file outside root.
4
6
  class StaticResolver
5
7
  def initialize(root)
6
8
  @root = Pathname(root).expand_path
7
9
  end
8
10
 
11
+ # The file served at the path, or nil.
9
12
  def resolve(url_path)
10
- path = Rack::Utils.unescape_path(url_path.to_s.split(/[?#]/, 2).first.to_s)
11
- return if path.include?("\0")
13
+ segments, slash = parse(url_path)
14
+ return unless segments
12
15
 
13
- segments = path.split("/").reject { it.empty? || it == "." }
14
- return if segments.include?("..")
16
+ base = @root.join(*segments)
17
+
18
+ candidates =
19
+ if segments.empty? || slash then [ base.join("index.html") ]
20
+ elsif segments.last.end_with?(".html") then []
21
+ else [ base, Pathname("#{base}.html") ]
22
+ end
23
+ candidates.find { servable?(it) }
24
+ end
25
+
26
+ # The URL the path redirects to, or nil. Only meaningful when resolve returns nil.
27
+ def redirect(url_path)
28
+ segments, slash = parse(url_path)
29
+ return if segments.nil? || segments.empty?
15
30
 
16
31
  base = @root.join(*segments)
17
- candidates = path.end_with?("/") || segments.empty? ? [ base.join("index.html") ] : [ base, Pathname("#{base}.html"), base.join("index.html") ]
18
- candidates.find { it.file? && inside_root?(it) }
19
- rescue ArgumentError, Encoding::CompatibilityError
20
- nil
32
+ *parents, name = segments
33
+ target =
34
+ if slash
35
+ segments if servable?(Pathname("#{base}.html"))
36
+ elsif name == "index.html"
37
+ [ *parents, "" ] if servable?(base)
38
+ elsif name.end_with?(".html") && name != ".html"
39
+ [ *parents, name.delete_suffix(".html") ] if servable?(base)
40
+ else
41
+ [ *segments, "" ] if servable?(base.join("index.html"))
42
+ end
43
+ "/#{target.map { Rack::Utils.escape_path(it) }.join("/")}" if target
21
44
  end
22
45
 
23
46
  private
24
- def inside_root?(file)
25
- file.realpath.to_s.start_with?("#{@root.realpath}/")
47
+ # The decoded segments and whether the path ends in a slash, or nil for a path that can't name
48
+ # a file under root.
49
+ def parse(url_path)
50
+ path = Rack::Utils.unescape_path(url_path.to_s.split(/[?#]/, 2).first.to_s)
51
+ return if path.include?("\0")
52
+
53
+ segments = path.split("/").reject { it.empty? || it == "." }
54
+ [ segments, path.end_with?("/") ] unless segments.include?("..")
55
+ rescue ArgumentError, Encoding::CompatibilityError
56
+ nil
57
+ end
58
+
59
+ def servable?(file)
60
+ file.file? && file.realpath.to_s.start_with?("#{@root.realpath}/")
61
+ rescue ArgumentError, Encoding::CompatibilityError
62
+ false
26
63
  end
27
64
  end
28
65
  end
data/lib/kiln/verifier.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  module Kiln
2
- # Checks the built output: internal links, anchors and importmap modules.
2
+ # Checks the built output: internal links, anchors and importmap modules. A link to a URL the host
3
+ # answers with a redirect, such as "/about/" for about.html, is an error: links use the served URL.
3
4
  class Verifier
4
5
  def initialize(output, config:)
5
6
  @output = Pathname(output)
@@ -34,7 +35,11 @@ module Kiln
34
35
  target = @resolver.resolve(uri.path)
35
36
 
36
37
  if target.nil?
37
- error(:broken_link, url, page) unless @redirect_sources.include?(uri.path)
38
+ if (location = @resolver.redirect(uri.path))
39
+ error(:redirected_link, "#{url} (served at #{location})", page)
40
+ elsif !@redirect_sources.include?(uri.path)
41
+ error(:broken_link, url, page)
42
+ end
38
43
  elsif uri.fragment.present? && uri.fragment != "top" && target.extname == ".html"
39
44
  fragment = Rack::Utils.unescape_path(uri.fragment)
40
45
  error(:missing_anchor, url, page) unless anchors(target).include?(fragment)
@@ -71,7 +76,7 @@ module Kiln
71
76
  Nokogiri::HTML5(file.read)
72
77
  end
73
78
 
74
- # The URL a file is served at: index.html -> /, now/index.html -> /now/, components.html -> /components.
79
+ # The URL a file is served at: index.html -> /, docs/index.html -> /docs/, now.html -> /now.
75
80
  def page_path(file)
76
81
  path = "/#{file}"
77
82
  return path.delete_suffix("index.html") if File.basename(file) == "index.html"
data/lib/kiln/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Kiln
2
- VERSION = "0.1.6"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/kiln/writer.rb CHANGED
@@ -6,29 +6,26 @@ module Kiln
6
6
  class Writer
7
7
  attr_reader :output
8
8
 
9
- def initialize(output, mode:, report:)
10
- raise Kiln::Error, "Unknown paths mode #{mode.inspect} (expected :directory or :file)" unless %i[directory file].include?(mode)
11
-
9
+ def initialize(output, report:)
12
10
  @output = Pathname(output).expand_path
13
- @mode = mode
14
11
  @report = report
15
12
  end
16
13
 
17
- # Relative file for a request path: "/now" -> "now/index.html" (or "now.html" in :file mode).
14
+ # Relative file for a request path. "/" is index.html. An HTML response is written with .html
15
+ # unless its path already ends in .html: "/now" -> "now.html", "/posts/v1.2" -> "posts/v1.2.html".
16
+ # Other responses keep their extension, or get one from the MIME type: "/api/status" -> "api/status.json".
18
17
  def file_for(path, content_type)
19
18
  segments = segments_for(path)
20
19
  content_type = content_type.to_s.split(";").first.to_s.strip.downcase
21
20
  return "index.html" if segments.empty?
22
21
 
23
22
  name = segments.last
24
- if File.extname(name).empty?
25
- if content_type == "text/html"
26
- segments = @mode == :directory ? [ *segments, "index.html" ] : [ *segments[...-1], "#{name}.html" ]
27
- elsif (extension = extension_for(content_type))
28
- segments = [ *segments[...-1], "#{name}.#{extension}" ]
29
- end
23
+ if content_type == "text/html"
24
+ name = "#{name}.html" unless name.end_with?(".html")
25
+ elsif File.extname(name).empty? && (extension = extension_for(content_type))
26
+ name = "#{name}.#{extension}"
30
27
  end
31
- segments.join("/")
28
+ [ *segments[...-1], name ].join("/")
32
29
  end
33
30
 
34
31
  def write(path, content_type, body)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kiln
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.6
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Afonso