From 3a6bec352c68279bf9b67efe7e0cab6067f1b0c5 Mon Sep 17 00:00:00 2001 From: Aleteoryx Date: Thu, 28 Aug 2025 14:07:20 -0400 Subject: [PATCH] improved see also, handle title properly, add /italics/ --- gloss.py | 59 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 46 insertions(+), 13 deletions(-) diff --git a/gloss.py b/gloss.py index d6ce5026833450970a9792b9f2efde53c20bbc54..33cd4529f9abc6131add9c81ad74488c461d25f5 100755 --- a/gloss.py +++ b/gloss.py @@ -39,6 +39,7 @@ class Block: @dataclass class GlsFile: slug: str + title: str names: Set[str] blocks: List[Block] see_also: Optional[List[str]] @@ -57,9 +58,12 @@ def first_pass(slug, fp): lines = readlines(fp) names = set() + title = None for name in lines: if len(name) == 0 and len(names) != 0: # section end break + if title is None: + title = name.strip(); names.add(name.strip()) if len(names) == 0: @@ -106,11 +110,15 @@ def first_pass(slug, fp): see_also = None if do_see_also: see_also = [*filter(len, lines)] + elif block_type is not None: + blocks.append(Block(block_type, block_text, block_meta)) - return GlsFile(slug, [*names], blocks, see_also) + return GlsFile(slug, title, [*names], blocks, see_also) quote_pat = re.compile("((?:(?!@@)(?!//).)+)(?:@@((?:(?!//).)+))?(?://(.+))?") link_pat = re.compile("(?|]+)(?:\\|([^>]*))?>(?)") +stripped_link_pat = "(?|]+(?:\\|[^>]*)?>(?)" +italic_pat = re.compile(f"(?:(?<=\\W)|^)/(?:[^<>/]|{stripped_link_pat})+/(?:(?=\\W)|$)") ### GENERATION ### @@ -146,15 +154,28 @@ class Indexes: sorted(self.names_sorted, key=lambda x: len(x[0])) def gen_inner_html(file, idx): - for block in file.blocks: # populate external links, listify block.text + for block in file.blocks: # populate italics+external links, listify block.text text = block.text - block.text = [] - while (m := link_pat.search(text)) is not None: + working = [] + while (m := italic_pat.search(text)) is not None: s,e = m.span() - block.text.append(text[:s].replace('\\<', '<').replace('\\>', '>')) + working.append(text[:s]) + working.append(('',)) + working.append(text[s:e]) + working.append(('',)) text = text[e:] - block.text.append((link_repl(m),)) - block.text.append(text) + working.append(text) + block.text = [] + for text in working: + if type(text) != str: + block.text.append(text) + continue + while (m := link_pat.search(text)) is not None: + s,e = m.span() + block.text.append(text[:s].replace('\\<', '<').replace('\\>', '>')) + block.text.append((link_repl(m),)) + text = text[e:] + block.text.append(text) blacklist = set() for name, pat in idx.names_sorted: # populate local links @@ -178,7 +199,7 @@ def gen_inner_html(file, idx): block.text.insert(i, seg[:s]) break - content = f"

{html.escape(file.names[0])}

" + content = f"

{html.escape(file.title)}

" for block in file.blocks: # ok generate the html text = ''.join(map(lambda x: html.escape(x) if type(x) == str else x[0], block.text)) if block.ty == 'para': @@ -201,10 +222,22 @@ def gen_inner_html(file, idx): if file.see_also is not None and len(file.see_also) != 0: content += "\n
\n

See Also:

\n" return content @@ -250,7 +283,7 @@ if __name__ == '__main__': for file in files: with open(f'{outdir}/{file.slug}.html', 'wt') as fp: ctx = { - 'title': html.escape(file.names[0]), + 'title': html.escape(file.title), 'slug': html.escape(file.slug), 'body': gen_inner_html(file, indexes), 'modtime': datetime.fromtimestamp(stat(f'{srcdir}/{file.slug}.gls').st_mtime)