git @ Cat's Eye Technologies Feedmark / master src / feedmark / feeds.py
master

Tree @master (Download .tar.gz)

feeds.py @master — raw · history · blame

# Copyright (c) 2019-2026 Chris Pressey, Cat's Eye Technologies
# This file is distributed under an MIT license.  See LICENSES/ directory.
# SPDX-License-Identifier: LicenseRef-MIT-X-Feedmark

# Feed-related, but Atom-independent, functions.

from collections import OrderedDict
from typing import Iterable
from urllib.parse import quote_plus

from .models import Document, Properties, Section


def construct_entry_url(section: Section) -> str | None:
    # Currently supports links to anchors generated by Github's Markdown renderer.

    if not section.document or "link-target-url" not in section.document.properties:
        return None

    return "{}#{}".format(
        section.document.properties["link-target-url"], quote_plus(section.anchor)
    )


def extract_feed_properties(document: Document) -> Properties:
    properties: Properties = OrderedDict()
    properties["title"] = document.title
    properties["author"] = document.properties["author"]
    properties["url"] = document.properties["url"]
    properties["link-target-url"] = document.properties.get("link-target-url") or ""
    return properties


def extract_sections(documents: Iterable[Document]) -> list[Section]:
    sections = []
    for document in documents:
        for section in document.sections:
            sections.append(section)
    sections.sort(key=lambda section: section.publication_date, reverse=True)
    return sections