2023-05-10 18:08:15 +01:00
|
|
|
import glob
|
2023-05-11 10:07:49 +01:00
|
|
|
import re
|
|
|
|
from typing import TypeVar, Iterator
|
|
|
|
|
|
|
|
T = TypeVar("T")
|
|
|
|
|
2023-05-10 18:08:15 +01:00
|
|
|
|
|
|
|
class WikiArticle:
|
2023-05-11 10:07:49 +01:00
|
|
|
'''Abstract class that defines an article in the wiki'''
|
2023-05-10 18:08:15 +01:00
|
|
|
def __init__(self, filename):
|
|
|
|
def tags(content):
|
|
|
|
for line in content:
|
|
|
|
if line.startswith('#+FILETAGS:'):
|
|
|
|
tags = line.replace(' ', '').strip().split(":")[1:]
|
|
|
|
tags = list(filter(identity, tags))
|
|
|
|
return tags
|
|
|
|
raise Exception(f'no tags in {filename}')
|
|
|
|
|
|
|
|
def title(content):
|
|
|
|
for line in content:
|
|
|
|
if line.startswith('#+TITLE:'):
|
|
|
|
res = line.strip().replace(' ', '', 1).split(':')
|
|
|
|
if len(res) > 2: raise Exception(f'Invalid title in {filename}')
|
|
|
|
return res[-1]
|
|
|
|
raise Exception(f'No title in {filename}')
|
|
|
|
|
|
|
|
with open(filename, 'r') as f:
|
|
|
|
content = f.readlines()
|
|
|
|
self.content = [c.strip() for c in content]
|
|
|
|
self.title = title(content)
|
|
|
|
self.tags = tags(content)
|
|
|
|
self.filename = filename
|
2023-05-11 10:07:49 +01:00
|
|
|
|
|
|
|
def enrich(self, articles: Iterator):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2023-05-10 18:08:15 +01:00
|
|
|
def __repr__(self):
|
|
|
|
return f'{type(self).__name__}({self.title}, {self.filename}, {self.tags})'
|
|
|
|
|
2023-05-11 10:07:49 +01:00
|
|
|
class ContentArticle(WikiArticle):
|
|
|
|
'''The articles we wrote'''
|
|
|
|
pass
|
|
|
|
|
2023-05-10 18:08:15 +01:00
|
|
|
class MetaPage(WikiArticle):
|
2023-05-11 10:07:49 +01:00
|
|
|
'''Wiki article containing code that must be generated by this program'''
|
2023-05-10 18:08:15 +01:00
|
|
|
def __init__(self, filename):
|
|
|
|
self.header = '** Pages in this category'
|
|
|
|
super().__init__(filename)
|
|
|
|
if len(self.tags) > 1:
|
|
|
|
raise Exception(f'Multiple tags in metapage: {self.filename}')
|
|
|
|
self.meta = self.tags[0]
|
|
|
|
|
|
|
|
correct_section = list(filter(lambda l: l == self.header, self.content))
|
|
|
|
if len(correct_section) != 1:
|
|
|
|
raise Exception(f'Invalid meta section in {filename}')
|
|
|
|
|
2023-05-11 10:07:49 +01:00
|
|
|
def enrich(self, linked_articles: Iterator[WikiArticle]):
|
|
|
|
'''Generator that returns content that will end up in the document before html conversion'''
|
2023-05-10 18:08:15 +01:00
|
|
|
content = map(identity, self.content)
|
|
|
|
def rep():
|
|
|
|
for line in content:
|
|
|
|
if line == self.header: break
|
|
|
|
else: yield line
|
|
|
|
yield self.header
|
2023-05-11 10:07:49 +01:00
|
|
|
for w in linked_articles:
|
2023-05-10 18:08:15 +01:00
|
|
|
yield f'- [[../{w.filename}][{w.title}]]'
|
|
|
|
return rep()
|
|
|
|
|
|
|
|
|
|
|
|
identity = lambda x: x
|
|
|
|
def merge(d1, d2):
|
|
|
|
d1.update(d2)
|
|
|
|
return d1
|
|
|
|
|
|
|
|
def files():
|
|
|
|
return filter(lambda f: f not in {'todo.org'}, glob.glob('*.org'))
|
|
|
|
|
|
|
|
def metafiles():
|
|
|
|
return glob.glob('meta/*.org')
|
|
|
|
|
2023-05-11 10:07:49 +01:00
|
|
|
def invert_map(map_: list[ContentArticle]):
|
2023-05-10 18:08:15 +01:00
|
|
|
'''from {a: [1, 2], b: [2, 3]} to {1: [a], 2: [a, b], 3: [b]}'''
|
|
|
|
keys = set(i for e in map_ for i in e.tags)
|
2023-05-11 10:07:49 +01:00
|
|
|
res: dict[str, list[WikiArticle]] = dict()
|
2023-05-10 18:08:15 +01:00
|
|
|
for k in keys:
|
|
|
|
res[k] = res.get(k, []) + [w for w in map_ if k in w.tags]
|
|
|
|
return res
|
|
|
|
|
2023-05-11 10:07:49 +01:00
|
|
|
def links(article: ContentArticle):
|
|
|
|
def haslink(line):
|
|
|
|
return re.findall(r'\[\[(.+?)\]\]', line)
|
|
|
|
|
|
|
|
def yield_links(line):
|
|
|
|
i = 0
|
|
|
|
while i < len(line)-1:
|
|
|
|
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]
|
|
|
|
i += sub.index(']]')
|
|
|
|
i += 1
|
|
|
|
|
|
|
|
for line in article.content:
|
|
|
|
if haslink(line):
|
|
|
|
yield from yield_links(line)
|
|
|
|
|
|
|
|
|
|
|
|
def writetodisk(m: WikiArticle, content: list[str]):
|
2023-05-10 18:08:15 +01:00
|
|
|
with open(m.filename, 'w') as f:
|
|
|
|
f.writelines(map(lambda l: l+'\n', content))
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-05-11 10:07:49 +01:00
|
|
|
files_ = [ContentArticle(f) for f in files()]
|
|
|
|
meta_ = [MetaPage(f) for f in metafiles()]
|
|
|
|
meta = {m.meta: m for m in meta_}
|
|
|
|
|
|
|
|
tags = invert_map(files_)
|
2023-05-10 18:08:15 +01:00
|
|
|
for t, articles in tags.items():
|
|
|
|
if t in meta:
|
2023-05-11 10:07:49 +01:00
|
|
|
newcontent = meta[t].enrich(articles)
|
|
|
|
writetodisk(meta[t], newcontent)
|
|
|
|
|
|
|
|
for a in files_:
|
|
|
|
print(a, links(a))
|