<commit_before>-module(impel_atom).
-export([to_atom/3]).
-export([join/1]).

to_atom(ReqData, State, Entries) ->
    to_atom(ReqData, State, [header(ReqData, State)], Entries).

to_atom(ReqData, State, A, []) ->
    lists:reverse([footer(ReqData, State) | A]);
to_atom(ReqData, State, A, [H | T]) ->
    to_atom(ReqData, State, [entry(ReqData, State, H) | A], T).

header(ReqData, _) ->
    Self = case cowboy_http_req:path_info(ReqData) of
	       {undefined, _} ->
		   <<"nodes">>;
	       {Components, _} ->
		   join(Components)
	   end,
    [<<"<?xml version=\"1.0\" encoding=\"utf-8\"?>">>,
     <<"<feed xmlns=\"http://www.w3.org/2005/Atom\">">>,
     io_lib:format("<title>~s</title>", [Self]),
     io_lib:format("<link href=\"~s~s/~s\" rel=\"self\"/>", ph(ReqData) ++ [Self]),
     io_lib:format("<link href=\"~s~s/\"/>", ph(ReqData))].

entry(ReqData, State, Entry) ->
    Path = case cowboy_http_req:path_info(ReqData) of
	       {undefined, _} ->
		   [];
	       {Components, _} ->
		   Components
	   end,
    entry(ReqData, State, Entry, Path, impel_hierarchy:type(Entry)).

entry(ReqData, _, Entry, Path, branch) ->
    Node = [join(Path ++ [impel_hierarchy:key(Entry)])],
    [<<"<entry>">>,
     title(Entry),
     io_lib:format("<link href=\"~s~s/node/~s\"/>", ph(ReqData) ++ Node),
     io_lib:format("<id>urn:tag:nodes.example.com,~s/~s</id>", [ymd(impel_hierarchy:created(Entry)), impel_hierarchy:key(Entry)]),
     updated(Entry),
     <<"</entry>">>];
entry(ReqData, _, Entry, Path, leaf) ->
    Node = [join(Path ++ [impel_hierarchy:key(Entry)])],
    [<<"<entry>">>,
     title(Entry),
     io_lib:format("<link href=\"~s~s/es/~s\"/>", ph(ReqData) ++ Node),
     io_lib:format("<id>urn:tag:nodes.example.com,~s/~s</id>", [ymd(impel_hierarchy:created(Entry)), Node]),
     updated(Entry),
     <<"</entry>">>].

footer(_, _) ->
    [<<"</feed>">>].

join(Items) ->
    join(Items, <<"/">>).

join([H1, H2 | T], Separator) ->
    [H1, Separator | join([H2 | T], Separator)];
join([H | T], Separator) ->
    [H | join(T, Separator)];
join([], _) ->
    [].



ph(ReqData) ->
    [protocol(ReqData), host(ReqData)].

title(L) ->
    io_lib:format("<title>~s</title>", [impel_hierarchy:key(L)]).

updated(L) ->
    [<<"<updated>">>,
     rfc3339(impel_hierarchy:updated(L)),
     <<"</updated>">>].

<commit_msg>include the media type in entries in the ATOM response.<commit_after>-module(impel_atom).
-export([to_atom/3]).
-export([join/1]).

to_atom(ReqData, State, Entries) ->
    to_atom(ReqData, State, [header(ReqData, State)], Entries).

to_atom(ReqData, State, A, []) ->
    lists:reverse([footer(ReqData, State) | A]);
to_atom(ReqData, State, A, [H | T]) ->
    to_atom(ReqData, State, [entry(ReqData, State, H) | A], T).

header(ReqData, _) ->
    Self = case cowboy_http_req:path_info(ReqData) of
	       {undefined, _} ->
		   <<"nodes">>;
	       {Components, _} ->
		   join(Components)
	   end,
    [<<"<?xml version=\"1.0\" encoding=\"utf-8\"?>">>,
     <<"<feed xmlns=\"http://www.w3.org/2005/Atom\">">>,
     io_lib:format("<title>~s</title>", [Self]),
     io_lib:format("<link href=\"~s~s/~s\" rel=\"self\"/>", ph(ReqData) ++ [Self]),
     io_lib:format("<link href=\"~s~s/\"/>", ph(ReqData))].

entry(ReqData, State, Entry) ->
    Path = case cowboy_http_req:path_info(ReqData) of
	       {undefined, _} ->
		   [];
	       {Components, _} ->
		   Components
	   end,
    entry(ReqData, State, Entry, Path, impel_hierarchy:type(Entry)).

entry(ReqData, _, Entry, Path, branch) ->
    Node = [join(Path ++ [impel_hierarchy:key(Entry)])],
    [<<"<entry>">>,
     title(Entry),
     io_lib:format("<link href=\"~s~s/node/~s\" type=\"application/atom+xml\"/>", ph(ReqData) ++ Node),
     io_lib:format("<id>urn:tag:nodes.example.com,~s/~s</id>", [ymd(impel_hierarchy:created(Entry)), impel_hierarchy:key(Entry)]),
     updated(Entry),
     <<"</entry>">>];
entry(ReqData, _, Entry, Path, leaf) ->
    Node = [join(Path ++ [impel_hierarchy:key(Entry)])],
    [<<"<entry>">>,
     title(Entry),
     io_lib:format("<link href=\"~s~s/es/~s\" type=\"text/event-stream\"/>", ph(ReqData) ++ Node),
     io_lib:format("<id>urn:tag:nodes.example.com,~s/~s</id>", [ymd(impel_hierarchy:created(Entry)), Node]),
     updated(Entry),
     <<"</entry>">>].

footer(_, _) ->
    [<<"</feed>">>].

join(Items) ->
    join(Items, <<"/">>).

join([H1, H2 | T], Separator) ->
    [H1, Separator | join([H2 | T], Separator)];
join([H | T], Separator) ->
    [H | join(T, Separator)];
join([], _) ->
    [].



ph(ReqData) ->
    [protocol(ReqData), host(ReqData)].

title(L) ->
    io_lib:format("<title>~s</title>", [impel_hierarchy:key(L)]).

updated(L) ->
    [<<"<updated>">>,
     rfc3339(impel_hierarchy:updated(L)),
     <<"</updated>">>].


{'fraction_non_alphanumeric': 0.25424439320897085, 'fraction_numerical': 0.007545587927059317, 'mean_word_length': 3.66015625, 'pattern_counts': {'":': 0, '<': 89, '<?xml version=': 2, '>': 127, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 5}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3993061', 'n_tokens_mistral': 1931, 'n_tokens_neox': 1861, 'n_words': 283}