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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +65 -0
- data/LICENSE +1 -1
- data/README.md +127 -102
- data/lib/kiln/assets.rb +73 -24
- data/lib/kiln/build.rb +73 -33
- data/lib/kiln/build_cache.rb +4 -4
- data/lib/kiln/configuration.rb +11 -38
- data/lib/kiln/content/collection.rb +10 -10
- data/lib/kiln/content/entry.rb +36 -10
- data/lib/kiln/content/handlers.rb +8 -3
- data/lib/kiln/content/markdown.rb +52 -9
- data/lib/kiln/content.rb +24 -12
- data/lib/kiln/pages.rb +15 -1
- data/lib/kiln/processors/strip_csrf.rb +1 -1
- data/lib/kiln/redirects.rb +7 -43
- data/lib/kiln/report.rb +1 -1
- data/lib/kiln/router.rb +0 -14
- data/lib/kiln/server.rb +2 -2
- data/lib/kiln/test_helper.rb +14 -18
- data/lib/kiln/verifier.rb +4 -0
- data/lib/kiln/version.rb +1 -1
- data/lib/tasks/kiln/build.rake +0 -14
- data/lib/tasks/kiln/serve.rake +4 -3
- metadata +1 -7
- data/lib/kiln/check.rb +0 -22
- data/lib/kiln/processors/absolute_urls.rb +0 -26
- data/lib/kiln/processors/external_links.rb +0 -26
- data/lib/kiln/processors.rb +0 -22
- data/lib/tasks/kiln/routes.rake +0 -12
- data/lib/tasks/kiln/verify.rake +0 -15
data/lib/kiln/build.rb
CHANGED
|
@@ -4,8 +4,10 @@ module Kiln
|
|
|
4
4
|
# Runs the whole pipeline and returns a Report. Never raises for page-level problems: those go in
|
|
5
5
|
# the report so the build can show every failure at once.
|
|
6
6
|
class Build
|
|
7
|
-
#
|
|
8
|
-
|
|
7
|
+
# Development middleware that injects markup into every HTML response.
|
|
8
|
+
DEV_MIDDLEWARE = %w[Hotwire::Spark::Middleware WebConsole::Middleware Rack::MiniProfiler Rack::LiveReload].freeze
|
|
9
|
+
# Source-position attributes from in-page editors (Herb source attribution, Tensaku).
|
|
10
|
+
EDITOR_MARKUP = /\sdata-(?:herb|tensaku)-[\w-]+=/
|
|
9
11
|
|
|
10
12
|
attr_reader :app, :config, :pages_dsl, :output, :report, :manifest, :previous_manifest
|
|
11
13
|
|
|
@@ -17,9 +19,10 @@ module Kiln
|
|
|
17
19
|
@report = Report.new
|
|
18
20
|
@manifest = Manifest.new
|
|
19
21
|
@redirects = []
|
|
22
|
+
@editor_markup = []
|
|
20
23
|
end
|
|
21
24
|
|
|
22
|
-
# Pages added, removed and changed since the previous build
|
|
25
|
+
# Pages added, removed and changed since the previous build of the configured output, or nil.
|
|
23
26
|
def changes
|
|
24
27
|
previous_manifest&.diff(manifest)
|
|
25
28
|
end
|
|
@@ -27,25 +30,27 @@ module Kiln
|
|
|
27
30
|
def run
|
|
28
31
|
Kiln.building! do
|
|
29
32
|
guard_output!
|
|
30
|
-
|
|
33
|
+
guard_dev_middleware!
|
|
34
|
+
ensure_secret_key_base
|
|
31
35
|
cleanup = Assets.precompile(app)
|
|
32
36
|
begin
|
|
33
|
-
|
|
37
|
+
Assets.check_builds!(app)
|
|
34
38
|
prepare_output
|
|
35
39
|
|
|
36
40
|
pages = Router.new(app.routes, pages_dsl, report:).pages
|
|
37
41
|
pages.reject { it.path == config.not_found }.each { render_page(it) }
|
|
38
42
|
render_not_found if config.not_found
|
|
43
|
+
report_editor_markup
|
|
39
44
|
|
|
40
|
-
Assets.new(renderer:, output:, config:, report:).export
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
45
|
+
Assets.new(renderer:, output:, config:, report:, app:).export
|
|
46
|
+
write_files
|
|
47
|
+
Assets.copy_public(from: app.root.join("public"), to: output, report:, generated: generated_files)
|
|
48
|
+
write_redirects if @redirects.any?
|
|
49
|
+
Sitemap.new(manifest, url: config.url).write(output) if config.url
|
|
44
50
|
write_cname if config.cname
|
|
45
|
-
|
|
51
|
+
write_manifest
|
|
46
52
|
|
|
47
|
-
Verifier.new(output, config:).verify(report)
|
|
48
|
-
run_hooks(config.after_build_hooks)
|
|
53
|
+
Verifier.new(output, config:).verify(report)
|
|
49
54
|
ensure
|
|
50
55
|
cleanup&.call
|
|
51
56
|
end
|
|
@@ -68,7 +73,8 @@ module Kiln
|
|
|
68
73
|
if response.redirect?
|
|
69
74
|
@redirects << Redirects::Entry.new(from: page.path, to: response.location, status: response.status)
|
|
70
75
|
elsif response.success?
|
|
71
|
-
body = response.html? ? Processors.
|
|
76
|
+
body = response.html? ? Processors::StripCSRF.call(response.body) : response.body
|
|
77
|
+
@editor_markup << page.path if response.html? && body.match?(EDITOR_MARKUP)
|
|
72
78
|
file = writer.write(page.path, response.content_type, body)
|
|
73
79
|
manifest.add(page.path, file:, body:, content_type: response.content_type, noindex: Sitemap.noindex?(response, body))
|
|
74
80
|
report.wrote(file)
|
|
@@ -79,43 +85,70 @@ module Kiln
|
|
|
79
85
|
report.error(:render, describe(error), path: page.path)
|
|
80
86
|
end
|
|
81
87
|
|
|
88
|
+
def report_editor_markup
|
|
89
|
+
return if @editor_markup.empty?
|
|
90
|
+
|
|
91
|
+
pages = @editor_markup.first(3).join(", ") + (@editor_markup.size > 3 ? " and #{@editor_markup.size - 3} more" : "")
|
|
92
|
+
report.error(:editor_markup, "data-herb-* or data-tensaku-* attributes reached #{pages}. " \
|
|
93
|
+
"Turn off Herb source attribution and Tensaku outside development, or build in production.")
|
|
94
|
+
end
|
|
95
|
+
|
|
82
96
|
def render_not_found
|
|
83
97
|
response = renderer.get(config.not_found)
|
|
84
98
|
unless [ 200, 404 ].include?(response.status)
|
|
85
99
|
return report.error(:render, "404 page responded with #{response.status}", path: config.not_found)
|
|
86
100
|
end
|
|
87
101
|
|
|
88
|
-
body = Processors.
|
|
102
|
+
body = Processors::StripCSRF.call(response.body)
|
|
89
103
|
report.wrote(writer.write_file("404.html", body))
|
|
90
104
|
rescue StandardError => error
|
|
91
105
|
report.error(:render, describe(error), path: config.not_found)
|
|
92
106
|
end
|
|
93
107
|
|
|
94
108
|
def write_redirects
|
|
95
|
-
Redirects.new(@redirects,
|
|
109
|
+
report.wrote(Redirects.new(@redirects, host: config.host).write(output))
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
# The manifest is build state, not part of the site: it lives in tmp/, and only for the configured
|
|
113
|
+
# output, so throwaway builds (tests, temp dirs) never overwrite it.
|
|
114
|
+
def manifest_dir
|
|
115
|
+
app.root.join("tmp") if output.expand_path == config.output_path(app.root).expand_path
|
|
116
|
+
end
|
|
117
|
+
|
|
118
|
+
def write_manifest
|
|
119
|
+
return unless manifest_dir
|
|
120
|
+
|
|
121
|
+
FileUtils.mkdir_p(manifest_dir)
|
|
122
|
+
manifest.write(manifest_dir)
|
|
123
|
+
end
|
|
124
|
+
|
|
125
|
+
def write_files
|
|
126
|
+
config.files.each do |target, source|
|
|
127
|
+
source = app.root.join(source)
|
|
128
|
+
next report.error(:write, "#{source} does not exist", path: "/#{target}") unless source.file?
|
|
129
|
+
|
|
130
|
+
report.wrote(writer.write_file(target.to_s, source.binread))
|
|
131
|
+
rescue Kiln::Error => error
|
|
132
|
+
report.error(:write, error.message, path: "/#{target}")
|
|
133
|
+
end
|
|
96
134
|
end
|
|
97
135
|
|
|
98
136
|
# Files Kiln writes on purpose that may also exist in public/ (Rails ships a public/404.html).
|
|
99
137
|
def generated_files
|
|
100
|
-
[ ("404.html" if config.not_found), ("CNAME" if config.cname) ].compact
|
|
138
|
+
[ ("404.html" if config.not_found), ("CNAME" if config.cname), *config.files.keys.map(&:to_s) ].compact
|
|
101
139
|
end
|
|
102
140
|
|
|
141
|
+
# config.cname = true takes the host from config.url.
|
|
103
142
|
def write_cname
|
|
104
|
-
|
|
105
|
-
|
|
143
|
+
host = config.cname == true ? config.url && URI(config.url).host : config.cname
|
|
144
|
+
return report.error(:write, "config.cname = true needs config.url", path: "/CNAME") unless host
|
|
106
145
|
|
|
107
|
-
|
|
108
|
-
context = Context.new(output:, env: Rails.env, manifest:, report:)
|
|
109
|
-
hooks.each do |hook|
|
|
110
|
-
hook.call(context)
|
|
111
|
-
rescue StandardError => error
|
|
112
|
-
report.error(:hook, describe(error))
|
|
113
|
-
end
|
|
146
|
+
report.wrote(writer.write_file("CNAME", "#{host}\n"))
|
|
114
147
|
end
|
|
115
148
|
|
|
116
149
|
def prepare_output
|
|
117
|
-
previous =
|
|
118
|
-
@previous_manifest = Manifest.load(previous) if previous
|
|
150
|
+
previous = manifest_dir&.join(Manifest::FILENAME)
|
|
151
|
+
@previous_manifest = Manifest.load(previous) if previous&.file?
|
|
119
152
|
FileUtils.rm_rf(output)
|
|
120
153
|
FileUtils.mkdir_p(output)
|
|
121
154
|
end
|
|
@@ -129,13 +162,20 @@ module Kiln
|
|
|
129
162
|
end
|
|
130
163
|
end
|
|
131
164
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
return
|
|
135
|
-
|
|
165
|
+
def guard_dev_middleware!
|
|
166
|
+
active = app.middleware.map(&:name) & DEV_MIDDLEWARE
|
|
167
|
+
return if active.empty?
|
|
168
|
+
|
|
169
|
+
raise Kiln::Error, "#{active.to_sentence} would add development markup to every page. " \
|
|
170
|
+
"Load it only under bin/rails server (if defined?(Rails::Server)), or build in production."
|
|
171
|
+
end
|
|
136
172
|
|
|
137
|
-
|
|
138
|
-
|
|
173
|
+
# Rails refuses to render in production without a secret. A build ships nothing signed (CSRF tokens
|
|
174
|
+
# are stripped), so an app without one gets a throwaway secret instead of a failed build.
|
|
175
|
+
def ensure_secret_key_base
|
|
176
|
+
app.config.secret_key_base
|
|
177
|
+
rescue ArgumentError
|
|
178
|
+
app.config.secret_key_base = SecureRandom.hex(64)
|
|
139
179
|
end
|
|
140
180
|
|
|
141
181
|
def describe(error)
|
data/lib/kiln/build_cache.rb
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
module Kiln
|
|
2
2
|
# A plain in-memory store that returns the cached objects themselves (no copies), so memoized
|
|
3
|
-
# repositories and collections keep their state for the whole build.
|
|
3
|
+
# repositories and collections keep their state for the whole build. Outside a build, fetch runs
|
|
4
|
+
# the block every time, so a development server never serves stale data.
|
|
4
5
|
class BuildCache
|
|
5
6
|
def initialize
|
|
6
7
|
@store = {}
|
|
@@ -8,6 +9,8 @@ module Kiln
|
|
|
8
9
|
end
|
|
9
10
|
|
|
10
11
|
def fetch(key)
|
|
12
|
+
return yield unless Kiln.building?
|
|
13
|
+
|
|
11
14
|
@mutex.synchronize do
|
|
12
15
|
return @store[key] if @store.key?(key)
|
|
13
16
|
|
|
@@ -15,9 +18,6 @@ module Kiln
|
|
|
15
18
|
end
|
|
16
19
|
end
|
|
17
20
|
|
|
18
|
-
def read(key) = @mutex.synchronize { @store[key] }
|
|
19
|
-
def write(key, value) = @mutex.synchronize { @store[key] = value }
|
|
20
|
-
def delete(key) = @mutex.synchronize { @store.delete(key) }
|
|
21
21
|
def clear = @mutex.synchronize { @store.clear }
|
|
22
22
|
end
|
|
23
23
|
end
|
data/lib/kiln/configuration.rb
CHANGED
|
@@ -2,64 +2,37 @@ module Kiln
|
|
|
2
2
|
class Configuration
|
|
3
3
|
# Output directory, relative to Rails.root unless absolute.
|
|
4
4
|
attr_accessor :output
|
|
5
|
-
# Public base URL ("https://davafons.com").
|
|
5
|
+
# Public base URL ("https://davafons.com"). Sets the request host and enables sitemap.xml.
|
|
6
6
|
attr_accessor :url
|
|
7
7
|
# :directory ("/now" -> now/index.html) or :file ("/now" -> now.html).
|
|
8
8
|
attr_accessor :paths
|
|
9
|
-
#
|
|
10
|
-
attr_accessor :assets
|
|
11
|
-
# Writes a CNAME file with this domain when set.
|
|
9
|
+
# Writes a CNAME file with this domain when set. true takes the host from `url`.
|
|
12
10
|
attr_accessor :cname
|
|
13
|
-
# Removes `sourceMappingURL` comments from JS and CSS.
|
|
14
|
-
attr_accessor :strip_source_maps
|
|
15
|
-
# Extra HTML processors: objects responding to call(html, context) -> html.
|
|
16
|
-
attr_accessor :processors
|
|
17
|
-
# Built-in optional processors, off by default.
|
|
18
|
-
attr_accessor :absolute_urls, :external_links
|
|
19
11
|
# Path rendered to 404.html (expected to respond with 404 or 200), nil to skip.
|
|
20
12
|
attr_accessor :not_found
|
|
21
|
-
#
|
|
22
|
-
attr_accessor :
|
|
23
|
-
#
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
attr_accessor :verify
|
|
27
|
-
# Copy public/ into the output (minus public/assets).
|
|
28
|
-
attr_accessor :copy_public
|
|
29
|
-
|
|
30
|
-
attr_reader :before_build_hooks, :after_build_hooks
|
|
31
|
-
attr_writer :host
|
|
13
|
+
# CSS selectors for sample markup whose links the verifier ignores (for example component demos).
|
|
14
|
+
attr_accessor :verify_skip
|
|
15
|
+
# Extra files that aren't routes: { "octicons-license.txt" => "vendor/octicons/LICENSE" }.
|
|
16
|
+
# Sources are relative to Rails.root.
|
|
17
|
+
attr_accessor :files
|
|
32
18
|
|
|
33
19
|
def initialize
|
|
34
20
|
@output = "dist"
|
|
35
21
|
@url = nil
|
|
36
|
-
@host = nil
|
|
37
22
|
@paths = :directory
|
|
38
|
-
@assets = :referenced
|
|
39
23
|
@cname = nil
|
|
40
|
-
@strip_source_maps = true
|
|
41
|
-
@processors = []
|
|
42
|
-
@absolute_urls = false
|
|
43
|
-
@external_links = false
|
|
44
24
|
@not_found = nil
|
|
45
|
-
@
|
|
46
|
-
@
|
|
47
|
-
@verify = true
|
|
48
|
-
@copy_public = true
|
|
49
|
-
@before_build_hooks = []
|
|
50
|
-
@after_build_hooks = []
|
|
25
|
+
@verify_skip = []
|
|
26
|
+
@files = {}
|
|
51
27
|
end
|
|
52
28
|
|
|
53
|
-
# Host sent with every request
|
|
29
|
+
# Host sent with every request: the host of `url`, or "localhost".
|
|
54
30
|
def host
|
|
55
|
-
|
|
31
|
+
(url && URI(url).host) || "localhost"
|
|
56
32
|
end
|
|
57
33
|
|
|
58
34
|
def output_path(root = Rails.root)
|
|
59
35
|
Pathname(output).absolute? ? Pathname(output) : Pathname(root).join(output)
|
|
60
36
|
end
|
|
61
|
-
|
|
62
|
-
def before_build(&block) = @before_build_hooks << block
|
|
63
|
-
def after_build(&block) = @after_build_hooks << block
|
|
64
37
|
end
|
|
65
38
|
end
|
|
@@ -8,8 +8,9 @@ module Kiln
|
|
|
8
8
|
def initialize(name, dir)
|
|
9
9
|
@name, @dir = name.to_s, Pathname(dir)
|
|
10
10
|
@stamp = stamp
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
translations, entries = files.map { Entry.new(@name, it) }.partition(&:locale)
|
|
12
|
+
@entries = entries.sort_by { [ it.published_at&.in_time_zone || Time.at(0), it.slug ] }.reverse
|
|
13
|
+
translations.each { attach(it) }
|
|
13
14
|
end
|
|
14
15
|
|
|
15
16
|
# Visible entries, newest first.
|
|
@@ -19,11 +20,7 @@ module Kiln
|
|
|
19
20
|
|
|
20
21
|
def each(&) = all.each(&)
|
|
21
22
|
|
|
22
|
-
def find(slug
|
|
23
|
-
entry = @entries.find { it.slug == slug.to_s }
|
|
24
|
-
return entry if entry && (Content.show_drafts || entry.published? || preview?(entry, preview_token))
|
|
25
|
-
raise NotFound, "no #{name} entry with slug #{slug.inspect}"
|
|
26
|
-
end
|
|
23
|
+
def find(slug) = find_by(slug:) || raise(NotFound, "no #{name} entry with slug #{slug.inspect}")
|
|
27
24
|
|
|
28
25
|
def find_by(slug:) = all.find { it.slug == slug.to_s }
|
|
29
26
|
|
|
@@ -33,11 +30,14 @@ module Kiln
|
|
|
33
30
|
private
|
|
34
31
|
def files = Dir[@dir.join("*.md")].sort
|
|
35
32
|
|
|
36
|
-
def
|
|
33
|
+
def attach(translation)
|
|
34
|
+
entry = @entries.find { it.slug == translation.slug }
|
|
35
|
+
raise ArgumentError, "#{translation.path} is a translation, but no entry has the slug #{translation.slug.inspect}" unless entry
|
|
37
36
|
|
|
38
|
-
|
|
39
|
-
token.present? && ActiveSupport::SecurityUtils.secure_compare(entry.preview_token, token.to_s)
|
|
37
|
+
entry.add_translation(translation)
|
|
40
38
|
end
|
|
39
|
+
|
|
40
|
+
def stamp = files.map { [ it, File.mtime(it) ] }
|
|
41
41
|
end
|
|
42
42
|
end
|
|
43
43
|
end
|
data/lib/kiln/content/entry.rb
CHANGED
|
@@ -1,36 +1,62 @@
|
|
|
1
1
|
module Kiln
|
|
2
2
|
module Content
|
|
3
3
|
class Entry
|
|
4
|
-
|
|
4
|
+
# SLUG.md, or SLUG.LOCALE.md for a translation. A locale is two lowercase letters, optionally
|
|
5
|
+
# followed by a dash and two uppercase letters: es, ja, pt-BR.
|
|
6
|
+
FILENAME = /\A(?<name>.+?)(?:\.(?<locale>[a-z]{2}(?:-[A-Z]{2})?))?\.md\z/
|
|
7
|
+
# A leading YYYY-MM-DD or YYYY-MM date. The slug is the rest of the name, or the whole name when
|
|
8
|
+
# it is only a date. A month-only date means the 1st of that month.
|
|
9
|
+
DATED = /\A(?<date>\d{4}-\d{2}(?:-\d{2})?)(?:-(?<slug>.+))?\z/
|
|
5
10
|
|
|
6
|
-
attr_reader :collection, :path, :slug, :attributes, :body, :published_at
|
|
11
|
+
attr_reader :collection, :path, :slug, :locale, :attributes, :body, :published_at
|
|
7
12
|
|
|
8
13
|
def initialize(collection, path)
|
|
9
14
|
@collection, @path = collection.to_s, Pathname(path)
|
|
10
15
|
match = FILENAME.match(@path.basename.to_s)
|
|
11
|
-
@
|
|
12
|
-
|
|
13
|
-
@
|
|
16
|
+
@locale = match[:locale]
|
|
17
|
+
dated = DATED.match(match[:name])
|
|
18
|
+
@slug = (dated && dated[:slug]) || match[:name]
|
|
19
|
+
text = @path.read
|
|
20
|
+
@attributes, @body = Frontmatter.parse(text)
|
|
21
|
+
@frontmatter_lines = text.delete_suffix(body).count("\n")
|
|
22
|
+
@published_at = attributes["published_at"] || (filename_date(dated[:date]) if dated)
|
|
23
|
+
@translations = {}
|
|
14
24
|
end
|
|
15
25
|
|
|
26
|
+
# The visible translation from SLUG.LOCALE.md, or nil.
|
|
27
|
+
def translation(locale)
|
|
28
|
+
entry = @translations[locale.to_s]
|
|
29
|
+
entry if entry && (Content.show_drafts || entry.published?)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def add_translation(entry) = @translations[entry.locale] = entry
|
|
33
|
+
|
|
16
34
|
def title = attributes["title"] || slug.titleize
|
|
17
35
|
def draft? = attributes["draft"] == true
|
|
18
36
|
def scheduled? = published_at.present? && published_at.in_time_zone.future?
|
|
19
37
|
def published? = !draft? && !scheduled?
|
|
20
|
-
|
|
38
|
+
|
|
39
|
+
def html
|
|
40
|
+
return Markdown.render(body, path:, line_offset: @frontmatter_lines).html_safe if Content.source_positions
|
|
41
|
+
@html ||= Markdown.render(body).html_safe
|
|
42
|
+
end
|
|
43
|
+
|
|
21
44
|
def to_param = slug
|
|
22
45
|
def [](key) = attributes[key.to_s]
|
|
23
46
|
def to_h = attributes
|
|
24
47
|
|
|
25
|
-
def preview_token
|
|
26
|
-
OpenSSL::HMAC.hexdigest("SHA256", Content.preview_secret, "#{collection}/#{slug}")
|
|
27
|
-
end
|
|
28
|
-
|
|
29
48
|
def respond_to_missing?(name, include_private = false) = attributes.key?(name.to_s) || super
|
|
30
49
|
|
|
31
50
|
def method_missing(name, *args)
|
|
32
51
|
attributes.key?(name.to_s) && args.empty? ? attributes[name.to_s] : super
|
|
33
52
|
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
def filename_date(date)
|
|
56
|
+
Date.iso8601(date.length == 7 ? "#{date}-01" : date)
|
|
57
|
+
rescue Date::Error
|
|
58
|
+
raise ArgumentError, "#{path} starts with an invalid date: #{date}"
|
|
59
|
+
end
|
|
34
60
|
end
|
|
35
61
|
end
|
|
36
62
|
end
|
|
@@ -2,11 +2,16 @@ module Kiln
|
|
|
2
2
|
module Content
|
|
3
3
|
# ActionView template handlers. A handler returns Ruby source that evaluates to the output.
|
|
4
4
|
module Handlers
|
|
5
|
-
# Plain Markdown never changes between renders, so convert it once at compile time.
|
|
6
|
-
|
|
5
|
+
# Plain Markdown never changes between renders, so convert it once at compile time. Source
|
|
6
|
+
# positions are a development aid, so that variant renders on each request.
|
|
7
|
+
Markdown = ->(template, source) do
|
|
8
|
+
positioned = "Kiln::Content::Markdown.render(#{source.inspect}, path: #{template.identifier.inspect})"
|
|
9
|
+
"(Kiln::Content.source_positions ? #{positioned} : #{Content::Markdown.render(source).inspect}).html_safe"
|
|
10
|
+
end
|
|
7
11
|
|
|
8
12
|
# ERB fills a fresh output buffer, then its result goes through Markdown on every render.
|
|
9
|
-
# "begin;" keeps the ERB source on its original line numbers.
|
|
13
|
+
# "begin;" keeps the ERB source on its original line numbers. No source positions: after ERB,
|
|
14
|
+
# Markdown lines no longer match the lines in the template file.
|
|
10
15
|
MarkdownERB = ->(template, source) do
|
|
11
16
|
erb = ActionView::Template.registered_template_handler(:erb).call(template, source)
|
|
12
17
|
"Kiln::Content::Markdown.render(begin;#{erb}\nend.to_s).html_safe"
|
|
@@ -1,18 +1,61 @@
|
|
|
1
1
|
module Kiln
|
|
2
2
|
module Content
|
|
3
3
|
module Markdown
|
|
4
|
-
|
|
4
|
+
OPTIONS = {
|
|
5
|
+
render: { unsafe: true },
|
|
6
|
+
extension: { table: true, strikethrough: true, autolink: true, header_ids: "" }
|
|
7
|
+
}.freeze
|
|
8
|
+
PLUGINS = { syntax_highlighter: nil }.freeze
|
|
9
|
+
# header_ids also appends an empty anchor link to every heading. Headings keep only the id;
|
|
10
|
+
# the app decides how to link them.
|
|
11
|
+
HEADING_ANCHOR = %r{<a href="#[^"]*" aria-label="[^"]*" data-heading-content="[^"]*" class="anchor"></a>}
|
|
5
12
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
13
|
+
# With a path, block elements carry their source position. line_offset is the number of lines
|
|
14
|
+
# above the text in that file, such as frontmatter.
|
|
15
|
+
def self.render(text, path: nil, line_offset: 0)
|
|
16
|
+
options = path ? OPTIONS.deep_merge(render: { sourcepos: true }) : OPTIONS
|
|
17
|
+
document = Commonmarker.parse(text.to_s, options:)
|
|
18
|
+
document.walk { highlight(it, sourcepos: path) if it.type == :code_block }
|
|
19
|
+
html = document.to_html(options:, plugins: PLUGINS).gsub(HEADING_ANCHOR, "")
|
|
20
|
+
path ? SourcePosition.rewrite(html, path, line_offset) : html
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Renders text that is a single paragraph without the wrapping <p>, for titles and captions.
|
|
24
|
+
# Text with any other block structure renders as with render.
|
|
25
|
+
def self.render_inline(text)
|
|
26
|
+
html = render(text)
|
|
27
|
+
block = Commonmarker.parse(text.to_s, options: OPTIONS).first_child
|
|
28
|
+
return html unless block&.type == :paragraph && block.next_sibling.nil?
|
|
29
|
+
|
|
30
|
+
html.delete_prefix("<p>").delete_suffix("</p>\n")
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Swaps a code block for raw HTML highlighted by Rouge, with CSS classes and no inline styles.
|
|
34
|
+
def self.highlight(node, sourcepos:)
|
|
35
|
+
code = node.string_content
|
|
36
|
+
lexer = Rouge::Lexer.find_fancy(node.fence_info.split.first.to_s, code) || Rouge::Lexers::PlainText.new
|
|
37
|
+
position = node.source_position.values_at(:start_line, :start_column, :end_line, :end_column)
|
|
38
|
+
attribute = %( data-sourcepos="%d:%d-%d:%d") % position if sourcepos
|
|
39
|
+
html = %(<pre class="highlight #{lexer.tag}"#{attribute}><code>#{Rouge::Formatters::HTML.new.format(lexer.lex(code))}</code></pre>\n)
|
|
40
|
+
node.replace(Commonmarker::Node.new(:html_block, literal: html))
|
|
11
41
|
end
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Editor markup for Tensaku: data-tensaku-md="path:startLine:startCol-endLine:endCol", with the
|
|
45
|
+
# path relative to Rails.root. Only block elements carry it; commonmarker's positions on inline
|
|
46
|
+
# elements are dropped. The format is a draft, and this is the one place that writes it.
|
|
47
|
+
module SourcePosition
|
|
48
|
+
BLOCKS = %w[p h1 h2 h3 h4 h5 h6 ul ol li blockquote pre table hr].to_set.freeze
|
|
49
|
+
ATTRIBUTE = /<(\w+)([^>]*?) data-sourcepos="(\d+):(\d+)-(\d+):(\d+)"/
|
|
12
50
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
51
|
+
def self.rewrite(html, path, line_offset)
|
|
52
|
+
relative = ERB::Util.html_escape(Pathname(path).relative_path_from(Rails.root).to_s)
|
|
53
|
+
html.gsub(ATTRIBUTE) do
|
|
54
|
+
tag, rest = $1, $2
|
|
55
|
+
next "<#{tag}#{rest}" unless BLOCKS.include?(tag)
|
|
56
|
+
|
|
57
|
+
%(<#{tag}#{rest} data-tensaku-md="#{relative}:#{$3.to_i + line_offset}:#{$4}-#{$5.to_i + line_offset}:#{$6}")
|
|
58
|
+
end
|
|
16
59
|
end
|
|
17
60
|
end
|
|
18
61
|
end
|
data/lib/kiln/content.rb
CHANGED
|
@@ -2,14 +2,15 @@
|
|
|
2
2
|
# Optional and independent of the exporter, so server-rendered Rails apps can use it too.
|
|
3
3
|
#
|
|
4
4
|
# # Gemfile
|
|
5
|
-
# gem "
|
|
5
|
+
# gem "commonmarker"
|
|
6
6
|
# gem "rouge"
|
|
7
7
|
#
|
|
8
8
|
# # config/initializers/kiln.rb
|
|
9
9
|
# require "kiln/content"
|
|
10
10
|
#
|
|
11
11
|
# Markdown views. app/views/pages/about.html.md renders through the layout like any view.
|
|
12
|
-
# about.html.md.erb runs ERB first, then Markdown.
|
|
12
|
+
# about.html.md.erb runs ERB first, then Markdown. commonmarker renders GitHub Flavored Markdown
|
|
13
|
+
# (tables, strikethrough, autolinks, heading ids). Code blocks are highlighted by Rouge with CSS
|
|
13
14
|
# classes, so add a Rouge theme to your stylesheet (`rougify style github`).
|
|
14
15
|
#
|
|
15
16
|
# Collections. content/blog/2024-05-01-hello.md:
|
|
@@ -29,19 +30,28 @@
|
|
|
29
30
|
# # config/kiln.rb
|
|
30
31
|
# params :post, -> { Kiln::Content.collection("blog").map { { slug: it.slug } } }
|
|
31
32
|
#
|
|
33
|
+
# Filenames. A leading YYYY-MM-DD or YYYY-MM date sets published_at (a month means its 1st day); a
|
|
34
|
+
# name that is only a date keeps it as the slug. SLUG.LOCALE.md is a translation of the SLUG entry,
|
|
35
|
+
# where LOCALE is two lowercase letters with an optional -XX region (es, pt-BR). Translations stay out
|
|
36
|
+
# of all/find/each: post.translation(:es) returns one or nil, and post.locale is nil or "es".
|
|
37
|
+
#
|
|
32
38
|
# Publishing. Entries with `draft: true` or a future `published_at` (frontmatter or filename date)
|
|
33
39
|
# are hidden. They show in development unless Kiln is building; set Kiln::Content.show_drafts to
|
|
34
|
-
# override.
|
|
35
|
-
#
|
|
40
|
+
# override.
|
|
41
|
+
#
|
|
42
|
+
# Source positions. With Kiln::Content.source_positions on, each rendered block of a .md view or
|
|
43
|
+
# collection entry carries data-tensaku-md="path:startLine:startCol-endLine:endCol" for the Tensaku
|
|
44
|
+
# editor. .md.erb views get none. It defaults to on in development when Tensaku is loaded, and is
|
|
45
|
+
# always off while Kiln is building.
|
|
36
46
|
#
|
|
37
47
|
# Settings: Kiln::Content.root (default Rails.root/content), Kiln::Content.show_drafts,
|
|
38
|
-
# Kiln::Content.
|
|
48
|
+
# Kiln::Content.source_positions.
|
|
39
49
|
begin
|
|
40
|
-
require "
|
|
50
|
+
require "commonmarker"
|
|
41
51
|
require "rouge"
|
|
42
52
|
rescue LoadError => error
|
|
43
|
-
raise LoadError, "kiln/content needs the
|
|
44
|
-
"Add `gem \"
|
|
53
|
+
raise LoadError, "kiln/content needs the commonmarker and rouge gems. " \
|
|
54
|
+
"Add `gem \"commonmarker\"` and `gem \"rouge\"` to your Gemfile. (#{error.message})"
|
|
45
55
|
end
|
|
46
56
|
|
|
47
57
|
require "kiln"
|
|
@@ -57,7 +67,7 @@ module Kiln
|
|
|
57
67
|
class NotFound < Kiln::Error; end
|
|
58
68
|
|
|
59
69
|
class << self
|
|
60
|
-
attr_writer :root, :show_drafts, :
|
|
70
|
+
attr_writer :root, :show_drafts, :source_positions
|
|
61
71
|
|
|
62
72
|
def root
|
|
63
73
|
Pathname(@root || Rails.root.join("content"))
|
|
@@ -68,8 +78,10 @@ module Kiln
|
|
|
68
78
|
Rails.env.development? && !Kiln.building?
|
|
69
79
|
end
|
|
70
80
|
|
|
71
|
-
def
|
|
72
|
-
|
|
81
|
+
def source_positions
|
|
82
|
+
return false if Kiln.building?
|
|
83
|
+
return @source_positions unless @source_positions.nil?
|
|
84
|
+
Rails.env.development? && !defined?(::Tensaku).nil?
|
|
73
85
|
end
|
|
74
86
|
|
|
75
87
|
def collection(name)
|
|
@@ -83,7 +95,7 @@ module Kiln
|
|
|
83
95
|
end
|
|
84
96
|
|
|
85
97
|
def reset!
|
|
86
|
-
@root = @show_drafts = @
|
|
98
|
+
@root = @show_drafts = @source_positions = nil
|
|
87
99
|
@collections = {}
|
|
88
100
|
end
|
|
89
101
|
|
data/lib/kiln/pages.rb
CHANGED
|
@@ -9,7 +9,15 @@ module Kiln
|
|
|
9
9
|
end
|
|
10
10
|
|
|
11
11
|
# params :post, -> { Post.all.map { { slug: it.slug } } }
|
|
12
|
-
|
|
12
|
+
# params :health, locale: [ nil, "es", "ja" ]
|
|
13
|
+
#
|
|
14
|
+
# Value lists build every combination of their values. A nil value leaves the key out, for
|
|
15
|
+
# optional segments such as "(:locale)". A list may be a lambda, called at build time.
|
|
16
|
+
def params(route_name, source = nil, **values)
|
|
17
|
+
if values.any?
|
|
18
|
+
raise ArgumentError, "params :#{route_name} takes a source or value lists, not both" if source
|
|
19
|
+
source = -> { combinations(values) }
|
|
20
|
+
end
|
|
13
21
|
raise ArgumentError, "params :#{route_name} needs a callable source" unless source.respond_to?(:call)
|
|
14
22
|
@sources[route_name.to_sym] = source
|
|
15
23
|
end
|
|
@@ -31,5 +39,11 @@ module Kiln
|
|
|
31
39
|
end
|
|
32
40
|
|
|
33
41
|
def declared_names = (sources.keys + skips.keys).uniq
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
def combinations(values)
|
|
45
|
+
lists = values.map { |key, list| (list.respond_to?(:call) ? list.call : list).to_a.map { { key => it } } }
|
|
46
|
+
lists.first.product(*lists.drop(1)).map { it.reduce(:merge).compact }
|
|
47
|
+
end
|
|
34
48
|
end
|
|
35
49
|
end
|
|
@@ -7,7 +7,7 @@ module Kiln
|
|
|
7
7
|
# The newline csrf_meta_tags puts between its two tags goes with the first one.
|
|
8
8
|
META_TAG = /<meta\b(?=[^>]*\sname="csrf-(?:param|token)")[^>]*>(?:\n(?=<meta\b[^>]*\sname="csrf-))?/
|
|
9
9
|
|
|
10
|
-
def self.call(html
|
|
10
|
+
def self.call(html)
|
|
11
11
|
html.gsub(TOKEN_INPUT, "").gsub(META_TAG, "")
|
|
12
12
|
end
|
|
13
13
|
end
|