Customized Layouts¶
Layouts are written in YAML syntax. Before starting to create a custom layout, it is a good idea
to study the pre-designed layouts, to get a better
understanding of how they work. Then, create a new layout and reference it in the
social_cards
conf.py option (or within the social-card
directive’s argument).
size: { width: 1200, height: 630 }
layers: []
social_cards = { # (1)!
"cards_layout_dir": "layouts",
"cards_layout": "custom"
}
The required
site_url
is elided here for brevity.
.. social-card:: {
"cards_layout_dir" = "layouts",
"cards_layout" = "custom",
}
Card Layout Options¶
- class Cards_Layout_Options¶
There are some options that are used as default values for the layout’s subsequent layers. These values are set with
cards_layout_options
and are added to thelayout.*
jinja context (for customizable re-use inlayer
attributes).Seealso
This section heavily relies on knowledge about Using Jinja Syntax within the Layout
- background_image : Annotated[str | Path, AfterValidator(func=_validate_path)] | None¶
The fallback value used for a layer’s
background.image
attribute. Default isNone
. This image will not be shown if thebackground_color
has no alpha channel (transparency) value.
- background_color : Color | Linear_Gradient | Radial_Gradient | Conical_Gradient | None¶
The fallback value used for a layer’s
background.color
attribute in most pre-designed layouts. By default, this value is set to thepalette
[primary
] color or"#4051B2"
for themes other than sphinx-immaterial.
- color : Color | Linear_Gradient | Radial_Gradient | Conical_Gradient | None¶
The color used for the foreground text in most pre-designed layouts. By default, this will be computed as
"white"
or"black"
based on thebackground_color
.social_cards = { "description": "Generate social media cards for documentation pages with Sphinx", "site_url": "https://2bndy5.github.io/sphinx-social-cards/", "cards_layout_options": { "color": "#0FF1CE" } }
size: { width: 600, height: 125 } layers: - background: { color: black } - typography: content: "'{{ layout.color }}'" color: '{{ layout.color | yaml }}' align: center line: { amount: 2 }
- accent : Color | Linear_Gradient | Radial_Gradient | Conical_Gradient | None¶
The color used as a foreground accentuating color. By default, this value is set to the
palette
[accent
] color or"#4051B2"
for themes other than sphinx-immaterial.
- font : Font | None¶
The
font
specification to be used.Seealso
Please review Choosing the font section.
social_cards = { "description": "Generate social media cards for documentation pages with Sphinx", "site_url": "https://2bndy5.github.io/sphinx-social-cards/", "cards_layout_options": { "font": { "family": "Roboto", "style": "italic" } } }
size: { width: 600, height: 125 } layers: - background: { color: black } - typography: content: '{{ layout.font.family }}' '{{ layout.font.style }}' line: { amount: 2 } align: center
- logo : Icon | None¶
The icon used for branding of the site. By default, this will be the
html_logo
(or the sphinx-immaterial theme’sicon
[logo
]).In most pre-designed layouts, the image’s
color
is used as is. This behavior can be changed by setting this option.Most pre-designed layouts use the
icon
metadata field to overridden theimage
value per page.social_cards = { "description": "Generate social media cards for documentation pages with Sphinx", "site_url": "https://2bndy5.github.io/sphinx-social-cards/", "cards_layout_options": { "logo": { "image": "images/message.png", "color": "#4051B2" } } }
size: { width: 250, height: 250 } layers: - background: { color: black } - icon: image: '{{ layout.logo.image }}' color: '{{ layout.logo.color | yaml }}'
Layout Attributes¶
- class Layout¶
The
size
attribute is not required (seewidth
andheight
for default values), but thelayers
attribute is required.Each layout supports these options:
- size : Size¶
The card’s absolute maximum
size
. Anylayers
with nosize
specified will fallback to thislayout.size
. If this is not specified, then the layout uses the defaultwidth
andheight
values.
- layers : List[Layer]¶
A YAML list of layers in the layout that define the entire content of the layout.
Positioning Attributes¶
The layout’s and individual layer’s size
and the individual layer’s offset
are
defined in pixels. The size is defined by a width
and height
property, and the offset is defined by x
and y
properties:
size: { width: 300, height: 300 } # the layout maximum size
layers: # each layer in this list is denoted with a dash ('-')
- {}
- size: { width: 240, height: 240 }
offset: { x: 30, y: 30 }
The layer outline and grid are visible because we enabled debug
in the
social_cards
configuration. The rest of the image is transparent because there
are no layers with any actual content specified (like background
, icon
or
typography
).
Upon closer inspection you’ll notice that