Packages

A sitemap XML builder for Gleam

Current section

Files

Jump to
sitemapb src sitemapb.erl
Raw

src/sitemapb.erl

-module(sitemapb).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/sitemapb.gleam").
-export([render/1, render_basic/1]).
-export_type([url/0, change_frequency/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(
" Sitemap XML!\n"
" <https://www.sitemaps.org/protocol.html>\n"
).
-type url() :: {url,
binary(),
gleam@option:option(gleam@time@timestamp:timestamp()),
gleam@option:option(change_frequency()),
gleam@option:option(float())}.
-type change_frequency() :: always |
hourly |
daily |
weekly |
monthly |
yearly |
never.
-file("src/sitemapb.gleam", 118).
-spec render_priority(float()) -> xmb:xml().
render_priority(Priority) ->
_pipe = Priority,
_pipe@1 = gleam@float:clamp(_pipe, +0.0, 1.0),
_pipe@2 = gleam_stdlib:float_to_string(_pipe@1),
xmb:text(_pipe@2).
-file("src/sitemapb.gleam", 141).
-spec 'maybe'(binary(), gleam@option:option(DYW), fun((DYW) -> xmb:xml())) -> xmb:xml().
'maybe'(Tag, Item, Render) ->
case Item of
{some, Value} ->
xmb:x(Tag, [], [Render(Value)]);
none ->
xmb:nothing()
end.
-file("src/sitemapb.gleam", 129).
-spec render_change_frequency(change_frequency()) -> xmb:xml().
render_change_frequency(Frequency) ->
xmb:text(case Frequency of
always ->
<<"always"/utf8>>;
hourly ->
<<"hourly"/utf8>>;
daily ->
<<"daily"/utf8>>;
weekly ->
<<"weekly"/utf8>>;
monthly ->
<<"monthly"/utf8>>;
yearly ->
<<"yearly"/utf8>>;
never ->
<<"never"/utf8>>
end).
-file("src/sitemapb.gleam", 125).
-spec render_timestamp(gleam@time@timestamp:timestamp()) -> xmb:xml().
render_timestamp(Timestamp) ->
xmb:text(gleam@time@timestamp:to_rfc3339(Timestamp, {duration, 0, 0})).
-file("src/sitemapb.gleam", 89).
?DOC(
" Render an XML sitemap.\n"
"\n"
" This function lets you specify all the properties of each page.\n"
).
-spec render(list(url())) -> gleam@string_tree:string_tree().
render(Site) ->
Url@1 = fun(Url) ->
{url, Location, Last_modified, Change_frequency, Priority} = Url,
xmb:x(
<<"url"/utf8>>,
[],
[xmb:x(<<"loc"/utf8>>, [], [xmb:text(Location)]),
'maybe'(
<<"lastmod"/utf8>>,
Last_modified,
fun render_timestamp/1
),
'maybe'(
<<"changefreq"/utf8>>,
Change_frequency,
fun render_change_frequency/1
),
'maybe'(<<"priority"/utf8>>, Priority, fun render_priority/1)]
)
end,
_pipe = xmb:x(
<<"urlset"/utf8>>,
[{<<"xmlns"/utf8>>,
<<"http://www.sitemaps.org/schemas/sitemap/0.9"/utf8>>}],
gleam@list:map(Site, Url@1)
),
_pipe@1 = gleam@list:wrap(_pipe),
xmb:render(_pipe@1).
-file("src/sitemapb.gleam", 110).
?DOC(
" Render an XML sitemap.\n"
"\n"
" This function takes only URLs. Use the `render` function if you want to\n"
" specify the other properties for eaech page.\n"
).
-spec render_basic(list(binary())) -> gleam@string_tree:string_tree().
render_basic(Site) ->
Url@1 = fun(Url) ->
xmb:x(<<"url"/utf8>>, [], [xmb:x(<<"loc"/utf8>>, [], [xmb:text(Url)])])
end,
_pipe = xmb:x(
<<"urlset"/utf8>>,
[{<<"xmlns"/utf8>>,
<<"http://www.sitemaps.org/schemas/sitemap/0.9"/utf8>>}],
gleam@list:map(Site, Url@1)
),
_pipe@1 = gleam@list:wrap(_pipe),
xmb:render(_pipe@1).