Skip to content

Commit e622438

Browse files
committed
Detect asset names and contents at compile-time whenever possible
1 parent 62b600c commit e622438

File tree

11 files changed

+59
-37
lines changed

11 files changed

+59
-37
lines changed

assets/build/build.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const formatters = [
2626
formatter: 'html',
2727
outdir: path.resolve('../formatters/html/dist'),
2828
entryPoints: [
29-
'js/entry/html_inline.js',
29+
'js/entry/inline_html.js',
3030
'js/entry/html.js',
3131
'css/entry/html-elixir.css',
3232
'css/entry/html-erlang.css'

assets/js/constants.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
// Constants separated to allow importing into html_inline.js without
1+
// Constants separated to allow importing into inline_html.js without
22
// bringing in other code.
33
export const SETTINGS_KEY = 'ex_doc:settings'
File renamed without changes.

lib/ex_doc/formatter/epub/assets.ex

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,21 @@ defmodule ExDoc.Formatter.EPUB.Assets do
1111
end)
1212
end
1313

14-
def dist(proglang), do: dist_js() ++ dist_css(proglang)
15-
1614
defp dist_js(), do: embed_pattern("dist/*.js")
1715
defp dist_css(:elixir), do: embed_pattern("dist/epub-elixir-*.css")
1816
defp dist_css(:erlang), do: embed_pattern("dist/epub-erlang-*.css")
1917

18+
## Assets
19+
20+
def dist(proglang), do: dist_js() ++ dist_css(proglang)
2021
def metainfo, do: embed_pattern("metainfo/*")
22+
23+
## Filenames
24+
25+
def js_filename(), do: dist_js() |> extract_filename!()
26+
def css_filename(language), do: dist_css(language) |> extract_filename!()
27+
28+
## Helpers
29+
30+
defp extract_filename!([{location, _}]), do: location
2131
end

lib/ex_doc/formatter/epub/templates.ex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ defmodule ExDoc.Formatter.EPUB.Templates do
77
only: [before_closing_body_tag: 2, before_closing_head_tag: 2, h: 1, text_to_id: 1]
88

99
alias ExDoc.Formatter.HTML.Templates, as: H
10+
alias ExDoc.Formatter.EPUB.Assets
1011

1112
@doc """
1213
Generate content from the module template for a given `node`

lib/ex_doc/formatter/epub/templates/head_template.eex

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
<meta charset="utf-8" />
66
<title><%= title %> - <%= config.project %> v<%= config.version %></title>
77
<meta name="generator" content="ExDoc v<%= ExDoc.version() %>" />
8-
<link type="text/css" rel="stylesheet"
9-
href="<%= H.asset_rev "#{config.output}/OEBPS", "dist/epub-#{config.proglang}-*.css" %>" />
10-
<script src="<%= H.asset_rev "#{config.output}/OEBPS", "dist/epub-*.js" %>"></script>
8+
<link type="text/css" rel="stylesheet" href="dist/<%= Assets.css_filename(config.proglang) %>" />
9+
<script src="dist/<%= Assets.js_filename() %>"></script>
1110
<%= before_closing_head_tag(config, :epub) %>
1211
</head>
1312
<body class="content-inner">

lib/ex_doc/formatter/html/assets.ex

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,45 @@ defmodule ExDoc.Formatter.HTML.Assets do
88
|> Enum.map(&{Path.basename(&1), File.read!(&1)})
99
end
1010

11-
def dist(proglang), do: dist_js() ++ dist_css(proglang) ++ dist_license()
12-
13-
defp dist_js(), do: embed_pattern("dist/*.js")
11+
defp dist_js(), do: embed_pattern("dist/html-*.js")
12+
defp dist_inline_js(), do: embed_pattern("dist/inline_html-*.js")
1413
defp dist_css(:elixir), do: embed_pattern("dist/html-elixir-*.css")
1514
defp dist_css(:erlang), do: embed_pattern("dist/html-erlang-*.css")
1615
defp dist_license(), do: embed_pattern("dist/*.LICENSE.txt")
1716

17+
## Assets
18+
19+
def dist(proglang), do: dist_js() ++ dist_css(proglang) ++ dist_license()
1820
def fonts, do: embed_pattern("dist/*.woff2")
21+
22+
## Sources
23+
24+
def inline_js_source(), do: dist_inline_js() |> extract_source!()
25+
26+
## Filenames
27+
28+
def js_filename(), do: dist_js() |> extract_filename!()
29+
def css_filename(language), do: dist_css(language) |> extract_filename!()
30+
31+
## Helpers
32+
33+
@doc """
34+
Some assets are generated automatically, so we find the revision at runtime.
35+
"""
36+
def rev(output, pattern) do
37+
output = Path.expand(output)
38+
39+
matches =
40+
output
41+
|> Path.join(pattern)
42+
|> Path.wildcard()
43+
44+
case matches do
45+
[] -> raise("could not find matching #{output}/#{pattern}")
46+
[asset | _] -> Path.relative_to(asset, output)
47+
end
48+
end
49+
50+
defp extract_filename!([{location, _}]), do: location
51+
defp extract_source!([{_, source}]), do: source
1952
end

lib/ex_doc/formatter/html/templates.ex

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule ExDoc.Formatter.HTML.Templates do
22
@moduledoc false
33
require EEx
4+
alias ExDoc.Formatter.HTML.Assets
45

56
import ExDoc.Utils,
67
only: [
@@ -192,28 +193,6 @@ defmodule ExDoc.Formatter.HTML.Templates do
192193
defp sidebar_type(:livemd), do: "extras"
193194
defp sidebar_type(:extra), do: "extras"
194195

195-
defp resolve_asset(output, pattern) do
196-
output = Path.expand(output)
197-
198-
matches =
199-
output
200-
|> Path.join(pattern)
201-
|> Path.wildcard()
202-
203-
case matches do
204-
[] -> raise("could not find matching #{output}/#{pattern}")
205-
[asset | _] -> asset
206-
end
207-
end
208-
209-
def asset_rev(output, pattern) do
210-
resolve_asset(output, pattern) |> Path.relative_to(output)
211-
end
212-
213-
def asset_inline(output, pattern) do
214-
resolve_asset(output, pattern) |> File.read!()
215-
end
216-
217196
# TODO: Move link_headings and friends to html.ex or even to autolinking code,
218197
# so content is built with it upfront instead of added at the template level.
219198

lib/ex_doc/formatter/html/templates/head_template.eex

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@
1313
<meta name="robots" content="noindex">
1414
<% end %>
1515
<title><%= title %> — <%= config.project %> v<%= config.version %></title>
16-
<link rel="stylesheet" href="<%= asset_rev config.output, "dist/html-#{config.proglang}*.css" %>" />
16+
<link rel="stylesheet" href="dist/<%= Assets.css_filename(config.proglang) %>" />
1717
<%= if config.canonical do %>
1818
<link rel="canonical" href="<%= config.canonical %>" />
1919
<% end %>
20-
<script src="<%= asset_rev config.output, "dist/sidebar_items-*.js" %>"></script>
20+
<script src="<%= Assets.rev config.output, "dist/sidebar_items-*.js" %>"></script>
2121
<script src="docs_config.js"></script>
22-
<script async src="<%= asset_rev config.output, "dist/html-*.js" %>"></script>
22+
<script async src="dist/<%= Assets.js_filename() %>"></script>
2323
<%= before_closing_head_tag(config, :html) %>
2424
</head>
2525
<body class="sidebar-closed">
26-
<script><%= asset_inline config.output, "dist/html_inline-*.js" %></script>
26+
<script><%= ExDoc.Formatter.HTML.Assets.inline_js_source() %></script>

0 commit comments

Comments
 (0)