swap_guids plugin fixes, feed settings

This commit is contained in:
Tanner Collin 2025-06-03 16:09:38 -06:00
parent 219c44054d
commit ea2c9519cd
2 changed files with 11 additions and 28 deletions

View File

@ -3,6 +3,11 @@
from __future__ import unicode_literals
import os
import sys
sys.path.append('.')
import swap_guids
AUTHOR = 'Tanner'
SITENAME = 'Tanner\'s Site (t0.vc)'
SITEURL = 'https://t0.vc'
@ -14,6 +19,7 @@ TIMEZONE = 'Canada/Mountain'
DEFAULT_LANG = 'en'
# Feed generation is usually not desired when developing
FEED_MAX_ITEMS = 15
FEED_ALL_ATOM = None
CATEGORY_FEED_ATOM = None
TRANSLATION_FEED_ATOM = None
@ -43,6 +49,7 @@ MARKDOWN = {
PLUGINS = [
'obsidian',
'linkclass',
'swap_guids',
]
STATIC_PATHS = ['media', 'extra', 'text']

View File

@ -23,7 +23,6 @@ def modify_feed(context, feed):
if not hasattr(article, 'guid') or not article.guid:
log.info(f"Article '{article.title}' ({article.source_path}) is missing a guid. Generating and embedding one.")
new_guid_str = uuid.uuid4().hex
# The string to embed in the paragraph. Note the leading space.
guid_text_to_embed = f"Guid: {new_guid_str}"
source_path = article.source_path
@ -38,29 +37,11 @@ def modify_feed(context, feed):
# Pelican's MarkdownReader also provides article._content with \n newlines.
try:
with open(source_path, 'r', encoding='utf-8') as f:
original_file_content_universal_newlines = f.read()
current_body_content = f.read()
except Exception as e:
log.error(f"Failed to read original content from '{source_path}': {e}")
raise
# Sanity check: the article's body content should be a suffix of the read file content.
if not original_file_content_universal_newlines.endswith(article._content):
log.error(f"Content mismatch for '{article.title}' in '{source_path}'. "
"The article's parsed content (article._content) does not match the "
"ending of the raw file (read with universal newlines). This is unexpected "
"and may indicate issues with file parsing or concurrent modifications.")
# For debugging, one might log tails of both strings here.
# log.debug(f"Tail of original file content: '{original_file_content_universal_newlines[-200:]}'")
# log.debug(f"Tail of article._content: '{article._content[-200:]}'")
raise Exception(f"Content boundary determination error for article '{article.title}'.")
# Determine the metadata part by subtracting the length of the content body.
metadata_section_length = len(original_file_content_universal_newlines) - len(article._content)
metadata_part_from_file = original_file_content_universal_newlines[:metadata_section_length]
# current_body_content is what Pelican parsed as the article's body.
current_body_content = article._content
# Split this body content to find its first paragraph.
# Paragraphs in Markdown are separated by one or more blank lines (\n\n).
body_parts = current_body_content.split('\n\n', 1)
@ -69,17 +50,12 @@ def modify_feed(context, feed):
# Append the Guid text to the end of the first paragraph of the body.
# .rstrip() removes any trailing whitespace/newlines from the paragraph itself before appending.
modified_first_paragraph_of_body = first_paragraph_of_body.rstrip() + guid_text_to_embed
# Reconstruct the new body content with the embedded Guid.
new_body_content_with_guid = modified_first_paragraph_of_body
if rest_of_body_content: # Add back the rest of the body if it existed.
new_body_content_with_guid += '\n\n' + rest_of_body_content
modified_first_paragraph_of_body = first_paragraph_of_body.rstrip() + '\n' + guid_text_to_embed
# Construct the full new file content by combining the original metadata part and the new body.
# This preserves the original metadata block verbatim (including comments, formatting, and original newline characters if any within it,
# as metadata_part_from_file is a direct slice from original_file_content_universal_newlines which has \n newlines).
full_new_content = metadata_part_from_file + new_body_content_with_guid
full_new_content = modified_first_paragraph_of_body + '\n\n' + rest_of_body_content
try:
with open(source_path, 'w', encoding='utf-8') as f: