Current section
Files
Jump to
Current section
Files
src/inlay@pixelfed.erl
-module(inlay@pixelfed).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/inlay/pixelfed.gleam").
-export([detect/2, render/2]).
-file("src/inlay/pixelfed.gleam", 78).
-spec detect_pixelfed(binary(), gleam@uri:uri()) -> gleam@option:option(inlay@embed:embed()).
detect_pixelfed(Server, Url) ->
case gleam@uri:path_segments(erlang:element(6, Url)) of
[<<"p"/utf8>>, User, Id] ->
{some, {pixelfed_post, Server, User, Id}};
_ ->
none
end.
-file("src/inlay/pixelfed.gleam", 9).
-spec detect(gleam@uri:uri(), inlay@embed:pixelfed_config()) -> gleam@option:option(inlay@embed:embed()).
detect(Url, Config) ->
case erlang:element(4, Url) of
{some, Host} ->
case gleam@list:contains(erlang:element(2, Config), Host) of
true ->
detect_pixelfed(Host, Url);
false ->
none
end;
none ->
none
end.
-file("src/inlay/pixelfed.gleam", 92).
-spec layout_to_string(inlay@embed:pixelfed_layout()) -> binary().
layout_to_string(Layout) ->
case Layout of
{full, _, _} ->
<<"full"/utf8>>;
compact ->
<<"compact"/utf8>>
end.
-file("src/inlay/pixelfed.gleam", 85).
-spec bool_to_string(boolean()) -> binary().
bool_to_string(Value) ->
case Value of
true ->
<<"true"/utf8>>;
false ->
<<"false"/utf8>>
end.
-file("src/inlay/pixelfed.gleam", 20).
-spec render(inlay@embed:embed(), inlay@embed:config()) -> {ok,
lustre@vdom@vnode:element(any())} |
{error, nil}.
render(Embed, Config) ->
case Embed of
{pixelfed_post, Server, User, Id} ->
{Caption, Likes, Layout, Width} = case erlang:element(14, Config) of
{some, {pixelfed_config, _, {full, C, L}, W}} ->
{C, L, {full, C, L}, gleam@option:unwrap(W, 400)};
{some, {pixelfed_config, _, compact, W@1}} ->
{false, false, compact, gleam@option:unwrap(W@1, 400)};
none ->
{true, true, {full, true, true}, 400}
end,
Caption_str = bool_to_string(Caption),
Likes_str = bool_to_string(Likes),
Layout_str = layout_to_string(Layout),
Src = <<<<<<<<<<<<<<<<<<<<<<"https://"/utf8, Server/binary>>/binary,
"/p/"/utf8>>/binary,
User/binary>>/binary,
"/"/utf8>>/binary,
Id/binary>>/binary,
"/embed?caption="/utf8>>/binary,
Caption_str/binary>>/binary,
"&likes="/utf8>>/binary,
Likes_str/binary>>/binary,
"&layout="/utf8>>/binary,
Layout_str/binary>>,
{ok,
lustre@element@html:'div'(
[],
[lustre@element@html:iframe(
[lustre@attribute:attribute(
<<"title"/utf8>>,
<<"Pixelfed Post Embed"/utf8>>
),
lustre@attribute:src(Src),
lustre@attribute:class(
<<"pixelfed__embed"/utf8>>
),
lustre@attribute:styles(
[{<<"max-width"/utf8>>, <<"100%"/utf8>>},
{<<"border"/utf8>>, <<"0"/utf8>>}]
),
lustre@attribute:width(Width),
lustre@attribute:attribute(
<<"allowfullscreen"/utf8>>,
<<"allowfullscreen"/utf8>>
)]
),
lustre@element@html:script(
[lustre@attribute:src(
<<<<"https://"/utf8, Server/binary>>/binary,
"/embed.js"/utf8>>
),
lustre@attribute:attribute(
<<"async"/utf8>>,
<<"true"/utf8>>
),
lustre@attribute:attribute(
<<"defer"/utf8>>,
<<"true"/utf8>>
)],
<<""/utf8>>
)]
)};
_ ->
{error, nil}
end.