Generate index-slim.md without metadata for blog.json
- rst2md.py now creates both index.md (with metadata) and index-slim.md (clean) - generate_jsonblog.py references index-slim.md for clean content - Fixes "Error: No content found" in blog viewer - strip_pelican_metadata() removes title and metadata fields
This commit is contained in:
parent
d8cf83e051
commit
e1fdae237e
2 changed files with 81 additions and 7 deletions
|
|
@ -134,10 +134,10 @@ def generate_jsonblog(atom_path, output_dir, site_url):
|
||||||
'createdAt': entry['createdAt'],
|
'createdAt': entry['createdAt'],
|
||||||
}
|
}
|
||||||
|
|
||||||
# Check if markdown file exists, then link to it
|
# Check if slim markdown file exists (without metadata), then link to it
|
||||||
md_path = output_dir / entry['slug'] / 'index.md'
|
md_path = output_dir / entry['slug'] / 'index-slim.md'
|
||||||
if md_path.exists():
|
if md_path.exists():
|
||||||
post['content_url'] = f"{site_url}/{entry['slug']}/index.md"
|
post['content_url'] = f"{site_url}/{entry['slug']}/index-slim.md"
|
||||||
post['content_type'] = 'markdown'
|
post['content_type'] = 'markdown'
|
||||||
markdown_found += 1
|
markdown_found += 1
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,41 +1,115 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
"""
|
"""
|
||||||
Convert RST files to Markdown using pandoc.
|
Convert RST files to Markdown using pandoc.
|
||||||
|
Strips Pelican metadata headers from the resulting markdown.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
import re
|
||||||
|
|
||||||
|
|
||||||
def convert_file(rst_path, md_path=None):
|
def strip_pelican_metadata(content):
|
||||||
"""Convert a single RST file to Markdown using pandoc."""
|
"""Remove Pelican metadata header from markdown content.
|
||||||
|
|
||||||
|
Pandoc converts RST metadata to a format like:
|
||||||
|
# Title
|
||||||
|
|
||||||
|
field_name
|
||||||
|
field_value
|
||||||
|
|
||||||
|
another_field
|
||||||
|
another_value
|
||||||
|
|
||||||
|
Content starts here...
|
||||||
|
"""
|
||||||
|
lines = content.split('\n')
|
||||||
|
i = 0
|
||||||
|
|
||||||
|
# First line is usually the title (keep it)
|
||||||
|
if i < len(lines) and lines[i].strip().startswith('#'):
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
# Skip blank lines after title
|
||||||
|
while i < len(lines) and not lines[i].strip():
|
||||||
|
i += 1
|
||||||
|
|
||||||
|
# Skip metadata fields (field name on one line, value on next)
|
||||||
|
metadata_fields = ['author', 'slug', 'date', 'tags', 'status', 'category', 'summary']
|
||||||
|
|
||||||
|
while i < len(lines):
|
||||||
|
line = lines[i].strip().lower()
|
||||||
|
|
||||||
|
# Check if this line is a metadata field name
|
||||||
|
if any(line == field for field in metadata_fields):
|
||||||
|
# Skip field name line
|
||||||
|
i += 1
|
||||||
|
# Skip value line(s) - could be multiline
|
||||||
|
while i < len(lines) and lines[i].strip():
|
||||||
|
# If next line is another field name, stop
|
||||||
|
if any(lines[i].strip().lower() == field for field in metadata_fields):
|
||||||
|
break
|
||||||
|
i += 1
|
||||||
|
# Skip blank line after field
|
||||||
|
if i < len(lines) and not lines[i].strip():
|
||||||
|
i += 1
|
||||||
|
elif not line:
|
||||||
|
# Skip blank lines between metadata
|
||||||
|
i += 1
|
||||||
|
else:
|
||||||
|
# Hit content - stop stripping
|
||||||
|
break
|
||||||
|
|
||||||
|
# Return content from this point forward
|
||||||
|
return '\n'.join(lines[i:]).strip() + '\n'
|
||||||
|
|
||||||
|
|
||||||
|
def convert_file(rst_path, md_path=None, create_slim=True):
|
||||||
|
"""Convert a single RST file to Markdown using pandoc.
|
||||||
|
|
||||||
|
Creates two versions:
|
||||||
|
- index.md: Full markdown with metadata
|
||||||
|
- index-slim.md: Clean markdown without metadata
|
||||||
|
"""
|
||||||
rst_path = Path(rst_path)
|
rst_path = Path(rst_path)
|
||||||
if md_path is None:
|
if md_path is None:
|
||||||
md_path = rst_path.with_suffix('.md')
|
md_path = rst_path.with_suffix('.md')
|
||||||
else:
|
else:
|
||||||
md_path = Path(md_path)
|
md_path = Path(md_path)
|
||||||
|
|
||||||
|
# First convert with pandoc (full version with metadata)
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
['pandoc', '-f', 'rst', '-t', 'gfm', '-o', str(md_path), str(rst_path)],
|
['pandoc', '-f', 'rst', '-t', 'gfm', '-o', str(md_path), str(rst_path)],
|
||||||
check=True
|
check=True
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Create slim version without metadata
|
||||||
|
if create_slim:
|
||||||
|
content = md_path.read_text()
|
||||||
|
clean_content = strip_pelican_metadata(content)
|
||||||
|
|
||||||
|
# Create -slim.md version
|
||||||
|
slim_path = md_path.parent / md_path.name.replace('.md', '-slim.md')
|
||||||
|
slim_path.write_text(clean_content)
|
||||||
|
|
||||||
return md_path
|
return md_path
|
||||||
|
|
||||||
|
|
||||||
def convert_output_directory(output_dir):
|
def convert_output_directory(output_dir):
|
||||||
"""Convert all index.rst files in output directory to index.md."""
|
"""Convert all index.rst files in output directory to index.md and index-slim.md."""
|
||||||
output_path = Path(output_dir)
|
output_path = Path(output_dir)
|
||||||
count = 0
|
count = 0
|
||||||
|
|
||||||
for rst_file in output_path.rglob('index.rst'):
|
for rst_file in output_path.rglob('index.rst'):
|
||||||
md_file = rst_file.with_suffix('.md')
|
md_file = rst_file.with_suffix('.md')
|
||||||
try:
|
try:
|
||||||
convert_file(rst_file, md_file)
|
convert_file(rst_file, md_file, create_slim=True)
|
||||||
count += 1
|
count += 1
|
||||||
except subprocess.CalledProcessError as e:
|
except subprocess.CalledProcessError as e:
|
||||||
print(f"Warning: Failed to convert {rst_file}: {e}")
|
print(f"Warning: Failed to convert {rst_file}: {e}")
|
||||||
|
|
||||||
|
print(f"Generated {count} index.md files and {count} index-slim.md files")
|
||||||
return count
|
return count
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue