Skip to content

Commit

Permalink
[doc] Improve badge handling
Browse files Browse the repository at this point in the history
  • Loading branch information
IceflowRE committed Jun 24, 2024
1 parent 6002018 commit d364ac1
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 57 deletions.
52 changes: 20 additions & 32 deletions doc/docs/_static/css/badge.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,38 @@
font-size: .85em
}

[dir=ltr] .md-typeset .mdx-badge__icon {
border-top-left-radius: .1rem
}

[dir=rtl] .md-typeset .mdx-badge__icon {
border-top-right-radius: .1rem
}

[dir=ltr] .md-typeset .mdx-badge__icon {
border-bottom-left-radius: .1rem
}

[dir=rtl] .md-typeset .mdx-badge__icon {
border-bottom-right-radius: .1rem
}

.md-typeset .mdx-badge__icon {
color: var(--md-typeset-a-color);
background: var(--md-accent-fg-color--transparent);
padding: .2rem
box-shadow: inset 0 0 0.0rem 0.05rem var(--md-accent-fg-color);
padding: 0.2rem;
}

.md-typeset .mdx-badge__icon:last-child {
border-radius: .1rem
.md-typeset .mdx-badge__icon:first-child {
border-bottom-left-radius: 0.2rem;
border-top-left-radius: 0.2rem;
}

[dir=ltr] .md-typeset .mdx-badge__text {
border-top-right-radius: .1rem
.md-typeset .mdx-badge__icon:last-child {
border-bottom-right-radius: 0.2em;
border-top-right-radius: 0.2rem;
}

[dir=rtl] .md-typeset .mdx-badge__text {
border-top-left-radius: .1rem
.md-typeset .mdx-badge__text {
color: var(--md-typeset-a-color);
box-shadow: inset 0 0 0.0rem 1px var(--md-accent-fg-color);
padding: 0.2rem 0.3rem;
}

[dir=ltr] .md-typeset .mdx-badge__text {
border-bottom-right-radius: .1rem
.md-typeset .mdx-badge__text:first-child {
border-bottom-left-radius: 0.2rem;
border-top-left-radius: 0.2rem;
}

[dir=rtl] .md-typeset .mdx-badge__text {
border-bottom-left-radius: .1rem
.md-typeset .mdx-badge__text:last-child {
border-bottom-right-radius: 0.2rem;
border-top-right-radius: 0.2rem;
}

.md-typeset .mdx-badge__text {
color: var(--md-typeset-a-color);
box-shadow: 0 0 0 1px inset var(--md-accent-fg-color--transparent);
padding: .2rem .3rem
.kny-badge-bg {
background: var(--md-accent-fg-color--transparent);
}
8 changes: 4 additions & 4 deletions doc/docs/addons/licenses.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ Providing static utility and static functions to save and load licenses.

### Component

{{ kny:badge extends RefCounted }}
{{ kny:badge extends RefCounted --left-bg }}

{{ kny:source /addons/licenses/component.gd res://addons/licenses/component.gd }}
{{ kny:source "/addons/licenses/component.gd" "res://addons/licenses/component.gd" }}

Component class, data wrapper for all information regarding one license item.

Expand Down Expand Up @@ -100,9 +100,9 @@ Component class, data wrapper for all information regarding one license item.

### Component.License

{{ kny:badge extends RefCounted }}
{{ kny:badge extends RefCounted --left-bg }}

{{ kny:source /addons/licenses/licenses.gd res://addons/licenses/licenses.gd }}
{{ kny:source "/addons/licenses/licenses.gd" "res://addons/licenses/licenses.gd" }}

License class.

Expand Down
4 changes: 2 additions & 2 deletions doc/docs/addons/qr_code.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ QR Code generation either with the included `QRCodeRect` node or use the encodin

### QRCodeRect

{{ kny:badge extends TextureRect }}
{{ kny:badge extends TextureRect --left-bg }}

{{ kny:source /addons/qr_code/qr_code_rect.gd res://addons/qr_code/qr_code_rect.gd }}

Expand Down Expand Up @@ -50,7 +50,7 @@ When using byte encoding you can also pass strings for specific ECI values (ISO

### QRCode

{{ kny:badge extends RefCounted }}
{{ kny:badge extends RefCounted --left-bg }}

{{ kny:source /addons/qr_code/qr_code.gd res://addons/qr_code/qr_code.gd }}

Expand Down
4 changes: 2 additions & 2 deletions doc/docs/addons/texture_button_colored.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ Let you apply the icon color theme properties for the texture button. Uses `self

### TextureButtonColored

{{ kny:badge extends TextureButton }}
{{ kny:badge extends TextureButton --left-bg }}

{{ kny:source /addons/custom_theme_overrides/texture_button_colored.gd res://addons/custom_theme_overrides/texture_button_colored.gd }}
{{ kny:source "/addons/custom_theme_overrides/texture_button_colored.gd" "res://addons/custom_theme_overrides/texture_button_colored.gd" }}

#### Theme Overrides

Expand Down
45 changes: 30 additions & 15 deletions doc/hooks/badge.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,52 @@

from mkdocs.config.defaults import MkDocsConfig

def _badge_html(left_text: str, right_text: str = "", typ: str = ""):
classes = f"mdx-badge mdx-badge--{typ}" if typ else "mdx-badge"
left_classes = f"mdx-badge__icon" if left_text[0] == ":" and left_text[-1] == ":" else "mdx-badge__text"
right_classes = f"mdx-badge__icon" if right_text[0] == ":" and right_text[-1] == ":" else "mdx-badge__text"

def _badge_html(args: argparse.Namespace):
left_classes = f"mdx-badge__icon" if args.left_text[0] == ":" and args.left_text[-1] == ":" else "mdx-badge__text"
if args.left_bg:
left_classes += " kny-badge-bg"
right_classes = f"mdx-badge__icon" if len(args.right_text) > 2 and args.right_text[0] == ":" and args.right_text[-1] == ":" else "mdx-badge__text"
if args.right_bg:
right_classes += " kny-badge-bg"
return "".join([
f'<span class="{classes}">',
f'<span class="{left_classes}">{left_text}</span>' if left_text else "",
f'<span class="{right_classes}">{right_text}</span>' if right_text else "",
f'<span class="mdx-badge">',
f'<span class="{left_classes}">{args.left_text}</span>' if args.left_text else "",
f'<span class="{right_classes}">{args.right_text}</span>' if args.right_text else "",
f"</span>",
])


def badge(args: argparse.Namespace, config: MkDocsConfig) -> str:
if args.command == "badge-version":
return _badge_html(":material-tag-outline:", args.right_text)
args.left_text = ":material-tag-outline:"
args.left_bg = True
if args.command == "badge-experimental":
return _badge_html(":material-flask-outline:", args.right_text)
return _badge_html(args.left_text, args.right_text)
args.left_text = ":material-flask-outline:"
args.left_bg = True
if args.command == "badge-download":
args.left_text = ":material-download:"
args.left_bg = True
return _badge_html(args)


def HOOKS(sub_parser) -> list[tuple[str, Callable[[argparse.Namespace, MkDocsConfig], str]]]:
parser = sub_parser.add_parser("badge", help="badge")
parser.add_argument("left_text", type=str, default="", help="left text of the badge")
parser.add_argument("right_text", type=str, default="", help="right text of the badge")
parser.add_argument("right_text", nargs='?', type=str, default="", help="right text of the badge")
parser.add_argument("--left-bg", action="store_true", default=False, help="left background color")
parser.add_argument("--right-bg", action="store_true", default=False, help="left background color")

parser = sub_parser.add_parser("badge-version", help="experimental badge")
parser: argparse.ArgumentParser = sub_parser.add_parser("badge-version", help="experimental badge")
parser.add_argument("right_text", type=str, default="", help="right text of the badge")
parser.add_argument("--right-bg", action="store_true", default=False, help="left background color")

parser = sub_parser.add_parser("experimental", help="experimental badge")
parser = sub_parser.add_parser("badge-experimental", help="experimental badge")
parser.add_argument("right_text", type=str, default="", help="right text of the badge")
parser.add_argument("--right-bg", action="store_true", default=False, help="left background color")

parser = sub_parser.add_parser("download", help="download badge")
parser = sub_parser.add_parser("badge-download", help="download badge")
parser.add_argument("right_text", type=str, default="", help="right text of the badge")
parser.add_argument("--right-bg", action="store_true", default=False, help="left background color")

return [("badge", badge), ("badge-version", badge), ("badge-experimental", badge)]
return [("badge", badge), ("badge-version", badge), ("badge-experimental", badge), ("badge-download", badge)]
5 changes: 3 additions & 2 deletions doc/hooks/hooks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import argparse
import re
from re import Match
import shlex
from typing import Callable

from mkdocs.config.defaults import MkDocsConfig
Expand Down Expand Up @@ -35,11 +36,11 @@ def add_hooks(hook: Callable[[any], list[tuple[str, Callable[[argparse.Namespace

def on_page_markdown(markdown: str, *, page: Page, config: MkDocsConfig, files: Files):
def replace(match: re.Match):
args: argparse.Namespace = PARSER.parse_args(match.groups()[0].split(" "))
args: argparse.Namespace = PARSER.parse_args(shlex.split(match.groups()[0]))

fn: Callable[[argparse.Namespace, MkDocsConfig], str] = HOOKS.get(args.command, None)
if fn is None:
raise RuntimeError(f"Unknown shortcode: {type}")
raise RuntimeError(f"Unknown shortcode: {args.command}")

return fn(args, config)

Expand Down

0 comments on commit d364ac1

Please sign in to comment.