pyjs 0.0.1__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.
- pyjs/__init__.py +6 -0
- pyjs/__main__.py +6 -0
- pyjs/cli.py +816 -0
- pyjs/decorators.py +170 -0
- pyjs/docs/__init__.py +6 -0
- pyjs/docs/builder.py +351 -0
- pyjs/docs/cli.py +55 -0
- pyjs/docs/config.py +45 -0
- pyjs/docs/favicon.svg +18 -0
- pyjs/docs/markdown.py +61 -0
- pyjs/docs/models.py +38 -0
- pyjs/docs/navigation.py +61 -0
- pyjs/docs/style.css +901 -0
- pyjs/docs/ui.py +349 -0
- pyjs/nativescript/__init__.py +77 -0
- pyjs/scaffolding/__init__.py +418 -0
- pyjs/scaffolding/templates/app/README.md.template +33 -0
- pyjs/scaffolding/templates/app/__PYJS_SOURCE_ROOT__/__PYJS_PACKAGE__/__PYJS_PACKAGE__.py.template +81 -0
- pyjs/scaffolding/templates/app/__PYJS_SOURCE_ROOT__/__PYJS_PACKAGE__/__init__.py.template +0 -0
- pyjs/scaffolding/templates/app/__PYJS_SOURCE_ROOT__/__PYJS_PACKAGE__/__main__.py.template +5 -0
- pyjs/scaffolding/templates/app/tests/test_app.py.template +22 -0
- pyjs/scaffolding/templates/app/tests/test_browser_app.py.template +19 -0
- pyjs/scaffolding/templates/app_test.py.template +8 -0
- pyjs/server.py +468 -0
- pyjs/skills/pyjs/SKILL.md +161 -0
- pyjs/state.py +101 -0
- pyjs/testing/__init__.py +3 -0
- pyjs/testing/android.py +248 -0
- pyjs/testing/artifacts.py +37 -0
- pyjs/testing/assertion_rewriter.py +125 -0
- pyjs/testing/builder.py +191 -0
- pyjs/testing/capture.py +133 -0
- pyjs/testing/client/__init__.py +1 -0
- pyjs/testing/client/app.py +718 -0
- pyjs/testing/client/assertions.py +49 -0
- pyjs/testing/client/runtime.py +123 -0
- pyjs/testing/client/sidebar.py +377 -0
- pyjs/testing/client/state.py +107 -0
- pyjs/testing/client/viewer.py +361 -0
- pyjs/testing/daemon.py +641 -0
- pyjs/testing/helpers.py +155 -0
- pyjs/testing/protocol.py +56 -0
- pyjs/testing/pytest_plugin.py +797 -0
- pyjs/testing/web.py +97 -0
- pyjs/testing/webgpu.py +27 -0
- pyjs/tooling/__init__.py +4 -0
- pyjs/tooling/android.py +138 -0
- pyjs/tooling/browser.py +676 -0
- pyjs/tooling/command.py +75 -0
- pyjs/tooling/nativescript.py +156 -0
- pyjs/tooling/node.py +170 -0
- pyjs/tooling/perf.py +163 -0
- pyjs/transpiler/__init__.py +23 -0
- pyjs/transpiler/_builtins.py +485 -0
- pyjs/transpiler/analyzer.py +1495 -0
- pyjs/transpiler/objects.py +1015 -0
- pyjs/transpiler/source.py +189 -0
- pyjs/transpiler/transpiler.py +748 -0
- pyjs/transpiler/utils.py +150 -0
- pyjs/web/__init__.py +5 -0
- pyjs/web/console.py +16 -0
- pyjs/web/dom.py +1529 -0
- pyjs/web/domx.py +474 -0
- pyjs/web/dragdrop.py +102 -0
- pyjs/web/events.py +1306 -0
- pyjs/web/files.py +203 -0
- pyjs/web/geometry.py +475 -0
- pyjs/web/html.py +927 -0
- pyjs/web/indexeddb.py +410 -0
- pyjs/web/javascript.py +112 -0
- pyjs/web/network.py +193 -0
- pyjs/web/ssr.py +70 -0
- pyjs/web/streams.py +17 -0
- pyjs/web/svg.py +684 -0
- pyjs/web/timers.py +29 -0
- pyjs/web/ui/__init__.py +18 -0
- pyjs/web/ui/accordion.py +166 -0
- pyjs/web/ui/alert.py +52 -0
- pyjs/web/ui/alert_dialog.py +102 -0
- pyjs/web/ui/aspect_ratio.py +12 -0
- pyjs/web/ui/attachment.py +80 -0
- pyjs/web/ui/avatar.py +105 -0
- pyjs/web/ui/badge.py +48 -0
- pyjs/web/ui/breadcrumb.py +85 -0
- pyjs/web/ui/bubble.py +38 -0
- pyjs/web/ui/button.py +86 -0
- pyjs/web/ui/calendar.py +124 -0
- pyjs/web/ui/card.py +58 -0
- pyjs/web/ui/carousel.py +99 -0
- pyjs/web/ui/chart.py +56 -0
- pyjs/web/ui/checkbox.py +31 -0
- pyjs/web/ui/collapsible.py +71 -0
- pyjs/web/ui/combobox.py +36 -0
- pyjs/web/ui/command.py +118 -0
- pyjs/web/ui/context_menu.py +65 -0
- pyjs/web/ui/date_picker.py +24 -0
- pyjs/web/ui/dialog.py +289 -0
- pyjs/web/ui/direction.py +9 -0
- pyjs/web/ui/drawer.py +72 -0
- pyjs/web/ui/dropdown_menu.py +239 -0
- pyjs/web/ui/empty.py +69 -0
- pyjs/web/ui/field.py +96 -0
- pyjs/web/ui/floating.py +90 -0
- pyjs/web/ui/hover_card.py +128 -0
- pyjs/web/ui/input.py +48 -0
- pyjs/web/ui/input_group.py +77 -0
- pyjs/web/ui/input_otp.py +76 -0
- pyjs/web/ui/item.py +78 -0
- pyjs/web/ui/kbd.py +26 -0
- pyjs/web/ui/label.py +14 -0
- pyjs/web/ui/marker.py +29 -0
- pyjs/web/ui/menubar.py +52 -0
- pyjs/web/ui/message.py +57 -0
- pyjs/web/ui/message_scroller.py +45 -0
- pyjs/web/ui/native_select.py +47 -0
- pyjs/web/ui/navigation_menu.py +110 -0
- pyjs/web/ui/pagination.py +63 -0
- pyjs/web/ui/popover.py +217 -0
- pyjs/web/ui/progress.py +44 -0
- pyjs/web/ui/radio_group.py +129 -0
- pyjs/web/ui/resizable.py +194 -0
- pyjs/web/ui/scroll_area.py +29 -0
- pyjs/web/ui/select.py +83 -0
- pyjs/web/ui/separator.py +24 -0
- pyjs/web/ui/sheet.py +77 -0
- pyjs/web/ui/sidebar.py +162 -0
- pyjs/web/ui/skeleton.py +15 -0
- pyjs/web/ui/slider.py +46 -0
- pyjs/web/ui/sonner.py +70 -0
- pyjs/web/ui/spinner.py +14 -0
- pyjs/web/ui/switch.py +35 -0
- pyjs/web/ui/table.py +84 -0
- pyjs/web/ui/tabs.py +167 -0
- pyjs/web/ui/text.py +15 -0
- pyjs/web/ui/textarea.py +28 -0
- pyjs/web/ui/toggle.py +109 -0
- pyjs/web/ui/tooltip.py +164 -0
- pyjs/web/ui/typography.py +79 -0
- pyjs/web/url.py +76 -0
- pyjs/web/webgpu.py +496 -0
- pyjs-0.0.1.dist-info/METADATA +441 -0
- pyjs-0.0.1.dist-info/RECORD +146 -0
- pyjs-0.0.1.dist-info/WHEEL +5 -0
- pyjs-0.0.1.dist-info/entry_points.txt +5 -0
- pyjs-0.0.1.dist-info/licenses/LICENSE +28 -0
- pyjs-0.0.1.dist-info/top_level.txt +1 -0
pyjs/decorators.py
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import inspect
|
|
2
|
+
|
|
3
|
+
DECORATIONS = {
|
|
4
|
+
# safe to analyze and transpile
|
|
5
|
+
"__js__", # bool
|
|
6
|
+
|
|
7
|
+
# do not analyze or transpile
|
|
8
|
+
# eg. class is marked safe but you want to exclude a specific method
|
|
9
|
+
"__nojs__", # bool
|
|
10
|
+
|
|
11
|
+
# include in output even if not visited
|
|
12
|
+
"__js_include__", # bool
|
|
13
|
+
|
|
14
|
+
# use different implementation of a Python method on client by
|
|
15
|
+
# replacing it with the function in the decoration, which is then
|
|
16
|
+
# transpiled
|
|
17
|
+
"__js_replace__", # function
|
|
18
|
+
|
|
19
|
+
# similar to __js_replace__ but no transpiling occurs; instead,
|
|
20
|
+
# the function is expected to return the JavaScript source as string
|
|
21
|
+
"__js_rewrite_func__", # function
|
|
22
|
+
|
|
23
|
+
# returns JavaScript source as string, replaces the code at
|
|
24
|
+
# every call site where this function is called
|
|
25
|
+
"__js_rewrite_call_site__", # function
|
|
26
|
+
|
|
27
|
+
# returns JavaScript source as string, appending the code
|
|
28
|
+
# after the definition of the wrapped code object
|
|
29
|
+
"__js_append__", # function
|
|
30
|
+
|
|
31
|
+
# dispatch a builtin function through a special method on its first argument
|
|
32
|
+
"__js_dunder_dispatch__", # str
|
|
33
|
+
|
|
34
|
+
# safe to analyze but don't include in output
|
|
35
|
+
"__builtin__", # bool
|
|
36
|
+
|
|
37
|
+
# do not analyze function
|
|
38
|
+
"__no_analyze__", # bool
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def dunder_dispatch(method_name: str):
|
|
43
|
+
def decorate(func):
|
|
44
|
+
func.__js_dunder_dispatch__ = method_name
|
|
45
|
+
return func
|
|
46
|
+
|
|
47
|
+
return decorate
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def is_cls_or_func(obj):
|
|
51
|
+
return inspect.isfunction(obj) or inspect.isclass(obj)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def has_inline_decorator(obj):
|
|
55
|
+
return getattr(obj, "__dict__", {}).get("__js_rewrite_call_site__", False)
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def has_source_decorator(obj):
|
|
59
|
+
return getattr(obj, "__dict__", {}).get("__js_rewrite_func__", False)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def has_no_analyze(obj):
|
|
63
|
+
return getattr(obj, "__dict__", {}).get("__no_analyze__", False)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def should_analyze_func_body(obj):
|
|
67
|
+
return not (
|
|
68
|
+
has_inline_decorator(obj) or
|
|
69
|
+
has_source_decorator(obj) or
|
|
70
|
+
has_no_analyze(obj)
|
|
71
|
+
)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def has_include_decorator(obj):
|
|
75
|
+
return getattr(obj, "__dict__", {}).get("__js_include__", False)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def should_include(obj):
|
|
79
|
+
if has_inline_decorator(obj.py_obj) and not has_include_decorator(obj.py_obj):
|
|
80
|
+
return False
|
|
81
|
+
return (
|
|
82
|
+
has_include_decorator(obj.py_obj) or
|
|
83
|
+
bool(obj.visited)
|
|
84
|
+
) and not getattr(obj.py_obj, "__dict__", {}).get("__builtin__", False)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def js(*cls_func_args, inline=None, builtin=None, include=None, analyze=None, client=None):
|
|
88
|
+
|
|
89
|
+
class Wrapper:
|
|
90
|
+
|
|
91
|
+
def __init__(self, inline, builtin, include, analyze, client):
|
|
92
|
+
self.inline = inline
|
|
93
|
+
self.builtin = builtin
|
|
94
|
+
self.include = include
|
|
95
|
+
self.analyze = analyze
|
|
96
|
+
self.client_implementation = client
|
|
97
|
+
|
|
98
|
+
def __call__(self, cls_func):
|
|
99
|
+
cls_func.__js__ = True
|
|
100
|
+
|
|
101
|
+
if self.builtin is True:
|
|
102
|
+
cls_func.__builtin__ = True
|
|
103
|
+
|
|
104
|
+
if self.inline is not None:
|
|
105
|
+
if isinstance(self.inline, str):
|
|
106
|
+
inline_template = self.inline.replace("{*args}", "{_args_suffix}")
|
|
107
|
+
cls_func.__js_rewrite_call_site__ = (
|
|
108
|
+
lambda **kwargs: inline_template.format(**kwargs)
|
|
109
|
+
)
|
|
110
|
+
else:
|
|
111
|
+
cls_func.__js_rewrite_call_site__ = self.inline
|
|
112
|
+
|
|
113
|
+
if inspect.isclass(cls_func):
|
|
114
|
+
assert self.include is None, (
|
|
115
|
+
"@js(include=True) is only supported for class methods."
|
|
116
|
+
)
|
|
117
|
+
assert self.client_implementation is None, (
|
|
118
|
+
"@js(client=...) is not supported for classes."
|
|
119
|
+
)
|
|
120
|
+
return cls_func
|
|
121
|
+
|
|
122
|
+
if self.client_implementation is not None:
|
|
123
|
+
assert self.inline is None, "@js(client=...) cannot be combined with inline=."
|
|
124
|
+
cls_func.__js_replace__ = self.client_implementation
|
|
125
|
+
|
|
126
|
+
if self.include is True:
|
|
127
|
+
assert "." in cls_func.__qualname__, (
|
|
128
|
+
"@js(include=True) is only supported for class methods."
|
|
129
|
+
)
|
|
130
|
+
cls_func.__js_include__ = True
|
|
131
|
+
|
|
132
|
+
if self.analyze is False:
|
|
133
|
+
cls_func.__no_analyze__ = True
|
|
134
|
+
|
|
135
|
+
def client(new_func: type):
|
|
136
|
+
cls_func.__js_replace__ = new_func
|
|
137
|
+
return cls_func
|
|
138
|
+
cls_func.client = client
|
|
139
|
+
|
|
140
|
+
def source(new_func: type):
|
|
141
|
+
cls_func.__js_rewrite_func__ = new_func
|
|
142
|
+
return cls_func
|
|
143
|
+
cls_func.source = source
|
|
144
|
+
|
|
145
|
+
def inline(new_func: type):
|
|
146
|
+
cls_func.__js_rewrite_call_site__ = new_func
|
|
147
|
+
return cls_func
|
|
148
|
+
cls_func.inline = inline
|
|
149
|
+
|
|
150
|
+
return cls_func
|
|
151
|
+
|
|
152
|
+
if cls_func_args and is_cls_or_func(cls_func_args[0]):
|
|
153
|
+
return Wrapper(inline, builtin, include, analyze, client)(cls_func_args[0])
|
|
154
|
+
|
|
155
|
+
return Wrapper(inline, builtin, include, analyze, client)
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def nojs(cls_func):
|
|
159
|
+
cls_func.__js__ = False
|
|
160
|
+
return cls_func
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def js_object(obj, builtin=None, include=None):
|
|
164
|
+
obj.__js__ = True
|
|
165
|
+
obj.__static__ = True
|
|
166
|
+
if builtin is True:
|
|
167
|
+
obj.__builtin__ = builtin
|
|
168
|
+
if include is True:
|
|
169
|
+
obj.__js_include__ = True
|
|
170
|
+
return obj
|
pyjs/docs/__init__.py
ADDED
pyjs/docs/builder.py
ADDED
|
@@ -0,0 +1,351 @@
|
|
|
1
|
+
import html
|
|
2
|
+
import json
|
|
3
|
+
import os
|
|
4
|
+
import posixpath
|
|
5
|
+
import re
|
|
6
|
+
import shutil
|
|
7
|
+
from pathlib import Path
|
|
8
|
+
from urllib.parse import urlsplit
|
|
9
|
+
|
|
10
|
+
from pyjs.transpiler import bundle
|
|
11
|
+
from pyjs.web.domx import tag
|
|
12
|
+
from pyjs.web.ssr import raw_html, render_document
|
|
13
|
+
|
|
14
|
+
from .config import DocsConfig, load_config
|
|
15
|
+
from .markdown import parse_document, render_markdown
|
|
16
|
+
from .models import NavNode, Page
|
|
17
|
+
from .navigation import build_navigation, humanize, navigation_pages, route_for
|
|
18
|
+
from .ui import main as docs_main
|
|
19
|
+
|
|
20
|
+
LINK = re.compile(r'(?P<attribute>href|src)="(?P<url>[^"]+)"')
|
|
21
|
+
FIRST_HEADING = re.compile(r"<h1[^>]*>(.*?)</h1>", re.DOTALL)
|
|
22
|
+
TAG = re.compile(r"<[^>]+>")
|
|
23
|
+
SPACE = re.compile(r"\s+")
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _prefixed_route(route: str, prefix: str) -> str:
|
|
27
|
+
prefix = prefix.strip("/")
|
|
28
|
+
if not prefix:
|
|
29
|
+
return route
|
|
30
|
+
if route == "/":
|
|
31
|
+
return f"/{prefix}/"
|
|
32
|
+
return f"/{prefix}{route}"
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _page_title(html_source: str, fallback: str) -> str:
|
|
36
|
+
match = FIRST_HEADING.search(html_source)
|
|
37
|
+
if match is None:
|
|
38
|
+
return fallback
|
|
39
|
+
return html.unescape(TAG.sub("", match.group(1))).replace("¶", "").strip()
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def _discover_pages(config: DocsConfig) -> list[Page]:
|
|
43
|
+
if not config.docs_dir.is_dir():
|
|
44
|
+
raise FileNotFoundError(f"Documentation directory does not exist: {config.docs_dir}")
|
|
45
|
+
pages = []
|
|
46
|
+
routes = {}
|
|
47
|
+
for source_path in sorted(config.docs_dir.rglob("*.md")):
|
|
48
|
+
if any(part.startswith(".") for part in source_path.relative_to(config.docs_dir).parts):
|
|
49
|
+
continue
|
|
50
|
+
relative_path = source_path.relative_to(config.docs_dir)
|
|
51
|
+
metadata, source = parse_document(source_path)
|
|
52
|
+
rendered, headings, inferred_description = render_markdown(source)
|
|
53
|
+
source_route = route_for(relative_path)
|
|
54
|
+
route = _prefixed_route(source_route, config.route_prefix)
|
|
55
|
+
if route in routes:
|
|
56
|
+
raise ValueError(f"{source_path} and {routes[route]} both produce the route {route!r}")
|
|
57
|
+
routes[route] = source_path
|
|
58
|
+
route_parts = route.strip("/").split("/") if route != "/" else []
|
|
59
|
+
output_path = config.site_dir.joinpath(*route_parts, "index.html")
|
|
60
|
+
fallback = humanize(relative_path.stem)
|
|
61
|
+
default_order = 0 if source_route == "/" else 1000
|
|
62
|
+
pages.append(
|
|
63
|
+
Page(
|
|
64
|
+
source_path=source_path,
|
|
65
|
+
relative_path=relative_path,
|
|
66
|
+
output_path=output_path,
|
|
67
|
+
route=route,
|
|
68
|
+
title=str(metadata.get("title", _page_title(rendered, fallback))),
|
|
69
|
+
description=str(metadata.get("description", inferred_description)),
|
|
70
|
+
order=float(metadata.get("order", default_order)),
|
|
71
|
+
hidden=bool(metadata.get("hidden", False)),
|
|
72
|
+
html=rendered,
|
|
73
|
+
headings=headings,
|
|
74
|
+
)
|
|
75
|
+
)
|
|
76
|
+
if not pages:
|
|
77
|
+
raise ValueError(f"No Markdown pages found in {config.docs_dir}")
|
|
78
|
+
return pages
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def _relative_url(page: Page, target: Page, fragment: str = "") -> str:
|
|
82
|
+
relative = os.path.relpath(target.output_path.parent, page.output_path.parent)
|
|
83
|
+
relative = relative.replace(os.sep, "/")
|
|
84
|
+
href = "./" if relative == "." else relative.rstrip("/") + "/"
|
|
85
|
+
return href + fragment
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def _site_root_url(page: Page, config: DocsConfig) -> str:
|
|
89
|
+
relative = os.path.relpath(config.site_dir, page.output_path.parent).replace(os.sep, "/")
|
|
90
|
+
return "./" if relative == "." else relative.rstrip("/") + "/"
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
def _docs_output_root(config: DocsConfig) -> Path:
|
|
94
|
+
return config.site_dir / config.route_prefix.strip("/")
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _docs_home(pages: list[Page]) -> Page:
|
|
98
|
+
return next(
|
|
99
|
+
(page for page in pages if page.relative_path == Path("index.md")),
|
|
100
|
+
pages[0],
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _rewrite_links(page: Page, pages: list[Page], config: DocsConfig) -> str:
|
|
105
|
+
by_source = {candidate.relative_path: candidate for candidate in pages}
|
|
106
|
+
|
|
107
|
+
def replace(match: re.Match):
|
|
108
|
+
url = match.group("url")
|
|
109
|
+
parsed = urlsplit(url)
|
|
110
|
+
if parsed.scheme or parsed.netloc or url.startswith(("#", "/")):
|
|
111
|
+
return match.group(0)
|
|
112
|
+
source_target = page.relative_path.parent / parsed.path
|
|
113
|
+
normalized = Path(posixpath.normpath(source_target.as_posix()))
|
|
114
|
+
target_page = by_source.get(normalized)
|
|
115
|
+
if target_page is not None:
|
|
116
|
+
suffix = f"#{parsed.fragment}" if parsed.fragment else ""
|
|
117
|
+
rewritten = _relative_url(page, target_page, suffix)
|
|
118
|
+
else:
|
|
119
|
+
asset = config.docs_dir / normalized
|
|
120
|
+
if not asset.is_file():
|
|
121
|
+
return match.group(0)
|
|
122
|
+
relative = os.path.relpath(
|
|
123
|
+
_docs_output_root(config) / normalized,
|
|
124
|
+
page.output_path.parent,
|
|
125
|
+
).replace(os.sep, "/")
|
|
126
|
+
rewritten = relative
|
|
127
|
+
if parsed.fragment:
|
|
128
|
+
rewritten += f"#{parsed.fragment}"
|
|
129
|
+
return f'{match.group("attribute")}="{html.escape(rewritten, quote=True)}"'
|
|
130
|
+
|
|
131
|
+
return LINK.sub(replace, page.html)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def _render_nav_node(node: NavNode, current: Page, depth: int) -> str:
|
|
135
|
+
children = "".join(_render_nav_node(child, current, depth + 1) for child in node.children)
|
|
136
|
+
link = ""
|
|
137
|
+
if node.page is not None:
|
|
138
|
+
current_attribute = ' aria-current="page"' if node.page is current else ""
|
|
139
|
+
link = (
|
|
140
|
+
f'<a class="nav-link" href="{_relative_url(current, node.page)}"'
|
|
141
|
+
f"{current_attribute}>{html.escape(node.title)}</a>"
|
|
142
|
+
)
|
|
143
|
+
elif depth > 0:
|
|
144
|
+
link = f'<p class="nav-heading">{html.escape(node.title)}</p>'
|
|
145
|
+
if children:
|
|
146
|
+
list_class = "nav-list" if depth == 0 else "nav-nested"
|
|
147
|
+
children = f'<ul class="{list_class}">{children}</ul>'
|
|
148
|
+
if depth == 0:
|
|
149
|
+
return children
|
|
150
|
+
return f'<li class="nav-group">{link}{children}</li>'
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _render_navigation(root: NavNode, current: Page) -> str:
|
|
154
|
+
return _render_nav_node(root, current, 0)
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def _render_outline(page: Page, config: DocsConfig) -> str:
|
|
158
|
+
links = []
|
|
159
|
+
for heading in page.headings:
|
|
160
|
+
if heading.level == 1:
|
|
161
|
+
continue
|
|
162
|
+
links.append(
|
|
163
|
+
f'<li><a class="outline-link outline-level-{heading.level}" '
|
|
164
|
+
f'href="#{html.escape(heading.id, quote=True)}">'
|
|
165
|
+
f"{html.escape(heading.title)}</a></li>"
|
|
166
|
+
)
|
|
167
|
+
edit_link = ""
|
|
168
|
+
if config.repo_url:
|
|
169
|
+
source_path = page.source_path.relative_to(config.project_root).as_posix()
|
|
170
|
+
edit_link = (
|
|
171
|
+
f'<a class="outline-edit" href="{html.escape(config.repo_url, quote=True)}'
|
|
172
|
+
f'/edit/main/{html.escape(source_path, quote=True)}">Edit this page on GitHub</a>'
|
|
173
|
+
)
|
|
174
|
+
if not links and not edit_link:
|
|
175
|
+
return ""
|
|
176
|
+
return (
|
|
177
|
+
'<p class="outline-title">On this page</p>'
|
|
178
|
+
f'<ul class="outline-list">{"".join(links)}</ul>{edit_link}'
|
|
179
|
+
)
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
def _render_breadcrumbs(page: Page, pages: list[Page]) -> str:
|
|
183
|
+
home = _docs_home(pages)
|
|
184
|
+
crumbs = ['<a href="' + _relative_url(page, home) + '">Docs</a>']
|
|
185
|
+
for part in page.relative_path.parent.parts:
|
|
186
|
+
crumbs.append(f"<span>/</span><span>{html.escape(humanize(part))}</span>")
|
|
187
|
+
return f'<nav class="breadcrumbs" aria-label="Breadcrumb">{"".join(crumbs)}</nav>'
|
|
188
|
+
|
|
189
|
+
|
|
190
|
+
def _pager_link(page: Page, target: Page | None, direction: str) -> str:
|
|
191
|
+
if target is None:
|
|
192
|
+
return "<span></span>"
|
|
193
|
+
css_direction = "pager-next" if direction == "Next" else "pager-previous"
|
|
194
|
+
return (
|
|
195
|
+
f'<a class="pager-link {css_direction}" href="{_relative_url(page, target)}">'
|
|
196
|
+
f'<span class="pager-direction">{direction}</span>'
|
|
197
|
+
f'<span class="pager-title">{html.escape(target.title)}</span></a>'
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
def _render_article(page: Page, pages: list[Page], config: DocsConfig) -> str:
|
|
202
|
+
description = ""
|
|
203
|
+
if page.description:
|
|
204
|
+
description = f'<p class="page-description">{html.escape(page.description)}</p>'
|
|
205
|
+
section = (
|
|
206
|
+
humanize(page.relative_path.parts[0]) if len(page.relative_path.parts) > 1 else "Guide"
|
|
207
|
+
)
|
|
208
|
+
pager = (
|
|
209
|
+
'<nav class="docs-pager" aria-label="Documentation pages">'
|
|
210
|
+
f"{_pager_link(page, page.previous, 'Previous')}"
|
|
211
|
+
f"{_pager_link(page, page.next, 'Next')}</nav>"
|
|
212
|
+
)
|
|
213
|
+
content = _rewrite_links(page, pages, config)
|
|
214
|
+
return (
|
|
215
|
+
'<article class="docs-article">'
|
|
216
|
+
f"{_render_breadcrumbs(page, pages)}"
|
|
217
|
+
f'<p class="page-eyebrow">{html.escape(section)}</p>'
|
|
218
|
+
f'<h1 class="page-title">{html.escape(page.title)}</h1>'
|
|
219
|
+
f"{description}"
|
|
220
|
+
f'<div class="docs-content">{content}</div>'
|
|
221
|
+
f"{pager}</article>"
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def _search_records(pages: list[Page], current: Page) -> str:
|
|
226
|
+
records = []
|
|
227
|
+
for page in pages:
|
|
228
|
+
text = html.unescape(SPACE.sub(" ", TAG.sub(" ", page.html))).replace("¶", "").strip()
|
|
229
|
+
records.append(
|
|
230
|
+
{
|
|
231
|
+
"title": page.title,
|
|
232
|
+
"section": humanize(page.relative_path.parent.name or "Guide"),
|
|
233
|
+
"text": text[:240],
|
|
234
|
+
"url": _relative_url(current, page),
|
|
235
|
+
}
|
|
236
|
+
)
|
|
237
|
+
for heading in page.headings:
|
|
238
|
+
if heading.level == 1:
|
|
239
|
+
continue
|
|
240
|
+
records.append(
|
|
241
|
+
{
|
|
242
|
+
"title": heading.title,
|
|
243
|
+
"section": page.title,
|
|
244
|
+
"text": text[:240],
|
|
245
|
+
"url": _relative_url(current, page, f"#{heading.id}"),
|
|
246
|
+
}
|
|
247
|
+
)
|
|
248
|
+
return json.dumps(records, separators=(",", ":")).replace("</", "<\\/")
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def _render_page(
|
|
252
|
+
page: Page,
|
|
253
|
+
pages: list[Page],
|
|
254
|
+
navigation: NavNode,
|
|
255
|
+
config: DocsConfig,
|
|
256
|
+
) -> str:
|
|
257
|
+
asset_prefix = os.path.relpath(config.site_dir / "_pyjs", page.output_path.parent)
|
|
258
|
+
asset_prefix = asset_prefix.replace(os.sep, "/")
|
|
259
|
+
home = _docs_home(pages)
|
|
260
|
+
site_root = _site_root_url(page, config)
|
|
261
|
+
site_navigation = ""
|
|
262
|
+
if config.route_prefix:
|
|
263
|
+
site_navigation = raw_html(
|
|
264
|
+
'<nav class="site-links" aria-label="Main navigation">'
|
|
265
|
+
f'<a href="{site_root}">Home</a>'
|
|
266
|
+
f'<a href="{site_root}examples/">Examples</a>'
|
|
267
|
+
f'<a href="{_relative_url(page, home)}" aria-current="page">Docs</a>'
|
|
268
|
+
"</nav>"
|
|
269
|
+
)
|
|
270
|
+
app = docs_main(
|
|
271
|
+
config.site_name,
|
|
272
|
+
_relative_url(page, home),
|
|
273
|
+
raw_html(_render_navigation(navigation, page)),
|
|
274
|
+
raw_html(_render_article(page, pages, config)),
|
|
275
|
+
raw_html(_render_outline(page, config)),
|
|
276
|
+
_search_records(pages, page),
|
|
277
|
+
site_root,
|
|
278
|
+
site_navigation,
|
|
279
|
+
)
|
|
280
|
+
canonical = config.site_url.rstrip("/") + page.route if config.site_url else ""
|
|
281
|
+
head_children = [
|
|
282
|
+
tag("meta", {"charset": "utf-8"}),
|
|
283
|
+
tag("meta", {"name": "viewport", "content": "width=device-width, initial-scale=1"}),
|
|
284
|
+
tag("title", f"{page.title} · {config.site_name}"),
|
|
285
|
+
tag("meta", {"name": "description", "content": page.description}),
|
|
286
|
+
tag("link", {"rel": "icon", "href": f"{asset_prefix}/favicon.svg"}),
|
|
287
|
+
tag("link", {"rel": "stylesheet", "href": f"{asset_prefix}/docs.css"}),
|
|
288
|
+
]
|
|
289
|
+
if canonical:
|
|
290
|
+
head_children.append(tag("link", {"rel": "canonical", "href": canonical}))
|
|
291
|
+
document = tag(
|
|
292
|
+
"html",
|
|
293
|
+
{"lang": "en"},
|
|
294
|
+
tag("head", *head_children),
|
|
295
|
+
tag(
|
|
296
|
+
"body",
|
|
297
|
+
app,
|
|
298
|
+
tag("script", {"src": f"{asset_prefix}/docs.js", "type": "module"}, " "),
|
|
299
|
+
),
|
|
300
|
+
)
|
|
301
|
+
return render_document(document)
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def _copy_assets(config: DocsConfig):
|
|
305
|
+
for source in config.docs_dir.rglob("*"):
|
|
306
|
+
if not source.is_file() or source.suffix == ".md":
|
|
307
|
+
continue
|
|
308
|
+
relative = source.relative_to(config.docs_dir)
|
|
309
|
+
if any(part.startswith(".") for part in relative.parts):
|
|
310
|
+
continue
|
|
311
|
+
target = _docs_output_root(config) / relative
|
|
312
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
313
|
+
shutil.copy2(source, target)
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def build(config: DocsConfig | None = None, *, clean: bool = True) -> Path:
|
|
317
|
+
config = config or load_config()
|
|
318
|
+
if config.site_dir == config.docs_dir or config.docs_dir in config.site_dir.parents:
|
|
319
|
+
raise ValueError("Documentation output must be outside the source directory")
|
|
320
|
+
pages = _discover_pages(config)
|
|
321
|
+
navigation = build_navigation(pages)
|
|
322
|
+
ordered_pages = navigation_pages(navigation)
|
|
323
|
+
hidden_pages = [page for page in pages if page.hidden]
|
|
324
|
+
pages = ordered_pages + hidden_pages
|
|
325
|
+
for index, page in enumerate(ordered_pages):
|
|
326
|
+
page.previous = ordered_pages[index - 1] if index else None
|
|
327
|
+
page.next = ordered_pages[index + 1] if index + 1 < len(ordered_pages) else None
|
|
328
|
+
|
|
329
|
+
if clean:
|
|
330
|
+
shutil.rmtree(config.site_dir, ignore_errors=True)
|
|
331
|
+
assets_dir = config.site_dir / "_pyjs"
|
|
332
|
+
assets_dir.mkdir(parents=True)
|
|
333
|
+
javascript, generated_css = bundle(docs_main, include_css=False)
|
|
334
|
+
docs_css = Path(__file__).with_name("style.css").read_text(encoding="utf-8")
|
|
335
|
+
(assets_dir / "docs.js").write_text(javascript, encoding="utf-8")
|
|
336
|
+
(assets_dir / "docs.css").write_text(generated_css + docs_css, encoding="utf-8")
|
|
337
|
+
shutil.copy2(Path(__file__).with_name("favicon.svg"), assets_dir / "favicon.svg")
|
|
338
|
+
_copy_assets(config)
|
|
339
|
+
|
|
340
|
+
for page in pages:
|
|
341
|
+
page.output_path.parent.mkdir(parents=True, exist_ok=True)
|
|
342
|
+
page.output_path.write_text(
|
|
343
|
+
_render_page(page, pages, navigation, config),
|
|
344
|
+
encoding="utf-8",
|
|
345
|
+
)
|
|
346
|
+
home = _docs_home(pages)
|
|
347
|
+
if not config.route_prefix:
|
|
348
|
+
shutil.copy2(home.output_path, config.site_dir / "404.html")
|
|
349
|
+
if config.nojekyll:
|
|
350
|
+
(config.site_dir / ".nojekyll").touch()
|
|
351
|
+
return config.site_dir
|
pyjs/docs/cli.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from functools import partial
|
|
2
|
+
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
|
|
5
|
+
from .builder import build
|
|
6
|
+
from .config import load_config
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def _serve(project_root: Path, host: str, port: int):
|
|
10
|
+
output = build(load_config(project_root))
|
|
11
|
+
handler = partial(SimpleHTTPRequestHandler, directory=str(output))
|
|
12
|
+
server = ThreadingHTTPServer((host, port), handler)
|
|
13
|
+
display_host = host or "localhost"
|
|
14
|
+
print(f"Built documentation in {output}")
|
|
15
|
+
print(f"Serving documentation at http://{display_host}:{server.server_port}/")
|
|
16
|
+
try:
|
|
17
|
+
server.serve_forever()
|
|
18
|
+
except KeyboardInterrupt:
|
|
19
|
+
print("\nStopping documentation server.")
|
|
20
|
+
finally:
|
|
21
|
+
server.server_close()
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _add_project_dir(command):
|
|
25
|
+
command.add_argument(
|
|
26
|
+
"--project-dir",
|
|
27
|
+
type=Path,
|
|
28
|
+
default=Path.cwd(),
|
|
29
|
+
help="project containing pyproject.toml and docs/ (default: current directory)",
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def add_parser(subcommands):
|
|
34
|
+
docs = subcommands.add_parser(
|
|
35
|
+
"docs",
|
|
36
|
+
help="build and serve static documentation",
|
|
37
|
+
description="Build static documentation with PyJS.",
|
|
38
|
+
)
|
|
39
|
+
docs_commands = docs.add_subparsers(dest="docs_command", required=True)
|
|
40
|
+
build_command = docs_commands.add_parser("build", help="build the static site")
|
|
41
|
+
_add_project_dir(build_command)
|
|
42
|
+
serve = docs_commands.add_parser("serve", help="build and serve the static site")
|
|
43
|
+
_add_project_dir(serve)
|
|
44
|
+
serve.add_argument("--host", default="localhost")
|
|
45
|
+
serve.add_argument("--port", type=int, default=8000)
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def run(args) -> int:
|
|
49
|
+
project_root = args.project_dir.resolve()
|
|
50
|
+
if args.docs_command == "build":
|
|
51
|
+
output = build(load_config(project_root))
|
|
52
|
+
print(f"Built documentation in {output}")
|
|
53
|
+
return 0
|
|
54
|
+
_serve(project_root, args.host, args.port)
|
|
55
|
+
return 0
|
pyjs/docs/config.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from pathlib import Path
|
|
3
|
+
from tomllib import load
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass(frozen=True)
|
|
7
|
+
class DocsConfig:
|
|
8
|
+
project_root: Path
|
|
9
|
+
docs_dir: Path
|
|
10
|
+
site_dir: Path
|
|
11
|
+
site_name: str
|
|
12
|
+
site_url: str
|
|
13
|
+
repo_url: str
|
|
14
|
+
repo_name: str
|
|
15
|
+
route_prefix: str = ""
|
|
16
|
+
nojekyll: bool = True
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _project_name(name: str) -> str:
|
|
20
|
+
return "PyJS" if name.lower() == "pyjs" else name.replace("-", " ").title()
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def load_config(project_root: str | Path = ".") -> DocsConfig:
|
|
24
|
+
project_root = Path(project_root).resolve()
|
|
25
|
+
pyproject_path = project_root / "pyproject.toml"
|
|
26
|
+
pyproject = {}
|
|
27
|
+
if pyproject_path.exists():
|
|
28
|
+
with pyproject_path.open("rb") as file:
|
|
29
|
+
pyproject = load(file)
|
|
30
|
+
|
|
31
|
+
project = pyproject.get("project", {})
|
|
32
|
+
docs = pyproject.get("tool", {}).get("pyjs", {}).get("docs", {})
|
|
33
|
+
urls = project.get("urls", {})
|
|
34
|
+
repo_url = docs.get("repo_url", urls.get("Source", ""))
|
|
35
|
+
return DocsConfig(
|
|
36
|
+
project_root=project_root,
|
|
37
|
+
docs_dir=project_root / docs.get("source", "docs"),
|
|
38
|
+
site_dir=project_root / docs.get("output", "site"),
|
|
39
|
+
site_name=docs.get("site_name", _project_name(project.get("name", project_root.name))),
|
|
40
|
+
site_url=docs.get("site_url", urls.get("Documentation", "")),
|
|
41
|
+
repo_url=repo_url,
|
|
42
|
+
repo_name=docs.get("repo_name", repo_url.removesuffix("/").rsplit("/", 2)[-1]),
|
|
43
|
+
route_prefix=docs.get("route_prefix", ""),
|
|
44
|
+
nojekyll=docs.get("nojekyll", True),
|
|
45
|
+
)
|
pyjs/docs/favicon.svg
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">
|
|
2
|
+
<defs>
|
|
3
|
+
<linearGradient id="background" x1="8" y1="8" x2="56" y2="56">
|
|
4
|
+
<stop stop-color="#6969e8"/>
|
|
5
|
+
<stop offset="1" stop-color="#4747bc"/>
|
|
6
|
+
</linearGradient>
|
|
7
|
+
</defs>
|
|
8
|
+
<rect width="64" height="64" rx="16" fill="url(#background)"/>
|
|
9
|
+
<text
|
|
10
|
+
x="32"
|
|
11
|
+
y="40"
|
|
12
|
+
fill="white"
|
|
13
|
+
font-family="ui-monospace, monospace"
|
|
14
|
+
font-size="25"
|
|
15
|
+
font-weight="800"
|
|
16
|
+
text-anchor="middle"
|
|
17
|
+
>Py</text>
|
|
18
|
+
</svg>
|