207 lines
8.1 KiB
Python
207 lines
8.1 KiB
Python
import os
|
|
import time
|
|
import math
|
|
import markdown
|
|
|
|
#templates
|
|
with open('templates/blog_post.html', 'r') as file: blog_post_temp = file.readlines()
|
|
with open('templates/head.html', 'r') as file: head_temp = file.readlines()
|
|
with open('templates/header.html', 'r') as file: header_temp = file.readlines()
|
|
with open('templates/footer.html', 'r') as file: footer_temp = file.readlines()
|
|
|
|
with open('templates/blog.html', 'r') as file: blog_temp = file.readlines()
|
|
with open('templates/blog_card.html', 'r') as file: blog_card_temp = file.readlines()
|
|
with open('templates/blog_list.html', 'r') as file: blog_list_temp = file.readlines()
|
|
with open('templates/index.html', 'r') as file: index_temp = file.readlines()
|
|
with open('templates/rss.xml', 'r') as file: rss_temp = file.readlines()
|
|
with open('templates/rss_item.xml', 'r') as file: rss_item_temp = file.readlines()
|
|
|
|
root = '../blog/'
|
|
posts = os.listdir(str(root + 'md/'))
|
|
posts.remove('draft')
|
|
|
|
# unix_time = int(time.time())
|
|
# rss_time = print(time.strftime("%a, %d %b %Y %H:%M:%S %z", time.localtime(unix_time)))
|
|
|
|
# MD TO HTML ==============================================================
|
|
for post in posts:
|
|
#read file
|
|
path = str(root + 'md/' + post)
|
|
with open(path, 'r') as file:
|
|
text = file.readlines()
|
|
|
|
#read metadata
|
|
for line in text:
|
|
if 'title = ' in line: title = line.replace('title = ', '')
|
|
if 'timestamp = ' in line: timestamp = line.replace('timestamp = ', '')
|
|
if 'preview = ' in line: preview = line.replace('preview = ', '')
|
|
if 'thumb = ' in line: thumb = line.replace('thumb = ', '')
|
|
if 'auto' in timestamp:
|
|
timestamp = str(int(time.time())) # !! should write this into the md file later
|
|
|
|
#delete metadata before converting
|
|
index = 0
|
|
for i in range(len(text)):
|
|
if '+++' in text[i]:
|
|
index = i
|
|
text = text[index+2:] #remove lines before '+++' and 2 more
|
|
|
|
#convert to HTML
|
|
text = ''.join(text)
|
|
html = markdown.markdown(text)
|
|
|
|
#add title, pubication date and reading time
|
|
date = str(time.strftime("%d/%m/%Y", time.localtime(int(timestamp))))
|
|
word_count = len(text.split())
|
|
reading_time = str(math.ceil(word_count / 200))
|
|
insert = str('<h1>' + title + '</h1>' + '<p class="light">' + date + ' · ' + reading_time + ' min</p>' + '<div class="spacer" style="--size: 1em"></div>')
|
|
html = str(insert + html)
|
|
|
|
#insert in template
|
|
head = head_temp.copy()
|
|
for i in range(len(head_temp)):
|
|
if '$title$' in head_temp[i]: head[i] = head[i].replace('$title$', title)
|
|
if '$preview$' in head_temp[i]: head[i] = head[i].replace('$preview$', preview)
|
|
if '$thumb$' in head_temp[i]: head[i] = head[i].replace('$thumb$', thumb)
|
|
header = header_temp.copy()
|
|
footer = footer_temp.copy()
|
|
blog = blog_post_temp.copy()
|
|
for i in range(len(blog_post_temp)):
|
|
if '$head.html$' in blog_post_temp[i]: blog[i] = blog[i].replace('$head.html$', ''.join(head))
|
|
if '$header.html$' in blog_post_temp[i]: blog[i] = blog[i].replace('$header.html$', ''.join(header))
|
|
if '$footer.html$' in blog_post_temp[i]: blog[i] = blog[i].replace('$footer.html$', ''.join(footer))
|
|
if '$post$' in blog_post_temp[i]: blog[i] = blog[i].replace('$post$', ''.join(html))
|
|
html = ''.join(blog)
|
|
|
|
#export to HTML file
|
|
output_filename = post[:-3] #remove .md extension
|
|
index = 0
|
|
for i in range(len(output_filename)):
|
|
if output_filename[i] == '+':
|
|
index = i
|
|
output_filename = output_filename[index+1:] #remove date from filename for cleaner URL
|
|
output_path = str(root + output_filename + '.html')
|
|
if not os.path.isfile(output_path): #check that file doesn't exist to prevent overwriting it
|
|
print('CREATE\t', output_path)
|
|
with open(output_path, 'w') as output:
|
|
output.write(html)
|
|
output.close()
|
|
else: print('SKIP\t',output_path)
|
|
|
|
# GEN LIST ==============================================================
|
|
post_list = []
|
|
|
|
for post in posts:
|
|
#read file
|
|
path = str(root + 'md/' + post)
|
|
with open(path, 'r') as file:
|
|
text = file.readlines()
|
|
|
|
#read metadata
|
|
for line in text:
|
|
if 'title = ' in line: title = line.replace('title = ', '')
|
|
if 'timestamp = ' in line: timestamp = line.replace('timestamp = ', '')
|
|
if 'preview = ' in line: preview = line.replace('preview = ', '')
|
|
if 'thumb = ' in line: thumb = line.replace('thumb = ', '')
|
|
if 'auto' in timestamp:
|
|
timestamp = str(int(time.time()))
|
|
|
|
output_filename = post[:-3] #remove .md extension
|
|
index = 0
|
|
for i in range(len(output_filename)):
|
|
if output_filename[i] == '+':
|
|
index = i
|
|
output_filename = output_filename[index+1:] #remove date from filename for cleaner URL
|
|
url = str('blog/' + output_filename + '.html')
|
|
|
|
#fill post_list dict
|
|
post_list.append([title, timestamp, preview, thumb, url])
|
|
|
|
# GEN PAGES ===============================================================
|
|
|
|
#create blog cards
|
|
latest_posts = post_list[-3:]
|
|
latest_posts.reverse()
|
|
|
|
latest_posts_cards = ''
|
|
|
|
for post in latest_posts:
|
|
blog_card = blog_card_temp.copy()
|
|
title = post[0]
|
|
timestamp = post[1]
|
|
preview = post[2]
|
|
thumb = post[3]
|
|
url = post[4]
|
|
for i in range(len(blog_card_temp)):
|
|
if '$title$' in blog_card_temp[i]: blog_card[i] = blog_card[i].replace('$title$', title)
|
|
if '$preview$' in blog_card_temp[i]: blog_card[i] = blog_card[i].replace('$preview$', preview)
|
|
if '$thumb$' in blog_card_temp[i]: blog_card[i] = blog_card[i].replace('$thumb$', thumb)
|
|
if '$url$' in blog_card_temp[i]: blog_card[i] = blog_card[i].replace('$url$', url)
|
|
latest_posts_cards = str(latest_posts_cards + ''.join(blog_card))
|
|
|
|
#replace in index
|
|
index = index_temp.copy()
|
|
for i in range(len(index_temp)):
|
|
if '$blog_card$' in index_temp[i]: index[i] = index[i].replace('$blog_card$', latest_posts_cards)
|
|
index = ''.join(index)
|
|
with open('../index.html', 'w') as output:
|
|
output.write(index)
|
|
output.close()
|
|
|
|
#replace in rss
|
|
post_list.reverse()
|
|
rss_items = ''
|
|
|
|
for post in post_list:
|
|
rss_item = rss_item_temp.copy()
|
|
title = post[0]
|
|
timestamp = post[1]
|
|
preview = post[2]
|
|
thumb = post[3]
|
|
url = post[4]
|
|
for i in range(len(rss_item_temp)):
|
|
if '$title$' in rss_item[i]: rss_item[i] = rss_item[i].replace('$title$', title)
|
|
if '$preview$' in rss_item[i]: rss_item[i] = rss_item[i].replace('$preview$', preview)
|
|
if '$thumb$' in rss_item[i]: rss_item[i] = rss_item[i].replace('$thumb$', thumb)
|
|
if '$url$' in rss_item[i]: rss_item[i] = rss_item[i].replace('$url$', url)
|
|
if '$date$' in rss_item[i]: rss_item[i] = rss_item[i].replace('$date$', time.strftime("%a, %d %b %Y %H:%M:%S %z", time.localtime(int(timestamp))))
|
|
rss_items = str(rss_items + ''.join(rss_item))
|
|
|
|
rss = rss_temp.copy()
|
|
for i in range(len(rss_temp)):
|
|
if '$items$' in rss_temp[i]: rss[i] = rss[i].replace('$items$', rss_items)
|
|
with open('../rss.xml', 'w') as output:
|
|
output.writelines(rss)
|
|
output.close()
|
|
|
|
#replace in blog
|
|
year = '0000'
|
|
blog_post_list = ''
|
|
|
|
for post in post_list: #replace blog list
|
|
blog_list = blog_list_temp.copy()
|
|
title = post[0]
|
|
timestamp = post[1]
|
|
preview = post[2]
|
|
thumb = post[3]
|
|
url = post[4]
|
|
|
|
blog_list[0] = blog_list[0].replace('$title$', title)
|
|
blog_list[0] = blog_list[0].replace('$date$', time.strftime("%d/%m", time.localtime(int(timestamp))))
|
|
blog_list[0] = blog_list[0].replace('$url$', url)
|
|
|
|
if time.strftime("%Y", time.localtime(int(timestamp))) != year:
|
|
year = time.strftime("%Y", time.localtime(int(timestamp)))
|
|
blog_post_list = str(blog_post_list + '<hr><h3>' + year + '</h3>')
|
|
|
|
blog_post_list = str(blog_post_list + ''.join(blog_list))
|
|
|
|
blog = blog_temp.copy()
|
|
for i in range(len(blog_temp)):
|
|
if '$blog_card$' in blog_temp[i]: blog[i] = blog[i].replace('$blog_card$', latest_posts_cards)
|
|
if '$blog_list$' in blog_temp[i]: blog[i] = blog[i].replace('$blog_list$', blog_post_list)
|
|
blog = ''.join(blog)
|
|
with open('../blog.html', 'w') as output:
|
|
output.write(blog)
|
|
output.close()
|