Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[
inputs: ["{mix,.formatter}.exs", "{lib,test}/**/*.{ex,exs}"]
]
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ notifications:
before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
- mix local.hex --force
- mix deps.get
env:
- MIX_ENV=test
script:
- mix do local.hex --force
- mix deps.get
- mix format --check-formatted
- mix test
- mix coveralls.travis
- bash test/prerelease.sh
Expand Down
2 changes: 1 addition & 1 deletion lib/ex_doc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ defmodule ExDoc do
if Code.ensure_loaded?(modname) do
modname
else
raise "formatter module #{inspect argname} not found"
raise "formatter module #{inspect(argname)} not found"
end
end

Expand Down
24 changes: 13 additions & 11 deletions lib/ex_doc/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,14 @@ defmodule ExDoc.CLI do
o: :output,
r: :source_root,
u: :source_url,
v: :version,
v: :version
],

switches: [
debug: :boolean,
extra: :keep,
language: :string,
source_ref: :string,
version: :boolean,
version: :boolean
]
)

Expand All @@ -41,18 +40,20 @@ defmodule ExDoc.CLI do
end

defp print_version do
IO.puts "ExDoc v#{ExDoc.version}"
IO.puts("ExDoc v#{ExDoc.version()}")
end

defp generate(args, opts, generator) do
[project, version, source_beam] = parse_args(args)

Code.prepend_path(source_beam)

opts =
opts
|> Keyword.put(:source_beam, source_beam)
|> extra_files_options()
|> merge_config()

generator.(project, version, opts)
end

Expand All @@ -62,6 +63,7 @@ defmodule ExDoc.CLI do
opts
|> Keyword.delete(:config)
|> Keyword.merge(read_config(config))

_ ->
opts
end
Expand All @@ -80,7 +82,7 @@ defmodule ExDoc.CLI do
{result, _} = Code.eval_string(config)

unless is_list(result) do
raise "expected a keyword list from config file: #{inspect path}"
raise "expected a keyword list from config file: #{inspect(path)}"
end

result
Expand All @@ -89,19 +91,19 @@ defmodule ExDoc.CLI do
defp parse_args([_project, _version, _source_beam] = args), do: args

defp parse_args([_, _, _ | _]) do
IO.puts "Too many arguments.\n"
IO.puts("Too many arguments.\n")
print_usage()
exit {:shutdown, 1}
exit({:shutdown, 1})
end

defp parse_args(_) do
IO.puts "Too few arguments.\n"
IO.puts("Too few arguments.\n")
print_usage()
exit {:shutdown, 1}
exit({:shutdown, 1})
end

defp print_usage do
IO.puts ~S"""
IO.puts(~S"""
Usage:
ex_doc PROJECT VERSION BEAMS [OPTIONS]

Expand Down Expand Up @@ -158,6 +160,6 @@ defmodule ExDoc.CLI do

https://github.com/elixir-lang/ecto/blob/v1.0/%{path}#L%{line}

"""
""")
end
end
73 changes: 42 additions & 31 deletions lib/ex_doc/formatter/epub.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ defmodule ExDoc.Formatter.EPUB do
@doc """
Generate EPUB documentation for the given modules.
"""
@spec run(list, ExDoc.Config.t) :: String.t
@spec run(list, ExDoc.Config.t()) :: String.t()
def run(project_nodes, config) when is_map(config) do
config = normalize_config(config)
File.rm_rf!(config.output)
Expand Down Expand Up @@ -53,6 +53,7 @@ defmodule ExDoc.Formatter.EPUB do
config.output
|> Path.expand()
|> Path.join("#{config.project}")

%{config | output: output}
end

