From e3b5feb09b0cb80ed1dc63903e527efc2dadc720 Mon Sep 17 00:00:00 2001 From: stev Date: Wed, 20 Sep 2023 22:21:12 +0200 Subject: [PATCH] break down functions into smaller blocks --- src/gmirator/generate.py | 99 +++++++++++++++++++++++++--------------- tests/test_generate.py | 46 ++++++++++++------- 2 files changed, 92 insertions(+), 53 deletions(-) diff --git a/src/gmirator/generate.py b/src/gmirator/generate.py index de001c0..9b68959 100644 --- a/src/gmirator/generate.py +++ b/src/gmirator/generate.py @@ -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'^```.*', "", 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'^```.*', "
", newline)
-            if n:
-                in_preformated = True
-                continue
 
-        if re.match(r'^[a-zA-Z]', newline):
-            newline = "

" + newline.strip() + "

\n" newline, in_list = process_list(newline, in_list) - newline = re.sub(r'^=> (?P[^ ]*) ?(?P.*)\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[^ ]*) ?(?P.*)\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'^```.*', "
", line) + in_preformated = False if n else True + skip = True + newline = newline.lstrip() + else: + newline, n = re.subn(r'^```.*', "
", 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 = "

" + line.strip() + "

\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'=> {text if text else url}
\n' -def repl_heading(matchobj: re.Match): +def _repl_heading(matchobj: re.Match): x = len(matchobj.group(1)) return f"{matchobj.string.strip()}" diff --git a/tests/test_generate.py b/tests/test_generate.py index ae97e22..4270f6a 100644 --- a/tests/test_generate.py +++ b/tests/test_generate.py @@ -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"
  • {test_str[2:]}
  • ", True) @@ -31,28 +39,34 @@ def test_process_list_empty_line(): def test_process_inline(): assert process_inline("*Hello* ~~world~~!") == "Hello world!" -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[^ ]*) ?(?P.*)\n', repl_url, external_url) + new_url = process_url(external_url) assert new_url == '=> gemini://domain/gempage.gmi
    \n' -def test_repl_url_internal_gmi_url(): - external_url = "=> /gempage.gmi title\n" - new_url = re.sub(r'^=> (?P[^ ]*) ?(?P.*)\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 == '=> title
    \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 == "

    # this is h1 title

    " assert html_h2 == "

    ## this is h2 title

    " assert html_h3 == "

    ### this is h3 title

    " -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 == "

    #### this is h4 title

    " + +def test_make_paragraph(): + line = "hello world!" + assert make_paragraph(line) == "

    hello world!

    \n" + line = "*hello world!*" + assert make_paragraph(line) == "

    *hello world!*

    \n"