mdhtml2docx 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ __version__ = "0.1.0"
2
+
3
+ from .convert import convert
mdhtml2docx/asvocab.py ADDED
@@ -0,0 +1,88 @@
1
+ "Ergonomic exploration of an appscript app's scripting vocabulary: live terminology tables, plus the sdef XML for full docs."
2
+ import re
3
+ from functools import cache
4
+ from importlib.resources import files
5
+ from aem import AEEnum
6
+ from lxml import etree
7
+
8
+ __all__ = ['vocab', 'props', 'sd', 'sdfind']
9
+
10
+ SDEF = files('mdhtml2docx')/'word.sdef'
11
+ _kinds = {b'p': 'property', b'e': 'element', b'c': 'command'}
12
+
13
+ def vocab(app, pat=''):
14
+ "Search `app`'s terminology for names matching regex `pat` (case-insensitive), as sorted (kind, name) rows"
15
+ r = re.compile(pat, re.I)
16
+ ad = app.AS_appdata
17
+ res = [(_kinds[v[0]], n) for n,v in ad.referencebyname().items() if r.search(n)]
18
+ res += [('enum' if isinstance(v, AEEnum) else 'class', n) for n,v in ad.typebyname().items() if r.search(n)]
19
+ return sorted(res)
20
+
21
+ def props(ref, pat=None, maxlen=80, timeout=15):
22
+ "`ref.properties.get()` as a readable {name: truncated-repr} dict, optionally filtered by regex `pat`"
23
+ r = re.compile(pat, re.I) if pat else None
24
+ res = {}
25
+ for key, v in ref.properties.get(timeout=timeout).items():
26
+ n = str(key).removeprefix('k.')
27
+ if r and not r.search(n): continue
28
+ s = repr(v)
29
+ res[n] = s if len(s) <= maxlen else s[:maxlen] + '...'
30
+ return res
31
+
32
+ class _Doc(str):
33
+ def __repr__(self): return str(self)
34
+
35
+ @cache
36
+ def _sdef(path): return etree.parse(str(path))
37
+
38
+ def _typ(e):
39
+ if e.get('type'): return e.get('type')
40
+ return ' | '.join(('list of ' if t.get('list') else '')+t.get('type','') for t in e.findall('type'))
41
+
42
+ def _desc(e): return f' -- {e.get("description")}' if e.get('description') else ''
43
+
44
+ def _fmt_cmd(c):
45
+ lines = [f'command {c.get("name")}{_desc(c)}']
46
+ dp = c.find('direct-parameter')
47
+ if dp is not None: lines.append(f' direct: {_typ(dp)}{_desc(dp)}')
48
+ for p in c.findall('parameter'):
49
+ opt = ' (optional)' if p.get('optional')=='yes' else ''
50
+ lines.append(f' {p.get("name")}: {_typ(p)}{opt}{_desc(p)}')
51
+ r = c.find('result')
52
+ if r is not None: lines.append(f' result: {_typ(r)}{_desc(r)}')
53
+ return lines
54
+
55
+ def _fmt_cls(c):
56
+ inh = f' < {c.get("inherits")}' if c.get('inherits') else ''
57
+ lines = [f'class {c.get("name")}{inh}{_desc(c)}']
58
+ for e in c.findall('element'): lines.append(f' element: {e.get("type")}')
59
+ for p in c.findall('property'):
60
+ acc = ' (r/o)' if p.get('access')=='r' else ''
61
+ lines.append(f' {p.get("name")}: {p.get("type")}{acc}{_desc(p)}')
62
+ return lines
63
+
64
+ def _fmt_enum(c):
65
+ lines = [f'enumeration {c.get("name")}']
66
+ for e in c.findall('enumerator'): lines.append(f' {e.get("name")}{_desc(e)}')
67
+ return lines
68
+
69
+ _fmts = {'command': _fmt_cmd, 'class': _fmt_cls, 'enumeration': _fmt_enum}
70
+ _children = ('parameter', 'property', 'enumerator')
71
+
72
+ def sd(name, path=SDEF):
73
+ "Full sdef doc for `name` (a command, class, or enumeration; underscore and space spellings both work), with types and descriptions"
74
+ n = name.replace('_', ' ')
75
+ nodes = [e for tag in _fmts for e in _sdef(path).iter(tag) if e.get('name') in (n, name)]
76
+ if not nodes: raise KeyError(f'{name!r} not found in sdef; try sdfind')
77
+ return _Doc('\n\n'.join('\n'.join(_fmts[e.tag](e)) for e in nodes))
78
+
79
+ def sdfind(pat, path=SDEF, maxlen=80):
80
+ "Search sdef names and descriptions for regex `pat`, as (kind, name, description) rows; child nodes show as parent.name"
81
+ r = re.compile(pat, re.I)
82
+ res = []
83
+ for e in _sdef(path).iter(*_fmts, *_children):
84
+ n, d = e.get('name') or '', e.get('description') or ''
85
+ if not (r.search(n) or r.search(d)): continue
86
+ if e.tag in _children: n = f'{e.getparent().get("name")}.{n}'
87
+ res.append((e.tag, n, d if len(d)<=maxlen else d[:maxlen]+'...'))
88
+ return res