-
Notifications
You must be signed in to change notification settings - Fork 12
/
mdx_bib.py
206 lines (160 loc) · 6.18 KB
/
mdx_bib.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
from markdown.extensions import Extension
from markdown.preprocessors import Preprocessor
from markdown.treeprocessors import Treeprocessor
from markdown.inlinepatterns import Pattern
import xml.etree.ElementTree as etree
from pybtex.database.input import bibtex
from collections import OrderedDict
import re
BRACKET_RE = re.compile(r"\[([^\[]+)\]")
CITE_RE = re.compile(r"@(\w+)")
DEF_RE = re.compile(r"\A {0,3}\[@(\w+)\]:\s*(.*)")
INDENT_RE = re.compile(r"\A\t| {4}(.*)")
CITATION_RE = r"@(\w+)"
class Bibliography(object):
"""Keep track of document references and citations for exporting"""
def __init__(self, extension, bibtex_file, order):
self.extension = extension
self.order = order
self.citations = OrderedDict()
self.references = dict()
if bibtex_file:
try:
parser = bibtex.Parser()
self.bibsource = parser.parse_file(bibtex_file).entries
except Exception as e:
print(f"Error loading bibtex file - {e}")
self.bibsource = dict()
else:
self.bibsource = dict()
def addCitation(self, citekey):
self.citations[citekey] = self.citations.get(citekey, 0) + 1
def setReference(self, citekey, reference):
self.references[citekey] = reference
def citationID(self, citekey):
return "cite-" + citekey
def referenceID(self, citekey):
return "ref-" + citekey
def formatAuthor(self, author):
out = "%s %s." % (author.last()[0], author.first()[0][0])
if author.middle():
out += "%s." % (author.middle()[0][0])
return out
def formatReference(self, ref):
authors = ", ".join(map(self.formatAuthor, ref.persons["author"]))
title = ref.fields["title"]
journal = ref.fields.get("journal", "")
volume = ref.fields.get("volume", "")
year = ref.fields.get("year")
reference = "<p>%s: <i>%s</i>." % (authors, title)
if journal:
reference += " %s." % journal
if volume:
reference += " <b>%s</b>," % volume
reference += " (%s)</p>" % year
return reference
def makeBibliography(self, root):
if self.order == "alphabetical":
raise (NotImplementedError)
div = etree.Element("div")
div.set("class", "references")
if not self.citations:
return div
table = etree.SubElement(div, "table")
tbody = etree.SubElement(table, "tbody")
for id in self.citations:
tr = etree.SubElement(tbody, "tr")
tr.set("id", self.referenceID(id))
ref_id = etree.SubElement(tr, "td")
ref_id.text = id
ref_txt = etree.SubElement(tr, "td")
if id in self.references:
self.extension.parser.parseChunk(ref_txt, self.references[id])
elif id in self.bibsource:
ref_txt.text = self.formatReference(self.bibsource[id])
else:
ref_txt.text = "Missing citation"
return div
class CitationsPreprocessor(Preprocessor):
"""Gather reference definitions and citation keys"""
def __init__(self, bibliography):
super().__init__()
self.bib = bibliography
def subsequentIndents(self, lines, i):
"""Concatenate consecutive indented lines"""
linesOut = []
while i < len(lines):
m = INDENT_RE.match(lines[i])
if m:
linesOut.append(m.group(1))
i += 1
else:
break
return " ".join(linesOut), i
def run(self, lines):
linesOut = []
i = 0
while i < len(lines):
# Check to see if the line starts a reference definition
m = DEF_RE.match(lines[i])
if m:
key = m.group(1)
reference = m.group(2)
indents, i = self.subsequentIndents(lines, i + 1)
reference += " " + indents
self.bib.setReference(key, reference)
continue
# Look for all @citekey patterns inside hard brackets
for bracket in BRACKET_RE.findall(lines[i]):
for c in CITE_RE.findall(bracket):
self.bib.addCitation(c)
linesOut.append(lines[i])
i += 1
return linesOut
class CitationsPattern(Pattern):
"""Handles converting citations keys into links"""
def __init__(self, pattern, bibliography):
super(CitationsPattern, self).__init__(pattern)
self.bib = bibliography
def handleMatch(self, m):
id = m.group(2)
if id in self.bib.citations:
a = etree.Element("a")
a.set("id", self.bib.citationID(id))
a.set("href", "#" + self.bib.referenceID(id))
a.set("class", "citation")
a.text = id
return a
else:
return None
class CitationsTreeprocessor(Treeprocessor):
"""Add a bibliography/reference section to the end of the document"""
def __init__(self, bibliography):
super().__init__()
self.bib = bibliography
def run(self, root):
citations = self.bib.makeBibliography(root)
root.append(citations)
class CitationsExtension(Extension):
def __init__(self, *args, **kwargs):
self.config = {
"bibtex_file": ["", "Bibtex file path"],
"order": ["unsorted", "Order of the references (unsorted, alphabetical)"],
}
super(CitationsExtension, self).__init__(*args, **kwargs)
self.bib = Bibliography(
self,
self.getConfig("bibtex_file"),
self.getConfig("order"),
)
def extendMarkdown(self, md):
md.registerExtension(self)
self.parser = md.parser
self.md = md
md.preprocessors.register(CitationsPreprocessor(self.bib), "mdx_bib", 10)
md.inlinePatterns.register(
CitationsPattern(CITATION_RE, self.bib), "mdx_bib", 20
)
md.treeprocessors.register(CitationsTreeprocessor(self.bib), "mdx_bib", 30)
def makeExtension(*args, **kwargs):
return CitationsExtension(*args, **kwargs)