11defmodule JolaDev.OGImage.Renderer do
22 @ moduledoc """
3- Pure image-rendering primitives for OGImage. Kept in a sibling module so
4- that `JolaDev.OGImage` can call into it from a compile-time module
5- attribute (Elixir module attributes can't call functions defined in the
6- same module they're being compiled into).
7-
8- Requires Inter installed system-wide so fontconfig can find it
9- (`brew install --cask font-inter` on macOS, `fonts-inter` apt package
10- in the Docker builder stage).
3+ Pure image-rendering primitives for OGImage.
114 """
125
13- alias JolaDev.Blog
14-
156 @ width 1200
167 @ height 630
178 @ padding 80
@@ -30,57 +21,12 @@ defmodule JolaDev.OGImage.Renderer do
3021
3122 @ logo_path "priv/static/images/logo.png"
3223
33- @ static_content % {
34- "home" =>
35- { "Johanna Larsson" ,
36- "Software engineer, engineering leader, writer, and speaker with many years of experience building products and leading teams." } ,
37- "about" =>
38- { "About" ,
39- "About Johanna Larsson: software engineer, engineering leader, writer, and speaker with many years of experience." } ,
40- "projects" =>
41- { "Projects" ,
42- "Open source projects by Johanna Larsson, including HexDiff, ElixirEvents, and more." } ,
43- "talks" =>
44- { "Talks" ,
45- "Conference talks and presentations by Johanna Larsson on Elixir, distributed systems, and engineering leadership." } ,
46- "posts" =>
47- { "Blog" ,
48- "Blog posts by Johanna Larsson on software engineering, Elixir, and engineering leadership." }
49- }
50-
5124 def generate_bytes ( title , description ) when is_binary ( title ) and is_binary ( description ) do
5225 title
5326 |> build_canvas ( description )
5427 |> Image . write! ( :memory , suffix: ".png" )
5528 end
5629
57- def content_for ( slug ) when is_map_key ( @ static_content , slug ) ,
58- do: Map . fetch! ( @ static_content , slug )
59-
60- def content_for ( "posts/tag/" <> tag ) do
61- if tag in Blog . all_tags ( ) do
62- { ~s( Posts tagged "#{ tag } ") , "Blog posts by Johanna Larsson tagged with #{ tag } ." }
63- else
64- :error
65- end
66- end
67-
68- def content_for ( "posts/" <> id ) do
69- case Blog . find_by_id ( id ) do
70- nil -> :error
71- post -> { post . title , post . description }
72- end
73- end
74-
75- def content_for ( _ ) , do: :error
76-
77- def all_slugs do
78- static = Map . keys ( @ static_content )
79- posts = Enum . map ( Blog . all_posts ( ) , & "posts/#{ & 1 . id } " )
80- tags = Enum . map ( Blog . all_tags ( ) , & "posts/tag/#{ & 1 } " )
81- static ++ posts ++ tags
82- end
83-
8430 defp build_canvas ( title , description ) do
8531 canvas = Image . new! ( @ width , @ height , color: @ background )
8632
0 commit comments