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"""
2### `show_src`
4Copies delimitted stanzas within arbitrary files (usually source code) into your docs
5and creates links to those files on the repo server.
7Format within the source file: :docs:matchstring (no space)
9#### Parameters
11- delim: "matchstring" in the example above (without the ":docs:" prefix).
12- dir: Start directly, relative to repo root dir or absolute path
13- hide: Optional. True or String: will result in collapsed block
15The plugin uses ripgrep to find matches.
17"""
20import json lp|features/lp/eval.md
21import os lp|features/lp/eval.md
23from lcdoc.mkdocs.tools import srclink lp|features/lp/eval.md
24from lcdoc.mkdocs.markdown import deindent lp|features/lp/eval.md
25from lcdoc.tools import app, dirname, exists, now, os, read_file, require lp|features/lp/eval.md
27fmt_default = 'mk_console' lp|features/lp/eval.md
29T = '''
30=== "Code"
31 ```%(lang)s
32 %(body)s
33 ```
34=== "%(srclink)s"
35 %(url)s
36'''
38TM = '''
39??? note "%(header)s"
40 %(body)s
41'''
44def md(**kw): lp|features/lp/eval.md
45 kw['body'] = kw['body'].replace('\n', '\n ') lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
46 return T % kw lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
49def mdhide(**kw): lp|features/lp/eval.md
50 M = md(**kw) lp|features/lp/eval.mdlp|features/lp/plugs/show_src/index.md
51 kw['body'] = md(**kw).replace('\n', '\n ') lp|features/lp/eval.mdlp|features/lp/plugs/show_src/index.md
52 return TM % kw lp|features/lp/eval.mdlp|features/lp/plugs/show_src/index.md
55def run(cmd, kw): lp|features/lp/eval.md
56 require('rg --version') lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
57 LP = kw['LP'] lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
58 delim = kw['delim'] lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
59 dir = kw['dir'] lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
60 if not dir[0] == '/': 60 ↛ 63line 60 didn't jump to line 63, because the condition on line 60 was never falselp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
61 droot = os.path.dirname(LP.config['docs_dir']) lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
62 dir = droot + '/' + dir lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
63 if not exists(dir): 63 ↛ 64line 63 didn't jump to line 64, because the condition on line 63 was never truelp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
64 return app.fatal('Not exists', dir=dir)
65 expr = ':docs:%s' % delim lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
66 cmd = 'rg "%s" %s --json --max-filesize 1M' % (expr, dir) lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
67 rg = os.popen(cmd).read().strip() lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
68 rg2 = [json.loads(l) for l in rg.splitlines()] lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
69 j = [k for k in rg2 if k['type'] == 'begin'] lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
70 if len(j) == 0: 70 ↛ 71line 70 didn't jump to line 71, because the condition on line 70 was never truelp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
71 return app.fatal('Expression not found', cmd=cmd, expr=delim)
72 if len(j) != 1: 72 ↛ 73line 72 didn't jump to line 73, because the condition on line 72 was never truelp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
73 return app.fatal('Expression not unique', cmd=cmd, found=len(j), expr=expr)
74 fn = j[0]['data']['path']['text'] lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
75 s = read_file(fn).split(expr) lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
76 if len(s) != 3: 76 ↛ 77line 76 didn't jump to line 77, because the condition on line 76 was never truelp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
77 return app.fatal(
78 'Expression not correct in file',
79 cmd=cmd,
80 found=len(j),
81 expr=expr,
82 fn=fn,
83 matches_file=len(s),
84 )
85 line = len(s[0].splitlines()) lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
86 res = s[1].split('\n', 1)[1].rsplit('\n', 1)[0] lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
87 res = deindent(res) lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
88 l = srclink(fn, LP.config, line=line + 1) lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
89 h = kw.get('hide') lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
90 if h == True: 90 ↛ 91line 90 didn't jump to line 91, because the condition on line 90 was never truelp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
91 h = 'Implementation'
92 f = mdhide if h else md lp|features/lp/bash/sessions.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/eval.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/_tech.mdlp|features/lp/plugs/column/index.mdlp|features/lp/plugs/kroki/index.mdlp|features/lp/plugs/lightbox/index.mdlp|features/lp/plugs/show_src/index.md
93 return {
94 'formatted': True,
95 'res': f(header=h, lang=kw['lang'], body=res, srclink=l['link'], url=l['url']),
96 }