Coverage for src/lcdoc/mkdocs/replace/admons.py: 79.31%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""
2We replace via md-find
4 !!! :foo: ["some title"]
6with
8 <style>
9 ...
10 </style>
12 !!! some-title "some title"
14see: https://squidfunk.github.io/mkdocs-material/reference/admonitions/#customization
15"""
17import os lp
18from functools import partial lp
19from lcdoc.mkdocs.tools import read_file, app lp
20import material lp
23def style(typ, ico, col, bgcol=None): lp
24 if not bgcol: 24 ↛ 28line 24 didn't jump to line 28lp|about/navigation.md
25 if not 'rgb(' in col: 25 ↛ 26line 25 didn't jump to line 26, because the condition on line 25 was never truelp|about/navigation.md
26 raise Exception('You need an rgb col if you do not specify bgcol')
27 bgcol = col.strip().replace('rgb(', 'rgba(')[:-1] + ', 0.1)' lp|about/navigation.md
28 s = '''
29<style>
30:root { --md-admonition-icon--%(typ)s: url('data:image/svg+xml;charset=utf-8,%(ico)s') }
31.md-typeset .admonition.%(typ)s,
32.md-typeset details.%(typ)s {
33 border-color: %(col)s;
34}
35.md-typeset .%(typ)s > .admonition-title,
36.md-typeset .%(typ)s > summary {
37 background-color: %(bgcol)s;
38 border-color: %(col)s;
39}
40.md-typeset .%(typ)s > .admonition-title::before,
41.md-typeset .%(typ)s > summary::before {
42 background-color: %(col)s;
43 -webkit-mask-image: var(--md-admonition-icon--%(typ)s);
44 mask-image: var(--md-admonition-icon--%(typ)s);
45}
46</style>
47'''
48 return s % locals() lp|about/navigation.md
51import httpx lp
54def admons(*which): lp
55 _ = cust_admons lp
56 return {k: partial(admon, **v) for k, v in _.items() if k in which} lp
59d_material = os.path.dirname(material.__file__) lp
62def get_raw(svg): lp
63 """
64 ico = '<svg ....' # raw svg from anywhere.
65 ico = 'https://twemoji.maxcdn.com/v/latest/svg/1f4f7.svg' # url
66 ico = 'material/camera-account.svg' # file in your site-directories/material/.icons
67 """
68 if svg.startswith('<svg'): 68 ↛ 69line 68 didn't jump to line 69, because the condition on line 68 was never truelp|about/navigation.md
69 return svg
70 if svg.startswith('http'): 70 ↛ 71line 70 didn't jump to line 71, because the condition on line 70 was never truelp|about/navigation.md
71 return httpx.get(svg).text
72 fn = os.path.join(d_material + '/.icons/', svg) lp|about/navigation.md
73 s = read_file(fn, dflt='') lp|about/navigation.md
74 if s: 74 ↛ 76line 74 didn't jump to line 76, because the condition on line 74 was never falselp|about/navigation.md
75 return s lp|about/navigation.md
76 app.die('Icon not loadable', ico=svg)
79def admon(title, ico, col, bgcol=None, **kw): lp
80 p = kw['page'] lp|about/navigation.md
81 ind = kw['line'].split('!!!', 1)[0] lp|about/navigation.md
82 l = kw['line'].split(':') lp|about/navigation.md
83 t = title.lower().replace(' ', '-') lp|about/navigation.md
84 if len(l) == 3 and l[2]: 84 ↛ 85line 84 didn't jump to line 85, because the condition on line 84 was never truelp|about/navigation.md
85 title = l[2]
86 # avoid duplicate style defs in one page:
87 s, tn = '', 'admon_style_' + t lp|about/navigation.md
88 if not hasattr(p, tn): 88 ↛ 92line 88 didn't jump to line 92, because the condition on line 88 was never falselp|about/navigation.md
89 ico = get_raw(ico) lp|about/navigation.md
90 s = style(t, ico, col, bgcol) lp|about/navigation.md
91 setattr(p, tn, True) lp|about/navigation.md
92 r = {'line': '''%s!!! %s "%s"''' % (ind, t, title), 'markdown_header': s} lp|about/navigation.md
93 return r lp|about/navigation.md
96ico_dev = 'fontawesome/brands/dev.svg' lp
98# import this within mdreplace and extend to your liking
99cust_admons = {
100 'dev': dict(title='Developer Tip', ico=ico_dev, col='rgb(139, 209, 36)'),
101}
104# then say e.g.
106# table.update(admons.admons('dev'))