From c38a60c33945f41ac993a5ede3ba67306e55e976 Mon Sep 17 00:00:00 2001 From: Francesco Mecca Date: Thu, 11 May 2023 11:28:40 +0200 Subject: [PATCH] more --- gamewiki/parser.py | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/gamewiki/parser.py b/gamewiki/parser.py index 815aa55..d19b33a 100644 --- a/gamewiki/parser.py +++ b/gamewiki/parser.py @@ -31,15 +31,27 @@ class WikiArticle: self.tags = tags(content) self.filename = filename - def enrich(self, articles: Iterator): - raise NotImplementedError() + def enrich(self, linked_articles: Iterator): + '''Generator that returns content that will end up in the document before html conversion''' + content = map(identity, self.content) + def rep(): + for line in content: + if line == self.header: break + else: yield line + yield self.header + for w in linked_articles: + yield f'- [[../{w.filename}][{w.title}]]' + return rep() def __repr__(self): return f'{type(self).__name__}({self.title}, {self.filename}, {self.tags})' class ContentArticle(WikiArticle): '''The articles we wrote''' - pass + def __init__(self, filename): + self.header = '** Related articles' + super().__init__(filename) + class MetaPage(WikiArticle): '''Wiki article containing code that must be generated by this program''' @@ -54,19 +66,6 @@ class MetaPage(WikiArticle): if len(correct_section) != 1: raise Exception(f'Invalid meta section in {filename}') - def enrich(self, linked_articles: Iterator[WikiArticle]): - '''Generator that returns content that will end up in the document before html conversion''' - content = map(identity, self.content) - def rep(): - for line in content: - if line == self.header: break - else: yield line - yield self.header - for w in linked_articles: - yield f'- [[../{w.filename}][{w.title}]]' - return rep() - - identity = lambda x: x def merge(d1, d2): d1.update(d2) @@ -96,7 +95,7 @@ def links(article: ContentArticle): sub = line[i:] if sub.startswith('[[./') and ']]' in sub and '.org' in sub: # org link to one org file ridx = sub.index('.org') - yield sub[:ridx+4] + yield sub[4:ridx+4] # skip initial [[ i += sub.index(']]') i += 1 @@ -111,6 +110,7 @@ def writetodisk(m: WikiArticle, content: list[str]): if __name__ == '__main__': files_ = [ContentArticle(f) for f in files()] + byfilename = {c.filename: c for c in files_} meta_ = [MetaPage(f) for f in metafiles()] meta = {m.meta: m for m in meta_} @@ -120,5 +120,7 @@ if __name__ == '__main__': newcontent = meta[t].enrich(articles) writetodisk(meta[t], newcontent) - for a in files_: - print(a, links(a)) + for f in files_: + related = set(links(f)) + newcontent = f.enrich(map(lambda f: byfilename[f], related)) + writetodisk(f, newcontent)