import os
import sys
import re
from datetime import datetime
import pytz


def replace_meta(content: str):
    # all meta has to be between ---, so lets append that
    content = '+++\n' + content
    # match and replace the `Title: <something>` to `title = "<something>"`
    content = re.sub(r'Title: *(.*)\n', r'title = "\1"\n', content)
    # match and replace the `Slug: <something>` to `slug = "<something>"`
    content = re.sub(r'Slug: *(.*)\n', r'slug = "\1"\n', content)
    # match and replace the `FacebookImage: <something>` to `image = "<something>"`
    content = re.sub(r'FacebookImage: *(.*)\n', r'image = "\1"\n', content)

    # to replace Date and Modified, we need to first get the string, parse it and convert it
    # to the format golang wants
    date_regex = re.compile(r'Date: *(?P<date> .*)\n')
    # date_search = date_regex.search(content)
    if match := date_regex.search(content):
        date = match.groupdict().get('date')
        # the date format is like `2016-02-19 23:03`
        date_time = datetime.strptime(date.strip(), '%Y-%m-%d %H:%M').astimezone(pytz.timezone('Asia/Kolkata'))
        content = date_regex.sub(F'date = "{date_time.isoformat()}"\n', content)

    # same like Date, but for Modified
    modified_regex = re.compile(r'Modified: *(?P<date> .*)\n')
    if match := modified_regex.search(content):
        date = match.groupdict().get('date')
        # the date format is like `2016-02-19 23:03`
        date_time = datetime.strptime(date.strip(), '%Y-%m-%d %H:%M').astimezone(pytz.timezone('Asia/Kolkata'))
        content = modified_regex.sub(F'lastmod = "{date_time.isoformat()}"\n', content)

    categories_regex = re.compile(r'Category: *(?P<category> .*)\n')
    if match := categories_regex.search(content):
        categories = match.groupdict().get('category')
        replacement_string = ', '.join([F'"{s.strip().lower()}"' for s in categories.split(',')])
        content = categories_regex.sub(F'categories = [{replacement_string}]\n', content)

    tags_regex = re.compile(r'Tags: *(?P<tags> .*)\n')
    if match := tags_regex.search(content):
        tags = match.groupdict().get('tags')
        replacement_string = ', '.join([F'"{s.strip().lower()}"' for s in tags.split(',')])
        content = tags_regex.sub(F'tags = [{replacement_string}]\n', content)

    # same like how the title was replaced. We will also append it `---` to close the meta
    # block
    content = re.sub(r'Summary: *(.*)\n', r'description = "\1"\n+++\n', content)
    return content


def convert(path):
    for root, dirs, files in os.walk(path):
        for name in files:
            if name.endswith('.md'):
                file = os.path.join(root, name)
                print(file)
                with open(file, 'r') as f:
                    updated_content = replace_meta(f.read())
                with open(file, 'w') as f:
                    f.write(updated_content)


if __name__ == '__main__':
    convert(sys.argv[1])

@@ -173,19 +173,19 @@
 tent = '
-+++
+---
 %5Cn' + co
@@ -313,18 +313,17 @@
  r'title
- =
+:
  %22%5C1%22%5Cn'
@@ -452,18 +452,17 @@
 , r'slug
- =
+:
  %22%5C1%22%5Cn'
@@ -611,18 +611,17 @@
  r'image
- =
+:
  %22%5C1%22%5Cn'
@@ -1153,18 +1153,17 @@
 b(F'date
- =
+:
  %22%7Bdate_
@@ -1601,18 +1601,17 @@
 'lastmod
- =
+:
  %22%7Bdate_
@@ -1966,18 +1966,17 @@
 tegories
- =
+:
  %5B%7Brepla
@@ -2280,18 +2280,17 @@
 b(F'tags
- =
+:
  %5B%7Brepla
@@ -2481,20 +2481,19 @@
 tion
- =
+:
  %22%5C1%22%5Cn
-+++
+---
 %5Cn',

use json style