Skip to content

Commit d295d8e

Browse files
committed
WIP for #1976
1 parent 8bd3cba commit d295d8e

File tree

12 files changed

+425
-73
lines changed

12 files changed

+425
-73
lines changed

Diff for: lib/ex_doc.ex

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ defmodule ExDoc do
4444
if Code.ensure_loaded?(modname) do
4545
modname
4646
else
47+
IO.inspect(modname)
4748
raise "formatter module #{inspect(argname)} not found"
4849
end
4950
end

Diff for: lib/ex_doc/cli.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ defmodule ExDoc.CLI do
103103
defp normalize_formatters(opts) do
104104
formatters =
105105
case Keyword.get_values(opts, :formatter) do
106-
[] -> opts[:formatters] || ["html", "epub", "markdown"]
106+
[] -> opts[:formatters] || ["html", "epub"]
107107
values -> values
108108
end
109109

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

+6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@
2222
Download ePub version
2323
</a>
2424
<% end %>
25+
26+
<%= if "markdown" in config.formatters do %>
27+
<a href="<%= config.project %>-markdown.zip" title="Markdown version">
28+
Download Markdown version
29+
</a>
30+
<% end %>
2531
</span>
2632
</p>
2733

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

+8
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
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 %>
19+
1220
<span translate="no"><%= module.title %></span> <%= module_type(module) %>
1321
<small class="app-vsn" translate="no">(<%= config.project %> v<%= config.version %>)</small>
1422
<%= for annotation <- module.annotations do %>

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

+29-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
defmodule ExDoc.Formatter.Markdown do
1+
defmodule ExDoc.Formatter.MARKDOWN do
22
@moduledoc false
33

4-
@mimetype "text/markdown"
54
@assets_dir "MD/assets"
65
alias __MODULE__.{Assets, Templates}
76
alias ExDoc.Formatter.HTML
@@ -28,24 +27,24 @@ defmodule ExDoc.Formatter.Markdown do
2827

2928
extras =
3029
config
31-
|> HTML.build_extras(".xhtml")
30+
|> HTML.build_extras(".md")
3231
|> Enum.chunk_by(& &1.group)
3332
|> Enum.map(&{hd(&1).group, &1})
3433

3534
config = %{config | extras: extras}
3635

37-
static_files = HTML.generate_assets("MD", default_assets(config), config)
38-
HTML.generate_logo(@assets_dir, config)
39-
HTML.generate_cover(@assets_dir, config)
40-
41-
# generate_nav(config, nodes_map)
36+
generate_nav(config, nodes_map)
4237
generate_extras(config)
4338
generate_list(config, nodes_map.modules)
4439
generate_list(config, nodes_map.tasks)
4540

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

5150
defp normalize_config(config) do
@@ -57,6 +56,23 @@ defmodule ExDoc.Formatter.Markdown do
5756
%{config | output: output}
5857
end
5958

