Skip to content

Commit

Permalink
Merge pull request #26 from vkatsuba/support-addon-to-head-element
Browse files Browse the repository at this point in the history
Allow to expand <head/> with the content of `doc/_head.html`
  • Loading branch information
dumbbell authored Jun 9, 2022
2 parents 53de439 + e556438 commit 59bed39
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
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

0 comments on commit 59bed39

Please sign in to comment.