Hide keyboard shortcuts

Hot-keys 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. 

3 

4""" 

5 

6import json lp|features/lp/plugs/kroki/index.md

7 

8import httpx lp|features/lp/plugs/kroki/index.md

9 

10from lcdoc import lp lp|features/lp/plugs/kroki/index.md

11from lcdoc.mkdocs.tools import os, make_img lp|features/lp/plugs/kroki/index.md

12from lcdoc.tools import dirname, os, read_file, exists lp|features/lp/plugs/kroki/index.md

13 

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|features/lp/plugs/kroki/index.md

18d_assets = dirname(__file__).split(s, 1)[0] + s + '/assets/plantuml' lp|features/lp/plugs/kroki/index.md

19pumls = {} lp|features/lp/plugs/kroki/index.md

20 

21env = os.environ.get lp|features/lp/plugs/kroki/index.md

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 

29 

30 

31def read_puml_file(fn, kw): lp|features/lp/plugs/kroki/index.md

32 ofn = fn lp|features/lp/plugs/kroki/index.md

33 p = pumls.get(fn) 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 fnp = dirname(kw['LP'].page.file.abs_src_path) lp|features/lp/plugs/kroki/index.md

37 if not '/' in fn: 37 ↛ 40line 37 didn't jump to line 40, because the condition on line 37 was never falselp|features/lp/plugs/kroki/index.md

38 # default dir: 

39 fn = '%s/%s.puml' % (d_assets, fn) lp|features/lp/plugs/kroki/index.md

40 elif fn[0] == '/': 

41 fn = fn 

42 else: 

43 fn = fnp + '/' + fn 

44 return pumls.setdefault(ofn, read_file(fn, '')) lp|features/lp/plugs/kroki/index.md

45 

46 

47def run(cmd, kw): lp|features/lp/plugs/kroki/index.md

48 

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

51 

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') 

56 

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

60 

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

69 

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 ) 

86 

87 res = res.text.replace('\r\n', '\n') lp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md

88 imglnk = make_img(res, kw=kw) lp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md

89 r = {'res': imglnk, 'formatted': True} lp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md

90 if kw.get('add_svg'): 90 ↛ 91line 90 didn't jump to line 91, because the condition on line 90 was never truelp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md

91 r['svg'] = res 

92 return r lp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/kroki/index.md