import markdown_extensions
import markdown
import re
import os
import glob
import datetime
import pytz
import json
import frontmatter


TEST_FILES = [
    '/home/danaukes/repos/websites/danb0b.github.io/content/work-blog/2024-01-04-class-wrapup/index.md',
]

TIME_ZONE = 'America/Los_Angeles'
STATIC_URL='staticurl://'
MARKDOWN_SOURCE_PATH='/home/danaukes/repos/websites/danb0b.github.io/content/notebook/deprecated/ros'

import markdown_extensions.handle_static

MARKDOWN_EXTENSIONS = [
    'markdown.extensions.tables',
    'markdown.extensions.codehilite',
    'markdown.extensions.toc',
    'markdown.extensions.attr_list',
    'markdown.extensions.footnotes',
    'markdown.extensions.wikilinks',
    'pymdownx.superfences',
    markdown_extensions.handle_static.StaticExtension(find=r"""\\*\{% *static '(?P<group1>[/a-zA-Z0-9-_=.+]*)' *%\\*\}"""
,replace=STATIC_URL+r'\g<group1>')
    ]


class Post(object):

    def __init__(self,path):
        self.path = path

    def __str__(self):
        return str(self.path)

    def __repr__(self):
        return str(self)

    @staticmethod
    def split_content(markdown):
        kernel = re.compile('<!-- *split *-->')
        results = kernel.search(markdown)
        if results is not None:
            return markdown[:results.start()]
        else:
            return markdown

    def can_be_split(self):
        markdown = self.content
        kernel = re.compile('<!-- *split *-->')
        results = kernel.search(markdown)
        if results is not None:
            return True
        else:
            return False

    @staticmethod
    def replace_static(text):
        searchstring=r"\\*\{% *static '(?P<file>[/a-zA-Z0-9-_=.+]*)' *%\\*\}"
        # print('searchstring',searchstring)
        # print('text: ',text)
        replacement=STATIC_URL+r'\g<file>'
        results = re.sub(searchstring,replacement,text)
        # print(results)
        return results

    @staticmethod
    def render_markdown(input):
        md = markdown.Markdown(extensions=MARKDOWN_EXTENSIONS)
        html_pass_1=md.convert(input)
        toc_tokens=md.toc_tokens
        return html_pass_1,toc_tokens

    def render_content(self,split=False):
        if split:
            content = self.split_content(self.content)
        else:
            content = self.content
        rendered_html,toc_tokens=self.render_markdown(content)
        return rendered_html,toc_tokens

def fix_path(path_in):
    path = path_in
    path = os.path.expanduser(path)
    path = os.path.abspath(path)
    path = os.path.normcase(path)
    path = os.path.normpath(path)
    return path

def update_post(path):
    
    with open(path) as f:
        fm = frontmatter.load(f)

    my_dict = fm.to_dict()

    content = my_dict['content']
    del my_dict['content']

    post = Post(path)

    my_json = json.dumps(my_dict,default=str)
    post.json = my_json
    post.content = content

    try:
        my_tz = pytz.timezone(TIME_ZONE)
        date = my_dict['date']
        dt = datetime.datetime.combine(date, datetime.datetime.min.time())
        post.date = my_tz.localize(dt)
    except KeyError:
        pass
    return post


posts = []
for path in TEST_FILES:
    post = update_post(path)
    posts.append(post)

for post in posts:
    content,tokens=post.render_content()
    # print(content)