Hugo Instead of Astro??

Hugo Instead of Astro??

  • python
  • personal
  • migration
  • blog

I changed from Astro to Hugo because I can use drafts and templates there. I used a python script from chatGPT to convert the files from Astro to Hugo. But then I switched back because of reasons.

import os
import re

def convert_markdown_frontmatter(file_path, new_file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        content = file.read()
    
    # Regex to match old frontmatter format
    old_frontmatter_pattern = re.compile(r'^---\s*\n(.*?)\n---\s*', re.DOTALL)
    
    # Replace old frontmatter with new format
    match = old_frontmatter_pattern.match(content)
    if match:
        old_frontmatter = match.group(1)
        
        # Extract relevant parts using regex for each field
        title_match = re.search(r'title:\s*"(.+?)"', old_frontmatter)
        hero_image_match = re.search(r'heroImage:\s*["\']?([^"\']+)["\']?', old_frontmatter)
        pub_date_match = re.search(r'pubDate:\s*"(.+?)"', old_frontmatter)
        draft_match = re.search(r'draft:\s*(true|false)', old_frontmatter)
        
        # Get values with fallback to empty string if not found
        title = title_match.group(1) if title_match else ''
        hero_image = hero_image_match.group(1) if hero_image_match else ''  # Capture heroImage path
        pub_date = pub_date_match.group(1) if pub_date_match else ''
        
        # Determine draft status: true if found as true, otherwise false
        draft = 'true' if draft_match and draft_match.group(1) == 'true' else 'false'
        
        # Construct new frontmatter with proper newlines
        new_frontmatter = '+++\n'
        new_frontmatter += f"date = '{pub_date}'\n" if pub_date else ''
        new_frontmatter += f'draft = {draft}\n'  # Set draft as true or false
        new_frontmatter += f"title = '{title}'\n" if title else ''
        
        # Write heroImage in TOML format with '=' instead of ':'
        if hero_image:
            new_frontmatter += f"heroImage = '{hero_image}'\n"
        
        new_frontmatter += '+++\n'

        # Replace old frontmatter with the new one
        new_content = content.replace(match.group(0), new_frontmatter)
        
        # Write the new content to the new file
        with open(new_file_path, 'w', encoding='utf-8') as new_file:
            new_file.write(new_content)

def convert_files_in_directory(input_folder, output_folder):
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)
    
    # Iterate over files in the input folder
    for file_name in os.listdir(input_folder):
        file_path = os.path.join(input_folder, file_name)
        
        if file_name.endswith('.md') and os.path.isfile(file_path):
            new_file_path = os.path.join(output_folder, file_name)
            convert_markdown_frontmatter(file_path, new_file_path)
            print(f"Converted: {file_name}")


input_folder = './blog'  # Replace with the path to your input folder
output_folder = './bloghugo'  # Replace with the path to your output folder
convert_files_in_directory(input_folder, output_folder)