kiln 0.1.0 → 0.1.2

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.
@@ -1,8 +1,5 @@
1
- require "erb"
2
-
3
1
  module Kiln
4
- # Writes redirect responses as a _redirects file (Cloudflare Pages, Netlify) and/or meta refresh pages
5
- # (GitHub Pages).
2
+ # Writes redirect responses as a _redirects file (Cloudflare Pages, Netlify).
6
3
  class Redirects
7
4
  Entry = Data.define(:from, :to, :status)
8
5
 
@@ -20,52 +17,19 @@ module Kiln
20
17
  end
21
18
  end
22
19
 
23
- def initialize(entries, mode:, writer:, host:, url: nil)
20
+ def initialize(entries, host:)
24
21
  @entries = entries.map { it.with(to: relativize(it.to, host)) }
25
- @mode = mode
26
- @writer = writer
27
- @url = url
28
22
  end
29
23
 
24
+ # Rules from a copied public/_redirects come first, so hand-written rules win. Returns the filename.
30
25
  def write(output)
31
- files = []
32
- files << write_file(output) if %i[file both].include?(@mode)
33
- files.concat(write_pages) if %i[meta_refresh both].include?(@mode)
34
- files
26
+ file = Pathname(output).join(FILENAME)
27
+ existing = file.exist? ? file.read.chomp.then { it.empty? ? "" : "#{it}\n" } : ""
28
+ File.write(file, existing + @entries.map { "#{it.from} #{it.to} #{it.status}\n" }.join)
29
+ FILENAME
35
30
  end
36
31
 
37
32
  private
38
- # Rules from a copied public/_redirects come first, so hand-written rules win.
39
- def write_file(output)
40
- file = Pathname(output).join(FILENAME)
41
- existing = file.exist? ? file.read.chomp.then { it.empty? ? "" : "#{it}\n" } : ""
42
- File.write(file, existing + @entries.map { "#{it.from} #{it.to} #{it.status}\n" }.join)
43
- FILENAME
44
- end
45
-
46
- def write_pages
47
- @entries.map do |entry|
48
- @writer.write_file(@writer.file_for(entry.from, "text/html"), page(entry.to))
49
- end
50
- end
51
-
52
- def page(to)
53
- canonical = ERB::Util.html_escape(@url && to.start_with?("/") ? "#{@url.chomp("/")}#{to}" : to)
54
- to = ERB::Util.html_escape(to)
55
- <<~HTML
56
- <!DOCTYPE html>
57
- <html>
58
- <head>
59
- <meta charset="utf-8">
60
- <title>Redirecting</title>
61
- <meta http-equiv="refresh" content="0; url=#{to}">
62
- <link rel="canonical" href="#{canonical}">
63
- </head>
64
- <body><a href="#{to}">#{to}</a></body>
65
- </html>
66
- HTML
67
- end
68
-
69
33
  def relativize(location, host)
70
34
  uri = URI.parse(location)
71
35
  return location unless uri.is_a?(URI::HTTP) && uri.host&.casecmp?(host)
data/lib/kiln/report.rb CHANGED
@@ -3,7 +3,7 @@ module Kiln
3
3
  class Report
4
4
  Entry = Data.define(:kind, :message, :path)
5
5
 
6
- KINDS = %i[uncovered_route render asset broken_link missing_anchor importmap redirect write hook other].freeze
6
+ KINDS = %i[uncovered_route render editor_markup asset broken_link missing_anchor importmap redirect write other].freeze
7
7
 
8
8
  attr_reader :errors, :warnings, :written
9
9
 
data/lib/kiln/router.rb CHANGED
@@ -27,20 +27,6 @@ module Kiln
27
27
  @rows
28
28
  end
29
29
 
30
- # Text table for `kiln:routes`.
31
- def table
32
- lines = rows.map do |row|
33
- status = case row.status
34
- when :built then "built #{row.count}"
35
- when :skipped then "skipped: #{row.reason}"
36
- else "uncovered"
37
- end
38
- [ row.name || "-", row.path, status ]
39
- end
40
- widths = [ 0, 1 ].map { |i| lines.map { it[i].length }.max.to_i }
41
- lines.map { |name, path, status| "#{name.ljust(widths[0])} #{path.ljust(widths[1])} #{status}".rstrip }.join("\n")
42
- end
43
-
44
30
  private
45
31
  attr_reader :dsl, :report
46
32
 
