Skip to content

Commit 3835027

Browse files
authored
Merge pull request #3 from cryic/sidebar-title-updates
Add title tag with properly formatted mfa to sidebar
2 parents e4fa575 + abe80d5 commit 3835027

File tree

16 files changed

+221
-142
lines changed

16 files changed

+221
-142
lines changed

assets/js/templates/sidebar-items.handlebars

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,38 @@
2424

2525
<ul>
2626
<li>
27-
<a href="{{node.id}}.html#content">Top</a>
27+
<a href="{{node.id}}.html#content" title="{{node.title}}">Top</a>
2828
</li>
2929

3030
{{#isArray node.headers}}
3131
{{#each node.headers}}
3232
<li>
33-
<a href="{{node.id}}.html#{{{anchor}}}">{{id}}</a>
33+
<a href="{{node.id}}.html#{{{anchor}}}" title="{{node.title}}{{id}}">{{id}}</a>
3434
</li>
3535
{{/each}}
3636
{{else}}
3737
{{#showSummary node}}
3838
<li>
39-
<a href="{{node.id}}.html#summary">Summary</a>
39+
<a href="{{node.id}}.html#summary" title="{{node.title}} Summary">Summary</a>
4040
</li>
4141
{{/showSummary}}
4242
{{#each node.nodeGroups as |group|}}
4343
<li class="docs">
44-
<a href="{{node.id}}.html#{{group.key}}" class="expand">
44+
<a href="{{node.id}}.html#{{group.key}}" class="expand" title="{{node.title}} {{group.name}}">
4545
{{group.name}}
4646
<span class="icon-goto" title="Go to {{group.name}}"></span>
4747
</a>
4848
<ul class="{{group.key}}-list deflist">
4949
{{#each group.nodes}}
5050
<li>
51-
<a href="{{node.id}}.html#{{anchor}}">{{id}}</a>
51+
<a href="{{node.id}}.html#{{anchor}}" title="{{link_title}}">{{id}}</a>
5252
</li>
5353
{{/each}}
5454
</ul>
5555
</li>
5656
{{/each}}
5757
{{/isArray}}
58-
</ul>
58+
</ul>
5959
</li>
6060
{{/each}}
6161
</ul>

formatters/html/dist/html-17f11fce3c17ca7c2e4e.js renamed to formatters/html/dist/html-ee6ad1641e689858a6e0.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<%= if Enum.any?(summary, fn {_, v} -> v != [] end) do %>
1313
<section id="summary" class="details-list">
1414
<h1 class="section-heading">
15-
<a class="hover-link" href="#summary">
15+
<a class="hover-link" href="#summary" title="Link to module-summary">
1616
<span class="icon-link" aria-hidden="true"></span>
1717
<span class="sr-only">Anchor for this section</span>
1818
</a>
@@ -25,7 +25,7 @@
2525
<%= for {name, nodes} <- summary, nodes != [], key = HTML.text_to_id(name) do %>
2626
<section id="<%= key %>" class="details-list">
2727
<h1 class="section-heading">
28-
<a class="hover-link" href="#<%= key %>">
28+
<a class="hover-link" href="#<%= key %>" title="Link to <%= key %>">
2929
<span class="icon-link" aria-hidden="true"></span>
3030
<span class="sr-only">Anchor for this section</span>
3131
</a>

lib/ex_doc/formatter/html/autolink.ex

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ defmodule ExDoc.Formatter.HTML.Autolink do
1010
@elixir_docs "https://hexdocs.pm/"
1111
@erlang_docs "http://www.erlang.org/doc/man/"
1212
@basic_types_page "typespecs.html#basic-types"
13+
@basic_types_link_title "Basic types — Typespecs"
1314
@built_in_types_page "typespecs.html#built-in-types"
15+
@built_in_types_link_title "Built-in types — Typespecs"
1416

1517
@basic_types [
1618
any: 0,
@@ -188,6 +190,7 @@ defmodule ExDoc.Formatter.HTML.Autolink do
188190

189191
defp all_typespecs(module, compiled) do
190192
%{aliases: aliases, lib_dirs: lib_dirs} = compiled
193+
module_name = module.title
191194

192195
locals =
193196
Enum.map(module.typespecs, fn
@@ -196,14 +199,15 @@ defmodule ExDoc.Formatter.HTML.Autolink do
196199

197200
typespecs =
198201
for typespec <- module.typespecs do
199-
%{typespec | spec: typespec(typespec.spec, locals, aliases, lib_dirs)}
202+
%{typespec | spec: typespec(typespec.spec, locals, module_name, aliases, lib_dirs)}
200203
end
201204

202205
docs =
203206
for module_node <- module.docs do
204207
%{
205208
module_node
206-
| specs: Enum.map(module_node.specs, &typespec(&1, locals, aliases, lib_dirs))
209+
| specs:
210+
Enum.map(module_node.specs, &typespec(&1, locals, module_name, aliases, lib_dirs))
207211
}
208212
end
209213

@@ -216,15 +220,15 @@ defmodule ExDoc.Formatter.HTML.Autolink do
216220
It converts the given `ast` to string while linking
217221
the locals given by `typespecs` as HTML.
218222
"""
219-
def typespec(ast, typespecs, aliases \\ [], lib_dirs \\ default_lib_dirs()) do
223+
def typespec(ast, typespecs, module_name, aliases \\ [], lib_dirs \\ default_lib_dirs()) do
220224
{formatted, placeholders} =
221-
format_and_extract_typespec_placeholders(ast, typespecs, aliases, lib_dirs)
225+
format_and_extract_typespec_placeholders(ast, typespecs, module_name, aliases, lib_dirs)
222226

223227
replace_placeholders(formatted, placeholders)
224228
end
225229

226230
@doc false
227-
def format_and_extract_typespec_placeholders(ast, typespecs, aliases, lib_dirs) do
231+
def format_and_extract_typespec_placeholders(ast, typespecs, module_name, aliases, lib_dirs) do
228232
ref = make_ref()
229233
elixir_docs = get_elixir_docs(aliases, lib_dirs)
230234

@@ -244,16 +248,17 @@ defmodule ExDoc.Formatter.HTML.Autolink do
244248
cond do
245249
{name, arity} in @basic_types ->
246250
url = elixir_docs <> @basic_types_page
247-
put_placeholder(form, url, placeholders)
251+
put_placeholder(form, url, @basic_types_link_title, placeholders)
248252

249253
{name, arity} in @built_in_types ->
250254
url = elixir_docs <> @built_in_types_page
251-
put_placeholder(form, url, placeholders)
255+
put_placeholder(form, url, @built_in_types_link_title, placeholders)
252256

253257
{name, arity} in typespecs ->
254258
n = enc_h("#{name}")
255259
url = "#t:#{n}/#{arity}"
256-
put_placeholder(form, url, placeholders)
260+
title = "t:#{module_name}.#{n}/#{arity}"
261+
put_placeholder(form, url, title, placeholders)
257262

258263
true ->
259264
{form, placeholders}
@@ -265,7 +270,8 @@ defmodule ExDoc.Formatter.HTML.Autolink do
265270

266271
if source = get_source(alias, aliases, lib_dirs) do
267272
url = type_remote_url(source, alias, name, args)
268-
put_placeholder(form, url, placeholders)
273+
title = type_remote_link_title(source, alias, name, args)
274+
put_placeholder(form, url, title, placeholders)
269275
else
270276
{form, placeholders}
271277
end
@@ -288,14 +294,22 @@ defmodule ExDoc.Formatter.HTML.Autolink do
288294
"#{source}#{enc_h(inspect(alias))}.html#t:#{name}/#{length(args)}"
289295
end
290296

291-
defp typespec_string_to_link(string, url) do
297+
defp type_remote_link_title(@erlang_docs, module, name, _args) do
298+
"#{module}:#{name}"
299+
end
300+
301+
defp type_remote_link_title(_source, alias, name, args) do
302+
"t:#{inspect(alias)}.#{name}/#{length(args)}"
303+
end
304+
305+
defp typespec_string_to_link(string, url, title) do
292306
{string_to_link, _string_with_parens} = split_string_to_link(string)
293-
~s[<a href="#{url}">#{h(string_to_link)}</a>]
307+
~s[<a href="#{url}" title="#{title}">#{h(string_to_link)}</a>]
294308
end
295309

296-
defp put_placeholder(form, url, placeholders) do
310+
defp put_placeholder(form, url, title, placeholders) do
297311
string = Macro.to_string(form)
298-
link = typespec_string_to_link(string, url)
312+
link = typespec_string_to_link(string, url, title)
299313

300314
case Enum.find(placeholders, fn {_key, value} -> value == link end) do
301315
{placeholder, _} ->

lib/ex_doc/formatter/html/templates.ex

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ defmodule ExDoc.Formatter.HTML.Templates do
7171
end
7272
end
7373

74+
@doc """
75+
Generate a link title
76+
"""
77+
def link_title(module, node) do
78+
mfa_without_prefix = "#{module.id}.#{node.id}"
79+
link_id(mfa_without_prefix, node.type)
80+
end
81+
7482
@doc """
7583
Returns the HTML formatted title for the module page.
7684
"""
@@ -157,11 +165,20 @@ defmodule ExDoc.Formatter.HTML.Templates do
157165
headers =
158166
content
159167
|> extract_headers
160-
|> Enum.map_join(",", fn {header, anchor} -> sidebar_items_object(header, anchor) end)
168+
|> Enum.map_join(",", fn {header, anchor} ->
169+
sidebar_items_object(header, anchor, hyphenize(id, title))
170+
end)
161171

162172
~s/{"id":"#{id}","title":"#{title}","group":"#{group}","headers":[#{headers}]}/
163173
end
164174

175+
defp hyphenize(id, title) do
176+
[id, title]
177+
|> Enum.filter(&(&1 != ""))
178+
|> Enum.filter(&(&1 != nil))
179+
|> Enum.join(" — ")
180+
end
181+
165182
@h2_regex ~r/<h2.*?>(.*?)<\/h2>/m
166183
defp extract_headers(content) do
167184
@h2_regex
@@ -173,26 +190,30 @@ defmodule ExDoc.Formatter.HTML.Templates do
173190
end
174191

175192
defp sidebar_items_node(module_node) do
193+
sidebar_items_by_group_with_module = fn group ->
194+
sidebar_items_by_group(module_node, group)
195+
end
196+
176197
items =
177198
module_node
178199
|> module_summary()
179200
|> Enum.reject(fn {_type, nodes_map} -> nodes_map == [] end)
180-
|> Enum.map_join(",", &sidebar_items_by_group/1)
201+
|> Enum.map_join(",", fn group -> sidebar_items_by_group_with_module.(group) end)
181202

182203
sidebar_items_json_string(module_node, items)
183204
end
184205

185-
defp sidebar_items_by_group({group, docs}) do
206+
defp sidebar_items_by_group(module_node, {group, docs}) do
186207
objects =
187208
Enum.map_join(docs, ",", fn doc ->
188-
sidebar_items_object(doc.id, link_id(doc))
209+
sidebar_items_object(doc.id, link_id(doc), link_title(module_node, doc))
189210
end)
190211

191212
~s/{"key":"#{HTML.text_to_id(group)}","name":"#{group}","nodes":[#{objects}]}/
192213
end
193214

194-
defp sidebar_items_object(id, anchor) do
195-
~s/{"id":"#{id}","anchor":"#{URI.encode(anchor)}"}/
215+
defp sidebar_items_object(id, anchor, title) do
216+
~s/{"id":"#{id}","anchor":"#{URI.encode(anchor)}","link_title":"#{title}"}/
196217
end
197218

198219
defp sidebar_items_json_string(module_node, items) do
@@ -269,7 +290,7 @@ defmodule ExDoc.Formatter.HTML.Templates do
269290
defp link_heading(_match, tag, title, id, prefix) do
270291
"""
271292
<#{tag} id="#{prefix}#{id}" class="section-heading">
272-
<a href="##{prefix}#{id}" class="hover-link"><span class="icon-link" aria-hidden="true"></span></a>
293+
<a href="##{prefix}#{id}" class="hover-link" title="Link to #{prefix}#{id}"><span class="icon-link" aria-hidden="true"></span></a>
273294
#{title}
274295
</#{tag}>
275296
"""

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
<span id="<%=enc_h link_id(default, node.type) %>"></span>
77
<% end %>
88
<div class="detail-header">
9-
<a href="#<%=enc_h link_id(node) %>" class="detail-link" title="Link to <%= "#{module.title}.#{link_id(node)}" %>">
9+
<a href="#<%=enc_h link_id(node) %>" class="detail-link" title="Link to <%= link_title(module, node) %>">
1010
<span class="icon-link" aria-hidden="true"></span>
11-
<span class="sr-only">Link to <%= "#{module.title}.#{link_id(node)}" %></span>
11+
<span class="sr-only">Link to <%= link_title(module, node) %></span>
1212
</a>
1313
<h1 class="signature"><%=h node.signature %></span>
1414
<%= if node.source_url do %>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
<%= if Enum.any?(summary, fn {_, v} -> v != [] end) do %>
2828
<section id="summary" class="details-list">
2929
<h1 class="section-heading">
30-
<a class="hover-link" href="#summary">
30+
<a class="hover-link" href="#summary" title="Link to <%= module.title %> — Summary">
3131
<span class="icon-link" aria-hidden="true"></span>
3232
<span class="sr-only">Link to this section</span>
3333
</a>
@@ -40,7 +40,7 @@
4040
<%= for {name, nodes} <- summary, nodes != [], key = HTML.text_to_id(name) do %>
4141
<section id="<%= key %>" class="details-list">
4242
<h1 class="section-heading">
43-
<a class="hover-link" href="#<%= key %>">
43+
<a class="hover-link" href="#<%= key %>" title="Link to <%= module.title %> — <%= name %>">
4444
<span class="icon-link" aria-hidden="true"></span>
4545
<span class="sr-only">Link to this section</span>
4646
</a>

0 commit comments

Comments
 (0)