import datetime from datetime import datetime import markdown import os from os import path import pathlib WEBSITE = 'localhost:5000' ENTRY_DIR = 'templates/entry' def gen_arch_string(): path_ex = ENTRY_DIR if path.exists(path_ex): name_list = os.listdir(path_ex) full_list = [os.path.join(path_ex, i) for i in name_list] contents = sorted(full_list, key=os.path.getctime) content_string = '' for file in reversed(contents): filename = pathlib.PurePath(file) title = open(filename).readline().rstrip('\n') filename = filename.name if filename[0] != '.': filename = filename.split('.', 1)[0] content_string += '' + title + '
\n' return content_string def gen_index_string(): path_ex = ENTRY_DIR content_string = '' if path.exists(path_ex): name_list = os.listdir(path_ex) full_list = [os.path.join(path_ex, i) for i in name_list] contents = sorted(full_list, key=os.path.getctime) for file in reversed(contents): filename = pathlib.PurePath(file) purefile = filename title = open(filename).readline().rstrip('\n') text = open(filename).readlines()[1:] filename = filename.name if filename[0] != '.': filename = filename.split('.', 1)[0] content_string += '
\n' content_string += '

' + title + '

\n' if file.endswith('.html'): for line in text: content_string += line content_string += '
' if file.endswith('.md'): content_string += gen_md_content(file) content_string += '' + \ datetime.fromtimestamp(os.path.getctime( file)).strftime('%Y-%m-%d') + '' content_string += '
' return content_string def gen_md_content(path_ex): content_string = '' if path.exists(path_ex): filename = path_ex.split('.', 1) fileend = filename[len(filename) - 1] markdown_file = open(path_ex, "r") depth = 2 header = '#' for i in range(depth): header += '#' header += ' ' markdown_text = markdown_file.read().replace('# ', header) content_string = markdown.markdown( markdown_text, extensions=["fenced_code", "tables"] ) return content_string def get_rss_string(): path_ex = ENTRY_DIR if path.exists(path_ex): name_list = os.listdir(path_ex) full_list = [os.path.join(path_ex, i) for i in name_list] contents = sorted(full_list, key=os.path.getctime) content_string = '' for file in reversed(contents): filename = pathlib.PurePath(file) title = open(filename).readline().rstrip('\n') text = open(filename).readlines()[1:] filename = filename.name if filename[0] != '.': filename = filename.split('.', 1)[0] content_string += '\n' content_string += '' + title + '\n' content_string += '' + '/index.html#' + filename + '\n' content_string += '' + \ datetime.fromtimestamp(os.path.getctime(file)).strftime( '%Y-%m-%d') + '\n' content_string += '' for line in text: content_string += line content_string += '\n' content_string += '\n' return content_string