%% This Source Code Form is subject to the terms of the Mozilla Public %% License, v. 2.0. If a copy of the MPL was not distributed with this %% file, You can obtain one at https://mozilla.org/MPL/2.0/. %% %% Copyright (c) 2021-2022 VMware, Inc. or its affiliates. All rights reserved. %% Copyright (c) 2021-2022 Viacheslav Katsuba. %% -module(rebar3_edoc_extensions_wrapper). -include_lib("edoc/include/edoc_doclet.hrl"). -include("rebar3_edoc_extensions.hrl"). -export([module/2, overview/2, type/1]). -export([run/2]). -spec module(term(), term()) -> term(). module(Element, Options) -> edoc_layout:module(Element, Options). -spec overview(term(), term()) -> [binary() | list()]. overview(Element, Options) -> Overview = edoc_layout:overview(Element, Options), patch_html(Overview). -spec type(term()) -> term(). type(Element) -> edoc_layout:type(Element). -spec run(#doclet_gen{}, tuple()) -> ok | no_return(). run(#doclet_gen{app = App} = Cmd, Ctxt) -> ok = edoc_doclet:run(Cmd, Ctxt), %% Ctxt is a #context{} record in Erlang 23 and #doclet_context{} in Erlang %% 24. The directory is the second field in that record in both cases. Dir = element(2, Ctxt), File = filename:join(Dir, "modules-frame.html"), {ok, Content0} = file:read_file(File), Content1 = add_toc(App, Content0, Dir), Content2 = patch_html(Content1), case file:write_file(File, Content2) of ok -> ok; {error, Reason} -> exit({error, Reason}) end. -spec patch_html(list()) -> list(). patch_html(Html) -> Html2 = re:replace( Html, "
", "\n" ?SYNTAX_HIGHLIGHTING_JS, [{return, list}]), Html3 = re:replace( Html2, "(.*)", "
\\1",
[{return, list}, ungreedy, dotall, global]),
Html4 = re:replace(
Html3,
" +(" ?LANG_REGEX ")(?:\n)(.*)",
"\\2",
[{return, list}, ungreedy, dotall, global]),
Html4.
-spec add_toc(term(), binary(), list()) -> list().
add_toc(App, Html, Dir) ->
case generate_toc(Dir, App) of
undefined ->
Html;
Toc ->
re:replace(
Html,
"(