|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | + |
| 18 | +import pathlib |
| 19 | +from html.parser import HTMLParser |
| 20 | + |
| 21 | +import aiofiles |
| 22 | +import aiofiles.os |
| 23 | +import markupsafe |
| 24 | +import quart |
| 25 | + |
| 26 | +import atr.config as config |
| 27 | +import atr.route as route |
| 28 | +import atr.template as template |
| 29 | + |
| 30 | + |
| 31 | +class H1Parser(HTMLParser): |
| 32 | + def __init__(self) -> None: |
| 33 | + super().__init__() |
| 34 | + self.h1_content: str | None = None |
| 35 | + self._in_h1 = False |
| 36 | + |
| 37 | + def handle_starttag(self, tag: str, attrs: list[tuple[str, str | None]]) -> None: |
| 38 | + if (tag == "h1") and (self.h1_content is None): |
| 39 | + self._in_h1 = True |
| 40 | + |
| 41 | + def handle_endtag(self, tag: str) -> None: |
| 42 | + if tag == "h1": |
| 43 | + self._in_h1 = False |
| 44 | + |
| 45 | + def handle_data(self, data: str) -> None: |
| 46 | + if self._in_h1 and (self.h1_content is None): |
| 47 | + self.h1_content = data.strip() |
| 48 | + |
| 49 | + |
| 50 | +@route.public("/manual/") |
| 51 | +async def index(session: route.CommitterSession | None) -> str: |
| 52 | + return await _serve_manual_page("index") |
| 53 | + |
| 54 | + |
| 55 | +@route.public("/manual/<path:page>") |
| 56 | +async def page(session: route.CommitterSession | None, page: str) -> str: |
| 57 | + return await _serve_manual_page(page) |
| 58 | + |
| 59 | + |
| 60 | +async def _serve_manual_page(page: str) -> str: |
| 61 | + manual_dir = pathlib.Path(config.get().PROJECT_ROOT) / "atr" / "manual" |
| 62 | + |
| 63 | + if not page.endswith(".html"): |
| 64 | + page = f"{page}.html" |
| 65 | + |
| 66 | + file_path = manual_dir / page |
| 67 | + |
| 68 | + manual_root = manual_dir.resolve() |
| 69 | + try: |
| 70 | + resolved_file = file_path.resolve() |
| 71 | + except FileNotFoundError: |
| 72 | + quart.abort(404) |
| 73 | + try: |
| 74 | + resolved_file.relative_to(manual_root) |
| 75 | + except ValueError: |
| 76 | + quart.abort(404) |
| 77 | + |
| 78 | + if not await aiofiles.os.path.exists(resolved_file): |
| 79 | + quart.abort(404) |
| 80 | + |
| 81 | + if not await aiofiles.os.path.isfile(resolved_file): |
| 82 | + quart.abort(404) |
| 83 | + |
| 84 | + async with aiofiles.open(resolved_file, encoding="utf-8") as handle: |
| 85 | + html_content = await handle.read() |
| 86 | + |
| 87 | + safe_content = markupsafe.Markup(html_content) |
| 88 | + |
| 89 | + filename_title = resolved_file.stem.replace("-", " ").replace("_", " ").title() |
| 90 | + try: |
| 91 | + parser = H1Parser() |
| 92 | + first_lines = "\n".join(html_content.split("\n")[:8]) |
| 93 | + parser.feed(first_lines) |
| 94 | + title = parser.h1_content or filename_title |
| 95 | + except Exception: |
| 96 | + title = filename_title |
| 97 | + |
| 98 | + return await template.blank(title=title, content=safe_content) |
0 commit comments