Current section

Files

Jump to
webls src webls@sitemap.erl
Raw

src/webls@sitemap.erl

-module(webls@sitemap).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([to_string/1, item/1, with_frequency/2, with_priority/2, with_modified/2]).
-export_type([sitemap/0, sitemap_item/0, change_frequency/0]).
-type sitemap() :: {sitemap,
binary(),
gleam@option:option(birl:time()),
list(sitemap_item())}.
-type sitemap_item() :: {sitemap_item,
binary(),
gleam@option:option(birl:time()),
gleam@option:option(change_frequency()),
gleam@option:option(float())}.
-type change_frequency() :: always |
hourly |
daily |
weekly |
monthly |
yearly |
never.
-spec sitemap_item_to_string(sitemap_item()) -> binary().
sitemap_item_to_string(Item) ->
<<<<<<<<<<<<<<"<url>\n"/utf8, "<loc>"/utf8>>/binary,
(erlang:element(2, Item))/binary>>/binary,
"</loc>\n"/utf8>>/binary,
(case erlang:element(3, Item) of
{some, Date} ->
<<<<"<lastmod>"/utf8,
(begin
_pipe = Date,
birl:to_iso8601(_pipe)
end)/binary>>/binary,
"</lastmod>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case erlang:element(4, Item) of
{some, Freq} ->
<<<<"<changefreq>"/utf8, (case Freq of
always ->
<<"always"/utf8>>;
hourly ->
<<"hourly"/utf8>>;
daily ->
<<"daily"/utf8>>;
weekly ->
<<"weekly"/utf8>>;
monthly ->
<<"monthly"/utf8>>;
yearly ->
<<"yearly"/utf8>>;
never ->
<<"never"/utf8>>
end)/binary>>/binary, "</changefreq>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case erlang:element(5, Item) of
{some, Priority} ->
<<<<"<priority>"/utf8,
(begin
_pipe@1 = Priority,
_pipe@2 = gleam@float:clamp(_pipe@1, +0.0, 1.0),
gleam@float:to_string(_pipe@2)
end)/binary>>/binary,
"</priority>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
"</url>"/utf8>>.
-spec to_string(sitemap()) -> binary().
to_string(Sitemap) ->
Channel_content = begin
_pipe = erlang:element(4, Sitemap),
_pipe@2 = gleam@list:map(_pipe, fun(Item) -> _pipe@1 = Item,
sitemap_item_to_string(_pipe@1) end),
_pipe@3 = gleam@list:reduce(
_pipe@2,
fun(Acc, Item_string) ->
<<<<Acc/binary, "\n"/utf8>>/binary, Item_string/binary>>
end
),
gleam@result:unwrap(_pipe@3, <<""/utf8>>)
end,
<<<<"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n"/utf8,
Channel_content/binary>>/binary,
"\n</urlset>"/utf8>>.
-spec item(binary()) -> sitemap_item().
item(Loc) ->
{sitemap_item, Loc, none, none, none}.
-spec with_frequency(sitemap_item(), change_frequency()) -> sitemap_item().
with_frequency(Item, Frequency) ->
erlang:setelement(4, Item, {some, Frequency}).
-spec with_priority(sitemap_item(), float()) -> sitemap_item().
with_priority(Item, Priority) ->
erlang:setelement(5, Item, {some, Priority}).
-spec with_modified(sitemap_item(), birl:time()) -> sitemap_item().
with_modified(Item, Modified) ->
erlang:setelement(3, Item, {some, Modified}).