59+
defp normalize_output(output) do
60+
output
61+
|> String.replace(~r/\r\n|\r|\n/, "\n")
62+
|> String.replace(~r/\n{2,}/, "\n")
63+
end
64+
65+
defp generate_nav(config, nodes) do
66+
nodes =
67+
Map.update!(nodes, :modules, fn modules ->
68+
modules |> Enum.chunk_by(& &1.group) |> Enum.map(&{hd(&1).group, &1})
69+
end)
70+
71+
content = Templates.nav_template(config, nodes)
72+
|> normalize_output()
73+
File.write("#{config.output}/MD/index.md", content)
74+
end
75+
6076
defp generate_extras(config) do
6177
for {_title, extras} <- config.extras do
6278
Enum.each(extras, fn %{id: id, title: title, title_content: _title_content, source: content} ->
@@ -66,6 +82,7 @@ defmodule ExDoc.Formatter.Markdown do
6682
6783
#{content}
6884
"""
85+
|> normalize_output()
6986

7087
if File.regular?(output) do
7188
Utils.warn("file #{Path.relative_to_cwd(output)} already exists", [])
@@ -77,8 +94,6 @@ defmodule ExDoc.Formatter.Markdown do
7794
end
7895

7996

80-
81-
8297
defp generate_list(config, nodes) do
8398
nodes
8499
|> Task.async_stream(&generate_module_page(&1, config), timeout: :infinity)
@@ -99,13 +114,6 @@ defmodule ExDoc.Formatter.Markdown do
99114

100115
## Helpers
101116

102-
defp default_assets(config) do
103-
[
104-
{Assets.dist(config.proglang), "MD/dist"},
105-
{Assets.metainfo(), "META-INF"}
106-
]
107-
end
108-
109117
defp files_to_add(path) do
110118
Enum.reduce(Path.wildcard(Path.join(path, "**/*")), [], fn file, acc ->
111119
case File.read(file) do
@@ -120,6 +128,7 @@ defmodule ExDoc.Formatter.Markdown do
120128

121129
defp generate_module_page(module_node, config) do
122130
content = Templates.module_page(config, module_node)
131+
|> normalize_output()
123132
File.write("#{config.output}/MD/#{module_node.id}.md", content)
124133
end
125134

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
defmodule ExDoc.Formatter.Markdown.Assets do
1+
defmodule ExDoc.Formatter.MARKDOWN.Assets do
22
@moduledoc false
33

44
defmacrop embed_pattern(pattern) do

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

+26-51
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
defmodule ExDoc.Formatter.Markdown.Templates do
1+
defmodule ExDoc.Formatter.MARKDOWN.Templates do
22
@moduledoc false
33

44
require EEx
55

66
import ExDoc.Utils,
7-
only: [before_closing_body_tag: 2, before_closing_head_tag: 2, h: 1, text_to_id: 1]
7+
only: [before_closing_body_tag: 2, h: 1, text_to_id: 1]
88

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

@@ -119,48 +119,34 @@ defmodule ExDoc.Formatter.Markdown.Templates do
119119
trim: true
120120
)
121121

122-
# @doc """
123-
# Creates the table of contents.
124-
125-
# This template follows the EPUB Navigation Document Definition.
126-
127-
# See http://www.idpf.org/epub/30/spec/epub30-contentdocs.html#sec-xhtml-nav.
128-
# """
129-
# EEx.function_from_file(
130-
# :def,
131-
# :nav_template,
132-
# Path.expand("templates/nav_template.eex", __DIR__),
133-
# [:config, :nodes],
134-
# trim: true
135-
# )
122+
@doc """
123+
Creates the table of contents.
136124
137-
# @doc """
138-
# Creates a new chapter when the user provides additional files.
139-
# """
140-
# EEx.function_from_file(
141-
# :def,
142-
# :extra_template,
143-
# Path.expand("templates/extra_template.eex", __DIR__),
144-
# [:config, :title, :title_content, :content],
145-
# trim: true
146-
# )
125+
"""
126+
EEx.function_from_file(
127+
:def,
128+
:nav_template,
129+
Path.expand("templates/nav_template.eex", __DIR__),
130+
[:config, :nodes],
131+
trim: true
132+
)
147133

148134

149-
# EEx.function_from_file(
150-
# :defp,
151-
# :nav_item_template,
152-
# Path.expand("templates/nav_item_template.eex", __DIR__),
153-
# [:name, :nodes],
154-
# trim: true
155-
# )
135+
EEx.function_from_file(
136+
:defp,
137+
:nav_item_template,
138+
Path.expand("templates/nav_item_template.eex", __DIR__),
139+
[:name, :nodes],
140+
trim: true
141+
)
156142

157-
# EEx.function_from_file(
158-
# :defp,
159-
# :nav_grouped_item_template,
160-
# Path.expand("templates/nav_grouped_item_template.eex", __DIR__),
161-
# [:nodes],
162-
# trim: true
163-
# )
143+
EEx.function_from_file(
144+
:defp,
145+
:nav_grouped_item_template,
146+
Path.expand("templates/nav_grouped_item_template.eex", __DIR__),
147+
[:nodes],
148+
trim: true
149+
)
164150

165151
# EEx.function_from_file(
166152
# :defp,
@@ -170,17 +156,6 @@ defmodule ExDoc.Formatter.Markdown.Templates do
170156
# trim: true
171157
# )
172158

173-
# "templates/media-types.txt"
174-
# |> Path.expand(__DIR__)
175-
# |> File.read!()
176-
# |> String.split("\n", trim: true)
177-
# |> Enum.each(fn line ->
178-
# [extension, media] = String.split(line, ",")
179-
180-
# def media_type("." <> unquote(extension)) do
181-
# unquote(media)
182-
# end
183-
# end)
184159

185160
# def media_type(_arg), do: nil
186161

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<%= for {title, nodes} <- nodes do %>
2+
<%= if title do %>
3+
- <%=h to_string(title) %>
4+
<% end %>
5+
<%= for node <- nodes do %>
6+
- [<%=h node.title %>](<%= URI.encode node.id %>.md)
7+
<% end %>
8+
<% end %>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<%= unless Enum.empty?(nodes) do %>
2+
- <%= name %>
3+
<%= for node <- nodes do %>
4+
1. [<%=h node.title %>](<%= URI.encode node.id %>.md)
5+
<% end %>
6+
<% end %>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Table of contents
2+
3+
<%= nav_grouped_item_template config.extras %>
4+
<%= unless Enum.empty?(nodes.modules) do %>
5+
## Modules
6+
<%= nav_grouped_item_template nodes.modules %>
7+
<% end %>
8+
<%= nav_item_template "Mix Tasks", nodes.tasks %>
9+
<%= before_closing_body_tag(config, :markdown) %>

0 commit comments

Comments
 (0)