-
-
Notifications
You must be signed in to change notification settings - Fork 32
/
pubparse.py
1191 lines (1015 loc) · 39.8 KB
/
pubparse.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python2
###########################################################################
# Copyright (c) 2009--2014 Minh Van Nguyen <mvngu.name@gmail.com>
# Harald Schilly <harald.schilly@gmail.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# http://www.gnu.org/licenses/
###########################################################################
# This script requires Pybtex for parsing a BibTeX database. See
# https://launchpad.net/pybtex for more information about Pybtex.
#
# The general database of publications that cite Sage is contained in the
# text file named by the variable publications_general. The file referred to
# by this variable is a BibTeX database. Each publication entry is described
# in BibTeX format. If you want to add or delete items from the publications
# database, you should edit the file named by the variable
# publications_general. Make sure that your edit follows the formatting rules
# of BibTeX. Once you are done editing the file named by
# publications_general, then run this script which will generate an HTML
# page listing the publications. The HTML page listing the publications has a
# link to the BibTeX database, i.e. the file referred to by the variable
# publications_database.
# importing modules from Python library
import copy
import re
import os
import sys
from pprint import pprint
import six
unicode = six.u
# importing modules from third-party library
try:
import pybtex
except:
print("you have to install pybtex")
print("$ pip install -M -U --user pybtex")
sys.exit(1)
from pybtex.database.input import bibtex
from pybtex.style.names.plain import NameStyle
plain = NameStyle().format
# script has to run from the location where it is
os.chdir(os.path.dirname(os.path.abspath(sys.argv[0])))
# get the current working directory
PWD = os.getcwd()
# the file containing the general publications database
publications_general = os.path.join(PWD, "bibliography-sage.bib")
# the file containing the Sage-Combinat publications database
publications_combinat = os.path.join(PWD, "Sage-Combinat.bib")
# the file containing the general bibliography formatted in HTML
html_general = os.path.join(PWD, "publications-general.html")
# the file containing the Sage-Combinat bibliography formatted in HTML
html_combinat = os.path.join(PWD, "publications-combinat.html")
# upstream version of the BibTeX database of Sage-Combinat
#bibtex_sage_combinat = "http://combinat.sagemath.org/hgwebdir.cgi/misc/raw-file/tip/articles/Sage-Combinat.bib"
# the file containing the MuPAD-Combinat BibTeX database
publications_mupad = os.path.join(PWD, "MuPAD-Combinat.bib")
# the file containing the MuPAD publications list formatted in HTML
html_mupad = os.path.join(PWD, "publications-mupad.html")
# MathSciNet
publications_mathscinet = os.path.join(PWD, 'mathscinet.bib')
html_mathscinet = os.path.join(PWD, 'publications-mathscinet.html')
# Stuff relating to file permissions.
# whether we should change the permissions of a file
CHANGE_PERMISSIONS = True
# the permissions to enforce
PERMISSIONS = "755"
# Attributes associated with each type of publication. Attribute names
# are the same as in BibTeX. In all of the publication types below, we use
# the attribute "note" to specify a valid URL where the named publication
# could be downloaded. A special exception is the case of the publication
# type "misc". This type is used for specifying both a preprint and an
# undergraduate thesis. When "misc" is used for specifying an
# undergraduate thesis, the attribute "note" contains both a valid URL and
# the word "thesis".
#
# The attributes that describe an article should be listed in this order in
# the publications database.
#
# article
# author
# title
# journal
# volume
# number
# pages
# year
# note
#
# The attributes that describe a book should be listed in this order in
# the publications database.
#
# book
# author
# title
# edition
# publisher
# year
# note
#
# The attributes that describe a work in a collection should be listed in
# this order in the publications database.
#
# incollection
# author
# title
# editor
# booktitle
# pages
# publisher
# year
# note
#
# The attributes that describe a proceedings paper should be listed in
# this order in the publications database.
#
# inproceedings
# author
# title
# editor
# booktitle
# publisher
# series
# volume
# pages
# year
# note
#
# The attributes that describe a Master's thesis should be listed in
# this order in the publications database.
#
# mastersthesis
# author
# title
# school
# address
# year
# note
#
# The attributes that describe a miscellaneous item should be listed in
# this order in the publications database. This publication type is used for
# specifying both a preprint and an undergraduate thesis. When "misc" is
# used for specifying an undergraduate thesis, the attribute "note" contains
# both a valid URL and the word "thesis".
#
# misc
# author
# title
# howpublished
# year
# note
#
# The attributes that describe a PhD thesis should be listed in
# this order in the publications database.
#
# phdthesis
# author
# title
# school
# address
# year
# note
#
# The attributes that describe a technical report should be listed in
# this order in the publications database.
#
# techreport
# author
# title
# number
# institution
# address
# year
# note
#
# The attributes that describe an unpublished work should be listed in
# this order in the publications database.
#
# unpublished
# author
# title
# month
# year
# note
##############################
# helper functions
##############################
def extract_publication(entry_dict):
r"""
Extract a publication entry from the given dictionary.
INPUT:
- entry_dict -- a dictionary containing a publication entry that was
parsed from a BibTeX database using Pybtex.
OUTPUT:
A dictionary representing a publication entry, where the keys are
BibTeX attributes for a publication type and the values corresponding to
the keys are attribute values. An article is represented using this
dictionary:
{'author': <authors-names>,
'title': <article-title>,
'journal': <journal-name>,
'volume': <volume-number>,
'number': <issue-number>,
'pages': <article-page-span>,
'year': <publication-year>,
'note': <url>}
A book is represented using this dictionary:
{'author': <authors-names>,
'title': <book-title>,
'edition': <book-edition>,
'publisher': <publisher-name>,
'year': <publication-year>,
'note': <url>}
A work in a collection is represented using this dictionary:
{'author': <authors-names>,
'title': <work-title>,
'editor': <collection-editor>,
'booktitle': <collection-title>,
'pages': <work-page-span>,
'publisher': <publisher-name>,
'year': <publication-year>,
'note': <url>}
A proceedings paper is represented using this dictionary:
{'author': <authors-names>,
'title': <article-title>,
'editor': <proceedings-editor>,
'booktitle': <proceedings-title>,
'publisher': <publisher-name>,
'series': <series-name>,
'volume': <volume-number>,
'pages': <article-page-span>,
'year': <publication-year>,
'note': <url>}
A Master's thesis is represented using this dictionary:
{'author': <authors-names>,
'title': <thesis-title>,
'school': <school-department-name>,
'address': <institution-address>,
'year': <completion-year>,
'note': <url>}
A miscellaneous item is represented using this dictionary:
{'author': <authors-names>,
'title': <item-title>,
'howpublished': <how-where-published>,
'year': <publication-year>,
'note': <url>}
A PhD thesis is represented using this dictionary:
{'author': <authors-names>,
'title': <thesis-title>,
'school': <school-department-name>,
'address': <institution-address>,
'year': <completion-year>,
'note': <url>}
A technical report is represented using this dictionary:
{'author': <authors-name>,
'title': <report-title>,
'number': <report-number>,
'institution': <institution-name>,
'address': <institution-address>,
'year': <publication-year>,
'note': <url>}
An unpublished manuscript is represented using this dictionary:
{'author': <authors-names>,
'title': <manuscript-title>,
'month': <day-month>,
'year': <publication-year>,
'note': <url>}
"""
publication_dict = {}
for attribute in entry_dict.fields.keys():
publication_dict.setdefault(
str(attribute).strip().lower(),
unicode(entry_dict.fields[attribute]).strip())
# The author field is a required field in BibTeX format.
# Extract author names.
authors_str = ""
authors_list = entry_dict.persons["author"]
authors_str = str(unicode(plain(authors_list[0]).format()))
if len(authors_list) > 1:
for author in authors_list[1:]:
authors_str = u"".join(
[authors_str, " and ",
str(unicode(plain(author).format()))])
authors_str = authors_str.replace("<nbsp>", " ")
publication_dict.setdefault("author", authors_str)
# The editor field is an optional field in BibTeX format.
# Extract editor names.
if "editor" in entry_dict.persons:
editors_str = ""
editors_list = entry_dict.persons["editor"]
editors_str = str(unicode(plain(editors_list[0]).format()))
if len(editors_list) > 1:
for editor in editors_list[1:]:
editors_str = u"".join([
editors_str, " and ",
str(unicode(plain(editor).format()))
])
editors_str = editors_str.replace("<nbsp>", " ")
publication_dict.setdefault("editor", editors_str)
return publication_dict
def filter_undergraduate_theses(publications):
r"""
Filter out the preprints from the undergraduate theses in the given
list of miscellaneous publications.
INPUT:
- publications -- a list of dictionaries of miscellaneous publications.
These publications include preprints and undergraduate theses.
OUTPUT:
Separate the preprints from the undergraduate theses. The publication
type 'misc' is used for specifying both a preprint and an undergraduate
thesis. When 'misc' is used for specifying an undergraduate thesis, the
attribute 'note' contains both a valid URL and the word 'thesis'.
"""
preprints = []
undergraduate_theses = []
for item in publications:
if ("note" in item) and ("thesis" in item["note"]):
undergraduate_theses.append(item)
else:
preprints.append(item)
return {
"preprints": preprints,
"undergraduatetheses": undergraduate_theses
}
def format_articles(articles):
r"""
Format each article in HTML format.
INPUT:
- articles -- a list of dictionaries of articles. All articles are
assumed to be published, i.e. the list of articles considered does not
contain any preprints. Use the function format_preprints() to format a
list of preprints. Each article is required to have the following
mandatory attributes: author, title, journal, and year. Optional
attributes include: volume, number, pages, and note.
OUTPUT:
A list of articles all of which are formatted in HTML suitable for
displaying on websites.
"""
formatted_articles = []
optional_attributes = ["volume", "number", "pages"]
for article in articles:
try:
htmlstr = "".join([format_names(article["author"]), ". "])
htmlstr = "".join([htmlstr, html_title(article)])
aj = article.get("journal", article.get('journaltitle'))
htmlstr = "".join([htmlstr, aj, ", "])
for attribute in optional_attributes:
if attribute in article:
htmlstr = "".join(
[htmlstr, attribute, " ", article[attribute], ", "])
ay = article.get("year", article.get('date'))
htmlstr = "".join([htmlstr, ay, "."])
formatted_articles.append(htmlstr.strip())
except Exception as ex:
from pprint import pprint
pprint(article)
raise ex
return list(map(replace_special, formatted_articles))
def format_books(books):
r"""
Format each book in HTML format.
INPUT:
- books -- a list of dictionaries of books. Each book must be published.
Use the function format_unpublished() to format books that are
unpublished. Each book is required to have the following mandatory
attributes: author, title, publisher, and year. Optional attributes
include: edition and note.
OUTPUT:
A list of books all of which are formatted in HTML suitable for
displaying on websites.
"""
formatted_books = []
for book in books:
try:
htmlstr = "".join([format_names(book["author"]), ". "])
htmlstr = "".join([htmlstr, html_title(book)])
if "edition" in book:
htmlstr = "".join([htmlstr, book["edition"], " edition, "])
by = book.get("year", book.get("date"))
htmlstr = "".join([htmlstr, book["publisher"], ", ", by, "."])
formatted_books.append(htmlstr.strip())
except Exception as ex:
pprint(book)
raise ex
return list(map(replace_special, formatted_books))
def format_collections(collections):
r"""
Format each entry in a collection in HTML format.
INPUT:
- collections -- a list of dictionaries of collection entries. This
usually means a book chapter in a book. The mandatory attributes of
an entry in a collection are: author, title, booktitle, and year.
Some optional attributes include: editor, pages, publisher, and note.
Each entry in the collection must be published work.
OUTPUT:
A list of collection entries all of which are formatted in HTML
suitable for displaying on websites.
"""
formatted_entries = []
for entry in collections:
htmlstr = "".join([format_names(entry["author"]), ". "])
htmlstr = "".join([htmlstr, html_title(entry)])
if "editor" in entry:
htmlstr = "".join(
[htmlstr, "In ",
format_names(entry["editor"]), " (ed.). "])
htmlstr = "".join([htmlstr, entry["booktitle"], ". "])
if "publisher" in entry:
htmlstr = "".join([htmlstr, entry["publisher"], ", "])
if "pages" in entry:
htmlstr = "".join([htmlstr, "pages ", entry["pages"], ", "])
ay = entry.get("year", entry.get('date'))
htmlstr = "".join([htmlstr, ay, "."])
formatted_entries.append(htmlstr.strip())
return list(map(replace_special, formatted_entries))
def format_masterstheses(masterstheses):
r"""
Format each Master's thesis in HTML format.
INPUT:
- masterstheses -- a list of dictionaries of Master's theses. Each
Master's thesis has the following mandatory attributes: author, title,
school, and year. Some optional attributes include: address and note.
OUTPUT:
A list of Master's theses all of which are formatted in HTML
suitable for displaying on websites.
"""
formatted_theses = []
for thesis in masterstheses:
htmlstr = "".join([format_names(thesis["author"]), ". "])
htmlstr = "".join([htmlstr, html_title(thesis)])
htmlstr = "".join([htmlstr, "Masters thesis, "])
htmlstr = "".join([htmlstr, thesis["school"], ", "])
if "address" in thesis:
htmlstr = "".join([htmlstr, thesis["address"], ", "])
htmlstr = "".join([htmlstr, thesis["year"], "."])
formatted_theses.append(htmlstr.strip())
return list(map(replace_special, formatted_theses))
def format_miscs(miscs, thesis=False):
r"""
Format each miscellaneous entry in HTML format. Here, a miscellaneous
entry is usually a preprint.
INPUT:
- miscs -- a list of dictionaries of miscellaneous entries. There are no
mandatory attributes. The supported optional attributes are: author,
title, howpublished, year, and note. However, it is reasonable to
expect that at least the following attributes are given specific
values: author, title, and year.
- thesis -- (default: False) True if miscs only contains undergraduate
theses; False otherwise. If False, then miscs is assumed to only
contain preprints.
OUTPUT:
A list of miscellaneous entries all of which are formatted in HTML
suitable for displaying on websites.
"""
formatted_miscs = []
for entry in miscs:
try:
htmlstr = "".join([format_names(entry["author"]), ". "])
htmlstr = "".join([htmlstr, html_title(entry)])
if "howpublished" in entry:
htmlstr = "".join([htmlstr, entry["howpublished"], ", "])
if thesis:
note = entry["note"]
# handle the case: note = {<url> Bachelor thesis},
if "http://" in note:
note = note[note.find(" "):].strip()
htmlstr = "".join([htmlstr, note, ", "])
y = entry.get("year", entry.get('date'))
htmlstr = "".join([htmlstr, y, "."])
formatted_miscs.append(htmlstr.strip())
except Exception as ex:
pprint(entry)
raise ex
return list(map(replace_special, formatted_miscs))
def format_names(names):
r"""
Format the given list of author names so that it's suitable for display
on web pages.
INPUT:
- names -- a list of names.
OUTPUT:
The same list of author names, but formatted for display on web pages.
"""
formatted_names = [name.strip() for name in names.split(" and ")]
if len(formatted_names) == 1:
return formatted_names[0]
elif len(formatted_names) == 2:
formatted_names.insert(1, " and ")
return "".join(formatted_names)
# the string of author names contains more than 2 names
else:
formatted_names.insert(-1, "and ")
for i in range(len(formatted_names) - 2):
formatted_names[i] = "".join([formatted_names[i], ", "])
return "".join(formatted_names)
def format_phdtheses(phdtheses):
r"""
Format each PhD thesis in HTML format.
INPUT:
- phdtheses -- a list of dictionaries of PhD theses. The mandatory
attributes of a PhD thesis are: author, title, school, and year.
Some optional attributes include: address and note.
OUTPUT:
A list of PhD theses all of which are formatted in HTML
suitable for displaying on websites.
"""
formatted_theses = []
for thesis in phdtheses:
try:
htmlstr = "".join([format_names(thesis["author"]), ". "])
htmlstr = "".join([htmlstr, html_title(thesis)])
htmlstr = "".join([htmlstr, "PhD thesis, "])
ts = thesis.get('school', thesis.get('institution'))
htmlstr = "".join([htmlstr, ts, ", "])
if "address" in thesis:
htmlstr = "".join([htmlstr, thesis["address"], ", "])
ty = thesis.get('year', thesis.get('date'))
htmlstr = "".join([htmlstr, ty, "."])
formatted_theses.append(htmlstr.strip())
except Exception as ex:
pprint(thesis)
raise ex
return list(map(replace_special, formatted_theses))
def format_techreports(techreports):
r"""
Format each technical report in HTML format.
INPUT:
- techreports -- a list of dictionaries of technical reports. The
mandatory attributes of a technical report are: author, title,
institution, and year. Some optional attributes include: number,
address, and note.
OUTPUT:
A list of technical reports all of which are formatted in HTML
suitable for displaying on websites.
"""
formatted_reports = []
for report in techreports:
htmlstr = "".join([format_names(report["author"]), ". "])
htmlstr = "".join([htmlstr, html_title(report)])
htmlstr = "".join([htmlstr, report["institution"], ", "])
if "address" in report:
htmlstr = "".join([htmlstr, report["address"], ", "])
if "number" in report:
htmlstr = "".join(
[htmlstr, "technical report number ", report["number"], ", "])
y = report.get("year", report.get('date'))
htmlstr = "".join([htmlstr, y, "."])
formatted_reports.append(htmlstr.strip())
return list(map(replace_special, formatted_reports))
def format_proceedings(proceedings):
r"""
Format each proceedings article in HTML format.
INPUT:
- proceedings -- a list of dictionaries of proceedings articles. The
mandatory attributes are: author, title, booktitle, and year. Some
optional attributes include: editor, publisher, series, volume, pages,
and note.
OUTPUT:
A list of proceedings articles all of which are formatted in HTML
suitable for displaying on websites.
"""
formatted_proceedings = []
for article in proceedings:
htmlstr = "".join([format_names(article["author"]), ". "])
htmlstr = "".join([htmlstr, html_title(article)])
if "editor" in article:
htmlstr = "".join(
[htmlstr, "In ",
format_names(article["editor"]), " (ed.). "])
htmlstr = "".join([htmlstr, article["booktitle"], ". "])
if "publisher" in article:
htmlstr = "".join([htmlstr, article["publisher"], ", "])
if "series" in article:
htmlstr = "".join([htmlstr, article["series"], ", "])
if "volume" in article:
htmlstr = "".join([htmlstr, "volume ", article["volume"], ", "])
if "pages" in article:
if htmlstr.strip()[-1] == ".":
htmlstr = "".join([htmlstr, "Pages ", article["pages"], ", "])
else:
htmlstr = "".join([htmlstr, "pages ", article["pages"], ", "])
ay = article.get("year", article.get('date'))
htmlstr = "".join([htmlstr, ay, "."])
formatted_proceedings.append(htmlstr.strip())
return list(map(replace_special, formatted_proceedings))
def format_unpublisheds(unpublisheds):
r"""
Format each unpublished entry in HTML format.
INPUT:
- unpublisheds -- a list of dictionaries of unpublished entries. An
unpublished entry is required to have the following mandatory
attributes: author, title, and year. Optional attributes include:
month and note.
OUTPUT:
A list of unpublished entries all of which are formatted in HTML
suitable for displaying on websites.
"""
formatted_entries = []
for entry in unpublisheds:
try:
htmlstr = "".join([format_names(entry["author"]), ". "])
htmlstr = "".join([htmlstr, html_title(entry)])
if "month" in entry:
htmlstr = "".join([htmlstr, entry["month"], ", "])
y = entry.get('year', entry.get('date'))
htmlstr = "".join([htmlstr, y, "."])
formatted_entries.append(htmlstr.strip())
except Exception as ex:
pprint(entry)
raise ex
return list(map(replace_special, formatted_entries))
def html_title(publication):
r"""
Format the title of the given publication as an HTML hyperlink. This
depends on whether a URL is specified as part of the attributes of the
publication.
INPUT:
- publication -- a publication entry. This can be an article, book, thesis
and so on.
OUTPUT:
If possible, format the title of the given publication as a hyperlink.
Here, it is assumed that the BibTeX attribute 'note' (if present)
contains a valid URL.
"""
url = ""
if "note" in publication:
url = publication["note"].split()[0]
url = replace_special_url(url)
# override note url's with url url's (if they exist)
if "url" in publication:
url = publication["url"].split()[0]
url = replace_special_url(url)
title = publication["title"]
if url != "":
if ("http://" in url) or ("https://" in url):
return "".join(["<a href=\"", url, "\">", title, "</a>", ". "])
# handle the case where no URL is provided or the "note" field doesn't
# contain a URL
return "".join([title, ". "])
def output_html(publications, filename):
r"""
Format each publication entry in HTML format, and output the resulting
HTML formatted entries to a template file.
This HTML template contains macros for the entries, which are
included via the templating system into the actual page.
INPUT:
- publications -- a dictionary of publication entries. The following
types of publications are supported: article, book, incollection,
inproceedings, mastersthesis, misc, phdthesis, and unpublished.
- filename -- the name of the file to write to.
OUTPUT:
Output the HTML formatted publication entries to the file named by
filename. We are overwriting the content of this file.
"""
# format the various lists of publications in HTML format
articles = format_articles(publications["articles"])
collections = format_collections(publications["incollections"])
proceedings = format_proceedings(publications["inproceedings"])
masterstheses = format_masterstheses(publications["masterstheses"])
phdtheses = format_phdtheses(publications["phdtheses"])
books = format_books(publications["books"])
unpublisheds = format_unpublisheds(publications["unpublisheds"])
miscs = filter_undergraduate_theses(publications["miscs"])
preprints = format_miscs(miscs["preprints"])
techreports = format_techreports(publications["techreports"])
undergradtheses = format_miscs(miscs["undergraduatetheses"], thesis=True)
htmlcontent = "{# DON'T EDIT! File has been autogenerated by pubparse.py #}\n"
def macro(name, sorted_index, papers):
ret = "\n"
ret += "{%% macro %s() %%}\n" % name
ret += "<ol>\n"
for index in sorted_index:
ret += " <li>%s</li>\n" % papers[index]
ret += "</ol>\n"
ret += "{% endmacro %}\n\n"
return ret
# Sort the publication items. Journal articles, items in collections,
# and proceedings papers are grouped in one section. Sort these.
papers = articles + collections + proceedings + techreports
sorted_index = sort_publications(publications["articles"] +
publications["incollections"] +
publications["inproceedings"] +
publications["techreports"])
# insert the new list of articles
htmlcontent += macro("papers", sorted_index, papers)
# Sort the list of theses. These include PhD, Master's, and undergraduate
# theses.
theses = masterstheses + phdtheses + undergradtheses
sorted_index = sort_publications(publications["masterstheses"] +
publications["phdtheses"] +
miscs["undergraduatetheses"])
# insert the new list of theses
htmlcontent += macro("thesis", sorted_index, theses)
# Sort the list of books. These include both published books and
# unpublished manuscripts.
books_list = books + unpublisheds
sorted_index = sort_publications(publications["books"] +
publications["unpublisheds"])
htmlcontent += macro("books", sorted_index, books_list)
# Sort the list of preprints.
sorted_index = sort_publications(miscs["preprints"])
htmlcontent += macro("preprints", sorted_index, preprints)
# Replace the current publications page.
with open(filename, "wb") as outfile:
outfile.write(replace_maths(htmlcontent).encode("utf-8"))
outfile.write(b"\n")
if CHANGE_PERMISSIONS:
os.system("".join(["chmod ", PERMISSIONS, " ", filename]))
def process_database(dbfilename):
r"""
Process the publications database.
INPUT:
- dbfilename -- the name of the publications database file to process.
This is a BibTeX database.
OUTPUT:
A 9-key dictionary of processed publication entries. The number nine
corresponds to the number of publication entries considered in this
script. If other types of publication are added to the database besides
the type already listed above, then the new publication type should be
specified in the block at the beginning of this script. The 9-key
dictionary output by this function is of the form
{'articles': articles,
'books': books,
'incollections': incollections,
'inproceedings': inproceedings,
'masterstheses': masterstheses,
'miscs': miscs,
'phdtheses': phdtheses,
'techreports': techreports,
'unpublisheds': unpublisheds}
where each value (corresponding to a key) in the dictionary is a list of
processed publication entries. Each list is a dictionary of publications
of the same type. For example, the dictionary value 'article' is a list
of dictionaries of articles. Similarly, the dictionary value 'book' is a
list of dictionaries of books.
"""
# Lists of dictionaries of publication entries. Each list contains
# several dictionaries of publication entries of the same type.
article = []
book = []
incollection = []
inproceedings = []
mastersthesis = []
misc = []
phdthesis = thesis = []
techreport = report = []
unpublished = []
# parse the BibTeX database
parser = bibtex.Parser()
bibdb = parser.parse_file(dbfilename)
for key in bibdb.entries.keys():
pub_type = bibdb.entries[key].type
pub_list = locals()[pub_type]
try:
pub_list.append(extract_publication(bibdb.entries[key]))
except Exception as ex:
#raise ex
import json
print(key)
print(json.dumps(bibdb.entries[key]))
raise ex
return {
"articles": article,
"books": book,
"incollections": incollection,
"inproceedings": inproceedings,
"masterstheses": mastersthesis,
"miscs": misc,
"phdtheses": phdthesis,
"techreports": techreport,
"unpublisheds": unpublished
}
def replace_maths(s):
"""
Replace each special mathematics typesetting in the given string with
italics.
INPUT:
- s -- a string in HTML format.
"""
replace_table = [
("$0$", "0"),
("$_3F_2(1/4)$", "<i>_3F_2(1/4)</i>"),
("$_4$", "<sub>4</sub>"),
("$\~A_2$", "Ã<sub>2</sub>"),
("$f^*$", "f<sup>*</sup>"),
("$q$", "<i>q</i>"),
("$q=0$", "<i>q=0</i>"),
("$D$", "<i>D</i>"),
("$e$", "<i>e</i>"),
("$E_6$", "<i>E_6</i>"),
("$F_4$", "F<sub>4</sub>"),
("$\\Gamma$", "Γ"),
("$\\Gamma_0(9)$", "Γ<sub>0</sub>(9)"),
("$\\Gamma_H(N)$", "Γ<sub>H</sub>(N)"),
("$k$", "<i>k</i>"),
("$K$", "<i>K</i>"),
("$L$", "<i>L</i>"),
("$\\mathbbF_q[t]$", "<i>F_q[t]</i>"),
("$Br(k(\\mathcalC)/k)$", "<i>Br(k(C)/k)</i>"),
("$\\mathcalC$", "<i>C</i>"),
("$\\mathcalJ$", "<i>J</i>"),
("$N$", "<i>N</i>"),
("$\~n$", "ñ"),
("$p$", "<i>p</i>"),
("$PSL_2(\\mathbb Z)$", "<i>PSL_2(Z)</i>"),
("$S_n$", "<i>S_n</i>"),
("$S_N$", "<i>S_N</i>"),
("$U_7$", "<i>U_7</i>"),
("$w$", "<i>w</i>"),
("$Y^2=X^3+c$", "<i>Y^2=X^3+c</i>"),
("$Z_N$", "<i>Z_N</i>"),
(r"$\zeta(s) - c$", "ζ(s) - c"),
(r"$\mathbb Q$", "<i>Q</i>"),
]
cleansed_str = copy.copy(s)
for candidate, target in replace_table:
cleansed_str = cleansed_str.replace(candidate, target)
return cleansed_str
def replace_special(entry):
r"""
Replace each special character from the publication entry with an
equivalent that is suitable for display on web pages. The special
characters we consider usually include escape sequences specific to LaTeX.
INPUT:
- entry -- a dictionary containing attribute/value pairs that describe
a publication entry.
OUTPUT:
A dictionary containing attribute/value pairs that describes the same
publication entry as represented by 'entry'. However, all special
characters are replaced with equivalent characters.
"""
replace_table = [
("$\\frac{1}{2}$ + \\emph{it}", "1/2 + <i>it</i>"),
("\\emph{via}", "<i>via</i>"),
("\\&", "&"), # ampersand
("\\'a", "á"), # a acute
("\\u{a}", "ă"), # a breve
("\\'A", "Á"), # A acute
("\\`a", "à"), # a grave
("\\k{a}", "ą"), # a ogonek (Polish)
('\\"a', "ä"), # a umlaut
("\\'{c}", "ć"), # c acute (Polish)
("\\c{c}", "ç"), # c cedilla
("\\v{c}", "č"), # c czech (Czech)
("\\'e", "é"), # e acute
("\\'E", "É"), # E acute
("\\`e", "è"), # e grave
("\\k{e}", "ę"), # e ogonek (Polish)
('\\"e', "ë"), # e umlaut