data/lib/kiln/server.rb CHANGED
@@ -3,9 +3,9 @@ require "rack/mime"
3
3
  module Kiln
4
4
  # Rack app that serves the built output the way a static host would.
5
5
  class Server
6
- def self.start(root, port:)
6
+ def self.start(root, port:, host: "localhost")
7
7
  require "rackup"
8
- Rackup::Handler.default.run(new(root), Host: "localhost", Port: port)
8
+ Rackup::Handler.default.run(new(root), Host: host, Port: port)
9
9
  end
10
10
 
11
11
  def initialize(root)
@@ -10,43 +10,39 @@ module Kiln
10
10
  # class StaticSiteTest < ActiveSupport::TestCase
11
11
  # include Kiln::TestHelper
12
12
  #
13
- # test("builds the same output twice") { assert_repeatable_build }
13
+ # test("builds repeatably") { kiln_build(repeatable: true) { assert it.join("index.html").file? } }
14
14
  # test("home page") { assert_page "/", selector: "h1", text: "Home" }
15
15
  # end
16
16
  module TestHelper
17
17
  # Builds into a temp dir, fails with the report on any error, and yields the output path.
18
18
  # Returns the Build when no block is given (the temp dir is then left for the caller).
19
- def kiln_build(app: Rails.application, config: Kiln.config)
20
- dir = Dir.mktmpdir("kiln-build")
19
+ # With repeatable: true it builds a second time and fails with the diff when the two outputs
20
+ # differ, then yields the first output.
21
+ def kiln_build(app: Rails.application, config: Kiln.config, repeatable: false)
22
+ dir = Pathname(Dir.mktmpdir("kiln-build"))
21
23
  build = Build.new(app:, config:, output: dir)
22
24
  assert build.run.success?, build.report.to_s
23
- return build unless block_given?
24
-
25
- begin
26
- yield Pathname(dir)
27
- ensure
28
- FileUtils.rm_rf(dir)
29
- end
30
- end
31
-
32
- # Builds twice and asserts both outputs are byte-identical.
33
- def assert_repeatable_build(app: Rails.application, config: Kiln.config)
34
- kiln_build(app:, config:) do |first|
25
+ if repeatable
35
26
  kiln_build(app:, config:) do |second|
36
- diff = TreeDiff.call(first, second)
27
+ diff = TreeDiff.call(dir, second)
37
28
  assert diff.empty?, "Kiln output differs between two builds:\n#{diff}"
38
29
  end
39
30
  end
31
+ return build unless block_given?
32
+
33
+ yield dir
34
+ ensure
35
+ FileUtils.rm_rf(dir) if dir && block_given?
40
36
  end
41
37
 
42
- # Renders one path the way a build does (processors included) and asserts it succeeds.
38
+ # Renders one path the way a build does (CSRF tokens stripped) and asserts it succeeds.
43
39
  # Optional `selector` must match, and contain `text` when given. Returns the processed body.
44
40
  def assert_page(path, status: 200, selector: nil, text: nil, app: Rails.application, config: Kiln.config)
45
41
  response = Kiln.building! { Renderer.new(app, host: config.host).get(path) }
46
42
  assert_equal status, response.status, "Expected #{path} to respond with #{status}"
47
43
 
48
44
  body = response.body
49
- body = Processors.apply(body, Processors::Context.new(path:, config:, response:)) if response.html?
45
+ body = Processors::StripCSRF.call(body) if response.html?
50
46
 
51
47
  if selector
52
48
  nodes = Nokogiri::HTML5(body).css(selector)
data/lib/kiln/verifier.rb CHANGED
@@ -9,6 +9,7 @@ module Kiln
9
9
  @resolver = StaticResolver.new(@output)
10
10
  @redirect_sources = Redirects.load(@output.join(Redirects::FILENAME)).map(&:from).to_set
11
11
  @anchors = {}
12
+ @skip = config.verify_skip.join(", ").presence
12
13
  end
13
14
 
14
15
  def verify(report)
@@ -22,8 +23,11 @@ module Kiln
22
23
  def check_page(file)
23
24
  page = page_path(file)
24
25
  doc = parse(@output.join(file))
26
+ skipped = @skip ? doc.css(@skip).flat_map { [ it, *it.css("*") ] }.to_set : Set.new
25
27
 
26
28
  doc.xpath("//*[@href or @src or @srcset]").each do |element|
