Skip to content

Commit 6bd15cc

Browse files
committed
update footer links & md tests
1 parent 23b5f01 commit 6bd15cc

File tree

10 files changed

+90
-209
lines changed

10 files changed

+90
-209
lines changed

Diff for: lib/ex_doc/formatter/html/templates.ex

+15
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,21 @@ defmodule ExDoc.Formatter.HTML.Templates do
209209

210210
defp relative_asset([h | _], output, _pattern), do: Path.relative_to(h, output)
211211

212+
defp get_hex_url(config, source_path) do
213+
case config.package do
214+
nil ->
215+
nil
216+
217+
package ->
218+
base_url = "https://preview.hex.pm/preview/#{package}/#{config.version}"
219+
if source_path, do: "#{base_url}/show/#{source_path}", else: base_url
220+
end
221+
end
222+
223+
defp get_markdown_path(node) do
224+
if node && node.id, do: URI.encode(node.id), else: "index"
225+
end
226+
212227
# TODO: Move link_headings and friends to html.ex or even to autolinking code,
213228
# so content is built with it upfront instead of added at the template level.
214229

Diff for: lib/ex_doc/formatter/html/templates/footer_template.eex

+28-18
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,43 @@
11
<footer class="footer">
22
<p>
3-
<%= if config.package do %>
4-
<span class="line">
5-
<a href="https://hex.pm/packages/<%= config.package %>/<%= config.version %>" class="footer-hex-package">Hex Package</a>
3+
<span class="line">
64

7-
<a href="https://preview.hex.pm/preview/<%= config.package %>/<%= config.version %>">Hex Preview</a>
5+
<%= if config.package do %>
6+
<a href="https://hex.pm/packages/<%= config.package %>/<%= config.version %>" class="footer-hex-package">Hex Package</a>
7+
<% end %>
88

9-
<%= source_path = node && Map.get(node, :source_path); if source_path do %>
10-
(<a href="https://preview.hex.pm/preview/<%= config.package %>/<%= config.version %>/show/<%= source_path %>">current file</a>)
9+
<%
10+
hex_url = get_hex_url(config, node && (Map.get(node, :source_path) || Map.get(node, :moduledoc_file)))
11+
source_url = node && node.source_url
12+
%>
13+
<%= if hex_url || source_url do %>
14+
View Code:
15+
<%= if source_url do %>
16+
<a href="<%= source_url %>" title="View Source Code" class="footer-button">Source Repo</a>
1117
<% end %>
12-
</span>
13-
<% end %>
1418

15-
<span class="line">
16-
<button class="a-main footer-button display-quick-switch" title="Search HexDocs packages">
17-
Search HexDocs
18-
</button>
19+
<%= if hex_url do %>
20+
<a href="<%= hex_url %>" class="footer-button">Hex Preview</a>
21+
<% end %>
22+
<% end %>
1923

20-
<%= if "epub" in config.formatters do %>
21-
<a href="<%= config.project %>.epub" title="ePub version">
22-
Download ePub version
24+
<%= if "markdown" in config.formatters do %>
25+
<a href="./markdown/<%= get_markdown_path(node) %>.md" title="Markdown version" class="footer-button">
26+
View Markdown version
2327
</a>
2428
<% end %>
2529

26-
<%= if "markdown" in config.formatters do %>
27-
<a href="<%= config.project %>-markdown.zip" title="Markdown version">
28-
Download Markdown version
30+
<%= if config.package do %>
31+
<a href="https://repo.hex.pm/docs/<%= config.package %>-<%= config.version %>.tar.gz" title="Archive containing docs in all available formats" class="footer-button">
32+
Download docs archive
2933
</a>
3034
<% end %>
35+
36+
37+
<button class="a-main footer-button display-quick-switch" title="Search HexDocs packages">
38+
Search HexDocs
39+
</button>
40+
3141
</span>
3242
</p>
3343

Diff for: lib/ex_doc/formatter/html/templates/module_template.eex

-7
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@
99
<span class="sr-only">View Source</span>
1010
</a>
1111
<% end %>
12-
<%= if "markdown" in config.formatters do %>
13-
<a href="<%= config.project %>-markdown.zip" title="Markdown version">
14-
<%= IO.inspect(module).title %>
15-
<i class="ri-markdown-line" aria-hidden="true"></i>
16-
<span class="sr-only">Download Markdown version</span>
17-
</a>
18-
<% end %>
1912

2013
<span translate="no"><%= module.title %></span> <%= module_type(module) %>
2114
<small class="app-vsn" translate="no">(<%= config.project %> v<%= config.version %>)</small>

Diff for: lib/ex_doc/formatter/markdown.ex

+11-27
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,12 @@ defmodule ExDoc.Formatter.MARKDOWN do
3232

3333
config = %{config | extras: extras}
3434

35-
generate_nav(config, nodes_map)
35+
generate_nav(config, nodes_map)
3636
generate_extras(config)
3737
generate_list(config, nodes_map.modules)
3838
generate_list(config, nodes_map.tasks)
3939

