MkImage¶
Image including optional caption.¶
Example: With caption
Jinja
{{ "https://picsum.photos/200" | MkImage(caption="A caption!") }}
Python
MkImage('https://picsum.photos/200', caption='A caption!')
<picture>
<img alt="A caption!" src="https://picsum.photos/200">
</picture>
<picture>
<img alt="A caption!" src="https://picsum.photos/200">
</picture>
.. image:: https://picsum.photos/200
:alt: A caption!
<picture>
<img alt="A caption!" src="https://picsum.photos/200">
</picture>
Example: Right-aligned
Jinja
{{ "https://picsum.photos/200" | MkImage(align="right") }}
Python
MkImage('https://picsum.photos/200', align='right')
<picture>
<img alt="" src="https://picsum.photos/200">
</picture>
<picture>
<img alt="" src="https://picsum.photos/200">
</picture>
.. image:: https://picsum.photos/200
:align: right
<picture>
<img alt="" src="https://picsum.photos/200">
</picture>
Example: Fixed width
Jinja
{{ "https://picsum.photos/200" | MkImage(width=500) }}
Python
MkImage('https://picsum.photos/200', width=500)
<picture>
<img alt="" src="https://picsum.photos/200">
</picture>
<picture>
<img alt="" src="https://picsum.photos/200">
</picture>
.. image:: https://picsum.photos/200
:width: 500
<picture>
<img alt="" src="https://picsum.photos/200">
</picture>
Example: Hyperlinked
Jinja
{{ "https://picsum.photos/200" | MkImage(target="https://phil65.github.io/mknodes") }}
Python
MkImage('https://picsum.photos/200', target='https://phil65.github.io/mknodes')
<picture>
<img alt="" src="https://picsum.photos/200">
</picture>
<picture>
<img alt="" src="https://picsum.photos/200">
</picture>
.. image:: https://picsum.photos/200
:target: https://phil65.github.io/mknodes
<picture>
<img alt="" src="https://picsum.photos/200">
</picture>
Example: Separate dark mode image
Jinja
{{ "https://picsum.photos/200" | MkImage(path_dark_mode="https://picsum.photos/300") }}
Python
MkImage('https://picsum.photos/200', path_dark_mode='https://picsum.photos/300')
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://picsum.photos/300">
<source media="(prefers-color-scheme: light)" srcset="https://picsum.photos/200">
<img alt="" src="https://picsum.photos/200">
</picture>
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://picsum.photos/300">
<source media="(prefers-color-scheme: light)" srcset="https://picsum.photos/200">
<img alt="" src="https://picsum.photos/200">
</picture>
.. image:: https://picsum.photos/200
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://picsum.photos/300">
<source media="(prefers-color-scheme: light)" srcset="https://picsum.photos/200">
<img alt="" src="https://picsum.photos/200">
</picture>
Name |
Children |
Inherits |
|---|---|---|
MkBinaryImage |
||
MkBadge |
Name |
Children |
Inherits |
|---|---|---|
MkNode |
graph TD
4050704754["mkimage.MkImage"]
1359323199["mknode.MkNode"]
2751680898["builtins.object"]
1359323199 --> 4050704754
2751680898 --> 1359323199
/home/runner/work/mknodes/mknodes/src/mknodes/basenodes/mkimage/metadata.toml¶
[metadata]
icon = "mdi:image"
name = "MkImage"
group = "image"
[examples.with_caption]
title = "With caption"
jinja = """
{{ "https://picsum.photos/200" | MkImage(caption="A caption!") }}
"""
[examples.right_aligned]
title = "Right-aligned"
jinja = """
{{ "https://picsum.photos/200" | MkImage(align="right") }}
"""
[examples.fixed_width]
title = "Fixed width"
jinja = """
{{ "https://picsum.photos/200" | MkImage(width=500) }}
"""
[examples.hyperlinked]
title = "Hyperlinked"
jinja = """
{{ "https://picsum.photos/200" | MkImage(target="https://phil65.github.io/mknodes") }}
"""
[examples.dark_mode_image]
title = "Separate dark mode image"
jinja = """
{{ "https://picsum.photos/200" | MkImage(path_dark_mode="https://picsum.photos/300") }}
"""
[fragments]
image = """
) ~ ")" ~ (node.align | add("{ align=", " }")) ~ (node.width | add('{ width="', '" }')) }}{% if node.lazy %}{ loading=lazy }{% endif %}
"""
linked_image = """
{% if node.get_url() %}
[{{ "fragments/image" | render_template(node=node, path=path, mode=mode) }}]({{ node.get_url() }}){% else %}
{{ "fragments/image" | render_template(node=node, path=path, mode=mode) }}{% endif %}
"""
[output.markdown]
template = """
{% if node.path_dark_mode %}
{% set link_dark = "fragments/linked_image" | render_template(node=node, path=node.path_dark_mode, mode="dark") %}
{% set link_light = "fragments/linked_image" | render_template(node=node, path=node.path, mode="light") %}
{% set markdown_link = link_dark ~ " " ~ link_light %}
{% else %}
{% set markdown_link = "fragments/linked_image" | render_template(node=node, path=node.path, mode=None) %}
{% endif %}
{% if node.caption %}
<figure markdown>
{{ markdown_link }}
<figcaption>{{ node.caption }}</figcaption>
</figure>
{% else %}
{{ markdown_link }}{% endif %}
"""
[output.github]
template = """
<picture>
{% if node.path_dark_mode %}
<source media="(prefers-color-scheme: dark)" srcset="{{ node.path_dark_mode }}">
<source media="(prefers-color-scheme: light)" srcset="{{ node.path }}">
{% endif %}
<img alt="{{ node.caption }}" src="{{ node.path }}">
</picture>
"""
[output.rst]
template = """
.. image:: {{ node.path }}
{% if node.width %} :width: {{ node.width }}{% endif %}
{% if node.caption %} :alt: {{ node.caption }}{% endif %}
{% if node.align %} :align: {{ node.align }}{% endif %}
{% if node.get_url() %} :target: {{ node.get_url() }}{% endif %}
"""
mknodes.basenodes.mkimage.MkImage¶
16class MkImage(mknode.MkNode):
17 """Image including optional caption."""
18
19 ICON = "material/image"
20 ATTR_LIST_SEPARATOR = ""
21
22 def __init__(
23 self,
24 path: str,
25 *,
26 target: linkprovider.LinkableType | None = None,
27 caption: str = "",
28 title: str = "",
29 align: Literal["left", "right"] | None = None,
30 width: int | None = None,
31 lazy: bool = False,
32 path_dark_mode: str | None = None,
33 **kwargs: Any,
34 ) -> None:
35 """Constructor.
36
37 Args:
38 path: path of the image
39 target: Optional URL or node the image should link to
40 caption: Image caption
41 title: Image title
42 align: Image alignment
43 width: Image width in pixels
44 lazy: Whether to lazy-load image
45 path_dark_mode: Optional alternative image for dark mode
46 kwargs: Keyword arguments passed to parent
47 """
48 super().__init__(**kwargs)
49 self.title = title
50 self.caption = caption
51 self.target = target
52 self.align = align
53 self.width = width
54 self.lazy = lazy
55 self._path_dark_mode = path_dark_mode
56 self._path = path
57
58 @property
59 def path(self) -> str:
60 if helpers.is_url(self._path):
61 return self._path
62 # TODO: linkreplacer doesnt work yet with full path
63 return pathlib.Path(self._path).name # this should not be needed.
64
65 @property
66 def path_dark_mode(self):
67 match self._path_dark_mode:
68 case str() if helpers.is_url(self._path_dark_mode):
69 return self._path_dark_mode
70 case str():
71 return pathlib.Path(self._path_dark_mode).name
72 case _:
73 return None
74
75 def get_url(self) -> str:
76 return self.ctx.links.get_url(self.target) if self.target else ""
77
78 def to_md_unprocessed(self) -> str:
79 if not self.path_dark_mode:
80 markdown_link = self._build(self.path)
81 else:
82 link_2 = self._build(self.path, "light")
83 link_1 = self._build(self.path_dark_mode, "dark")
84 markdown_link = f"{link_1} {link_2}"
85 if not self.caption:
86 return markdown_link
87 lines = [
88 "<figure markdown>",
89 f" {markdown_link}",
90 f" <figcaption>{self.caption}</figcaption>",
91 "</figure>",
92 ]
93 return "\n".join(lines) + "\n"
94
95 def _build(self, path: str, mode: Literal["light", "dark"] | None = None) -> str:
96 if mode:
97 path += f"#only-{mode}"
98 markdown_link = f""
99 if self.align:
100 markdown_link += f"{{ align={self.align} }}"
101 if self.width:
102 markdown_link += f'{{ width="{self.width}" }}'
103 if self.lazy:
104 markdown_link += "{ loading=lazy }"
105 if self.target:
106 markdown_link = f"[{markdown_link}]({self.get_url()})"
107 return markdown_link
Page info
Code for this page
mknodes.manual.nodes_section.create_section_for_nodes¶
93def create_section_for_nodes(
94 nav: mk.MkNav,
95 klasses: Sequence[type[mk.MkNode]],
96) -> mk.MkTable:
97 table = mk.MkTable(columns=["Node", "Docstrings", "Markdown extensions"])
98 for kls in klasses:
99 tpl = "classpage_custom.jinja"
100 page = mk.MkClassPage(kls, icon=kls.ICON, template_path=tpl, parent=nav, hide="toc")
101 nav += page
102 if kls.STATUS:
103 page.metadata.status = kls.STATUS
104 elif kls.JS_FILES:
105 page.metadata.status = "js"
106 elif kls.CSS:
107 page.metadata.status = "css"
108 page.created_by = create_section_for_nodes
109 link = mk.MkLink(page, kls.__name__, icon=kls.ICON)
110 extensions = ", ".join(f"`{i}`" for i in kls.REQUIRED_EXTENSIONS)
111 table.add_row((link, kls.__doc__, extensions))
112 return table
Resources
Resources(css=[],
markdown_extensions={'admonition': {},
'pymdownx.details': {},
'pymdownx.highlight': {'anchor_linenums': True,
'line_spans': '__span',
'pygments_lang_class': True},
'pymdownx.superfences': {'custom_fences': [{'class': 'mermaid',
'format': <function fence_code_format at 0x7ff77e88b060>,
'name': 'mermaid'}]},
'pymdownx.tabbed': {},
'tables': {}},
plugins=[Plugin('mkdocstrings')],
js=[],
assets=[],
packages=[])
Metadata
created:
source_filename: /home/runner/work/mknodes/mknodes/src/mknodes/manual/nodes_section.py
source_function: create_section_for_nodes
source_line_no: 93
hide:
- toc
icon: material/image
title: MkImage