-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
129 lines (113 loc) · 5.31 KB
/
main.py
File metadata and controls
129 lines (113 loc) · 5.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
import yaml
# for processing supporting paper
from bibtexparser.bparser import BibTexParser
import bibtexparser
import latexcodec
import re
def decode_latex(s):
if not isinstance(s, str): return s
try:
s_dec = s.encode('utf-8').decode('latex')
s_clean = re.sub(r'\{([^\{\}]+?)\}', r'\1', s_dec)
return s_clean
except Exception as e:
print(f"Error decoding LaTeX string: {s}\n{e}")
return s
# Read the bibliography file on load
with open('bibliography.bib') as bibfile:
parser = BibTexParser(common_strings=True)
bib_database = bibtexparser.load(bibfile, parser=parser)
bib_entries = {entry["ID"]: entry for entry in bib_database.entries}
def define_env(env):
@env.macro
def model_table():
model_root = os.path.join(env.project_dir, "docs", "models")
table = "| Model name | Taxon - all terms | Process - all terms | Year | Author(s) |\n|-------|-------|---------|-------|-------|\n"
for model_name in sorted(os.listdir(model_root)):
model_dir = os.path.join(model_root, model_name)
index_path = os.path.join(model_dir, "index.md")
if not os.path.isdir(model_dir) or not os.path.exists(index_path):
continue
with open(index_path, encoding="utf-8") as f:
content = f.read()
if content.startswith("---"):
_, frontmatter, _ = content.split("---", 2)
meta = yaml.safe_load(frontmatter)
title = meta.get("title", model_name)
taxon = ", ".join(meta.get("taxon", []))
process = ", ".join(meta.get("process", []))
supporting_id = meta.get("supporting_paper")
year, author = "", ""
if (supporting_id and supporting_id in bib_entries):
entry = bib_entries[supporting_id]
year = entry.get("year", "")
author = decode_latex(entry.get("author", "Unknown Author"))
link = f"{model_name}/"
table += f"| [{title}]({link}) | {taxon} | {process} | {year} | {author} |\n"
return table
def on_post_page_macros(env):
file = env.page.file
if not file.src_path.startswith("models/") or \
not file.src_path.endswith("index.md") or \
file.src_path == "models/index.md": return
meta = env.page.meta
meta_md = ''
# Taxon
taxon = meta.get("taxon", [])
taxon_str = " | ".join(taxon) if isinstance(taxon, list) else str(taxon)
if taxon_str: meta_md += f"<small><b>Taxon:</b> {taxon_str}</small>  "
# Process
process = meta.get("process", [])
process_str = " | ".join(process) if isinstance(process, list) else str(process)
if process_str: meta_md += f"<small><b>Process:</b> {process_str}</small> \n"
# Submitter
#submitter = meta.get("submitter", [])
#if submitter: meta_md += f"<small><b>Submitter:</b> {submitter}</small>\n\n"
# Supporting paper
supporting_id = meta.get("supporting_paper")
if supporting_id and supporting_id in bib_entries:
entry = bib_entries[supporting_id]
author = decode_latex(entry.get("author", "Unknown Author"))
title = decode_latex(entry.get("title", "Untitled"))
journal = decode_latex(entry.get("journal", ""))
year = entry.get("year", "")
doi = entry.get("doi", "")
url = entry.get("url", "")
citation = f"{author} ({year}). *{title}*. {journal}."
if doi: citation += f" [{doi}](https://doi.org/{doi})"
elif url: citation += f" [Link]({url})"
#meta_md += f"<small>**Supporting paper:** {citation}</small><br>\n"
meta_md += f"<small>**Title:** {title}</small><br>\n"
meta_md += f"<small>**Author(s):** {author}</small><br>\n"
meta_md += f"<small>**Journal:** {journal}</small><br>\n"
meta_md += f"<small>**Year:** {year}</small><br>\n"
if doi: meta_md += f"<small>**DOI:** [{doi}](https://doi.org/{doi})</small>\n\n"
elif url: meta_md += f"<small>**URL:** [Link]({url})</small>\n\n"
elif supporting_id:
meta_md += f"<small>**Supporting paper:** Entry `{supporting_id}` not found in bibliography.\n\n"
# Model files
modelfiles = meta.get("files", [])
modeldesc = meta.get("file_descriptions", [])
meta_md += "| Model file(s) | Description(s) |\n|--|--|\n"
for i, f in enumerate(modelfiles):
meta_md += f"| [{f}]({f}) | {modeldesc[i]} |\n"
meta_md += "\n"
# Related paper
related_id = meta.get("related_paper")
if related_id and related_id in bib_entries:
entry = bib_entries[related_id]
author = decode_latex(entry.get("author", "Unknown Author"))
title = decode_latex(entry.get("title", "Untitled"))
journal = decode_latex(entry.get("journal", ""))
year = entry.get("year", "")
doi = entry.get("doi", "")
url = entry.get("url", "")
citation = f"{author} ({year}). *{title}*. {journal}."
if doi: citation += f" [{doi}](https://doi.org/{doi})"
elif url: citation += f" [Link]({url})"
meta_md += f"<br><small>**Related reference:** {citation}</small>\n\n"
elif related_id:
meta_md += f"<br><small>**Related reference:** Entry `{related_id}` not found in bibliography.\n\n"
meta_md += "\n**Summary:** \n"
env.markdown = meta_md + env.markdown