Expand All @@ -61,9 +62,11 @@ defmodule ExDoc.Formatter.EPUB do
Enum.each(extras, fn %{id: id, title: title, content: content} ->
output = "#{config.output}/OEBPS/#{id}.xhtml"
html = Templates.extra_template(config, title, content)
if File.regular? output do
IO.puts :stderr, "warning: file #{Path.relative_to_cwd output} already exists"

if File.regular?(output) do
IO.puts(:stderr, "warning: file #{Path.relative_to_cwd(output)} already exists")
end

File.write!(output, html)
end)
end
Expand All @@ -72,8 +75,8 @@ defmodule ExDoc.Formatter.EPUB do
defp generate_content(config, nodes, uuid, datetime, static_files) do
static_files =
static_files
|> Enum.filter(fn(name) ->
String.contains?(name, "OEBPS") and config.output |> Path.join(name) |> File.regular?()
|> Enum.filter(fn name ->
String.contains?(name, "OEBPS") and config.output |> Path.join(name) |> File.regular?()
end)
|> Enum.map(&Path.relative_to(&1, "OEBPS"))

Expand All @@ -90,17 +93,17 @@ defmodule ExDoc.Formatter.EPUB do
{extras_by_group, groups} =
extras
|> Enum.with_index()
|> Enum.reduce({%{}, %{}}, fn({x, index}, {extras_by_group, groups}) ->
group = if x.group != "", do: x.group, else: "Extras"
extras_by_group = Map.update(extras_by_group, group, [x], &([x | &1]))
groups = Map.put_new(groups, group, index)
{extras_by_group, groups}
end)
|> Enum.reduce({%{}, %{}}, fn {x, index}, {extras_by_group, groups} ->
group = if x.group != "", do: x.group, else: "Extras"
extras_by_group = Map.update(extras_by_group, group, [x], &[x | &1])
groups = Map.put_new(groups, group, index)
{extras_by_group, groups}
end)

groups
|> Map.to_list()
|> List.keysort(1)
|> Enum.map(fn({k, _}) -> {k, Enum.reverse(Map.get(extras_by_group, k))} end)
|> Enum.map(fn {k, _} -> {k, Enum.reverse(Map.get(extras_by_group, k))} end)
end

defp generate_title(config) do
Expand All @@ -115,43 +118,48 @@ defmodule ExDoc.Formatter.EPUB do
end

defp generate_epub(output) do
:zip.create(String.to_charlist("#{output}.epub"),
[{'mimetype', @mimetype} | files_to_add(output)],
compress: ['.css', '.xhtml', '.html', '.ncx', '.js',
'.opf', '.jpg', '.png', '.xml'])
:zip.create(
String.to_charlist("#{output}.epub"),
[{'mimetype', @mimetype} | files_to_add(output)],
compress: ['.css', '.xhtml', '.html', '.ncx', '.js', '.opf', '.jpg', '.png', '.xml']
)
end

## Helpers

defp default_assets do
[{Assets.dist(), "OEBPS/dist"},
{Assets.metainfo(), "META-INF"},
# Implementation Note:
# --------------------
# In the EPUB format, extra files such as CSS and Javascript must
# be included inside the OEBPS directory.
# The file "OEBPS/file.ext" will be referenced inside pages as "file.ext".
# This means that as long as the assets are included with the "OEBPS" prefix,
# they will be accessible as if it were an HTML document.
# This allows us to reuse pretty much all logic between the `:epub` and the `:html` formats.
{Assets.markdown_processor_assets(), "OEBPS"}]
[
{Assets.dist(), "OEBPS/dist"},
{Assets.metainfo(), "META-INF"},
# Implementation Note:
# --------------------
# In the EPUB format, extra files such as CSS and Javascript must
# be included inside the OEBPS directory.
# The file "OEBPS/file.ext" will be referenced inside pages as "file.ext".
# This means that as long as the assets are included with the "OEBPS" prefix,
# they will be accessible as if it were an HTML document.
# This allows us to reuse pretty much all logic between the `:epub` and the `:html` formats.
{Assets.markdown_processor_assets(), "OEBPS"}
]
end

defp files_to_add(path) do
Enum.reduce Path.wildcard(Path.join(path, "**/*")), [], fn file, acc ->
Enum.reduce(Path.wildcard(Path.join(path, "**/*")), [], fn file, acc ->
case File.read(file) do
{:ok, bin} ->
[{file |> Path.relative_to(path) |> String.to_charlist(), bin} | acc]

{:error, _} ->
acc
end
end
end)
end

# Helper to format Erlang datetime tuple
defp format_datetime do
{{year, month, day}, {hour, min, sec}} = :calendar.universal_time()
list = [year, month, day, hour, min, sec]

"~4..0B-~2..0B-~2..0BT~2..0B:~2..0B:~2..0BZ"
|> :io_lib.format(list)
|> IO.iodata_to_binary()
Expand All @@ -169,7 +177,10 @@ defmodule ExDoc.Formatter.EPUB do
bin = <<u0::48, 4::4, u1::12, 2::2, u2::62>>
<<u0::32, u1::16, u2::16, u3::16, u4::48>> = bin

Enum.map_join([<<u0::32>>, <<u1::16>>, <<u2::16>>, <<u3::16>>, <<u4::48>>], <<45>>,
&(Base.encode16(&1, case: :lower)))
Enum.map_join(
[<<u0::32>>, <<u1::16>>, <<u2::16>>, <<u3::16>>, <<u4::48>>],
<<45>>,
&Base.encode16(&1, case: :lower)
)
end
end
Loading