29
+ next if skipped.include?(element)
30
+
27
31
  urls_in(element).each { check_link(it, page) }
28
32
  end
29
33
  doc.css('script[type="importmap"]').each { check_importmap(it.text, page) }
data/lib/kiln/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Kiln
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
3
3
  end
@@ -16,18 +16,4 @@ namespace :kiln do
16
16
  puts format("Output: %s (%.1fs)", build.output, elapsed)
17
17
  exit 1 if report.failure?
18
18
  end
19
-
20
- desc "Fail if the committed output differs from a fresh build"
21
- task check: :environment do
22
- result = Kiln::Check.new.run
23
- if result.report.failure?
24
- puts result.report
25
- elsif result.diff.empty?
26
- puts "Kiln check: #{Kiln.config.output_path} is up to date."
27
- else
28
- puts "Kiln check: #{Kiln.config.output_path} is out of date. Run kiln:build and commit the result."
29
- puts result.diff
30
- end
31
- exit 1 unless result.success?
32
- end
33
19
  end
@@ -1,11 +1,12 @@
1
1
  namespace :kiln do
2
- desc "Serve the built output like a static host (PORT, default 4000)"
2
+ desc "Serve the built output like a static host (PORT, default 4000; BIND, default localhost)"
3
3
  task serve: :environment do
4
4
  output = Kiln.config.output_path
5
5
  abort "Kiln: #{output} does not exist. Run kiln:build first." unless output.directory?
6
6
 
7
7
  port = Integer(ENV.fetch("PORT", 4000))
8
- puts "Kiln: serving #{output} at http://localhost:#{port}"
9
- Kiln::Server.start(output, port:)
8
+ host = ENV.fetch("BIND", "localhost")
9
+ puts "Kiln: serving #{output} at http://#{host}:#{port}"
10
+ Kiln::Server.start(output, port:, host:)
10
11
  end
11
12
  end
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.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Afonso
@@ -81,7 +81,6 @@ files:
81
81
  - lib/kiln/assets.rb
82
82
  - lib/kiln/build.rb
83
83
  - lib/kiln/build_cache.rb
84
- - lib/kiln/check.rb
85
84
  - lib/kiln/configuration.rb
86
85
  - lib/kiln/content.rb
87
86
  - lib/kiln/content/collection.rb
@@ -92,9 +91,6 @@ files:
92
91
  - lib/kiln/manifest.rb
93
92
  - lib/kiln/page.rb
94
93
  - lib/kiln/pages.rb
95
- - lib/kiln/processors.rb
96
- - lib/kiln/processors/absolute_urls.rb
97
- - lib/kiln/processors/external_links.rb
98
94
  - lib/kiln/processors/strip_csrf.rb
99
95
  - lib/kiln/processors/strip_source_maps.rb
100
96
  - lib/kiln/railtie.rb
@@ -112,9 +108,7 @@ files:
112
108
  - lib/kiln/version.rb
113
109
  - lib/kiln/writer.rb
114
110
  - lib/tasks/kiln/build.rake
115
- - lib/tasks/kiln/routes.rake
116
111
  - lib/tasks/kiln/serve.rake
117
- - lib/tasks/kiln/verify.rake
118
112
  homepage: https://github.com/davafons/kiln
119
113
  licenses:
120
114
  - MIT
