-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbibtextohtml.py
executable file
·88 lines (65 loc) · 2.9 KB
/
bibtextohtml.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
#!/usr/bin/python3
"""
BibTex-to-HTML
This program is used to fill a template HTML file with information parsed
from a given BibTeX file and then store the result in a specified HTML
output file. These files are all given as command line arguments.
"""
from __future__ import print_function
import argparse
import fileinput
from shutil import copy2
import sys
import bibtexparser
"""We expect three actual arguments but they each have a flag and the first
item in argc is the program name so this totals to 7 expected items."""
EXPECTED_ARG_COUNT = 7
OPEN_BRACKETS = '{{'
CLOSE_BRACKETS = '}}'
def eprint(*args, **kwargs):
"""Prints to stderr"""
print(*args, file=sys.stderr, **kwargs)
def get_arguments():
"""Parses the command line arguments."""
program_description = ("Fills HTML template with information parsed from "
"a BibTeX file and stores the output in another HTML file.")
parser = argparse.ArgumentParser(description=program_description)
parser.add_argument('-t', metavar='template', help='template HTML file', required=True)
parser.add_argument('-i', metavar='inputFile', help='input BibTeX file', required=True)
parser.add_argument('-o', metavar='outputFile', help='output HTML file', required=True)
if len(sys.argv) != EXPECTED_ARG_COUNT:
# Unrecoverable error if user does not supply all arguments
parser.print_help(sys.stderr)
sys.exit(1)
# Get dictionary of arguments
args = vars(parser.parse_args())
return args
def fill_using_template(input_filename, output_filename):
"""Parses data from input BibTeX file and uses find replace to fill those
fields in the output file."""
with open(input_filename) as bibtex_file:
bib_database = bibtexparser.load(bibtex_file)
bib_dict = bib_database.entries[0]
with fileinput.FileInput(output_filename, inplace=True) as file:
for line in file:
last_found = 0
open_bracket_location = line.find(OPEN_BRACKETS, last_found)
close_bracket_location = line.find(CLOSE_BRACKETS, last_found)
while open_bracket_location != -1:
key = line[(open_bracket_location + 2):close_bracket_location]
if key in bib_dict:
line = line.replace(('{{' + key + '}}'), bib_dict[key])
else:
eprint(key + " not found in input file.")
last_found = close_bracket_location + 2
open_bracket_location = line.find(OPEN_BRACKETS, last_found)
close_bracket_location = line.find(CLOSE_BRACKETS, last_found)
print(line, end='')
def main():
"""Open files and perform template filling."""
args = get_arguments()
# To start just copy everything from template into output
copy2(args["t"], args["o"])
fill_using_template(args["i"], args["o"])
if __name__ == "__main__":
main()