break down functions into smaller blocks

This commit is contained in:
stev 2023-09-20 22:21:12 +02:00
parent 961e9bc1e3
commit e3b5feb09b
2 changed files with 92 additions and 53 deletions

View file

@ -22,25 +22,32 @@ def gen_gmi():
make_footer(GMI_FOOTER, DIR_SOURCE)
logging.info(" Done!")
def make_index(listing: list[Path]):
def make_index(list_files: list[Path]):
logging.info(" Populating gemlog index ...")
title, date = "", ""
files = (DIR_SOURCE / "gemlog").rglob(r'*.gmi')
files = [ \
file for file in files \
if re.search(r'\d\d\d\d.*\.gmi', file.as_posix()) \
gmi_files = (DIR_SOURCE / "gemlog").rglob(r'*.gmi')
gmi_files = [ \
file for file in gmi_files \
if re.search(r'\d\d\d\d.*\.gmi', file.as_posix()) \
]
articles = ""
for file in files[::-1]:
articles = create_articles_index(gmi_files)
write_index_to_files(list_files, articles)
def create_articles_index(gmi_files: list[Path]):
articles = []
for file in gmi_files[::-1]:
title, date = find_title_date(file)
logging.info(f" \u2713 {date} {title.strip()}")
articles += f"=> /gemlog/{file.stem}.gmi {date} - {title}"
articles.append(f"=> /gemlog/{file.stem}.gmi {date} - {title}")
return ''.join(articles)
for path in listing:
def write_index_to_files(list_files: list, articles: str):
separator = "## logs\n"
size = len(separator)
for path in list_files:
with open(path, "r+") as file:
line = file.readline(9)
while line != "## logs\n":
line = file.readline(9)
line = file.readline(size)
while line != separator:
line = file.readline(size)
file.seek(0, 1)
if not path.match(r"gemlog/index.gmi"):
file.write("=> /gemlog/index.gmi all gemlogs here\n")
@ -93,48 +100,66 @@ def gmi2html(file: str):
with open(file, 'r') as gemlog:
contents, title = process_file(gemlog)
with open(DIR_HELPER / "header-part.html", "r") as header_file:
header_content = header_file.read()
header_content = re.sub("PAGETITLE", f"{title}", header_content)
header = header_file.read()
header = re.sub("PAGETITLE", f"{title}", header)
with open(DIR_HELPER / "footer-part.html", "r") as footer_file:
footer_content = footer_file.read()
footer = footer_file.read()
with open(new_file_path, "w") as new_file:
new_file.write(header_content)
new_file.write(header)
new_file.write(contents)
new_file.write(footer_content)
new_file.write(footer)
def process_file(ifile: TextIOWrapper):
in_preformated = False
in_list = False
contents = ""
contents = []
title = ""
for line in ifile:
if line == "---\n":
break
elif line[:2] == "# ":
elif not title and line[:2] == "# ":
title = line[2:-1]
newline = line
if in_preformated:
newline, n = re.subn(r'^```.*', "</pre>", newline)
in_preformated = False if n else True
contents += newline.lstrip()
newline, in_preformated, skip = process_preformated(newline, in_preformated)
if skip:
contents.append(newline)
continue
else:
newline, n = re.subn(r'^```.*', "<pre>", newline)
if n:
in_preformated = True
continue
if re.match(r'^[a-zA-Z]', newline):
newline = "<p>" + newline.strip() + "</p>\n"
newline, in_list = process_list(newline, in_list)
newline = re.sub(r'^=> (?P<url>[^ ]*) ?(?P<text>.*)\n', repl_url, newline)
newline = re.sub(r'^(#+) (.*)', repl_heading, newline)
newline = make_paragraph(newline)
newline = process_url(newline)
newline = process_heading(newline)
newline = process_inline(newline)
contents += newline
return contents, title
contents.append(newline)
return ''.join(contents), title
def process_url(line: str):
return re.sub(r'^=> (?P<url>[^ ]*) ?(?P<text>.*)\n', _repl_url, line)
def process_heading(line: str):
return re.sub(r'^(#+) (.*)', _repl_heading, line)
def process_preformated(line: str, in_preformated: bool):
skip = False
if in_preformated:
newline, n = re.subn(r'^```.*', "</pre>", line)
in_preformated = False if n else True
skip = True
newline = newline.lstrip()
else:
newline, n = re.subn(r'^```.*', "<pre>", line)
if n:
in_preformated = True
skip = True
return newline, in_preformated, skip
def make_paragraph(line: str):
if re.match(r'(^[a-zA-Z]|^\*\w|^~)', line):
line = "<p>" + line.strip() + "</p>\n"
return line
def process_list(line: str, in_list: bool):
try:
@ -160,13 +185,13 @@ def process_inline(line: str):
line = re.sub(pattern, repl, line)
return line
def repl_url(matchobj: re.Match):
def _repl_url(matchobj: re.Match):
url, text = matchobj.groupdict().values()
if not re.findall(r'(http|gemini)', url):
url = re.sub(r'gmi$', r'html', url)
return f'<a href="{url}">=> {text if text else url}</a><br>\n'
def repl_heading(matchobj: re.Match):
def _repl_heading(matchobj: re.Match):
x = len(matchobj.group(1))
return f"<h{x}>{matchobj.string.strip()}</h{x}>"

View file

@ -1,16 +1,24 @@
import re
from gmirator.generate import (
process_file,
make_paragraph,
process_heading,
process_list,
process_inline,
repl_url,
repl_heading
process_url
)
############################
### generate gmi section ###
############################
def test_process_file():
def test_make_footer():
pass
def test_find_title_date():
pass
#############################
### generate html section ###
#############################
def test_process_list_legit_item():
test_str = "* this is a list item"
assert process_list(test_str, True) == (f"<li>{test_str[2:]}</li>", True)
@ -31,28 +39,34 @@ def test_process_list_empty_line():
def test_process_inline():
assert process_inline("*Hello* ~~world~~!") == "<em>Hello</em> <s>world</s>!"
def test_repl_url_external_gmi_url():
def test_process_url_external_gmi_url():
external_url = "=> gemini://domain/gempage.gmi\n"
new_url = re.sub(r'^=> (?P<url>[^ ]*) ?(?P<text>.*)\n', repl_url, external_url)
new_url = process_url(external_url)
assert new_url == '<a href="gemini://domain/gempage.gmi">=> gemini://domain/gempage.gmi</a><br>\n'
def test_repl_url_internal_gmi_url():
external_url = "=> /gempage.gmi title\n"
new_url = re.sub(r'^=> (?P<url>[^ ]*) ?(?P<text>.*)\n', repl_url, external_url)
def test_process_url_internal_gmi_url():
internal_url = "=> /gempage.gmi title\n"
new_url = process_url(internal_url)
assert new_url == '<a href="/gempage.html">=> title</a><br>\n'
def test_repl_heading():
def test_process_heading():
line = "# this is h1 title"
html_h1 = re.sub(r'^(#+) (.*)', repl_heading, line)
html_h1 = process_heading(line)
line = "## this is h2 title"
html_h2 = re.sub(r'^(#+) (.*)', repl_heading, line)
html_h2 = process_heading(line)
line = "### this is h3 title"
html_h3 = re.sub(r'^(#+) (.*)', repl_heading, line)
html_h3 = process_heading(line)
assert html_h1 == "<h1># this is h1 title</h1>"
assert html_h2 == "<h2>## this is h2 title</h2>"
assert html_h3 == "<h3>### this is h3 title</h3>"
def test_repl_heading_h4():
def test_process_heading_h4():
line = "#### this is h4 title"
html_h4 = re.sub(r'^(#+) (.*)', repl_heading, line)
html_h4 = process_heading(line)
assert html_h4 == "<h4>#### this is h4 title</h4>"
def test_make_paragraph():
line = "hello world!"
assert make_paragraph(line) == "<p>hello world!</p>\n"
line = "*hello world!*"
assert make_paragraph(line) == "<p>*hello world!*</p>\n"