data/lib/kiln/check.rb DELETED
@@ -1,22 +0,0 @@
1
- require "tmpdir"
2
-
3
- module Kiln
4
- # Builds into a temp dir and compares the result with the committed output.
5
- class Check
6
- Result = Data.define(:report, :diff) do
7
- def success? = report.success? && diff.empty?
8
- end
9
-
10
- def initialize(app: Rails.application, config: Kiln.config)
11
- @app = app
12
- @config = config
13
- end
14
-
15
- def run
16
- Dir.mktmpdir("kiln-check") do |dir|
17
- report = Build.new(app: @app, config: @config, output: dir).run
18
- Result.new(report:, diff: TreeDiff.call(@config.output_path(@app.root), dir))
19
- end
20
- end
21
- end
22
- end
@@ -1,26 +0,0 @@
1
- module Kiln
2
- module Processors
3
- # Prefixes root-relative image URLs with config.url: img src/srcset and og:/twitter: image meta tags.
4
- # Feed readers and social previews need absolute URLs.
5
- module AbsoluteUrls
6
- IMG_TAG = /<img\b[^>]*>/
7
- IMAGE_META = /<meta\b(?=[^>]*\s(?:property|name)="(?:og|twitter):image[^"]*")[^>]*>/
8
- ROOT_RELATIVE = %r{\A/(?!/)}
9
-
10
- def self.call(html, context)
11
- base = context.config.url.to_s.chomp("/")
12
- raise Kiln::Error, "config.url is required when absolute_urls is on" if base.empty?
13
-
14
- html
15
- .gsub(IMG_TAG) { |tag| tag.gsub(/(?<=\s)(src|srcset)="([^"]*)"/) { %(#{$1}="#{$1 == "src" ? absolute($2, base) : srcset($2, base)}") } }
16
- .gsub(IMAGE_META) { |tag| tag.sub(/(?<=\s)content="([^"]*)"/) { %(content="#{absolute($1, base)}") } }
17
- end
18
-
19
- def self.absolute(url, base) = url.match?(ROOT_RELATIVE) ? "#{base}#{url}" : url
20
-
21
- def self.srcset(value, base)
22
- value.gsub(/(\A|,)(\s*)([^\s,]+)/) { "#{$1}#{$2}#{absolute($3, base)}" }
23
- end
24
- end
25
- end
26
- end
@@ -1,26 +0,0 @@
1
- module Kiln
2
- module Processors
3
- # Adds target="_blank" rel="noopener" to http(s) links that point to another host.
4
- module ExternalLinks
5
- LINK_TAG = /<a\b[^>]*\shref="https?:[^"]*"[^>]*>/
6
-
7
- def self.call(html, context)
8
- host = context.config.host
9
- html.gsub(LINK_TAG) { |tag| external?(tag, host) ? mark(tag) : tag }
10
- end
11
-
12
- def self.external?(tag, host)
13
- link_host = URI(tag[/(?<=\s)href="([^"]*)"/, 1]).host
14
- link_host.present? && !link_host.casecmp?(host)
15
- rescue URI::InvalidURIError
16
- false
17
- end
18
-
19
- def self.mark(tag)
20
- tag = tag.sub(/(?<=\s)rel="([^"]*)"/) { $1.split.include?("noopener") ? $& : %(rel="#{$1} noopener") }
21
- additions = [ (' target="_blank"' unless tag.match?(/\starget=/)), (' rel="noopener"' unless tag.match?(/\srel=/)) ]
22
- tag.delete_suffix(">") + additions.join + ">"
23
- end
24
- end
25
- end
26
- end
@@ -1,22 +0,0 @@
1
- module Kiln
2
- # HTML transforms applied to every rendered HTML response, in order: built-ins, then config.processors.
3
- # Processors edit the HTML as a string and leave everything they don't match byte-for-byte intact.
4
- module Processors
5
- Context = Data.define(:path, :config, :response)
6
-
7
- def self.apply(html, context)
8
- config = context.config
9
- html = StripCSRF.call(html, context)
10
- html = AbsoluteUrls.call(html, context) if config.absolute_urls
11
- html = ExternalLinks.call(html, context) if config.external_links
12
-
13
- config.processors.reduce(html) do |current, processor|
14
- result = processor.call(current, context)
15
- unless result.is_a?(String)
16
- raise Kiln::Error, "Processor #{processor.inspect} returned #{result.class} for #{context.path}, expected a String"
17
- end
18
- result
19
- end
20
- end
21
- end
22
- end
@@ -1,12 +0,0 @@
1
- namespace :kiln do
2
- desc "List every GET route and whether Kiln builds, skips, or misses it"
3
- task routes: :environment do
4
- report = Kiln::Report.new
5
- router = Kiln::Router.new(Rails.application.routes, Kiln.pages, report:)
6
- puts router.table
7
- if report.failure?
8
- puts "", report
9
- exit 1
10
- end
11
- end
12
- end
@@ -1,15 +0,0 @@
1
- namespace :kiln do
2
- desc "Check internal links, anchors and importmap modules in the built output"
3
- task verify: :environment do
4
- output = Kiln.config.output_path
5
- abort "Kiln: #{output} does not exist. Run kiln:build first." unless output.directory?
6
-
7
- report = Kiln::Verifier.new(output, config: Kiln.config).verify(Kiln::Report.new)
8
- if report.success?
9
- puts "Kiln verify: no problems found in #{output}."
10
- else
11
- puts report.to_s(title: "Kiln verify")
12
- exit 1
13
- end
14
- end
15
- end