Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
mayel committed Dec 26, 2024
1 parent 0094e4f commit 28faff1
Show file tree
Hide file tree
Showing 12 changed files with 425 additions and 73 deletions.
1 change: 1 addition & 0 deletions lib/ex_doc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ defmodule ExDoc do
if Code.ensure_loaded?(modname) do
modname
else
IO.inspect(modname)
raise "formatter module #{inspect(argname)} not found"
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_doc/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ defmodule ExDoc.CLI do
defp normalize_formatters(opts) do
formatters =
case Keyword.get_values(opts, :formatter) do
[] -> opts[:formatters] || ["html", "epub", "markdown"]
[] -> opts[:formatters] || ["html", "epub"]
values -> values
end

Expand Down
6 changes: 6 additions & 0 deletions lib/ex_doc/formatter/html/templates/footer_template.eex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
Download ePub version
</a>
<% end %>

<%= if "markdown" in config.formatters do %>
<a href="<%= config.project %>-markdown.zip" title="Markdown version">
Download Markdown version
</a>
<% end %>
</span>
</p>

Expand Down
8 changes: 8 additions & 0 deletions lib/ex_doc/formatter/html/templates/module_template.eex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
<span class="sr-only">View Source</span>
</a>
<% end %>
<%= if "markdown" in config.formatters do %>
<a href="<%= config.project %>-markdown.zip" title="Markdown version">
<%= IO.inspect(module).title %>
<i class="ri-markdown-line" aria-hidden="true"></i>
<span class="sr-only">Download Markdown version</span>
</a>
<% end %>

<span translate="no"><%= module.title %></span> <%= module_type(module) %>
<small class="app-vsn" translate="no">(<%= config.project %> v<%= config.version %>)</small>
<%= for annotation <- module.annotations do %>
Expand Down
49 changes: 29 additions & 20 deletions lib/ex_doc/formatter/markdown.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule ExDoc.Formatter.Markdown do
defmodule ExDoc.Formatter.MARKDOWN do
@moduledoc false

@mimetype "text/markdown"
@assets_dir "MD/assets"

Check warning on line 4 in lib/ex_doc/formatter/markdown.ex

View workflow job for this annotation

GitHub Actions / mix_test (1.14, 25)

module attribute @assets_dir was set but never used
alias __MODULE__.{Assets, Templates}

Check warning on line 5 in lib/ex_doc/formatter/markdown.ex

View workflow job for this annotation

GitHub Actions / mix_test (1.14, 25)

unused alias Assets
alias ExDoc.Formatter.HTML
Expand All @@ -28,24 +27,24 @@ defmodule ExDoc.Formatter.Markdown do

extras =
config
|> HTML.build_extras(".xhtml")
|> HTML.build_extras(".md")
|> Enum.chunk_by(& &1.group)
|> Enum.map(&{hd(&1).group, &1})

config = %{config | extras: extras}

static_files = HTML.generate_assets("MD", default_assets(config), config)
HTML.generate_logo(@assets_dir, config)
HTML.generate_cover(@assets_dir, config)

# generate_nav(config, nodes_map)
generate_nav(config, nodes_map)
generate_extras(config)
generate_list(config, nodes_map.modules)
generate_list(config, nodes_map.tasks)

{:ok, epub} = generate_zip(config.output)
File.rm_rf!(config.output)
Path.relative_to_cwd(epub)
# if config[:generate_zip] do # TODO: add a command line flag?
# {:ok, zip} = generate_zip(config.output)
# File.rm_rf!(config.output)
# Path.relative_to_cwd(zip)
# else
config.output |> Path.join("index.md") |> Path.relative_to_cwd()
# end
end

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

defp normalize_output(output) do
output
|> String.replace(~r/\r\n|\r|\n/, "\n")
|> String.replace(~r/\n{2,}/, "\n")
end

defp generate_nav(config, nodes) do
nodes =
Map.update!(nodes, :modules, fn modules ->
modules |> Enum.chunk_by(& &1.group) |> Enum.map(&{hd(&1).group, &1})
end)

content = Templates.nav_template(config, nodes)
|> normalize_output()
File.write("#{config.output}/MD/index.md", content)
end

