-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbibxml2md.py
More file actions
110 lines (87 loc) · 3.09 KB
/
Copy pathbibxml2md.py
File metadata and controls
110 lines (87 loc) · 3.09 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
import xml.etree.ElementTree as ET
import yaml # pyyaml package
import sys
def removeNewline(str):
return str.replace('\n', ' ').replace('\r', '')
def parseXML(infile):
tree = ET.parse(infile)
root = tree.getroot()
# Validate root is a "references" tag
if root.tag != 'references':
sys.exit("Root tag should be 'references'")
# Good to go, initialize output map
references = {}
for item in root.findall('./reference'):
anchor = item.get("anchor")
if anchor == None:
print("Reference has no anchor, skipping")
continue
# initialize output reference
reference = {}
target = item.get("target")
if target:
reference["target"] = target
title = removeNewline(item.find("front/title").text)
if title == "":
print("Warning: anchor '"+anchor+"' has no title")
else:
reference["title"] = title
# Collect authors
authors = []
for author in item.findall('front/author'):
yauthor = {}
fullname = author.get("fullname")
if fullname:
yauthor["name"] = fullname
initials = author.get("initials")
surname = author.get("surname")
if initials and surname:
ins = initials + " " + surname
elif initials:
ins = initials
elif surname:
ins = surname
else:
ins = None
if ins:
yauthor["ins"] = ins
org = author.find("organization")
if org is not None and org.text is not None:
yauthor["org"] = org.text
authors.append(yauthor)
reference["author"] = authors
# The date
date = item.find("front/date")
if date is not None:
month = date.get("month")
year = date.get("year")
if month:
ydate = month + " " + year
elif year:
ydate = year
if month or year:
reference["date"] = ydate
else:
reference["date"] = False # An empty "date" element, this keeps xml2rfc happy
seriesinfoList = item.findall("seriesInfo")
if seriesinfoList != []:
yseriesinfo = {}
for si in seriesinfoList:
name = si.get("name")
value = si.get("value")
yseriesinfo[name] = value
reference["seriesinfo"] = yseriesinfo
references[anchor] = reference
# print(yaml.dump(references))
return references
def main():
if len(sys.argv) != 3:
sys.exit("Usage: " + sys.argv[0] + " infile outfile")
infile = sys.argv[1]
outfile = sys.argv[2]
refs = parseXML(infile)
references = {"references": refs} # Add a top level (this makes it easier to cut and paste)
out = open(outfile, "w")
out.write(yaml.dump(references, width = float("inf"))) # work around pyyaml line break behavior
if __name__ == "__main__":
main()