Coverage for src/lcdoc/mkdocs/lp/plugs/python/pyplugs/convert/__init__.py: 16.98%
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
3from functools import partial lp|index.md
5from mkdocs.structure.files import File lp|index.md
7from lcdoc.mkdocs.lp.plugs import lightbox, python lp|index.md
8from lcdoc.mkdocs.tools import make_img lp|index.md
9from lcdoc.tools import dirname, exists lp|index.md
11config, page, Session = (python.config, python.page, python.Session) lp|index.md
13LP = lambda: python.lpkw()['LP'] 13 ↛ exitline 13 didn't run the lambda on line 13lp|index.md
15# :docs:convert_defaults
16# Set png=img/foo.png in order to keep the produced pngs within the docs dir:
17# pages: whatever is accepted by convert. E.g. 0-4. 0=first page
18dflts = dict(width=400, pages=0, thumbwidth=200) lp|index.md
19# :docs:convert_defaults
22def pngs(fn_png): lp|index.md
23 k = os.path.basename(fn_png).rsplit('-', 1)[0].rsplit('.png', 1)[0]
24 dir = dirname(fn_png)
25 return [dir + '/' + i for i in os.listdir(dir) if k in i and i.endswith('.png')]
28def unlink_old_pngs(fn_png, dd): lp|index.md
29 files = LP().files
30 for fn in pngs(fn_png):
31 python.app.info('unlinking old', fn=fn)
32 os.unlink(fn)
33 fns = fn.replace(dd + '/', '')
34 [files.remove(f) for f in files if f.src_path.endswith(fns)]
37def png_pth(fn_png, relp, page, dd): lp|index.md
38 rp = relp(fn_png)
39 return rp.replace(
40 dd + '/build/', ('../' * (len(page().file.src_path.split('/')) - 1) + 'build/'),
41 )
44def add_files(dd, fn_png): lp|index.md
45 files = LP().files
46 f = File(fn_png.replace(dd + '/', ''), dd, config()['site_dir'], False)
47 files.append(f)
50def convert_pdf(fn_pdf, kw): lp|index.md
51 dp = os.path.dirname(page().file.abs_src_path) + '/'
52 dd = config()['docs_dir']
53 fn_png = kw.get('png')
54 if not fn_png:
55 nr = str(LP().spec['nr']) + '/'
56 fn_png = dp.replace(config()['docs_dir'], '')
57 fn_png = dd + '/build' + fn_png + nr + os.path.basename(fn_pdf) + '.png'
58 os.makedirs(dirname(fn_png), exist_ok=True)
59 if not exists(fn_pdf):
60 fn_pdf = dp + fn_pdf
61 if not exists(fn_pdf):
62 return 'Pdf not present: %s' % fn_pdf
63 if not fn_png.startswith('/'):
64 fn_png = dp + fn_png
65 d = dict(dflts)
66 d.update(kw)
67 width = d['width']
68 pages = d['pages']
69 unlink_old_pngs(fn_png, dd)
70 cmd = f'convert -thumbnail x{width} -background white -alpha remove "{fn_pdf}[{pages}]" "{fn_png}"'
71 python.app.info('Converting pdf to png', cmd=cmd)
72 if os.system(cmd):
73 return 'err running %s' % cmd
75 relp = lambda fn, dp=dp: fn.replace(dp, '')
76 if str(pages).isdigit():
77 # single png with link to pdf:
78 rp = png_pth(fn_png, relp, page, dd)
79 add_files(dd, fn_png)
80 return '[![](%s)](%s)' % (rp, relp(fn_pdf))
82 python.app.info('Creating slideshow')
83 pics = pngs(fn_png)
84 pdf = os.path.basename(fn_pdf)
85 pdfr = relp(fn_pdf)
86 r = ['<div style="display:flex; flex-wrap:wrap;" class="pdf-slides">']
87 twidth = d['thumbwidth']
88 ni = '' if page().file.src_path.endswith('/index.md') else '../'
89 for k in pics:
90 p = png_pth(k, relp, page, dd)
91 add_files(dd, k)
92 # pic = f'![]({p})'
93 pic = f'<div style="width:{twidth}px;margin: 5px;"><img width=100% src="{ni}{p}"></img></div>'
94 r.append(pic)
95 r += ['</div>']
96 r += [f'[{pdf}]({pdfr})']
98 res = lightbox.run('', {'mode': 'lightbox', 'outer_match': '.pdf-slides '})
99 res['res'] = '\n\n'.join(r)
100 res['page_assets'] = {'lightbox': lightbox.page_assets}
101 return res
104def convert(s, pdf=None, **kw): lp|index.md
105 if pdf:
106 return convert_pdf(pdf, kw)
107 return 'Not supported: %s' % str(locals())