defp generate_extras(config) do
for {_title, extras} <- config.extras do
Enum.each(extras, fn %{id: id, title: title, title_content: _title_content, source: content} ->
Expand All @@ -66,6 +82,7 @@ defmodule ExDoc.Formatter.Markdown do
#{content}
"""
|> normalize_output()

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




defp generate_list(config, nodes) do
nodes
|> Task.async_stream(&generate_module_page(&1, config), timeout: :infinity)
Expand All @@ -99,13 +114,6 @@ defmodule ExDoc.Formatter.Markdown do

## Helpers

defp default_assets(config) do
[
{Assets.dist(config.proglang), "MD/dist"},
{Assets.metainfo(), "META-INF"}
]
end

defp files_to_add(path) do

Check warning on line 117 in lib/ex_doc/formatter/markdown.ex

View workflow job for this annotation

GitHub Actions / mix_test (1.14, 25)

function files_to_add/1 is unused
Enum.reduce(Path.wildcard(Path.join(path, "**/*")), [], fn file, acc ->
case File.read(file) do
Expand All @@ -120,6 +128,7 @@ defmodule ExDoc.Formatter.Markdown do

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

Expand Down
2 changes: 1 addition & 1 deletion lib/ex_doc/formatter/markdown/assets.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule ExDoc.Formatter.Markdown.Assets do
defmodule ExDoc.Formatter.MARKDOWN.Assets do
@moduledoc false

defmacrop embed_pattern(pattern) do
Expand Down
77 changes: 26 additions & 51 deletions lib/ex_doc/formatter/markdown/templates.ex
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
defmodule ExDoc.Formatter.Markdown.Templates do
defmodule ExDoc.Formatter.MARKDOWN.Templates do
@moduledoc false

require EEx

import ExDoc.Utils,
only: [before_closing_body_tag: 2, before_closing_head_tag: 2, h: 1, text_to_id: 1]
only: [before_closing_body_tag: 2, h: 1, text_to_id: 1]

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

Expand Down Expand Up @@ -119,48 +119,34 @@ defmodule ExDoc.Formatter.Markdown.Templates do
trim: true
)

# @doc """
# Creates the table of contents.

# This template follows the EPUB Navigation Document Definition.

# See http://www.idpf.org/epub/30/spec/epub30-contentdocs.html#sec-xhtml-nav.
# """
# EEx.function_from_file(
# :def,
# :nav_template,
# Path.expand("templates/nav_template.eex", __DIR__),
# [:config, :nodes],
# trim: true
# )
@doc """
Creates the table of contents.
# @doc """
# Creates a new chapter when the user provides additional files.
# """
# EEx.function_from_file(
# :def,
# :extra_template,
# Path.expand("templates/extra_template.eex", __DIR__),
# [:config, :title, :title_content, :content],
# trim: true
# )
"""
EEx.function_from_file(
:def,
:nav_template,
Path.expand("templates/nav_template.eex", __DIR__),
[:config, :nodes],
trim: true
)


# EEx.function_from_file(
# :defp,
# :nav_item_template,
# Path.expand("templates/nav_item_template.eex", __DIR__),
# [:name, :nodes],
# trim: true
# )
EEx.function_from_file(
:defp,
:nav_item_template,
Path.expand("templates/nav_item_template.eex", __DIR__),
[:name, :nodes],
trim: true
)

# EEx.function_from_file(
# :defp,
# :nav_grouped_item_template,
# Path.expand("templates/nav_grouped_item_template.eex", __DIR__),
# [:nodes],
# trim: true
# )
EEx.function_from_file(
:defp,
:nav_grouped_item_template,
Path.expand("templates/nav_grouped_item_template.eex", __DIR__),
[:nodes],
trim: true
)

# EEx.function_from_file(
# :defp,
Expand All @@ -170,17 +156,6 @@ defmodule ExDoc.Formatter.Markdown.Templates do
# trim: true
# )

# "templates/media-types.txt"
# |> Path.expand(__DIR__)
# |> File.read!()
# |> String.split("\n", trim: true)
# |> Enum.each(fn line ->
# [extension, media] = String.split(line, ",")

# def media_type("." <> unquote(extension)) do
# unquote(media)
# end
# end)

# def media_type(_arg), do: nil

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<%= for {title, nodes} <- nodes do %>
<%= if title do %>
- <%=h to_string(title) %>
<% end %>
<%= for node <- nodes do %>
- [<%=h node.title %>](<%= URI.encode node.id %>.md)
<% end %>
<% end %>
6 changes: 6 additions & 0 deletions lib/ex_doc/formatter/markdown/templates/nav_item_template.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<%= unless Enum.empty?(nodes) do %>
- <%= name %>
<%= for node <- nodes do %>
1. [<%=h node.title %>](<%= URI.encode node.id %>.md)
<% end %>
<% end %>
9 changes: 9 additions & 0 deletions lib/ex_doc/formatter/markdown/templates/nav_template.eex
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Table of contents

<%= nav_grouped_item_template config.extras %>
<%= unless Enum.empty?(nodes.modules) do %>
## Modules
<%= nav_grouped_item_template nodes.modules %>
<% end %>
<%= nav_item_template "Mix Tasks", nodes.tasks %>
<%= before_closing_body_tag(config, :markdown) %>
Loading

0 comments on commit 28faff1

Please sign in to comment.