|
|
|
@ -1,14 +1,17 @@
|
|
|
|
|
import locale
|
|
|
|
|
import os
|
|
|
|
|
import pathlib
|
|
|
|
|
import urllib.parse
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
from os import path
|
|
|
|
|
|
|
|
|
|
import markdown
|
|
|
|
|
from bs4 import BeautifulSoup
|
|
|
|
|
|
|
|
|
|
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"
|
|
|
|
@ -18,11 +21,11 @@ locale.setlocale(locale.LC_TIME, LOCAL)
|
|
|
|
|
|
|
|
|
|
def gen_arch_string():
|
|
|
|
|
"""
|
|
|
|
|
Creates and returns a archive string of every file in ENTRY_DIR.
|
|
|
|
|
Creates and returns a archive string of every file in ENTRY_DIR.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
string: html-formatted archive-string
|
|
|
|
|
"""
|
|
|
|
|
Returns:
|
|
|
|
|
string: html-formatted archive-string
|
|
|
|
|
"""
|
|
|
|
|
path_ex = ENTRY_DIR
|
|
|
|
|
if path.exists(path_ex):
|
|
|
|
|
name_list = os.listdir(path_ex)
|
|
|
|
@ -57,11 +60,11 @@ def gen_arch_string():
|
|
|
|
|
|
|
|
|
|
def gen_index_string():
|
|
|
|
|
"""
|
|
|
|
|
Create and returns a string including every file in the ENTRY_DIR as an index.
|
|
|
|
|
Create and returns a string including every file in the ENTRY_DIR as an index.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
string: html-formatted index string
|
|
|
|
|
"""
|
|
|
|
|
Returns:
|
|
|
|
|
string: html-formatted index string
|
|
|
|
|
"""
|
|
|
|
|
path_ex = ENTRY_DIR
|
|
|
|
|
content_string = ""
|
|
|
|
|
if path.exists(path_ex):
|
|
|
|
@ -90,21 +93,44 @@ def gen_index_string():
|
|
|
|
|
if file.endswith(".md"):
|
|
|
|
|
content_string += gen_md_content(file, 2)
|
|
|
|
|
content_string += "</div>"
|
|
|
|
|
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.
|
|
|
|
|
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.
|
|
|
|
|
Parameters:
|
|
|
|
|
path_ex: path to a file.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
string: html-formatted string string equivalent to the 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):
|
|
|
|
@ -122,26 +148,25 @@ def gen_stand_string(path_ex):
|
|
|
|
|
content_string += line
|
|
|
|
|
if filename.endswith(".md"):
|
|
|
|
|
content_string += gen_md_content(filename, 1)
|
|
|
|
|
content_string = absolutize_html(content_string)
|
|
|
|
|
return content_string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def gen_md_content(path_ex, depth):
|
|
|
|
|
"""
|
|
|
|
|
Convert a markdown file to a html string.
|
|
|
|
|
Convert a markdown file to a html string.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
path_ex (string): path to the markdown file
|
|
|
|
|
depth (int): starting depth for markdown headings
|
|
|
|
|
Parameters:
|
|
|
|
|
path_ex (string): path to the markdown file
|
|
|
|
|
depth (int): starting depth for markdown headings
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
string: html-formatted string string equivalent to the markdown file
|
|
|
|
|
"""
|
|
|
|
|
Returns:
|
|
|
|
|
string: html-formatted string string equivalent to the markdown file
|
|
|
|
|
"""
|
|
|
|
|
content_string = ""
|
|
|
|
|
if path.exists(path_ex):
|
|
|
|
|
filename = path_ex.split(".", 1)
|
|
|
|
|
fileend = filename[len(filename) - 1]
|
|
|
|
|
header = "#"
|
|
|
|
|
for i in range(depth):
|
|
|
|
|
for _ in range(depth):
|
|
|
|
|
header += "#"
|
|
|
|
|
header += " "
|
|
|
|
|
markdown_lines = open(path_ex, "r").readlines()[1:]
|
|
|
|
@ -154,11 +179,12 @@ def gen_md_content(path_ex, depth):
|
|
|
|
|
|
|
|
|
|
def get_rss_string():
|
|
|
|
|
"""
|
|
|
|
|
Create a rss-string of the blog and return it.
|
|
|
|
|
Create a rss-string of the blog and return it.
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
string: rss-string of everything that is in the ENTRY_DIR.
|
|
|
|
|
"""
|
|
|
|
|
Returns:
|
|
|
|
|
string: rss-string of everything that is in the ENTRY_DIR.
|
|
|
|
|
"""
|
|
|
|
|
locale.setlocale(locale.LC_TIME, "en_US.UTF-8")
|
|
|
|
|
path_ex = ENTRY_DIR
|
|
|
|
|
content_string = ""
|
|
|
|
|
if path.exists(path_ex):
|
|
|
|
@ -174,29 +200,32 @@ def get_rss_string():
|
|
|
|
|
filename = filename.split(".", 1)[0]
|
|
|
|
|
content_string += "<item>\n"
|
|
|
|
|
content_string += "<title>" + title + "</title>\n"
|
|
|
|
|
content_string += "<guid>" + config.WEBSITE + \
|
|
|
|
|
content_string += "<guid>" + WEBSITE + \
|
|
|
|
|
"/index.html#" + filename + "</guid>\n"
|
|
|
|
|
content_string += "<pubDate>" + \
|
|
|
|
|
datetime.fromtimestamp(os.path.getmtime(file)).strftime(
|
|
|
|
|
"%Y-%m-%d") + "</pubDate>\n"
|
|
|
|
|
content_string += "<description>"
|
|
|
|
|
"%a, %d %b %Y %H:%M:%S") + " +0100</pubDate>\n"
|
|
|
|
|
content_string += "<description>\n<![CDATA[<html>\n<head>\n</head>\n<body>\n"
|
|
|
|
|
html_string = ""
|
|
|
|
|
for line in text:
|
|
|
|
|
content_string += line
|
|
|
|
|
content_string += "</description>\n"
|
|
|
|
|
html_string += line
|
|
|
|
|
content_string += absolutize_html(html_string)
|
|
|
|
|
content_string += "\n</body></html>\n]]>\n</description>\n"
|
|
|
|
|
content_string += "</item>\n"
|
|
|
|
|
locale.setlocale(locale.LC_TIME, LOCAL)
|
|
|
|
|
return content_string
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def gen_query_res_string(query_str):
|
|
|
|
|
"""
|
|
|
|
|
Return the results of a query.
|
|
|
|
|
Return the results of a query.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
query_str (string): term to search
|
|
|
|
|
Parameters:
|
|
|
|
|
query_str (string): term to search
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
string: html-formated search result
|
|
|
|
|
"""
|
|
|
|
|
Returns:
|
|
|
|
|
string: html-formated search result
|
|
|
|
|
"""
|
|
|
|
|
src_results = search.search(query_str)
|
|
|
|
|
res_string = ""
|
|
|
|
|
for result in src_results:
|
|
|
|
@ -222,29 +251,21 @@ def gen_query_res_string(query_str):
|
|
|
|
|
|
|
|
|
|
def create_preview(path, is_markdown):
|
|
|
|
|
"""
|
|
|
|
|
Create a preview of a given article and return it.
|
|
|
|
|
Create a preview of a given article and return it.
|
|
|
|
|
|
|
|
|
|
Parameters:
|
|
|
|
|
path (string): path to the article
|
|
|
|
|
Parameters:
|
|
|
|
|
path (string): path to the article
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
string: html-formated preview
|
|
|
|
|
"""
|
|
|
|
|
Returns:
|
|
|
|
|
string: html-formated preview
|
|
|
|
|
"""
|
|
|
|
|
file = open(path, "r", encoding="utf-8")
|
|
|
|
|
first_lines = file.readlines()
|
|
|
|
|
lines = file.read()
|
|
|
|
|
if is_markdown:
|
|
|
|
|
lines += markdown.markdown(lines)
|
|
|
|
|
preview = ""
|
|
|
|
|
preview_length = 3
|
|
|
|
|
for i, line in enumerate(first_lines):
|
|
|
|
|
if i == 0:
|
|
|
|
|
continue
|
|
|
|
|
if i > preview_length:
|
|
|
|
|
break
|
|
|
|
|
if not line.isspace():
|
|
|
|
|
if is_markdown:
|
|
|
|
|
preview += markdown.markdown(line)
|
|
|
|
|
else:
|
|
|
|
|
preview += line
|
|
|
|
|
else:
|
|
|
|
|
preview_length += 1
|
|
|
|
|
preview += "<br>...<br>"
|
|
|
|
|
first_p = BeautifulSoup(lines).find('p')
|
|
|
|
|
if first_p is not None:
|
|
|
|
|
preview = "\n<p>" + first_p.text + "</p>\n"
|
|
|
|
|
preview += "...<br>"
|
|
|
|
|
return preview
|
|
|
|
|