import glob
import locale
import os
import pathlib
import urllib.parse
from datetime import datetime
from os import path
import markdown
from bs4 import BeautifulSoup
from gtts import gTTS, gTTSError
import config
import search
WEBSITE = config.WEBSITE
ENTRY_DIR = config.ENTRY_DIR
LANGUAGE = config.LANGUAGE
LOCAL = "de_DE.UTF-8" if LANGUAGE == "de-de" else "en_US.UTF-8"
TIMEZONE = config.TIMEZONE
locale.setlocale(locale.LC_TIME, LOCAL)
def gen_arch_string():
"""
Creates and returns a archive string of every file in ENTRY_DIR.
Returns:
string: html-formatted archive-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.getmtime)
content_string = ""
last_month = ""
for file in reversed(contents):
curr_date = datetime.fromtimestamp(os.path.getmtime(file)).strftime("%Y-%m-%d")
curr_month = datetime.fromtimestamp(os.path.getmtime(file)).strftime("%B %Y")
if curr_month != last_month:
if last_month != "":
content_string += "\n"
content_string += "
\n"
content_string += "
\n"
content_string += "
" + \
datetime.fromtimestamp(os.path.getmtime(
file)).strftime("%Y-%m-%d") + ""
if file.endswith(".html"):
for line in text:
content_string += line
if file.endswith(".md"):
content_string += gen_md_content(file, 2)
content_string += "
"
content_string = absolutize_html(content_string)
return content_string
def absolutize_html(string):
"""
Creates a html string from another string that only uses absolute links that use the full domain.
Parameters:
string: html-formatted string.
Returns:
string: html-formatted string with absolute linksn
"""
soup = BeautifulSoup(string, "html.parser")
for a_tag in soup.find_all("a"):
href = str(a_tag.get("href"))
if href.startswith("/") or href.startswith("."):
a_tag["href"] = urllib.parse.urljoin(WEBSITE, href)
for img_tag in soup.find_all("img"):
src = str(img_tag.get("src"))
if src.startswith("/") or src.startswith("."):
img_tag["src"] = urllib.parse.urljoin(WEBSITE, src)
return str(soup)
def gen_stand_string(path_ex):
"""
Creates a html-string for a file.
If the file is markdown it will convert it.
This functions ensures upscaling for future formats.
Parameters:
path_ex: path to a file.
Returns:
string: html-formatted string string equivalent to the file
"""
filename = os.path.join(ENTRY_DIR, path_ex)
content_string = ""
if path.exists(filename):
title = open(filename).readline().rstrip("\n")
text = open(filename).readlines()[1:]
curr_date = datetime.fromtimestamp(os.path.getmtime(filename)).strftime("%Y-%m-%d")
filename_no_end = filename.split(".", 1)[0]
filename_no_end = filename_no_end.split("/")[-1]
content_string += "