MkBadge¶
Node for a locally-created badge (based on “anybadge”).¶
Description
The node creates a badge svg, appends it to the virtual files, and shows it as an image.
Example: Regular
Jinja
{{ ("Some", "Badge") | MkBadge }}
Python
MkBadge('Some', 'Badge')

<p><img src="https://img.shields.io/badge/Some-Badge-blue" alt="Some: Badge" /></p>

Example: Font size
Jinja
{{ ("Some", "Badge") | MkBadge(font_size=12) }}
Python
MkBadge('Some', 'Badge', font_size=12)

<p><img src="https://img.shields.io/badge/Some-Badge-blue" alt="Some: Badge" /></p>

Example: Char padding
Jinja
{{ ("Some", "Badge") | MkBadge(num_padding_chars=5) }}
Python
MkBadge('Some', 'Badge', num_padding_chars=5)

<p><img src="https://img.shields.io/badge/Some-Badge-blue" alt="Some: Badge" /></p>

Example: Badge color
Jinja
{{ ("Some", "Badge") | MkBadge(font_size=12) }}
Python
MkBadge('Some', 'Badge', font_size=12)

<p><img src="https://img.shields.io/badge/Some-Badge-blue" alt="Some: Badge" /></p>

Example: GitLab style
Jinja
{{ ("Some", "Badge") | MkBadge(use_gitlab_style=True) }}
Python
MkBadge('Some', 'Badge', use_gitlab_style=True)

<p><img src="https://img.shields.io/badge/Some-Badge-blue" alt="Some: Badge" /></p>

Name |
Children |
Inherits |
|---|---|---|
MkImage |
graph TD
3610159806["mkbadge.MkBadge"]
4050704754["mkimage.MkImage"]
1359323199["mknode.MkNode"]
2751680898["builtins.object"]
4050704754 --> 3610159806
1359323199 --> 4050704754
2751680898 --> 1359323199
/home/runner/work/mknodes/mknodes/src/mknodes/templatenodes/mkbadge/metadata.toml¶
[metadata]
icon = "simple-icons:shieldsdotio"
name = "MkBadge"
group = "badge"
[requirements.package.anybadge]
[examples.regular]
title = "Regular"
jinja = """
{{ ("Some", "Badge") | MkBadge }}
"""
[examples.font_size]
title = "Font size"
jinja = """
{{ ("Some", "Badge") | MkBadge(font_size=12) }}
"""
[examples.char_padding]
title = "Char padding"
jinja = """
{{ ("Some", "Badge") | MkBadge(num_padding_chars=5) }}
"""
[examples.badge_color]
title = "Badge color"
jinja = """
{{ ("Some", "Badge") | MkBadge(font_size=12) }}
"""
[examples.gitlab_style]
title = "GitLab style"
jinja = """
{{ ("Some", "Badge") | MkBadge(use_gitlab_style=True) }}
"""
[output.html]
template = """
<body>{{ node.get_data() | replace("\n", "") | html_link(node.get_url()) }}</body>
"""
mknodes.templatenodes.mkbadge.MkBadge¶
63class MkBadge(mkimage.MkImage):
64 """Node for a locally-created badge (based on "anybadge").
65
66 The node creates a badge svg, appends it to the virtual files, and
67 shows it as an image.
68 """
69
70 ICON = "simple/shieldsdotio"
71 REQUIRED_PACKAGES = [resources.Package("anybadge")]
72
73 def __init__(
74 self,
75 label: str | tuple[str, str],
76 value: str | None = None,
77 *,
78 font_size: Literal[10, 11, 12] | None = None,
79 font_name: str | None = None,
80 num_padding_chars: int | None = None,
81 badge_color: str | None = None,
82 text_color: str | None = None,
83 use_gitlab_style: bool = False,
84 **kwargs: Any,
85 ) -> None:
86 """Constructor.
87
88 Args:
89 label: Left part of the badge. If given a tuple, use 2nd item as value.
90 value: Right part of the badge
91 font_size: Size of font to use
92 font_name: Name of font to use
93 num_padding_chars: Number of chars to use for padding
94 badge_color: Badge color
95 text_color: Badge color
96 use_gitlab_style: Use Gitlab-scope style
97 kwargs: Keyword arguments passed to parent
98 """
99 super().__init__("", **kwargs)
100 self.value: str | None
101 if isinstance(label, tuple):
102 self.label, self.value = label
103 else:
104 self.label = label
105 self.value = value
106 self.font_size = font_size
107 self.font_name = font_name
108 self.num_padding_chars = num_padding_chars
109 self._badge_color = badge_color
110 self._text_color = text_color
111 self.use_gitlab_style = use_gitlab_style
112
113 @property
114 def badge_color(self) -> str | None:
115 if isinstance(self._badge_color, str):
116 return self._badge_color
117 return self.ctx.theme.primary_color
118
119 def to_md_unprocessed(self) -> str:
120 data = self.get_data()
121 inner = htmlfilters.html_link(data.replace("\n", ""), self.get_url())
122 return f"<body>{inner}</body>"
123
124 @property
125 def text_color(self) -> str | None:
126 if isinstance(self._text_color, str):
127 return self._text_color
128 color = self.ctx.theme.text_color
129 return f"{color},#fff" if self.use_gitlab_style else f"#fff,{color}"
130
131 def get_data(self) -> str:
132 return get_badge(
133 label=self.label,
134 value=self.value,
135 font_size=self.font_size,
136 font_name=self.font_name,
137 num_padding_chars=self.num_padding_chars,
138 badge_color=self.badge_color,
139 text_color=self.text_color,
140 use_gitlab_style=self.use_gitlab_style,
141 )
142
143 @property
144 def path(self) -> str:
145 hashed = utils.get_hash(repr(self))
146 unique = f"{self.label}_{self.value}_{hashed}.svg"
147 return textfilters.slugify(unique)
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: simple/shieldsdotio
title: MkBadge