Skip to content

Commit

Permalink
Reuse head_template as much as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
Milton Mazzarri committed Sep 20, 2016
1 parent ba7779f commit 87bc287
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 41 deletions.
14 changes: 7 additions & 7 deletions lib/ex_doc/formatter/epub.ex
Expand Up @@ -58,23 +58,23 @@ defmodule ExDoc.Formatter.EPUB do
defp generate_extras(output, config, module_nodes) do
config.extras
|> Enum.map(&Task.async(fn ->
generate_extra(&1, output, config, module_nodes)
create_extra_files(&1, output, config, module_nodes)
end))
|> Enum.map(&Task.await(&1, :infinity))
end

defp generate_extra(input, output, config, module_nodes) do
defp create_extra_files(input, output, config, module_nodes) do
if HTML.valid_extension_name?(input) do
file_name =
input
|> Path.basename(".md")
|> String.upcase()

content =
input
|> File.read!()
|> HTML.Autolink.project_doc(module_nodes, nil, ".xhtml")

file_name =
input
|> Path.basename(".md")
|> String.upcase()

config = Map.put(config, :title, file_name)
extra_html =
config
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_doc/formatter/epub/templates.ex
Expand Up @@ -67,7 +67,7 @@ defmodule ExDoc.Formatter.EPUB.Templates do

EEx.function_from_file(:defp, :head_template,
Path.expand("templates/head_template.eex", __DIR__),
[:config])
[:config, :page])

# Helpers
defp extra_title(path), do: path |> String.upcase |> Path.basename(".MD")
Expand Down
11 changes: 1 addition & 10 deletions lib/ex_doc/formatter/epub/templates/extra_template.eex
@@ -1,13 +1,4 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<meta charset="utf-8" />
<title><%= extra_title(config.title) %> - <%= config.project %> v<%= config.version %></title>
<meta name="generator" content="ExDoc v<%= ExDoc.version %>" />
<link type="text/css" rel="stylesheet" href="css/stylesheet.css" />
</head>
<body>
<%= head_template(config, %{title: extra_title(config.title)}) %>
<h1><%= extra_title(config.title) %></h1>
<%= H.to_html(content) %>
</body>
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_doc/formatter/epub/templates/head_template.eex
Expand Up @@ -3,7 +3,7 @@
xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<meta charset="utf-8" />
<title><%= config.project %> v<%= config.version %></title>
<title><%= page.title %> - <%= config.project %> v<%= config.version %></title>
<meta name="generator" content="ExDoc v<%= ExDoc.version %>" />
<link type="text/css" rel="stylesheet" href="css/stylesheet.css" />
</head>
Expand Down
11 changes: 1 addition & 10 deletions lib/ex_doc/formatter/epub/templates/module_template.eex
@@ -1,13 +1,4 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"
xmlns:epub="http://www.idpf.org/2007/ops">
<head>
<meta charset="utf-8" />
<title><%= module.id %> - <%= config.project %> v<%= config.version %></title>
<meta name="generator" content="ExDoc" />
<link type="text/css" rel="stylesheet" href="css/stylesheet.css" />
</head>
<body>
<%= head_template(config, %{title: module.id}) %>
<h1 id="content">
<%= module.id %>
<%= if module.type != :module do %>
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_doc/formatter/epub/templates/nav_template.eex
@@ -1,4 +1,4 @@
<%= head_template(config) %>
<%= head_template(config, %{title: "Table Of Contents"}) %>
<h1>Table of contents</h1>
<nav epub:type="toc">
<ol>
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_doc/formatter/epub/templates/title_template.eex
@@ -1,4 +1,4 @@
<%= head_template(config) %>
<%= head_template(config, %{title: "Cover"}) %>
<h1><%= config.project %></h1>
<h2>v<%= config.version %></h2>
<%= if config.logo do %>
Expand Down
19 changes: 9 additions & 10 deletions test/ex_doc/formatter/epub_test.exs
Expand Up @@ -58,7 +58,6 @@ defmodule ExDoc.Formatter.EPUBTest do
root_dir = "#{output_dir()}"
meta_dir = "#{root_dir}/META-INF"
oebps_dir = "#{root_dir}/OEBPS"
module_dir = "#{oebps_dir}/modules"
css_dir = "#{oebps_dir}/css"

assert File.regular?("#{root_dir}/mimetype")
Expand All @@ -69,19 +68,19 @@ defmodule ExDoc.Formatter.EPUBTest do
assert File.regular?("#{oebps_dir}/toc.ncx")
assert File.regular?("#{oebps_dir}/nav.xhtml")
assert File.regular?("#{oebps_dir}/title.xhtml")
assert File.regular?("#{module_dir}/README.xhtml")
assert File.regular?("#{module_dir}/CompiledWithDocs.xhtml")
assert File.regular?("#{module_dir}/CompiledWithDocs.Nested.xhtml")
assert File.regular?("#{oebps_dir}/README.xhtml")
assert File.regular?("#{oebps_dir}/CompiledWithDocs.xhtml")
assert File.regular?("#{oebps_dir}/CompiledWithDocs.Nested.xhtml")
end

test "check headers for module pages" do
generate_docs_and_unzip doc_config([main: "RandomError", ])

content = File.read!("#{output_dir()}/OEBPS/modules/RandomError.xhtml")
content = File.read!("#{output_dir()}/OEBPS/RandomError.xhtml")

assert content =~ ~r{<html.*xmlns:epub="http://www.idpf.org/2007/ops">}ms
assert content =~ ~r{<meta charset="utf-8" />}ms
assert content =~ ~r{<meta name="generator" content="ExDoc" />}
assert content =~ ~r{<meta name="generator" content="ExDoc v[^"]+" />}
assert content =~ ~r{<title>RandomError - Elixir v1.0.1</title>}
end

Expand All @@ -104,7 +103,7 @@ defmodule ExDoc.Formatter.EPUBTest do
config = doc_config([main: "README", ])
generate_docs_and_unzip(config)

content = File.read!("#{output_dir()}/OEBPS/modules/README.xhtml")
content = File.read!("#{output_dir()}/OEBPS/README.xhtml")

assert content =~ ~r{<title>README [^<]*</title>}
assert content =~ ~r{<a href="RandomError.xhtml"><code>RandomError</code>}
Expand All @@ -115,13 +114,13 @@ defmodule ExDoc.Formatter.EPUBTest do
assert content =~ ~r{<text>README</text>}

content = File.read!("#{output_dir()}/OEBPS/nav.xhtml")
assert content =~ ~r{<li><a href="modules/README.xhtml">README</a></li>}
assert content =~ ~r{<li><a href="README.xhtml">README</a></li>}
end

test "run should not generate the readme file" do
generate_docs_and_unzip(doc_config([extras: []]))

refute File.regular?("#{output_dir()}/OEBPS/modules/README.xhtml")
refute File.regular?("#{output_dir()}/OEBPS/README.xhtml")

content = File.read!("#{output_dir()}/OEBPS/content.opf")
refute content =~ ~r{<title>README [^<]*</title>}
Expand All @@ -130,6 +129,6 @@ defmodule ExDoc.Formatter.EPUBTest do
refute content =~ ~r{<text>README</text>}

content = File.read!("#{output_dir()}/OEBPS/nav.xhtml")
refute content =~ ~r{<li><a href="modules/README.html">README</a></li>}
refute content =~ ~r{<li><a href="README.html">README</a></li>}
end
end

0 comments on commit 87bc287

Please sign in to comment.