40-
# if config[:generate_zip] do # TODO: add a command line flag?
41-
# {:ok, zip} = generate_zip(config.output)
42-
# File.rm_rf!(config.output)
43-
# Path.relative_to_cwd(zip)
44-
# else
45-
config.output |> Path.join("index.md") |> Path.relative_to_cwd()
46-
# end
40+
config.output |> Path.join("index.md") |> Path.relative_to_cwd()
4741
end
4842

4943
defp normalize_config(config) do
@@ -58,7 +52,7 @@ defmodule ExDoc.Formatter.MARKDOWN do
5852
defp normalize_output(output) do
5953
output
6054
|> String.replace(~r/\r\n|\r|\n/, "\n")
61-
|> String.replace(~r/\n{3,}/, "\n\n")
55+
|> String.replace(~r/\n{3,}/, "\n\n")
6256
end
6357

6458
defp generate_nav(config, nodes) do
@@ -67,8 +61,10 @@ defmodule ExDoc.Formatter.MARKDOWN do
6761
modules |> Enum.chunk_by(& &1.group) |> Enum.map(&{hd(&1).group, &1})
6862
end)
6963

70-
content = Templates.nav_template(config, nodes)
71-
|> normalize_output()
64+
content =
65+
Templates.nav_template(config, nodes)
66+
|> normalize_output()
67+
7268
File.write("#{config.output}/index.md", content)
7369
end
7470

@@ -86,25 +82,12 @@ defmodule ExDoc.Formatter.MARKDOWN do
8682
end
8783
end
8884

89-
9085
defp generate_list(config, nodes) do
9186
nodes
9287
|> Task.async_stream(&generate_module_page(&1, config), timeout: :infinity)
9388
|> Enum.map(&elem(&1, 1))
9489
end
9590

96-
defp generate_zip(output) do
97-
:zip.create(
98-
String.to_charlist("#{output}-markdown.zip"),
99-
files_to_add(output),
100-
compress: [
101-
~c".md",
102-
~c".jpg",
103-
~c".png"
104-
]
105-
)
106-
end
107-
10891
## Helpers
10992

11093
defp files_to_add(path) do
@@ -120,9 +103,10 @@ defmodule ExDoc.Formatter.MARKDOWN do
120103
end
121104

122105
defp generate_module_page(module_node, config) do
123-
content = Templates.module_page(config, module_node)
124-
|> normalize_output()
106+
content =
107+
Templates.module_page(config, module_node)
108+
|> normalize_output()
109+
125110
File.write("#{config.output}/#{module_node.id}.md", content)
126111
end
127-
128112
end

Diff for: lib/ex_doc/formatter/markdown/templates.ex

+10-5
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,21 @@ defmodule ExDoc.Formatter.MARKDOWN.Templates do
1616
module_template(config, module_node, summary)
1717
end
1818

19+
@doc """
20+
Returns the formatted title for the module page.
21+
"""
22+
def module_type(%{type: :task}), do: ""
23+
def module_type(%{type: :module}), do: ""
24+
def module_type(%{type: type}), do: "(#{type})"
25+
1926
@doc """
2027
Generated ID for static file
2128
"""
2229
def static_file_to_id(static_file) do
2330
static_file |> Path.basename() |> text_to_id()
2431
end
2532

26-
def node_doc(%{source_doc: %{"en"=> source}}), do: source
33+
def node_doc(%{source_doc: %{"en" => source}}), do: source
2734
def node_doc(%{rendered_doc: source}), do: source
2835

