Current section

Files

Jump to
webls src webls@rss.erl
Raw

src/webls@rss.erl

-module(webls@rss).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([to_string/1, channel/3, with_channel_language/2, with_channel_copyright/2, with_channel_managing_editor/2, with_channel_web_master/2, with_channel_pub_date/2, with_channel_last_build_date/2, with_channel_category/2, with_channel_categories/2, with_channel_custom_generator/2, with_channel_generator/1, with_channel_docs/1, with_channel_cloud/2, with_channel_ttl/2, with_channel_image/2, with_channel_text_input/2, with_channel_skip_hours/2, with_channel_skip_days/2, with_channel_items/2, with_channel_item/2, item/2, with_item_link/2, with_item_author/2, with_item_categories/2, with_item_comments/2, with_item_enclosure/2, with_item_guid/2, with_item_pub_date/2, with_item_source/2]).
-export_type([rss_channel/0, image/0, cloud/0, text_input/0, enclosure/0, rss_item/0]).
-type rss_channel() :: {rss_channel,
binary(),
binary(),
binary(),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(birl:time()),
gleam@option:option(birl:time()),
list(binary()),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(cloud()),
gleam@option:option(integer()),
gleam@option:option(image()),
gleam@option:option(text_input()),
list(integer()),
list(birl:weekday()),
list(rss_item())}.
-type image() :: {image,
binary(),
binary(),
binary(),
gleam@option:option(binary()),
gleam@option:option(integer()),
gleam@option:option(integer())}.
-type cloud() :: {cloud, binary(), integer(), binary(), binary(), binary()}.
-type text_input() :: {text_input, binary(), binary(), binary(), binary()}.
-type enclosure() :: {enclosure, binary(), integer(), binary()}.
-type rss_item() :: {rss_item,
binary(),
binary(),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(binary()),
gleam@option:option(birl:time()),
list(binary()),
gleam@option:option(enclosure()),
gleam@option:option({binary(), gleam@option:option(boolean())})}.
-spec rss_item_to_string(rss_item()) -> binary().
rss_item_to_string(Item) ->
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"<item>\n"/utf8, "<title>"/utf8>>/binary,
(erlang:element(
2,
Item
))/binary>>/binary,
"</title>\n"/utf8>>/binary,
"<description>"/utf8>>/binary,
(erlang:element(3, Item))/binary>>/binary,
"</description>\n"/utf8>>/binary,
(case erlang:element(4, Item) of
{some, Link} ->
<<<<"<link>"/utf8, Link/binary>>/binary,
"</link>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case erlang:element(5, Item) of
{some, Author} ->
<<<<"<author>"/utf8, Author/binary>>/binary,
"</author>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case erlang:element(6, Item) of
{some, Comments} ->
<<<<"<comments>"/utf8, Comments/binary>>/binary,
"</comments>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case erlang:element(7, Item) of
{some, Source} ->
<<<<"<source>"/utf8, Source/binary>>/binary,
"</source>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case erlang:element(8, Item) of
{some, Pub_date} ->
<<<<"<pubDate>"/utf8,
(begin
_pipe = Pub_date,
birl:to_iso8601(_pipe)
end)/binary>>/binary,
"</pubDate>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(begin
_pipe@1 = erlang:element(9, Item),
_pipe@2 = gleam@list:map(
_pipe@1,
fun(Category) ->
<<<<"<category>"/utf8, Category/binary>>/binary,
"</category>\n"/utf8>>
end
),
_pipe@3 = gleam@list:reduce(
_pipe@2,
fun(Acc, Category@1) ->
<<Acc/binary, Category@1/binary>>
end
),
gleam@result:unwrap(_pipe@3, <<""/utf8>>)
end)/binary>>/binary,
(case erlang:element(10, Item) of
{some, Enclosure} ->
<<<<<<<<<<<<"<enclosure url=\""/utf8,
(erlang:element(2, Enclosure))/binary>>/binary,
"\" length=\""/utf8>>/binary,
(gleam@int:to_string(
erlang:element(3, Enclosure)
))/binary>>/binary,
"\" type=\""/utf8>>/binary,
(erlang:element(4, Enclosure))/binary>>/binary,
"\"/>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case erlang:element(11, Item) of
{some, Guid} ->
case Guid of
{Guid@1, {some, Is_permalink}} ->
<<<<<<<<"<guid isPermaLink=\""/utf8,
(case Is_permalink of
true ->
<<"true"/utf8>>;
false ->
<<"false"/utf8>>
end)/binary>>/binary,
"\">"/utf8>>/binary,
Guid@1/binary>>/binary,
"</guid>\n"/utf8>>;
_ ->
<<""/utf8>>
end;
_ ->
<<""/utf8>>
end)/binary>>/binary,
"</item>"/utf8>>.
-spec rss_channel_to_string(rss_channel()) -> binary().
rss_channel_to_string(Channel) ->
Channel_items = begin
_pipe = erlang:element(20, Channel),
_pipe@2 = gleam@list:map(_pipe, fun(Rss_item) -> _pipe@1 = Rss_item,
rss_item_to_string(_pipe@1) end),
_pipe@3 = gleam@list:reduce(
_pipe@2,
fun(Acc, Rss_item_string) ->
<<<<Acc/binary, "\n"/utf8>>/binary, Rss_item_string/binary>>
end
),
gleam@result:unwrap(_pipe@3, <<""/utf8>>)
end,
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"\n<channel>\n"/utf8,
"<title>"/utf8>>/binary,
(erlang:element(
2,
Channel
))/binary>>/binary,
"</title>\n"/utf8>>/binary,
"<link>"/utf8>>/binary,
(erlang:element(
3,
Channel
))/binary>>/binary,
"</link>\n"/utf8>>/binary,
"<description>"/utf8>>/binary,
(erlang:element(
4,
Channel
))/binary>>/binary,
"</description>\n"/utf8>>/binary,
(case erlang:element(
5,
Channel
) of
{some,
Language} ->
<<<<"<language>"/utf8,
Language/binary>>/binary,
"</language>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case erlang:element(
6,
Channel
) of
{some,
Copyright} ->
<<<<"<copyright>"/utf8,
Copyright/binary>>/binary,
"</copyright>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case erlang:element(
7,
Channel
) of
{some,
Managing_editor} ->
<<<<"<managingEditor>"/utf8,
Managing_editor/binary>>/binary,
"</managingEditor>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case erlang:element(
8,
Channel
) of
{some,
Web_master} ->
<<<<"<webMaster>"/utf8,
Web_master/binary>>/binary,
"</webMaster>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case erlang:element(
9,
Channel
) of
{some, Pub_date} ->
<<<<"<pubDate>"/utf8,
(begin
_pipe@4 = Pub_date,
birl:to_iso8601(
_pipe@4
)
end)/binary>>/binary,
"</pubDate>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case erlang:element(
10,
Channel
) of
{some, Last_build_date} ->
<<<<"<lastBuildDate>"/utf8,
(begin
_pipe@5 = Last_build_date,
birl:to_iso8601(
_pipe@5
)
end)/binary>>/binary,
"</lastBuildDate>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(begin
_pipe@6 = erlang:element(
11,
Channel
),
_pipe@7 = gleam@list:map(
_pipe@6,
fun(Category) ->
<<<<"<category>"/utf8,
Category/binary>>/binary,
"</category>\n"/utf8>>
end
),
_pipe@8 = gleam@list:reduce(
_pipe@7,
fun(Acc@1, Category@1) ->
<<Acc@1/binary,
Category@1/binary>>
end
),
gleam@result:unwrap(
_pipe@8,
<<""/utf8>>
)
end)/binary>>/binary,
(case erlang:element(12, Channel) of
{some, Generator} ->
<<<<"<generator>"/utf8,
Generator/binary>>/binary,
"</generator>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case erlang:element(13, Channel) of
{some, Docs} ->
<<<<"<docs>"/utf8, Docs/binary>>/binary,
"</docs>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case erlang:element(14, Channel) of
{some, Cloud} ->
<<<<<<<<<<<<<<<<<<<<"<cloud domain=\""/utf8,
(erlang:element(
2,
Cloud
))/binary>>/binary,
"\" port=\""/utf8>>/binary,
(gleam@int:to_string(
erlang:element(
3,
Cloud
)
))/binary>>/binary,
"\" path=\""/utf8>>/binary,
(erlang:element(
4,
Cloud
))/binary>>/binary,
"\" registerProcedure=\""/utf8>>/binary,
(erlang:element(
5,
Cloud
))/binary>>/binary,
"\" protocol=\""/utf8>>/binary,
(erlang:element(6, Cloud))/binary>>/binary,
"\"/>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case erlang:element(15, Channel) of
{some, Ttl} ->
<<<<"<ttl>"/utf8,
(gleam@int:to_string(Ttl))/binary>>/binary,
"</ttl>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case erlang:element(16, Channel) of
{some, Image} ->
<<<<<<<<<<<<<<<<<<<<<<<<<<"<image>"/utf8,
"<url>"/utf8>>/binary,
(erlang:element(
2,
Image
))/binary>>/binary,
"</url>\n"/utf8>>/binary,
"<title>"/utf8>>/binary,
(erlang:element(
3,
Image
))/binary>>/binary,
"</title>\n"/utf8>>/binary,
"<link>"/utf8>>/binary,
(erlang:element(
4,
Image
))/binary>>/binary,
"</link>\n"/utf8>>/binary,
(case erlang:element(
5,
Image
) of
{some, Description} ->
<<<<"<description>"/utf8,
Description/binary>>/binary,
"</description>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case erlang:element(6, Image) of
{some, Width} ->
<<<<"<width>"/utf8,
(gleam@int:to_string(
Width
))/binary>>/binary,
"</width>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case erlang:element(7, Image) of
{some, Height} ->
<<<<"<height>"/utf8,
(gleam@int:to_string(
Height
))/binary>>/binary,
"</height>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
"</image>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case erlang:element(17, Channel) of
{some, Text_input} ->
<<<<<<<<<<<<<<<<<<<<<<<<<<"<textInput>"/utf8,
"<title>"/utf8>>/binary,
(erlang:element(
2,
Text_input
))/binary>>/binary,
"</title>\n"/utf8>>/binary,
"<description>"/utf8>>/binary,
(erlang:element(
3,
Text_input
))/binary>>/binary,
"</description>\n"/utf8>>/binary,
"<name>"/utf8>>/binary,
(erlang:element(
4,
Text_input
))/binary>>/binary,
"</name>\n"/utf8>>/binary,
"<link>"/utf8>>/binary,
(erlang:element(5, Text_input))/binary>>/binary,
"</link>\n"/utf8>>/binary,
"</textInput>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case begin
_pipe@9 = erlang:element(18, Channel),
erlang:length(_pipe@9)
end
> 0 of
true ->
<<<<"<skipHours>"/utf8,
(begin
_pipe@10 = gleam@list:map(
erlang:element(18, Channel),
fun(Hour) ->
<<<<"<hour>"/utf8,
(gleam@int:to_string(
Hour
))/binary>>/binary,
"</hour>"/utf8>>
end
),
_pipe@11 = gleam@list:reduce(
_pipe@10,
fun(Acc@2, Hour@1) ->
<<<<Acc@2/binary, "\n"/utf8>>/binary,
Hour@1/binary>>
end
),
gleam@result:unwrap(
_pipe@11,
<<""/utf8>>
)
end)/binary>>/binary,
"</skipHours>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
(case begin
_pipe@12 = erlang:element(19, Channel),
erlang:length(_pipe@12)
end
> 0 of
true ->
<<<<"<skipDays>"/utf8,
(begin
_pipe@14 = gleam@list:map(
erlang:element(19, Channel),
fun(Day) ->
<<<<"<day>"/utf8,
(begin
_pipe@13 = Day,
birl:weekday_to_string(
_pipe@13
)
end)/binary>>/binary,
"</day>"/utf8>>
end
),
_pipe@15 = gleam@list:reduce(
_pipe@14,
fun(Acc@3, Day@1) ->
<<<<Acc@3/binary, "\n"/utf8>>/binary,
Day@1/binary>>
end
),
gleam@result:unwrap(_pipe@15, <<""/utf8>>)
end)/binary>>/binary,
"</skipDays>\n"/utf8>>;
_ ->
<<""/utf8>>
end)/binary>>/binary,
Channel_items/binary>>/binary,
"</channel>"/utf8>>.
-spec to_string(list(rss_channel())) -> binary().
to_string(Channels) ->
Channel_content = begin
_pipe = Channels,
_pipe@2 = gleam@list:map(_pipe, fun(Channel) -> _pipe@1 = Channel,
rss_channel_to_string(_pipe@1) end),
_pipe@3 = gleam@list:reduce(
_pipe@2,
fun(Acc, Channel_string) ->
<<<<Acc/binary, "\n"/utf8>>/binary, Channel_string/binary>>
end
),
gleam@result:unwrap(_pipe@3, <<""/utf8>>)
end,
<<<<"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rss version=\"2.0.1\">"/utf8,
Channel_content/binary>>/binary,
"\n</rss>"/utf8>>.
-spec channel(binary(), binary(), binary()) -> rss_channel().
channel(Title, Description, Link) ->
{rss_channel,
Title,
Link,
Description,
none,
none,
none,
none,
none,
none,
[],
none,
none,
none,
none,
none,
none,
[],
[],
[]}.
-spec with_channel_language(rss_channel(), binary()) -> rss_channel().
with_channel_language(Channel, Language) ->
erlang:setelement(5, Channel, {some, Language}).
-spec with_channel_copyright(rss_channel(), binary()) -> rss_channel().
with_channel_copyright(Channel, Copyright) ->
erlang:setelement(6, Channel, {some, Copyright}).
-spec with_channel_managing_editor(rss_channel(), binary()) -> rss_channel().
with_channel_managing_editor(Channel, Managing_editor) ->
erlang:setelement(7, Channel, {some, Managing_editor}).
-spec with_channel_web_master(rss_channel(), binary()) -> rss_channel().
with_channel_web_master(Channel, Web_master) ->
erlang:setelement(8, Channel, {some, Web_master}).
-spec with_channel_pub_date(rss_channel(), birl:time()) -> rss_channel().
with_channel_pub_date(Channel, Pub_date) ->
erlang:setelement(9, Channel, {some, Pub_date}).
-spec with_channel_last_build_date(rss_channel(), birl:time()) -> rss_channel().
with_channel_last_build_date(Channel, Last_build_date) ->
erlang:setelement(10, Channel, {some, Last_build_date}).
-spec with_channel_category(rss_channel(), binary()) -> rss_channel().
with_channel_category(Channel, Category) ->
erlang:setelement(11, Channel, [Category | erlang:element(11, Channel)]).
-spec with_channel_categories(rss_channel(), list(binary())) -> rss_channel().
with_channel_categories(Channel, Categories) ->
erlang:setelement(
11,
Channel,
gleam@list:concat([erlang:element(11, Channel), Categories])
).
-spec with_channel_custom_generator(rss_channel(), binary()) -> rss_channel().
with_channel_custom_generator(Channel, Generator) ->
erlang:setelement(12, Channel, {some, Generator}).
-spec with_channel_generator(rss_channel()) -> rss_channel().
with_channel_generator(Channel) ->
with_channel_custom_generator(Channel, <<"webls"/utf8>>).
-spec with_channel_docs(rss_channel()) -> rss_channel().
with_channel_docs(Channel) ->
erlang:setelement(
13,
Channel,
{some, <<"https://www.rssboard.org/rss-2-0-1"/utf8>>}
).
-spec with_channel_cloud(rss_channel(), cloud()) -> rss_channel().
with_channel_cloud(Channel, Cloud) ->
erlang:setelement(14, Channel, {some, Cloud}).
-spec with_channel_ttl(rss_channel(), integer()) -> rss_channel().
with_channel_ttl(Channel, Ttl) ->
erlang:setelement(15, Channel, {some, Ttl}).
-spec with_channel_image(rss_channel(), image()) -> rss_channel().
with_channel_image(Channel, Image) ->
erlang:setelement(16, Channel, {some, Image}).
-spec with_channel_text_input(rss_channel(), text_input()) -> rss_channel().
with_channel_text_input(Channel, Text_input) ->
erlang:setelement(17, Channel, {some, Text_input}).
-spec with_channel_skip_hours(rss_channel(), list(integer())) -> rss_channel().
with_channel_skip_hours(Channel, Skip_hours) ->
erlang:setelement(18, Channel, Skip_hours).
-spec with_channel_skip_days(rss_channel(), list(birl:weekday())) -> rss_channel().
with_channel_skip_days(Channel, Skip_days) ->
erlang:setelement(19, Channel, Skip_days).
-spec with_channel_items(rss_channel(), list(rss_item())) -> rss_channel().
with_channel_items(Channel, Items) ->
erlang:setelement(
20,
Channel,
gleam@list:concat([erlang:element(20, Channel), Items])
).
-spec with_channel_item(rss_channel(), rss_item()) -> rss_channel().
with_channel_item(Channel, Item) ->
erlang:setelement(20, Channel, [Item | erlang:element(20, Channel)]).
-spec item(binary(), binary()) -> rss_item().
item(Title, Description) ->
{rss_item, Title, Description, none, none, none, none, none, [], none, none}.
-spec with_item_link(rss_item(), binary()) -> rss_item().
with_item_link(Item, Link) ->
erlang:setelement(4, Item, {some, Link}).
-spec with_item_author(rss_item(), binary()) -> rss_item().
with_item_author(Item, Author) ->
erlang:setelement(5, Item, {some, Author}).
-spec with_item_categories(rss_item(), list(binary())) -> rss_item().
with_item_categories(Item, Categories) ->
erlang:setelement(9, Item, Categories).
-spec with_item_comments(rss_item(), binary()) -> rss_item().
with_item_comments(Item, Comments) ->
erlang:setelement(6, Item, {some, Comments}).
-spec with_item_enclosure(rss_item(), enclosure()) -> rss_item().
with_item_enclosure(Item, Enclosure) ->
erlang:setelement(10, Item, {some, Enclosure}).
-spec with_item_guid(rss_item(), {binary(), gleam@option:option(boolean())}) -> rss_item().
with_item_guid(Item, Guid) ->
erlang:setelement(11, Item, {some, Guid}).
-spec with_item_pub_date(rss_item(), birl:time()) -> rss_item().
with_item_pub_date(Item, Pub_date) ->
erlang:setelement(8, Item, {some, Pub_date}).
-spec with_item_source(rss_item(), binary()) -> rss_item().
with_item_source(Item, Source) ->
erlang:setelement(7, Item, {some, Source}).