diff --git a/README.md b/README.md index 940d054..2fa6ab9 100644 --- a/README.md +++ b/README.md @@ -107,7 +107,7 @@ cp soapbox/themes/green/static/style.css data/content/themes/skog/static/ # re Et tema kan være **delvis**: bare filene som finnes overstyrer det innebygde `green`-temaet. Vil du endre malene, legg `templates/base.html`, `index.html` eller `post.html` i temamappen. Malene får `site`, `posts`/`post`, `html`, `source_url` og `rel(sti)` (relativ lenke fra -gjeldende side). `static/` havner under `/theme/`. +gjeldende side), samt filtrene `date` og `excerpt(n)`. `static/` havner under `/theme/`. ## Lisens diff --git a/soapbox/build.py b/soapbox/build.py index e898644..d210388 100644 --- a/soapbox/build.py +++ b/soapbox/build.py @@ -36,6 +36,7 @@ def _env(cfg: Config) -> Environment: autoescape=select_autoescape(["html"]), ) env.filters["date"] = lambda d, fmt="%d.%m.%Y": d.strftime(fmt) + env.filters["excerpt"] = excerpt return env diff --git a/soapbox/render.py b/soapbox/render.py index 3f46755..af3e93c 100644 --- a/soapbox/render.py +++ b/soapbox/render.py @@ -12,6 +12,7 @@ interne lenker som forfatteren skriver (``/annet-innlegg/``) skrives om til from __future__ import annotations import posixpath +import re from urllib.parse import urlsplit from markdown_it import MarkdownIt @@ -69,5 +70,10 @@ def excerpt(text: str, limit: int = 300) -> str: para = para.strip() if para and not para.startswith(("#", "!", "```", "|")): plain = " ".join(para.split()) + # Fjern den vanligste inline-markeringen så utdraget leses som ren tekst + plain = re.sub(r"!\[[^\]]*\]\([^)]*\)", "", plain) # bilder + plain = re.sub(r"\[([^\]]+)\]\([^)]*\)", r"\1", plain) # lenker → tekst + plain = re.sub(r"[*_`~]{1,3}", "", plain) + plain = " ".join(plain.split()) # doble mellomrom etter fjernede bilder return plain if len(plain) <= limit else plain[:limit].rsplit(" ", 1)[0] + "…" return "" diff --git a/soapbox/static/admin/favicon.svg b/soapbox/static/admin/favicon.svg index b94ff70..984a79a 100644 --- a/soapbox/static/admin/favicon.svg +++ b/soapbox/static/admin/favicon.svg @@ -1,16 +1,9 @@ diff --git a/soapbox/themes/green/static/favicon.svg b/soapbox/themes/green/static/favicon.svg index b94ff70..984a79a 100644 --- a/soapbox/themes/green/static/favicon.svg +++ b/soapbox/themes/green/static/favicon.svg @@ -1,16 +1,9 @@ diff --git a/soapbox/themes/green/static/style.css b/soapbox/themes/green/static/style.css index e88088b..ebf25e5 100644 --- a/soapbox/themes/green/static/style.css +++ b/soapbox/themes/green/static/style.css @@ -80,6 +80,7 @@ h2 { font-size: 1.4rem; line-height: 1.25; margin: 2rem 0 .5rem; } .summary h2 a { color: var(--accent-strong); text-decoration: none; } .summary h2 a:hover { text-decoration: underline; } .summary .meta { margin: 0; } +.summary .excerpt { margin: .5rem 0 0; color: var(--fg); } .body p, .body ul, .body ol { margin: 0 0 1.1rem; } .body img { max-width: 100%; height: auto; display: block; margin: 1.5rem 0; border-radius: 4px; } .body pre { background: var(--code-bg); padding: 1rem; overflow-x: auto; border-radius: 4px; } diff --git a/soapbox/themes/green/templates/index.html b/soapbox/themes/green/templates/index.html index d244a34..ad40ccb 100644 --- a/soapbox/themes/green/templates/index.html +++ b/soapbox/themes/green/templates/index.html @@ -10,6 +10,7 @@ {% if post.guest %} · {{ post.author or post.guest }}{% endif %}
+ {% set text = post.body|excerpt(220) %}{% if text %}{{ text }}
{% endif %} {% endfor %} {% endblock %} diff --git a/tests/test_core.py b/tests/test_core.py index 3323141..80b96f5 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -57,3 +57,14 @@ def test_password_hashing(tmp_path): assert db.authenticate(conn, "a", "feil") is None assert db.has_owner(conn) assert datetime.now(UTC) # sanity: UTC alias tilgjengelig + + +def test_excerpt_plain_text(): + from soapbox.render import excerpt + + assert ( + excerpt("Hei **verden**. Se [forsiden](/) og  _her_.") + == "Hei verden. Se forsiden og her." + ) + assert excerpt("# Overskrift\n\n```\nkode\n```\n\nTekst") == "Tekst" + assert excerpt("x " * 200, 50).endswith("…")