2936
@doc """
@@ -49,8 +56,8 @@ defmodule ExDoc.Formatter.MARKDOWN.Templates do
4956
# it is simpler to guarantee they won't be duplicated in docs.
5057
Regex.replace(~r|(<[^>]*) id="[^"]*"([^>]*>)|, doc, ~S"\1\2", [])
5158
end
52-
53-
@doc """
59+
60+
@doc """
5461
Add link headings for the given `content`.
5562
5663
IDs are prefixed with `prefix`.
@@ -131,7 +138,6 @@ defmodule ExDoc.Formatter.MARKDOWN.Templates do
131138
trim: true
132139
)
133140

134-
135141
EEx.function_from_file(
136142
:defp,
137143
:nav_item_template,
@@ -156,7 +162,6 @@ defmodule ExDoc.Formatter.MARKDOWN.Templates do
156162
# trim: true
157163
# )
158164

159-
160165
# def media_type(_arg), do: nil
161166

162167
templates = [

Diff for: lib/ex_doc/formatter/markdown/templates/detail_template.eex

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
> This <%= node.type %> is deprecated. <%= h(deprecated) %>.
1313
<% end %>
1414

15-
<%= if specs = H.get_specs(node) do %>
16-
<%= for spec <- specs do %>
17-
> <%= H.format_spec_attribute(module, node) %> <%= spec %>
15+
<%= if node.specs != [] do %>
16+
<%= for spec <- node.specs do %>
17+
> <%= H.format_spec_attribute(module, node) %>: <%= spec %>
1818
<% end %>
1919
<% end %>
2020

Diff for: lib/ex_doc/formatter/markdown/templates/module_template.eex

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# <%= module.title %> <%= H.module_type(module) %>
1+
# <%= module.title %> <%= module_type(module) %>
22

33
<%= if deprecated = module.deprecated do %>
44
> This <%= module.type %> is deprecated. <%=h deprecated %>.

Diff for: lib/ex_doc/language/elixir.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ defmodule ExDoc.Language.Elixir do
289289
{:local, :..}
290290

291291
["//", "", ""] ->
292-
{:local, :"..//"}
292+
{:local, :..//}
293293

294294
["", ""] ->
295295
{:local, :.}

Diff for: test/ex_doc/formatter/markdown/templates_test.exs

+8-42
Original file line numberDiff line numberDiff line change
@@ -38,39 +38,6 @@ defmodule ExDoc.Formatter.MARKDOWN.TemplatesTest do
3838
:ok
3939
end
4040

41-
describe "content_template/5" do
42-
test "includes logo as a resource if specified in the config" do
43-
nodes = %{modules: [], tasks: []}
44-
45-
content =
46-
[logo: "my_logo.png"]
47-
|> doc_config()
48-
|> Templates.content_template(nodes, "uuid", "datetime", _static_files = [])
49-
50-
assert content =~ ~S|<item id="logo" href="assets/logo.png" media-type="image/png"/>|
51-
end
52-
53-
54-
test "includes modules as a resource" do
55-
module_node = %ExDoc.ModuleNode{
56-
module: XPTOModule,
57-
doc: nil,
58-
id: "XPTOModule",
59-
title: "XPTOModule"
60-
}
61-
62-
nodes = %{modules: [module_node], tasks: []}
63-
64-
content =
65-
Templates.content_template(doc_config(), nodes, "uuid", "datetime", _static_files = [])
66-
67-
assert content =~
68-
~S|<item id="XPTOModule" href="XPTOModule.md" media-type="application/md+xml" properties="scripted"/>|
69-
70-
assert content =~ ~S|<itemref idref="XPTOModule"/>|
71-
end
72-
end
73-
7441
describe "module_page/2" do
7542
test "generates only the module name when there's no more info" do
7643
module_node = %ExDoc.ModuleNode{
@@ -99,11 +66,11 @@ defmodule ExDoc.Formatter.MARKDOWN.TemplatesTest do
9966
~r{### .*Example H3 heading.*}ms
10067

10168
assert content =~
102-
~r{moduledoc.*Example.*<samp class="nc">CompiledWithDocs</samp><samp class="o">\.</samp><samp class="n">example</samp>.*}ms
69+
~r{moduledoc.*Example.*CompiledWithDocs\.example.*}ms
10370

104-
assert content =~ ~r{example/2.*Some example}ms
105-
assert content =~ ~r{example_without_docs/0.*}ms
106-
assert content =~ ~r{example_1/0.*> \(macro\)}ms
71+
assert content =~ ~r{Some example}ms
72+
assert content =~ ~r{example_without_docs().*}ms
73+
assert content =~ ~r{example_1().*> \(macro\)}ms
10774

10875
assert content =~ ~s{example(foo, bar \\\\ Baz)}
10976
end
@@ -121,21 +88,20 @@ defmodule ExDoc.Formatter.MARKDOWN.TemplatesTest do
12188
assert content =~ ~r{.*Legacy}ms
12289
end
12390

124-
12591
## BEHAVIOURS
12692

12793
test "outputs behavior and callbacks" do
12894
content = get_module_page([CustomBehaviourOne])
12995

13096
assert content =~
131-
~r{# \s*CustomBehaviourOne\s*behaviour\s*}m
97+
~r{# CustomBehaviourOne \(behaviour\)}m
13298

13399
assert content =~ ~r{Callbacks}
134100

135101
content = get_module_page([CustomBehaviourTwo])
136102

137103
assert content =~
138-
~r{# \s*CustomBehaviourTwo\s*behaviour\s*}m
104+
~r{# CustomBehaviourTwo \(behaviour\)}m
139105

140106
assert content =~ ~r{Callbacks}
141107
end
@@ -144,14 +110,14 @@ defmodule ExDoc.Formatter.MARKDOWN.TemplatesTest do
144110

145111
test "outputs the protocol type" do
146112
content = get_module_page([CustomProtocol])
147-
assert content =~ ~r{# \s*CustomProtocol\s*protocol\s*}m
113+
assert content =~ ~r{# CustomProtocol \(protocol\)}m
148114
end
149115

150116
## TASKS
151117

152118
test "outputs the task type" do
153119
content = get_module_page([Mix.Tasks.TaskWithDocs])
154-
assert content =~ ~r{# \s*mix task_with_docs\s*}m
120+
assert content =~ ~r{#\s*mix task_with_docs}m
155121
end
156122
end
157123
end

0 commit comments

Comments
 (0)