Created
April 26, 2024 21:16
-
-
Save vadimkantorov/f61afe964a99be8b26591c7d8638bd3a to your computer and use it in GitHub Desktop.
[WIP] Generate a RSS feed.xml from a posts collection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import xml.dom.minidom | |
def feed_write(ctx, path, generator_name = 'minimapython', generator_uri = 'https://github.com/vadimkantorov/minima', generator_version = 'https://github.com/vadimkantorov/minimapython'): | |
site = ctx.get('site', {}) | |
site__lang = site.get('lang') | |
page__url__absolute_url = '' | |
root__absolute_url = '' | |
site__time__date_to_xml_schema = '' | |
page__url__absolute_url__xml_escape = '' | |
#{% assign title = site.title | default: site.name %} | |
#{% if page.collection != "posts" %}{% assign title = title | append: " | " | append: (page.collection | capitalize) %}{% endif %} | |
#{% if page.category %}{% assign title = title | append: " | " | append: (page.category | capitalize) %}{% endif %} | |
#{% if page.tags %} | |
# {% assign posts = site.tags[page.tags] %} | |
#{% else %} | |
# {% assign posts = site[page.collection] %} | |
#{% endif %} | |
#{% if page.category %} | |
# {% assign posts = posts | where: "categories", page.category %} | |
#{% endif %} | |
#{% unless site.show_drafts %} | |
# {% assign posts = posts | where_exp: "post", "post.draft != true" %} | |
#{% endunless %} | |
#{% assign posts = posts | sort: "date" | reverse %} | |
#{% assign posts_limit = site.feed.posts_limit | default: 10 %} | |
title__smartify__xml_escape = '' | |
site__description__xml_escape = '' | |
site__author__name__xml_escape = '' #site.author.name | default: site.author | xml_escape | |
site__author__email__xml_escape = '' | |
site__author__uri__xml_escape = '' | |
posts = [] | |
posts_limit = 0 | |
node_doc = xml.dom.minidom.Document() | |
node_root = node_doc.appendChild(node_doc.createElement('feed')) | |
if site__lang: | |
node_root.setAttribute('xml:lang', site__lang) | |
node_root.setAttribute('xmlns', 'http://www.w3.org/2005/Atom') | |
node_generator = node_root.appendChild(node_doc.createElement('generator')) | |
node_generator.setAttribute('uri', generator_uri) | |
node_generator.setAttribute('version', generator_version) | |
node_generator.appendChild(node_generator.createTextNode(generator_name)) | |
node_link = node_root.appendChild(node_link.createElement('link')) | |
node_link.setAttribute('href', page__url__absolute_url) | |
node_link.setAttribute('rel', 'self') | |
node_link.setAttribute('type', 'application/atom+xml') | |
node_link = node_root.appendChild(node_link.createElement('link')) | |
node_link.setAttribute('href', root__absolute_url) | |
node_link.setAttribute('rel', 'alternate') | |
node_link.setAttribute('type', 'text/html') | |
if site__lang: | |
node_link.setAttribute('hreflang', site__lang) | |
node_root.appendChild(node_doc.createElement('updated')).appendChild(node_doc.createTextNode(site__time__date_to_xml_schema)) | |
node_root.appendChild(node_doc.createElement('id')).appendChild(node_doc.createTextNode(page__url__absolute_url__xml_escape)) | |
if title__smartify__xml_escape: | |
node_root.appendChild(node_doc.createElement('title')).appendChild(node_doc.createTextNode(title__smartify__xml_escape)) | |
if site__description__xml_escape: | |
node_root.appendChild(node_doc.createElement('subtitle')).appendChild(node_doc.createTextNode(site__description__xml_escape)) | |
if site__author__name__xml_escape: | |
node_author = node_root.appendChild(node_doc.createElement('author')) | |
node_author.appendChild(node_doc.createElement('name')).appendChild(node_doc.createTextNode(site__author__name__xml_escape)) | |
if site__author__email__xml_escape: | |
node_author.appendChild(node_author.createElement('email')).appendChild(node_doc.createTextNode(site__author__email__xml_escape)) | |
if site__author__uri__xml_escape: | |
node_author.appendChild(node_author.createElement('uri')).appendChild(node_doc.createTextNode(site__author__uri__xml_escape)) | |
for post in posts[:posts_limit]: | |
post__lang = '' | |
post__title = '' #{% assign post_title = post.title | smartify | strip_html | normalize_whitespace | xml_escape %} | |
post__url__absolute_url = '' | |
post__last_modified_at__date_to_xmlschema = '' | |
post__date__date_to_xmlschema = '' | |
post__id__absolute_url__xml_escape = '' | |
post_author_name__xml_escape = '' | |
post__author__email__xml_escape = '' | |
post__author__uri__xml_escape = '' | |
excerpt_only = False | |
post__url__absolute_url__xml_escape = '' | |
post__content__strip = '' | |
post__category__xml_escape = '' | |
post__categories__xml_escape = [] | |
post__tags__xml_escape = [] | |
post_summary__strip_html__normalize_whitespace = '' | |
post_image__xml_escape = '' | |
# {% assign excerpt_only = post.feed.excerpt_only | default: site.feed.excerpt_only %} | |
# {% assign post_author = post.author | default: post.authors[0] | default: site.author %} | |
# {% assign post_author = site.data.authors[post_author] | default: post_author %} | |
# {% assign post_author_email = post_author.email | default: nil %} | |
# {% assign post_author_uri = post_author.uri | default: nil %} | |
# {% assign post_author_name = post_author.name | default: post_author %} | |
# {% assign post_summary = post.description | default: post.excerpt %} | |
# {% assign post_image = post.image.path | default: post.image %} | |
# {% unless post_image contains "://" %}{% assign post_image = post_image | absolute_url %}{% endunless %} | |
node_entry = node_root.appendChild(node_doc.createElement('entry')) | |
if post__lang: | |
node_entry.setAttribute('xml:lang', post__lang) | |
node_title = node_entry.appendChild(node_doc.createElement('title')) | |
node_title.setAttribute('type', 'html') | |
node_title.appendChild(node_doc.createTextNode(post_title)) | |
node_link = node_entry.appendChild(node_link.createElement('link')) | |
node_link.setAttribute('href', post__url__absolute_url) | |
node_link.setAttribute('title', post__title) | |
node_link.setAttribute('rel', 'alternate') | |
node_link.setAttribute('type', 'text/html') | |
node_entry.appendChild(node_doc.createElement('published')).appendChild(node_doc.createTextNode(post__date__date_to_xmlschema)) | |
node_entry.appendChild(node_doc.createElement('updated')).appendChild(node_doc.createTextNode(post__last_modified_at__date_to_xmlschema)) | |
node_entry.appendChild(node_doc.createElement('id')).appendChild(node_doc.createTextNode(post__id__absolute_url__xml_escape)) | |
node_author = node_entry.appendChild(node_doc.createElement('author')) | |
node_author.appendChild(node_doc.createElement('name')).appendChild(node_doc.createTextNode(post_author_name__xml_escape)) | |
if post__author__email__xml_escape: | |
node_author.appendChild(node_author.createElement('email')).appendChild(node_doc.createTextNode(post__author__email__xml_escape)) | |
if post__author__uri__xml_escape: | |
node_author.appendChild(node_author.createElement('uri')).appendChild(node_doc.createTextNode(post__author__uri__xml_escape)) | |
if not excerpt_only: | |
node_content = node_entry.appendChild(node_doc.createElement('content')) | |
node_content.setAttribute('type', 'html') | |
node_content.setAttribute('xml:base', post__url__absolute_url__xml_escape) | |
node_content.appendChild(node_doc.createCDATASection(post__content__strip)) | |
if post__category__xml_escape: | |
node_entry.appendChild(node_doc.createElement('category')).setAttribute('term', post__category__xml_escape) | |
for post__category__xml_escape in post__categories__xml_escape: | |
node_entry.appendChild(node_doc.createElement('category')).setAttribute('term', post__category__xml_escape) | |
for tag__xml_escape in post__tags__xml_escape: | |
node_entry.appendChild(node_doc.createElement('category')).setAttribute('term', tag__xml_escape) | |
if post_summary__strip_html__normalize_whitespace: | |
node_content = node_entry.appendChild(node_doc.createElement('summary')) | |
node_content.setAttribute('type', 'html') | |
node_content.appendChild(node_doc.createCDATASection(post_summary__strip_html__normalize_whitespace)) | |
if post_image__xml_escape: | |
node_mediathumbnail = node_entry.appendChild(node_doc.createElement('media:thumbnail')) | |
node_mediathumbnail.setAttribute('xmlns:media', 'http://search.yahoo.com/mrss/') | |
node_mediathumbnail.setAttribute('url', post_image__xml_escape) | |
node_mediacontent = node_entry.appendChild(node_doc.createElement('media:content')) | |
node_mediacontent.setAttribute('xmlns:media', 'http://search.yahoo.com/mrss/') | |
node_mediacontent.setAttribute('url', post_image__xml_escape) | |
node_mediacontent.setAttribute('medium', 'image') | |
with open(path, 'w') as fp: | |
node_doc.writexml(fp, addindent = ' ', newl = '\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment