Use dominate

This commit is contained in:
John Doty 2024-11-22 12:53:39 -08:00
parent 08fe7c1cf7
commit 1c81d6fcd4
3 changed files with 56 additions and 46 deletions

View file

@ -1,8 +1,8 @@
import asyncio import asyncio
import contextlib import contextlib
import dataclasses import dataclasses
import dominate.tags as d
import functools import functools
import html
import http.server import http.server
import io import io
import pathlib import pathlib
@ -497,48 +497,37 @@ class Handler(http.server.BaseHTTPRequestHandler):
feeds.sort(key=feed.sort_key, reverse=True) feeds.sort(key=feed.sort_key, reverse=True)
buffer = io.StringIO() document = d.html()
buffer.write( with document:
""" with d.head():
<!doctype html> d.meta(charset="utf8")
<head> d.title("Subscribed Feeds")
<meta charset="utf8"> d.link(rel="stylesheet", href="/style.css", type="text/css")
<title>Subscribed Feeds</title> d.h1("Feeds")
<link rel="stylesheet" href="/style.css" type="text/css" /> with d.div():
</head> with d.form(method="post", action="/refresh"):
<h1>Feeds</h1> d.input_(type="submit", value="Refresh")
<div> with d.form(method="post", action="/subscribe"):
<form method="post" action="/refresh"> d.label("Feed url:", fr="url")
<input type="submit" value="Refresh" /> d.input_(type="url", name="url")
</form> d.input_(type="submit", value="Subscribe")
<form method="post" action="/subscribe">
<label for="url">Feed url: </label>
<input type="url" name="url" />
<input type="submit" value="Subscribe" />
</form>
</div>
"""
)
for f in feeds:
feed_title = html.escape(f.title)
buffer.write(f"<div class='feed'>")
buffer.write(
f'<h2><a href="{f.link}" target="_blank">{feed_title}</a></h2>'
)
if len(f.entries) > 0:
buffer.write(f"<ul>")
for entry in f.entries:
title = html.escape(entry.title)
buffer.write(
f'<li class="entry"><a href="{entry.link}" target="_blank">{title}</a> ({entry.time_ago()})</li>'
)
buffer.write(f"</ul>")
else:
buffer.write("<i>No entries...</i>")
buffer.write(f"</div>") # feed
buffer.flush()
self.write_html(buffer.getvalue()) for f in feeds:
with d.div(cls="feed"):
d.h2(d.a(f.title, href=f.link, target="_blank"))
if len(f.entries) > 0:
d.ul(
d.li(
d.a(entry.title, href=entry.link, target="_blank"),
f" ({entry.time_ago()})",
cls="entry",
)
for entry in f.entries
)
else:
d.i("No entries...")
self.write_html(document.render())
def serve_subscribe_choose(self): def serve_subscribe_choose(self):
try: try:

23
pdm.lock generated
View file

@ -3,9 +3,12 @@
[metadata] [metadata]
groups = ["default", "test"] groups = ["default", "test"]
strategy = ["cross_platform", "inherit_metadata"] strategy = ["inherit_metadata"]
lock_version = "4.4.2" lock_version = "4.5.0"
content_hash = "sha256:0bb8351861012d463ce318bf45ed26567c8e4e69b76fbda2a01ffa9492d77653" content_hash = "sha256:600f614700867fb12dfa95f2cd65b2339a6b17aaaede34b3adf9770e8f5d8011"
[[metadata.targets]]
requires_python = "==3.12.*"
[[package]] [[package]]
name = "certifi" name = "certifi"
@ -52,6 +55,7 @@ summary = "Composable command line interface toolkit"
groups = ["default"] groups = ["default"]
dependencies = [ dependencies = [
"colorama; platform_system == \"Windows\"", "colorama; platform_system == \"Windows\"",
"importlib-metadata; python_version < \"3.8\"",
] ]
files = [ files = [
{file = "click-8.1.7-py3-none-any.whl", hash = "sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28"}, {file = "click-8.1.7-py3-none-any.whl", hash = "sha256:ae74fb96c20a0277a1d615f1e4d73c8414f5a98db8b799a7931d1582f3390c28"},
@ -70,6 +74,17 @@ files = [
{file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"}, {file = "colorama-0.4.6.tar.gz", hash = "sha256:08695f5cb7ed6e0531a20572697297273c47b8cae5a63ffc6d6ed5c201be6e44"},
] ]
[[package]]
name = "dominate"
version = "2.9.1"
requires_python = ">=3.4"
summary = "Dominate is a Python library for creating and manipulating HTML documents using an elegant DOM API."
groups = ["default"]
files = [
{file = "dominate-2.9.1-py2.py3-none-any.whl", hash = "sha256:cb7b6b79d33b15ae0a6e87856b984879927c7c2ebb29522df4c75b28ffd9b989"},
{file = "dominate-2.9.1.tar.gz", hash = "sha256:558284687d9b8aae1904e3d6051ad132dd4a8c0cf551b37ea4e7e42a31d19dc4"},
]
[[package]] [[package]]
name = "feedparser" name = "feedparser"
version = "6.0.11" version = "6.0.11"
@ -147,9 +162,11 @@ summary = "pytest: simple powerful testing with Python"
groups = ["test"] groups = ["test"]
dependencies = [ dependencies = [
"colorama; sys_platform == \"win32\"", "colorama; sys_platform == \"win32\"",
"exceptiongroup>=1.0.0rc8; python_version < \"3.11\"",
"iniconfig", "iniconfig",
"packaging", "packaging",
"pluggy<2.0,>=1.5", "pluggy<2.0,>=1.5",
"tomli>=1; python_version < \"3.11\"",
] ]
files = [ files = [
{file = "pytest-8.2.2-py3-none-any.whl", hash = "sha256:c434598117762e2bd304e526244f67bf66bbd7b5d6cf22138be51ff661980343"}, {file = "pytest-8.2.2-py3-none-any.whl", hash = "sha256:c434598117762e2bd304e526244f67bf66bbd7b5d6cf22138be51ff661980343"},

View file

@ -18,6 +18,7 @@ dependencies = [
"platformdirs>=4.2.2", "platformdirs>=4.2.2",
"requests>=2.32.3", "requests>=2.32.3",
"click>=8.1.7", "click>=8.1.7",
"dominate>=2.9.1",
] ]
[project.urls] [project.urls]
@ -26,7 +27,10 @@ Changelog = "https://github.com/decarabas/cry/releases"
Issues = "https://github.com/decarabas/cry/issues" Issues = "https://github.com/decarabas/cry/issues"
CI = "https://github.com/decarabas/cry/actions" CI = "https://github.com/decarabas/cry/actions"
[project.entry-points.console_scripts] [project.scripts]
cry = "cry.cli:cli"
[project.entry_points.console_scripts]
cry = "cry.cli:cli" cry = "cry.cli:cli"
[project.optional-dependencies] [project.optional-dependencies]