Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to expand <head/> with the content of doc/_head.html #26

Merged
merged 1 commit into from
Jun 9, 2022
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
1 change: 1 addition & 0 deletions src/rebar3_edoc_extensions.hrl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ code[class*=\"language-\"] {
}").
-define(SYNTAX_HIGHLIGHTING_JS, "<script src=\"prism.js\"></script>").
-define(LANG_REGEX, "none|elixir|erlang").
-define(HEAD_ADDON_FILENAME, "_head.html").
-define(DEBUG(Str, Args), rebar_log:log(debug, Str, Args)).
-define(INFO(Str, Args), rebar_log:log(info, Str, Args)).
-define(WARN(Str, Args), rebar_log:log(warn, Str, Args)).
Expand Down
11 changes: 11 additions & 0 deletions src/rebar3_edoc_extensions_export.erl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@
xmerl_html:'#root#'(Data, Attrs, [], E).

-spec '#element#'(term(), term(), term(), term(), term()) -> term().
'#element#'(head = Tag, Data, Attrs, Parents, E) ->
%% FIXME: We don't have access to EDoc options here. Therefore we assume
%% that the EDoc directory is `doc' in the current working directory...
{ok, Cwd} = file:get_cwd(),
Dir = filename:join(Cwd, "doc"),
AddonFile = filename:join(Dir, ?HEAD_ADDON_FILENAME),
Data1 = case file:read_file(AddonFile) of
{ok, Addon} -> [Data, Addon];
_ -> Data
end,
xmerl_html:'#element#'(Tag, Data1, Attrs, Parents, E);
'#element#'(body = Tag, Data, _Attrs, Parents, E) ->
Data1 = [?SYNTAX_HIGHLIGHTING_JS,
Data],
Expand Down
48 changes: 42 additions & 6 deletions src/rebar3_edoc_extensions_wrapper.erl
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,45 @@ module(Element, Options) ->

-spec overview(term(), list()) -> [binary() | list()].
overview(Element, Options) ->
Dir = proplists:get_value(dir, Options),
Overview = edoc_layout:overview(Element, Options),
patch_html(Overview).
patch_html(Dir, Overview).

-spec run(#doclet_gen{}, #?RECORD{}) -> ok | no_return().
run(#doclet_gen{app = App} = Cmd, #?RECORD{dir = Dir} = Ctxt) ->
ok = edoc_doclet:run(Cmd, Ctxt),
ok = patch_index(Dir),
ok = patch_modules_frame(App, Dir).

-spec patch_index(Dir) -> ok when
Dir :: file:filename().
patch_index(Dir) ->
File = filename:join(Dir, "index.html"),
{ok, Content0} = file:read_file(File),
Content1 = patch_html(Dir, Content0),
case file:write_file(File, Content1) of
ok -> ok;
{error, Reason} -> exit({error, Reason})
end.

-spec patch_modules_frame(App, Dir) -> ok when
App :: atom(),
Dir :: file:filename().
patch_modules_frame(App, Dir) ->
File = filename:join(Dir, "modules-frame.html"),
{ok, Content0} = file:read_file(File),
Content1 = add_toc(App, Content0, Dir),
Content2 = patch_html(Content1),
Content2 = patch_html(Dir, Content1),
case file:write_file(File, Content2) of
ok -> ok;
{error, Reason} -> exit({error, Reason})
end.

-spec patch_html(list()) -> list().
patch_html(Html) ->
-spec patch_html(file:filename(), list() | binary()) -> list().
patch_html(Dir, Html) ->
Html1 = add_head_addon(Dir, Html),
Html2 = re:replace(
Html,
Html1,
"<body +bgcolor=\"[^\"]*\">",
"<body class=\"" ?BODY_CLASSES "\">\n"
?SYNTAX_HIGHLIGHTING_JS,
Expand All @@ -66,6 +86,22 @@ patch_html(Html) ->
[{return, list}, ungreedy, dotall, global]),
Html4.

-spec add_head_addon(Dir, Html) -> Html when
Dir :: file:filename(),
Html :: list() | binary().
add_head_addon(Dir, Html) ->
AddonFile = filename:join(Dir, ?HEAD_ADDON_FILENAME),
case file:read_file(AddonFile) of
{ok, Addon} ->
re:replace(
Html,
"</head>",
[Addon, "</head>"],
[{return, list}]);
_ ->
Html
end.

-spec add_toc(term(), binary(), list()) -> list().
add_toc(App, Html, Dir) ->
case generate_toc(Dir, App) of
Expand Down Expand Up @@ -133,7 +169,7 @@ generate_toc1([], CurrentLevel, Result, App) ->
"</li>\n",
["</ul>\n" || _ <- lists:seq(0, CurrentLevel - 1)]].

-spec get_overview(list()) -> binary() | undefined.
-spec get_overview(file:filename()) -> binary() | undefined.
get_overview(Dir) ->
OverviewFile = filename:join(Dir, "overview.edoc"),
case file:read_file(OverviewFile) of
Expand Down