-module(calendar_html_renderer). -include("records.hrl"). -export([render_month/3, init_cache/0]). init_cache() -> case ets:info(archive_html_cache) of undefined -> ets:new(archive_html_cache, [set, public, named_table, {keypos, 1}]); _ -> ok end. render_month(Year, Month, Events) -> Key = {Year, Month}, try ets:lookup(archive_html_cache, Key) of [{Key, Html}] -> Html; [] -> Html = generate_html(Year, Month, Events), ets:insert(archive_html_cache, {Key, Html}), Html catch _:_ -> generate_html(Year, Month, Events) end. generate_html(Year, Month, Events) -> DaysInMonth = calendar:last_day_of_the_month(Year, Month), EventsByDay = group_events_by_day(Events), DayList = lists:seq(1, DaysInMonth), DayCells = lists:map(fun(D) -> DayEvents = maps:get(D, EventsByDay, []), ["", integer_to_list(D), format_events(DayEvents), ""] end, DayList), ["", "", DayCells, "", "
"]. group_events_by_day(Events) -> lists:foldl(fun(Evt, Acc) -> case Evt of #event{start_time = {{_, _, Day}, {_, _, _}}} -> maps:update_with(Day, fun(List) -> [Evt | List] end, [Evt], Acc); _ -> Acc end end, #{}, Events). format_events(Events) -> case Events of [] -> []; _ -> [""] end.