Commit f88b68c1 authored by midgard's avatar midgard

Refactor nicer

parent 669818bd
......@@ -47,8 +47,8 @@ aliases = {
"robbert": "beardhatcode",
}
def coalesce(it):
for item in it:
def coalesce(*arguments):
for item in arguments:
if item is not None:
return item
return None
......@@ -72,6 +72,29 @@ def link_for(name, role):
return f'<a href="https://mattermost.zeus.gent/zeus/messages/@{mm}">@{escape(mm)}</a>'
return f'<a href="https://mattermost.zeus.gent/zeus/messages/@{mm}" role="{role}">@{escape(mm)}</a>'
def extract_quotee(quote):
# Multiple IRC style quotes
m = re.search(rf'{RE_IRCNICK}.*?{RE_IRCNICK}|{RE_IRCDATE}.*?{RE_IRCDATE}', quote, re.M | re.S)
if m:
return ""
# Multiple Mattermost style quotes
m = re.search(rf'{RE_MMNICK}.*\n{RE_MMNICK}|{RE_MMNICK}.* (?:[-–—]|\|\|) {RE_MMNICK}', quote, re.M | re.S)
if m:
return ""
# IRC quotes
m = re.search(RE_CAPTURING_IRCNICK, quote)
if m:
return coalesce(m.group(1), m.group(2))
# Quotes with Mattermost ID
m = re.search(RE_CAPTURING_MMNICK, quote)
if m:
return m.group(1)
return None
months = [
"januari", "februari", "maart", "april", "mei", "juni", "juli", "augustus", "september",
......@@ -281,25 +304,10 @@ for tr in skip(table.find_all("tr", recursive=False), 1):
# print(tds)
quoter, quotee, when, quote = (td.string for td in tds)
m = re.search(rf'{RE_IRCNICK}.*?{RE_IRCNICK}|{RE_IRCDATE}.*?{RE_IRCDATE}', quote, re.M | re.S)
if m:
# Multiple quotes
quotee = coalesce(extract_quotee(quote), quotee)
if quotee == "":
quotee = None
else:
m = re.search(rf'{RE_MMNICK}.*\n{RE_MMNICK}|{RE_MMNICK}.* [-–—] {RE_MMNICK}', quote, re.M | re.S)
if m:
quotee = None
else:
# IRC quotes
m = re.search(RE_CAPTURING_IRCNICK, quote)
if m:
quotee = coalesce((m.group(1), m.group(2)))
else:
m = re.search(RE_CAPTURING_MMNICK, quote)
if m:
quotee = m.group(1)
when = datetime.strptime(when, "%Y-%m-%d %H:%M:%S")
when = brussels.localize(when)
slur = when.strftime("%Y-%m-%d_%H:%M:%S")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment