Compare commits

..

No commits in common. "2f123185a22c02c5257b8e9d715a498e1ed2286f" and "c2ee859cf5ae51b7c64d5ccc2be1ced340cff30b" have entirely different histories.

2 changed files with 3 additions and 45 deletions

View File

@ -146,25 +146,6 @@ def owned_order(c):
c["Poems"].sort(key=key) c["Poems"].sort(key=key)
# Divan-owned details (#32): divan/<poet url>.poet holds a poet's name, pen name, years (CE) and intro, and
# divan/<book url>.book a book or section's title, as "label: value" lines with the intro after "تعارف:" (written
# by the Divan app when they are published). They take precedence over Wikisource and Wikipedia.
def owned_details(url, kind):
path = os.path.join(DIVAN_DIR, url.lstrip("/") + "." + kind)
if not os.path.exists(path):
return None
with open(path, encoding="utf-8") as f:
lines = f.read().split("\n")
out, intro = {}, next((i for i, l in enumerate(lines) if l.startswith("تعارف:")), len(lines))
for l in lines[:intro]:
if ":" in l:
k, v = l.split(":", 1)
out[k.strip()] = v.strip()
if intro < len(lines):
out["تعارف"] = "\n".join([lines[intro][len("تعارف:"):], *lines[intro + 1:]]).strip()
return out
def main(): def main():
for p in ("poets", "index"): for p in ("poets", "index"):
shutil.rmtree(os.path.join(D, p), ignore_errors=True) shutil.rmtree(os.path.join(D, p), ignore_errors=True)
@ -174,20 +155,14 @@ def main():
for page, name, born, died, desc, image in poets: for page, name, born, died, desc, image in poets:
pid = gid("poet", page) pid = gid("poet", page)
purl = f"/p{pid}" purl = f"/p{pid}"
own = owned_details(purl, "poet") # Divan's details take precedence
nick = name
if own:
name, nick = own.get("نام") or name, own.get("تخلص") or name
desc = own.get("تعارف", desc)
born, died = own.get("پیدائش") or None, own.get("وفات") or None
write(f"poets{purl}/poet.json", { write(f"poets{purl}/poet.json", {
"Id": pid, "Name": name, "Nickname": nick, "Description": ud(desc), "FullUrl": purl, "Id": pid, "Name": name, "Nickname": name, "Description": ud(desc), "FullUrl": purl,
"ImageUrl": f"https://commons.wikimedia.org/wiki/Special:FilePath/{image}" if image else None, "ImageUrl": f"https://commons.wikimedia.org/wiki/Special:FilePath/{image}" if image else None,
"BirthYearInLHijri": hijri(born), "ValidBirthDate": bool(hijri(born)), "BirthYearInLHijri": hijri(born), "ValidBirthDate": bool(hijri(born)),
"DeathYearInLHijri": hijri(died), "ValidDeathDate": bool(hijri(died)), "DeathYearInLHijri": hijri(died), "ValidDeathDate": bool(hijri(died)),
"BirthYearCE": year_ce(born), "DeathYearCE": year_ce(died), # Gregorian (عیسوی), divan extension "BirthYearCE": year_ce(born), "DeathYearCE": year_ce(died), # Gregorian (عیسوی), divan extension
"BirthPlace": None, "DeathPlace": None}) "BirthPlace": None, "DeathPlace": None})
manifest.append({"Id": pid, "Nickname": nick, "FullUrl": purl}) manifest.append({"Id": pid, "Nickname": name, "FullUrl": purl})
# category tree from section paths ("شاعری > بانگ درا (1924)") # category tree from section paths ("شاعری > بانگ درا (1924)")
first_line = {} first_line = {}
@ -201,8 +176,7 @@ def main():
slug = GENRES.get(path[-1], f"c{cid}") slug = GENRES.get(path[-1], f"c{cid}")
if any(c["FullUrl"] == f"{parent['FullUrl']}/{slug}" for c in parent["ChildCats"]): if any(c["FullUrl"] == f"{parent['FullUrl']}/{slug}" for c in parent["ChildCats"]):
slug = f"c{cid}" slug = f"c{cid}"
title = (owned_details(f"{parent['FullUrl']}/{slug}", "book") or {}).get("عنوان") or ud(path[-1]) c = {"Id": cid, "PoetId": pid, "ParentId": parent["Id"], "Title": ud(path[-1]),
c = {"Id": cid, "PoetId": pid, "ParentId": parent["Id"], "Title": title,
"FullUrl": f"{parent['FullUrl']}/{slug}", "Description": None, "DescriptionHtml": None, "FullUrl": f"{parent['FullUrl']}/{slug}", "Description": None, "DescriptionHtml": None,
"BookName": None, "ChildCats": [], "Poems": []} "BookName": None, "ChildCats": [], "Poems": []}
parent["ChildCats"].append({"Id": cid, "Title": c["Title"], "FullUrl": c["FullUrl"]}) parent["ChildCats"].append({"Id": cid, "Title": c["Title"], "FullUrl": c["FullUrl"]})

View File

@ -1,16 +0,0 @@
# python3 test_divan_details.py
import os, tempfile
import export_divan
with tempfile.TemporaryDirectory() as d:
export_divan.DIVAN_DIR = d
assert export_divan.owned_details("/p1", "poet") is None
with open(os.path.join(d, "p1.poet"), "w", encoding="utf-8") as f:
f.write("نام: محمد اقبال\nتخلص: اقبال\nپیدائش: 1877\nوفات: \nتعارف:\nپہلا: پیراگراف\n\nدوسرا\n")
p = export_divan.owned_details("/p1", "poet")
assert p == {"نام": "محمد اقبال", "تخلص": "اقبال", "پیدائش": "1877", "وفات": "", "تعارف": "پہلا: پیراگراف\n\nدوسرا"}, p
os.makedirs(os.path.join(d, "p1"))
with open(os.path.join(d, "p1", "ghazal.book"), "w", encoding="utf-8") as f:
f.write("عنوان: غزلیات\n")
assert export_divan.owned_details("/p1/ghazal", "book") == {"عنوان": "غزلیات"}
print("ok")