|
| 1 | +""" |
| 2 | +Copied from: |
| 3 | +https://github.com/projectpythia-mystmd/cookbook-gallery/blob/main/unist.py |
| 4 | +""" |
| 5 | + |
| 6 | + |
| 7 | +# Node Creation Tools |
| 8 | +def text(value, **opts): |
| 9 | + return {"type": "text", "value": value, **opts} |
| 10 | + |
| 11 | + |
| 12 | +def strong(children, **opts): |
| 13 | + return {"type": "strong", "children": children, **opts} |
| 14 | + |
| 15 | + |
| 16 | +def link(children, url, **opts): |
| 17 | + return {"type": "link", "url": url, "children": children, **opts} |
| 18 | + |
| 19 | + |
| 20 | +def table(children, **opts): |
| 21 | + return {"type": "table", "children": children, **opts} |
| 22 | + |
| 23 | + |
| 24 | +def table_cell(children, **opts): |
| 25 | + return {"type": "tableCell", "children": children, **opts} |
| 26 | + |
| 27 | + |
| 28 | +def table_row(cells, **opts): |
| 29 | + return {"type": "tableRow", "children": cells, **opts} |
| 30 | + |
| 31 | + |
| 32 | +def span(children, style, **opts): |
| 33 | + return {"type": "span", "children": children, "style": style, **opts} |
| 34 | + |
| 35 | + |
| 36 | +def definition_list(children, **opts): |
| 37 | + return {"type": "definitionList", "children": children, **opts} |
| 38 | + |
| 39 | + |
| 40 | +def definition_term(children, **opts): |
| 41 | + return {"type": "definitionTerm", "children": children, **opts} |
| 42 | + |
| 43 | + |
| 44 | +def definition_description(children, **opts): |
| 45 | + return {"type": "definitionDescription", "children": children, **opts} |
| 46 | + |
| 47 | + |
| 48 | +def list_(children, ordered=False, spread=False, **opts): |
| 49 | + return { |
| 50 | + "type": "list", |
| 51 | + "ordered": ordered, |
| 52 | + "spread": spread, |
| 53 | + "children": children, |
| 54 | + **opts, |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | +def list_item(children, spread=True, **opts): |
| 59 | + return {"type": "listItem", "spread": spread, "children": children, **opts} |
| 60 | + |
| 61 | + |
| 62 | +def image(url, **opts): |
| 63 | + return {"type": "image", "url": url, **opts} |
| 64 | + |
| 65 | + |
| 66 | +def grid(columns, children, **opts): |
| 67 | + return {"type": "grid", "columns": columns, "children": children, **opts} |
| 68 | + |
| 69 | + |
| 70 | +def div(children, **opts): |
| 71 | + return {"type": "div", "children": children, **opts} |
| 72 | + |
| 73 | + |
| 74 | +def find_all_by_type(parent: dict, type_: str): |
| 75 | + for node in parent["children"]: |
| 76 | + if node["type"] == type_: |
| 77 | + yield node |
| 78 | + if "children" not in node: |
| 79 | + continue |
| 80 | + yield from find_all_by_type(node, type_) |
0 commit comments