Skip to content

Commit d064f6c

Browse files
committed
5.15.6 - Upgrades for html directory viewer
1 parent 80240a0 commit d064f6c

File tree

6 files changed

+62
-32
lines changed

6 files changed

+62
-32
lines changed

.github/workflows/python-package.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ jobs:
7878
7979
- run: |
8080
mk python-release owner=libre-embedded \
81-
repo=runtimepy version=5.15.5
81+
repo=runtimepy version=5.15.6
8282
if: |
8383
matrix.python-version == '3.12'
8484
&& matrix.system == 'ubuntu-latest'

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
=====================================
33
generator=datazen
44
version=3.2.3
5-
hash=fd0e57419cf9437a64c4f82cae3625f0
5+
hash=0f8ffdbd46445346cb834d066f58e765
66
=====================================
77
-->
88

9-
# runtimepy ([5.15.5](https://pypi.org/project/runtimepy/))
9+
# runtimepy ([5.15.6](https://pypi.org/project/runtimepy/))
1010

1111
[![python](https://img.shields.io/pypi/pyversions/runtimepy.svg)](https://pypi.org/project/runtimepy/)
1212
![Build Status](https://github.com/libre-embedded/runtimepy/workflows/Python%20Package/badge.svg)

local/variables/package.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
22
major: 5
33
minor: 15
4-
patch: 5
4+
patch: 6
55
entry: runtimepy

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta:__legacy__"
44

55
[project]
66
name = "runtimepy"
7-
version = "5.15.5"
7+
version = "5.15.6"
88
description = "A framework for implementing Python services."
99
readme = "README.md"
1010
requires-python = ">=3.12"

runtimepy/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# =====================================
22
# generator=datazen
33
# version=3.2.3
4-
# hash=8ce7abe0f8c0e64d9afc0b5e4de6e2ad
4+
# hash=08c1dda8bd5fefe13af6a8eb49063498
55
# =====================================
66

77
"""
@@ -10,7 +10,7 @@
1010

1111
DESCRIPTION = "A framework for implementing Python services."
1212
PKG_NAME = "runtimepy"
13-
VERSION = "5.15.5"
13+
VERSION = "5.15.6"
1414

1515
# runtimepy-specific content.
1616
METRICS_NAME = "metrics"

runtimepy/net/server/markdown.py

Lines changed: 55 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
# built-in
66
from io import StringIO
7+
import mimetypes
78
from pathlib import Path
89
from typing import Iterable, cast
910

1011
# third-party
12+
from svgen.element.html import div
1113
from vcorelib.io.file_writer import IndentedFileWriter
1214
from vcorelib.math.time import byte_count_str
1315
from vcorelib.paths import rel
@@ -17,6 +19,58 @@
1719
DIR_FILE = "dir.html"
1820

1921

22+
def file_preview(path: Path, link: Path) -> str:
23+
"""Get possible preview text for a file."""
24+
25+
preview = ""
26+
27+
if path.is_file():
28+
mime, _ = mimetypes.guess_type(path, strict=False)
29+
if mime and mime.startswith("image"):
30+
preview = div(tag="img", src=f"/{link}", alt=str(link)).encode_str(
31+
newlines=False
32+
)
33+
34+
return preview
35+
36+
37+
def write_markdown_dir(
38+
writer: IndentedFileWriter, path: Path, base: Path
39+
) -> None:
40+
"""Write markdown contents for a single directory."""
41+
42+
curr_dir = rel(path, base=base)
43+
44+
line = f"### `{base.name}/{curr_dir}`"
45+
46+
# Link to go up a directory.
47+
if curr_dir != Path():
48+
line += f" ([..](/{curr_dir.parent}/{DIR_FILE}))"
49+
50+
writer.write(line)
51+
writer.empty()
52+
53+
writer.write("| name | size | preview |")
54+
writer.write("|------|------|---------|")
55+
56+
for item in sorted(path.iterdir()):
57+
curr = rel(item, base=base)
58+
59+
name = f"`{curr.name}`"
60+
if item.is_dir():
61+
name = f"**{name}**"
62+
63+
stats = _stats(item)
64+
assert stats
65+
size_str = byte_count_str(stats.st_size) if item.is_file() else ""
66+
67+
writer.write(
68+
f"| [{name}](/{curr}) | {size_str} | {file_preview(item, curr)} |"
69+
)
70+
71+
writer.empty()
72+
73+
2074
def markdown_for_dir(
2175
paths_bases: Iterable[tuple[Path, Path]],
2276
extra_links: dict[str, Iterable[str]] = None,
@@ -40,31 +94,7 @@ def markdown_for_dir(
4094
writer.empty()
4195

4296
for path, base in paths_bases:
43-
curr_dir = rel(path, base=base)
44-
writer.write(f"### `{base.name}/{curr_dir}`")
45-
writer.empty()
46-
47-
# Link to go up a directory.
48-
if curr_dir != Path():
49-
writer.write(f"* [..](/{curr_dir.parent}/{DIR_FILE})")
50-
51-
writer.write("| name | size |")
52-
writer.write("|------|------|")
53-
for item in sorted(path.iterdir()):
54-
curr = rel(item, base=base)
55-
56-
name = f"`{curr.name}`"
57-
if item.is_dir():
58-
name = f"**{name}**"
59-
60-
stats = _stats(item)
61-
assert stats
62-
size_str = (
63-
byte_count_str(stats.st_size) if item.is_file() else ""
64-
)
65-
writer.write(f"| [{name}](/{curr}) | {size_str} |")
66-
67-
writer.empty()
97+
write_markdown_dir(writer, path, base)
6898

6999
result: str = cast(StringIO, writer.stream).getvalue()
70100

0 commit comments

Comments
 (0)