|
| 1 | +""" |
| 2 | +A module implementing HTML-related interfaces. |
| 3 | +""" |
| 4 | + |
| 5 | +# built-in |
| 6 | +from io import StringIO |
| 7 | +from typing import Optional |
| 8 | + |
| 9 | +# third-party |
| 10 | +from svgen.element import Element |
| 11 | +from svgen.element.html import Html, div |
| 12 | +from vcorelib import DEFAULT_ENCODING |
| 13 | +from vcorelib.io import IndentedFileWriter |
| 14 | +from vcorelib.paths import find_file |
| 15 | + |
| 16 | +# internal |
| 17 | +from runtimepy import PKG_NAME |
| 18 | +from runtimepy.net.html.bootstrap import ( |
| 19 | + add_bootstrap_css, |
| 20 | + add_bootstrap_js, |
| 21 | + icon_str, |
| 22 | +) |
| 23 | +from runtimepy.net.html.bootstrap.elements import ( |
| 24 | + bootstrap_button, |
| 25 | + centered_markdown, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +def create_app_shell(parent: Element, **kwargs) -> tuple[Element, Element]: |
| 30 | + """Create a bootstrap-based application shell.""" |
| 31 | + |
| 32 | + container = div(parent=parent, **kwargs) |
| 33 | + container.add_class("d-flex", "align-items-start", "bg-body") |
| 34 | + |
| 35 | + # Dark theme. |
| 36 | + container["data-bs-theme"] = "dark" |
| 37 | + |
| 38 | + # Buttons. |
| 39 | + button_column = div(parent=container) |
| 40 | + button_column.add_class("d-flex", "flex-column", "h-100", "bg-dark-subtle") |
| 41 | + |
| 42 | + # Dark/light theme switch button. |
| 43 | + bootstrap_button( |
| 44 | + icon_str("lightbulb"), |
| 45 | + tooltip=" Toggle light/dark.", |
| 46 | + id="theme-button", |
| 47 | + parent=button_column, |
| 48 | + ) |
| 49 | + |
| 50 | + return container, button_column |
| 51 | + |
| 52 | + |
| 53 | +def markdown_page(parent: Element, markdown: str, **kwargs) -> None: |
| 54 | + """Compose a landing page.""" |
| 55 | + |
| 56 | + container = centered_markdown( |
| 57 | + create_app_shell(parent, **kwargs)[0], markdown, "h-100", "text-body" |
| 58 | + ) |
| 59 | + container.add_class("overflow-y-auto") |
| 60 | + |
| 61 | + |
| 62 | +def common_css(document: Html) -> None: |
| 63 | + """Add common CSS to an HTML document.""" |
| 64 | + |
| 65 | + append_kind(document.head, "font", kind="css", tag="style") |
| 66 | + add_bootstrap_css(document.head) |
| 67 | + append_kind( |
| 68 | + document.head, "main", "bootstrap_extra", kind="css", tag="style" |
| 69 | + ) |
| 70 | + |
| 71 | + |
| 72 | +def full_markdown_page(document: Html, markdown: str) -> None: |
| 73 | + """Render a full markdown HTML app.""" |
| 74 | + |
| 75 | + common_css(document) |
| 76 | + markdown_page(document.body, markdown, id=PKG_NAME) |
| 77 | + |
| 78 | + # JavaScript. |
| 79 | + append_kind(document.body, "markdown_page") |
| 80 | + add_bootstrap_js(document.body) |
| 81 | + |
| 82 | + |
| 83 | +def handle_worker(writer: IndentedFileWriter) -> int: |
| 84 | + """Boilerplate contents for worker thread block.""" |
| 85 | + |
| 86 | + # Not currently used. |
| 87 | + # return write_found_file( |
| 88 | + # writer, kind_url("js", "webgl-debug", subdir="third-party") |
| 89 | + # ) |
| 90 | + del writer |
| 91 | + |
| 92 | + return 0 |
| 93 | + |
| 94 | + |
| 95 | +def write_found_file(writer: IndentedFileWriter, *args, **kwargs) -> bool: |
| 96 | + """Write a file's contents to the file-writer's stream.""" |
| 97 | + |
| 98 | + result = False |
| 99 | + |
| 100 | + entry = find_file(*args, **kwargs) |
| 101 | + if entry is not None: |
| 102 | + with entry.open(encoding=DEFAULT_ENCODING) as path_fd: |
| 103 | + for line in path_fd: |
| 104 | + writer.write(line) |
| 105 | + |
| 106 | + result = True |
| 107 | + |
| 108 | + return result |
| 109 | + |
| 110 | + |
| 111 | +def kind_url( |
| 112 | + kind: str, name: str, subdir: str = None, package: str = PKG_NAME |
| 113 | +) -> str: |
| 114 | + """Return a URL to find a package resource.""" |
| 115 | + |
| 116 | + path = kind |
| 117 | + |
| 118 | + if subdir is not None: |
| 119 | + path += "/" + subdir |
| 120 | + |
| 121 | + path += f"/{name}" |
| 122 | + |
| 123 | + return f"package://{package}/{path}.{kind}" |
| 124 | + |
| 125 | + |
| 126 | +WORKER_TYPE = "text/js-worker" |
| 127 | + |
| 128 | + |
| 129 | +def append_kind( |
| 130 | + element: Element, |
| 131 | + *names: str, |
| 132 | + package: str = PKG_NAME, |
| 133 | + kind: str = "js", |
| 134 | + tag: str = "script", |
| 135 | + subdir: str = None, |
| 136 | + worker: bool = False, |
| 137 | +) -> Optional[Element]: |
| 138 | + """Append a new script element.""" |
| 139 | + |
| 140 | + elem = Element(tag=tag, allow_no_end_tag=False) |
| 141 | + |
| 142 | + with StringIO() as stream: |
| 143 | + writer = IndentedFileWriter(stream, per_indent=2) |
| 144 | + found_count = 0 |
| 145 | + for name in names: |
| 146 | + if write_found_file( |
| 147 | + writer, kind_url(kind, name, subdir=subdir, package=package) |
| 148 | + ): |
| 149 | + found_count += 1 |
| 150 | + |
| 151 | + if worker: |
| 152 | + found_count += handle_worker(writer) |
| 153 | + |
| 154 | + if found_count: |
| 155 | + elem.text = stream.getvalue() |
| 156 | + |
| 157 | + if found_count: |
| 158 | + element.children.append(elem) |
| 159 | + |
| 160 | + if worker: |
| 161 | + elem["type"] = WORKER_TYPE |
| 162 | + |
| 163 | + return elem if found_count else None |
0 commit comments