Manual
Almnck.
Price: 50ⓐ

Distill-Aware Refactor Opportunities

Date: 2026-03-29 Status: Proposal

Context

Almnck is not a conventional long-running Django app. Most page rendering is done once during django-distill builds and then served as static files.

That changes what optimization means:

  • Build-time repeated work matters more than request-path throughput.
  • Consistency of editorial rules across commands, scripts, and views matters more than shaving a few ORM queries.
  • Dev-server behavior still matters, because routing bugs there can hide dead code and distort the tests.

At the current scale, the architectural issues are more important than raw performance. The repo currently has only a handful of article directories and margin-box directories, so the main risk is duplicated logic drifting out of sync, not hot-path query load.

Feedback

1. Routing ambiguity is hiding dead code

src/almnck/units/urls.py defines two identical catch-all routes: one for article_detail, then one for page_detail.

src/almnck/units/views.py’s article_detail() does not constrain unit_type, so published pages are routed there first. In practice this means page_detail() is dead in normal Django routing, even though some tests call it directly.

This is the sharpest issue in the current structure because it is not merely a refactoring opportunity; it is a real ambiguity in behavior.

2. Editorial parsing rules are duplicated across multiple entry points

The same frontmatter parsing and content-file discovery logic appears in:

  • src/almnck/units/management/commands/sync_with_repo.py
  • src/almnck/manual/scanner.py
  • bin/update-index

The same almanac-day to output-path logic also appears in both sync_with_repo.py and build_with_distill.py, even though src/almnck/core/citations.py already holds the canonical date parsing and URL formatting primitives.

This is the main abstraction gap in the codebase. Useful editorial rules exist, but they are trapped inside commands and scripts instead of living in a shared module.

3. units/views.py is doing too many jobs

src/almnck/units/views.py currently mixes:

  • request handling
  • markdown rendering
  • image and video dimension injection
  • asset URL rewriting
  • frontpage selection
  • schema/video metadata assembly
  • ephemeris payload assembly
  • draft preview behavior
  • RSS, chronology, sitemap, and manifest generation

The strongest smell is that sync_with_repo.py imports helpers from units/views.py to generate the dynamic “previously” margin box. That is a sign that view code is currently acting as the de facto service layer.

4. Frontpage rules are scattered

Frontpage logic is currently implemented in several places:

  • homepage selection
  • previous-frontpage navigation
  • chronology generation
  • RSS generation
  • frontpage manifest generation
  • the “previously” margin box

With the current article count, this is not a performance problem. It is a coherence problem. If the rules around frontmatter["frontpage"] change, too many independent code paths need to be kept in sync.

5. The optimization story should be build-time, not request-time

Because distill renders the site ahead of time, the most plausible performance experiments are:

  • caching work repeated during builds
  • caching work repeated during manual-page browsing in development
  • reducing the amount of duplicated parsing and HTML post-processing

This is different from optimizing a dynamic site under production request load. For example, select_margin_boxes() is currently simple and Python-heavy, but with only a few margin boxes it is not the place to spend effort now.

Outcome

  1. Replace article_detail and page_detail with a single unit_detail path that resolves by output_path and then branches on unit.unit_type only where behavior truly differs.
  2. Extract shared editorial helpers into a real module, such as:
  3. parse_frontmatter()
  4. find_content_file()
  5. normalize_frontpage()
  6. output_path_from_almanac_day()
  7. Add regression tests that exercise actual routing, not just direct view calls.

This wave fixes the routing ambiguity and removes the most obvious duplicated business logic.

Extract a rendering/service layer from src/almnck/units/views.py:

  • markdown rendering
  • asset rewriting
  • image dimension injection
  • schema metadata builders
  • video metadata builders
  • ephemeris payload builders

Once this exists, both views and commands can depend on it cleanly, and the “previously” margin box no longer needs to import from the views module.

Move script logic such as bin/update-index into importable modules under src/, leaving thin wrappers in bin/.

That would let tests stop duplicating helper functions merely because the logic is trapped in a script file with a sys.path hack.

Optimization Experiments Worth Trying

Cache manual doc scans

src/almnck/manual/views.py currently rescans and reparses the entire doc/ tree on every manual page request. With roughly ninety markdown docs, a simple cache keyed by latest mtime would be reasonable and low risk.

Cache rendered unit HTML by Unit.file_hash

The most expensive repeated work in the rendering path is not the Django template render itself; it is the content pipeline around it:

  • Markdown conversion
  • BeautifulSoup traversal
  • image/video dimension lookup

Caching that pipeline by Unit.file_hash would fit the build-time model well, because the content already has a durable change key.

Do not optimize margin-box selection yet

The current margin-box count is too small to justify schema changes or more complex query machinery. Keep this simple until the editorial set grows enough to make it a real cost.

Non-Goals

  • Do not treat this as a high-traffic dynamic Django app and optimize for request throughput first.
  • Do not normalize every JSON field into relational tables yet.
  • Do not split every large file immediately; the routing fix and shared helper extraction already buy most of the clarity.

Proposed First Branch

If this turns into implementation work, the cleanest first branch would do only three things:

  1. Consolidate to a single unit_detail route.
  2. Extract shared editorial parsing/path helpers.
  3. Update tests to hit real routing behavior.

That is enough to remove the current ambiguity without turning the change into a broad framework rewrite.