Skip to content

Commit

Permalink
Simplify extra and static handling
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Mar 6, 2024
1 parent 3939192 commit 2a37f99
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 54 deletions.
6 changes: 1 addition & 5 deletions lib/ex_doc/formatter/epub/templates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ defmodule ExDoc.Formatter.EPUB.Templates do
import ExDoc.Utils,
only: [before_closing_body_tag: 2, before_closing_head_tag: 2, h: 1, text_to_id: 1]

alias ExDoc.Formatter.HTML
alias ExDoc.Formatter.HTML.Templates, as: H

@doc """
Expand All @@ -21,10 +20,7 @@ defmodule ExDoc.Formatter.EPUB.Templates do
Generated ID for static file
"""
def static_file_to_id(static_file) do
prefix = static_file |> HTML.filename_to_title() |> text_to_id()
extension = static_file |> Path.extname() |> String.replace_prefix(".", "-")

"#{prefix}#{extension}"
static_file |> Path.basename() |> text_to_id()
end

@doc """
Expand Down
57 changes: 9 additions & 48 deletions lib/ex_doc/formatter/html.ex
Original file line number Diff line number Diff line change
Expand Up @@ -388,48 +388,10 @@ defmodule ExDoc.Formatter.HTML do
Map.put(extra, :id, "#{extra.id}-#{discriminator}")
end

defp build_extra({input, options}, groups, language, autolink_opts, source_url_pattern) do
defp build_extra({input, input_options}, groups, language, autolink_opts, source_url_pattern) do
input = to_string(input)
id = options[:filename] || input |> filename_to_title() |> Utils.text_to_id()
source = options[:source] || input

build_extra(
input,
id,
options[:title],
groups,
language,
autolink_opts,
source,
source_url_pattern
)
end

defp build_extra(input, groups, language, autolink_opts, source_url_pattern) do
id = input |> filename_to_title() |> Utils.text_to_id()

build_extra(
input,
id,
nil,
groups,
language,
autolink_opts,
input,
source_url_pattern
)
end

defp build_extra(
input,
id,
title,
groups,
language,
autolink_opts,
source_file,
source_url_pattern
) do
id = input_options[:filename] || input |> filename_to_title() |> Utils.text_to_id()
source_file = input_options[:source] || input
opts = [file: source_file, line: 1]

{source, ast} =
Expand Down Expand Up @@ -462,14 +424,12 @@ defmodule ExDoc.Formatter.HTML do

title_text = title_ast && ExDoc.DocAST.text_from_ast(title_ast)
title_html = title_ast && ExDoc.DocAST.to_string(title_ast)

content_html = autolink_and_render(ast, language, [file: input] ++ autolink_opts, opts)

group = GroupMatcher.match_extra(groups, input)
title = title || title_text || filename_to_title(input)
title = input_options[:title] || title_text || filename_to_title(input)

source_path = source_file |> Path.relative_to(File.cwd!()) |> String.replace_leading("./", "")

source_url = Utils.source_url_pattern(source_url_pattern, source_path, 1)

%{
Expand All @@ -484,6 +444,10 @@ defmodule ExDoc.Formatter.HTML do
}
end

defp build_extra(input, groups, language, autolink_opts, source_url_pattern) do
build_extra({input, []}, groups, language, autolink_opts, source_url_pattern)
end

defp extension_name(input) do
input
|> Path.extname()
Expand All @@ -500,10 +464,7 @@ defmodule ExDoc.Formatter.HTML do

defp sectionize(ast, _), do: ast

@doc """
Convert the input file name into a title
"""
def filename_to_title(input) do
defp filename_to_title(input) do
input |> Path.basename() |> Path.rootname()
end

Expand Down
1 change: 0 additions & 1 deletion test/ex_doc/formatter/html_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ defmodule ExDoc.Formatter.HTMLTest do
use ExUnit.Case, async: true

import ExUnit.CaptureIO
alias ExDoc.Formatter.HTML

@moduletag :tmp_dir

Expand Down
1 change: 1 addition & 0 deletions test/ex_doc/utils_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ defmodule ExDoc.UtilsTest do
assert Utils.text_to_id("“Stale”") == "stale"
assert Utils.text_to_id("José") == "josé"
assert Utils.text_to_id(" a - b ") == "a-b"
assert Utils.text_to_id("foo.img") == "foo-img"
assert Utils.text_to_id(" ☃ ") == ""
assert Utils.text_to_id(" ² ") == ""
assert Utils.text_to_id(" ⏜ ") == ""
Expand Down

0 comments on commit 2a37f99

Please sign in to comment.