Coverage for src/lcdoc/mkdocs/lp/plugs/kroki/__init__.py: 73.08%
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"""
2Adds kroki support to the page.
4"""
6import json lp
8import httpx lp
10from lcdoc import lp lp
11from lcdoc.mkdocs.tools import os, make_img lp
12from lcdoc.tools import dirname, os, read_file, exists lp
14# dflt_server = 'https://kroki.io/'
15# dflt_server = os.environ.get('lp_kroki_server', dflt_server)
16# dflt_puml = os.environ.get('lp_kroki_puml', 'dark_blue')
17s = 'mkdocs/lp/' lp
18d_assets = dirname(__file__).split(s, 1)[0] + s + '/assets/plantuml' lp
19pumls = {} lp
21env = os.environ.get lp
22# :docs:lp_kroki_dflts
23lp_kroki_dflts = {
24 'server': env('lp_kroki_server', 'https://kroki.io/'),
25 'puml': env('lp_kroki_puml', 'dark_blue'),
26 'kroki_mode': 'plantuml', # when user gave no kroki mode we set this
27}
28# :docs:lp_kroki_dflts
31def read_puml_file(puml, kw): lp
32 ofn = puml lp|features/lp/plugs/kroki/index.md
33 p = pumls.get(puml) lp|features/lp/plugs/kroki/index.md
34 if p: 34 ↛ 35line 34 didn't jump to line 35, because the condition on line 34 was never truelp|features/lp/plugs/kroki/index.md
35 return p
36 if not '/' in puml: 36 ↛ 39line 36 didn't jump to line 39, because the condition on line 36 was never falselp|features/lp/plugs/kroki/index.md
37 # default dir:
38 puml = '%s/%s.puml' % (d_assets, puml) lp|features/lp/plugs/kroki/index.md
39 elif puml[0] == '/':
40 puml = puml
41 else:
42 fnp = dirname(kw['LP'].page.file.abs_src_path)
43 puml = fnp + '/' + puml
44 return pumls.setdefault(ofn, read_file(puml, '')) lp|features/lp/plugs/kroki/index.md
47def run(cmd, kw): lp
49 d = dict(lp_kroki_dflts) lp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md
50 d.update(kw) lp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md
52 if not cmd.strip(): lp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md
53 src = kw.get('abs_src') lp|features/lp/plugs/kroki/index.md
54 if not src: 54 ↛ 55line 54 didn't jump to line 55, because the condition on line 54 was never truelp|features/lp/plugs/kroki/index.md
55 return lp.err('Require a diag source')
57 if not exists(src): 57 ↛ 58line 57 didn't jump to line 58, because the condition on line 57 was never truelp|features/lp/plugs/kroki/index.md
58 return lp.err('Not found', src=src)
59 cmd = read_file(src) lp|features/lp/plugs/kroki/index.md
61 # lp:kroki:plantuml
62 typ = (kw['mode'] + ':' + d['kroki_mode'] + ':').split(':', 2)[1] lp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md
63 if typ == 'plantuml': lp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md
64 cmd = cmd.replace('@startuml', '') lp|features/lp/plugs/kroki/index.md
65 cmd = cmd.replace('@enduml', '') lp|features/lp/plugs/kroki/index.md
66 puml = read_puml_file(d['puml'], kw) lp|features/lp/plugs/kroki/index.md
67 cmd = puml + '\n' + cmd lp|features/lp/plugs/kroki/index.md
68 cmd = '@startuml\n%s\n@enduml' % cmd lp|features/lp/plugs/kroki/index.md
70 if isinstance(cmd, (dict, list, tuple)): 70 ↛ 71line 70 didn't jump to line 71, because the condition on line 70 was never truelp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md
71 data = json.dumps(cmd)
72 else:
73 data = cmd lp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md
74 # {'diagram_source': cmd, 'diagram_type': typ, 'output_format': 'svg'}
75 # )
76 server = d['server'] lp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md
77 server = server[:-1] if server[-1] == '/' else server lp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md
78 server += '/%s/svg' % typ lp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md
79 res = httpx.post(server, data=data) lp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md
80 if res.status_code > 299: 80 ↛ 81line 80 didn't jump to line 81, because the condition on line 80 was never truelp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md
81 return lp.err(
82 'Server returned error',
83 status=res.status_code,
84 txt=getattr(res, 'text', 'n.a.')[:200],
85 )
87 res = res.text.replace('\r\n', '\n') lp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md
88 if kw.get('get_svg'): 88 ↛ 89line 88 didn't jump to line 89, because the condition on line 88 was never truelp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md
89 return res
90 imglnk = make_img(res, kw=kw) lp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md
91 r = {'res': imglnk, 'formatted': True} lp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md
92 if kw.get('add_svg'): 92 ↛ 93line 92 didn't jump to line 93, because the condition on line 92 was never truelp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md
93 r['svg'] = res
94 return r lp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md