-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathmqb_bibstyle.py
More file actions
143 lines (115 loc) · 3.69 KB
/
mqb_bibstyle.py
File metadata and controls
143 lines (115 loc) · 3.69 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""Custom bibliography formatting for Jupyter Book/Sphinx.
Goal: remove URLs from the rendered reference list unless the entry is clearly a
webpage or software/package reference.
This keeps the BibTeX source intact and applies presentation logic at render
(time) via a custom pybtex formatting style registered as `mqb_nourl`.
"""
from __future__ import annotations
from typing import Any
from pybtex.plugin import register_plugin
from pybtex.richtext import Text
from pybtex.style.formatting.unsrt import Style as UnsrtStyle
def _lower(s: Any) -> str:
if s is None:
return ""
return str(s).lower()
def _is_scholarly(entry: Any) -> bool:
"""Heuristic: treat as scholarly if it looks like a paper/book/report."""
fields = getattr(entry, "fields", {}) or {}
# Presence of these fields usually indicates a conventional scholarly item.
scholarly_fields = {
"journal",
"booktitle",
"publisher",
"school",
"institution",
"volume",
"number",
"pages",
"doi",
"issn",
"isbn",
}
if any(k in fields for k in scholarly_fields):
return True
entry_type = _lower(getattr(entry, "type", ""))
scholarly_types = {
"article",
"book",
"incollection",
"inproceedings",
"proceedings",
"phdthesis",
"mastersthesis",
"techreport",
"report",
}
return entry_type in scholarly_types
def _keep_url(entry: Any) -> bool:
"""Return True iff the URL should be shown in the bibliography."""
fields = getattr(entry, "fields", {}) or {}
entry_type = _lower(getattr(entry, "type", ""))
url = _lower(fields.get("url"))
title = _lower(fields.get("title"))
note = _lower(fields.get("note"))
howpublished = _lower(fields.get("howpublished"))
if not url:
return False
# Strong signals: explicit web/software types.
if entry_type in {"online", "software"}:
return True
blob = " ".join([title, note, howpublished, url])
# Common software/package indicators.
software_keywords = {
"software",
"package",
"cran",
"pypi",
"conda",
"bioconductor",
"github",
"gitlab",
"source code",
"documentation",
"user guide",
"manual",
}
if any(k in blob for k in software_keywords):
return True
# Webpage indicator: has an access date and doesn't look like a paper.
if "urldate" in fields and not _is_scholarly(entry):
return True
return False
class NoUrlUnlessWebOrSoftwareStyle(UnsrtStyle):
"""unsrt-like references, but suppress URLs for non-web/software items."""
def format_url(self, e: Any) -> Text: # pybtex uses `e` for the entry
if not _keep_url(e):
return Text("")
# Prefer the base style's formatting if available.
try:
return super().format_url(e)
except Exception:
url = (getattr(e, "fields", {}) or {}).get("url")
if not url:
return Text("")
return Text(url)
def format_urldate(self, e: Any) -> Text:
# Only show access dates when we show the URL.
if not _keep_url(e):
return Text("")
try:
return super().format_urldate(e)
except Exception:
return Text("")
def setup(app: Any) -> dict[str, Any]:
# Register our style under a simple name.
register_plugin(
"pybtex.style.formatting",
"mqb_nourl",
NoUrlUnlessWebOrSoftwareStyle,
)
return {
"version": "1.0",
"parallel_read_safe": True,
"parallel_write_safe": True,
}