Post Organizer Utility¶
A comprehensive utility to organize Jekyll markdown posts based on frontmatter metadata. This tool automatically sorts posts into subdirectories based on their section field and renames them using their slug values.
π― Features¶
- Automatic Organization: Moves posts to subdirectories based on
sectionfrontmatter - Smart Slug Extraction: Extracts slugs from
permalink,slug,title, or filename - Validation: Only processes files with valid sections from your posts.yml config
- Comprehensive Logging: Tracks all operations, skipped files, and errors
- Dry Run Mode: Preview changes before applying them
- Flexible Configuration: Supports custom posts directories and config files
- Error Handling: Gracefully handles missing fields and invalid data
π File Structure¶
scripts/development/content/
βββ organize-posts.py # Main Python script
βββ organize-posts.sh # Bash wrapper script
βββ requirements.txt # Python dependencies
βββ README.md # This documentation
π Quick Start¶
Using the Bash Wrapper (Recommended)¶
# Navigate to the IT-Journey root directory
cd /path/to/it-journey
# Run with dry-run to preview changes
./scripts/development/content/organize-posts.sh --dry-run
# Run the actual organization
./scripts/development/content/organize-posts.sh
Using Python Directly¶
# Install dependencies
pip install -r scripts/development/content/requirements.txt
# Run the script
python3 scripts/development/content/organize-posts.py --dry-run
π Requirements¶
- Python 3.6+
- PyYAML library (
pip install pyyaml) - Jekyll site with posts in
pages/_posts/directory - Posts configuration file at
_data/navigation/posts.yml
π οΈ Usage Options¶
Bash Script Options¶
./organize-posts.sh [OPTIONS]
OPTIONS:
-d, --posts-dir PATH Path to posts directory
-c, --config-file PATH Path to posts.yml config file
-n, --dry-run Preview changes without moving files
-i, --install-deps Install Python dependencies only
-h, --help Show help message
Python Script Options¶
python3 organize-posts.py [OPTIONS]
OPTIONS:
--posts-dir PATH Path to posts directory (default: pages/_posts)
--config-file PATH Path to posts.yml configuration file
--dry-run Show what would be done without moving files
π How It Works¶
1. Frontmatter Analysis¶
The script analyzes each markdown file's YAML frontmatter:
2. Section Validation¶
Validates the section field against your posts.yml configuration:
3. Slug Extraction¶
Extracts slug in this priority order:
1. From permalink field (last segment)
2. From explicit slug field
3. Generated from title field
4. Generated from filename (without date prefix)
4. File Organization¶
Moves files to structure like:
pages/_posts/
βββ technology/
β βββ my-tech-post.md
β βββ another-tech-post.md
βββ business/
β βββ business-strategy.md
β βββ market-analysis.md
βββ world/
βββ global-trends.md
β οΈ File Processing Rules¶
Files are PROCESSED when they have:¶
- β Valid YAML frontmatter
- β
A
sectionfield with a value matching posts.yml - β Determinable slug from permalink, slug, title, or filename
Files are SKIPPED when they:¶
- β Don't have valid YAML frontmatter
- β Don't have a
sectionfield - β Have an invalid section (not in posts.yml)
- β Can't determine a slug
Error Handling:¶
- π΄ Files with processing errors are logged
- π Target files that already exist are not overwritten
- π All operations are logged to
post-organization.log
π Example Output¶
2024-08-24 10:30:15 - INFO - Starting post organization in: /path/to/pages/_posts
2024-08-24 10:30:15 - INFO - Found 45 markdown files to process
2024-08-24 10:30:15 - INFO - Processing: 2021-10-27-build-die-repeat.md
2024-08-24 10:30:15 - INFO - Created/verified directory: /path/to/pages/_posts/technology
2024-08-24 10:30:15 - INFO - Moved: 2021-10-27-build-die-repeat.md -> /path/to/pages/_posts/technology/build-destroy-repeat-mastery.md
============================================================
POST ORGANIZATION SUMMARY
============================================================
Successfully processed: 38 files
β 2021-10-27-build-die-repeat.md -> technology/build-destroy-repeat-mastery.md
β 2023-03-17-penrose-triangle.md -> technology/penrose-triangle-impossible-geometry.md
Skipped: 7 files
β 2024-05-09-bootstrap-your-theme-and-character.md: No valid section found in frontmatter
Section value: 'None'
Valid sections: ['business', 'home', 'tech', 'technology', 'world']
============================================================
π§ Configuration¶
Posts Configuration File¶
The script reads valid sections from your _data/navigation/posts.yml:
- title: Home
icon: home
url: /posts/
- title: Technology
icon: tech
url: /posts/tech/
- title: Business
icon: cash
url: /posts/business/
- title: World
icon: world
url: /posts/world/
Custom Configuration¶
You can specify a custom config file:
π¨ Safety Features¶
Dry Run Mode¶
Always test with --dry-run first:
Backup Recommendation¶
Consider backing up your posts before running:
Logging¶
All operations are logged to post-organization.log for audit trails.
π Troubleshooting¶
Common Issues¶
PyYAML Not Found
Permission Denied
Invalid Section Values
- Check your posts.yml file structure
- Verify section names match exactly
- Use --dry-run to see which sections are detected
Files Not Moving - Verify frontmatter syntax is valid YAML - Check that section field exists and has a valid value - Ensure slug can be determined from the post
Debug Steps¶
- Run with
--dry-runto see what would happen - Check the log file
post-organization.log - Verify your posts.yml configuration
- Test with a single file first
π Integration with IT-Journey Workflow¶
This utility is designed to work with the IT-Journey workflow:
- Content Creation: Authors create posts in
pages/_posts/ - Organization: This script organizes them by section
- Publication: Jekyll builds the organized structure
- Maintenance: Regular organization keeps content structured
π Related Documentation¶
π€ Contributing¶
To improve this utility:
- Test with various frontmatter configurations
- Add support for additional slug sources
- Enhance error handling and validation
- Add more configuration options
π License¶
This utility is part of the IT-Journey project and follows the same license terms.