1 | import os, re, urllib |
---|
2 | |
---|
3 | ## ----------------------------------------------------------------- |
---|
4 | ## Purpose: make a nice publication page with an ADS database link |
---|
5 | ## Author: Aymeric Spiga 19/05/2013 improvements 10-12/2013 |
---|
6 | ## ----------------------------------------------------------------- |
---|
7 | ## NB: uses BIBTEX2HTML https://www.lri.fr/~filliatr/bibtex2html/doc/manual.html |
---|
8 | ## ... and of course NASA ADS http://adsabs.harvard.edu/ |
---|
9 | ## ----------------------------------------------------------------- |
---|
10 | |
---|
11 | def makepage(authorref, |
---|
12 | bibstyle = "-s custom -nokeys", |
---|
13 | listyear = [0], |
---|
14 | customcond = None, |
---|
15 | embedded = False, |
---|
16 | linkads = None, |
---|
17 | title = None, |
---|
18 | retrieve = True, |
---|
19 | addpdf = None, |
---|
20 | addlink = None, |
---|
21 | target=None): |
---|
22 | |
---|
23 | htmlcontent = "" |
---|
24 | |
---|
25 | ### HEADER |
---|
26 | if embedded: |
---|
27 | htmlfile = open('header.html','r') |
---|
28 | htmlcontent = htmlfile.read() |
---|
29 | htmlfile.close() |
---|
30 | #else: |
---|
31 | if title is None: |
---|
32 | htmlcontent = htmlcontent + "<h2>"+authorref+"'s publications</h2>" |
---|
33 | elif title == "": |
---|
34 | pass |
---|
35 | else: |
---|
36 | htmlcontent = htmlcontent + title |
---|
37 | |
---|
38 | ### if linkads is None, we set it to "link.authorref" |
---|
39 | if linkads is None: |
---|
40 | linkads = authorref+'.link' |
---|
41 | |
---|
42 | ### GET INFO FROM ADS |
---|
43 | if retrieve: |
---|
44 | print "retrieving info from ADS" |
---|
45 | linkfile = open(linkads,'r') |
---|
46 | url = linkfile.read() |
---|
47 | linkfile.close() |
---|
48 | html = urllib.urlopen(url).read() |
---|
49 | bibfile = open(linkads+'.bib','w') |
---|
50 | print >> bibfile,html |
---|
51 | bibfile.close() |
---|
52 | |
---|
53 | ### if only one year and no customcond, make it useful. ask for years >= this value |
---|
54 | if len(listyear) == 1 and customcond is None: |
---|
55 | customcond = "-c 'year>=%s'" % (listyear[0]) |
---|
56 | listyear[0] = 99 |
---|
57 | |
---|
58 | ### ADD LINK WITH YEARS IN HEADER |
---|
59 | if customcond is None or len(listyear) > 1: |
---|
60 | htmlcontent += "Year: " |
---|
61 | for year in listyear: |
---|
62 | htmlcontent += "<a href='#"+str(year)+"'>"+str(year)+"</a>. " |
---|
63 | if addlink is not None: htmlcontent += "<br>"+addlink |
---|
64 | |
---|
65 | ### YEAR LOOP |
---|
66 | for year in listyear: |
---|
67 | |
---|
68 | author = authorref+str(year) |
---|
69 | print author |
---|
70 | |
---|
71 | # 0. define condition |
---|
72 | # if not user-defined, make it simply year in each listyear instance |
---|
73 | # if user-defined, then customcond will be the condition (possibly several) |
---|
74 | if customcond is None and len(listyear) > 1: cond = "-c 'year=%s'" % (year) |
---|
75 | elif len(listyear) > 1: cond = customcond + " -c 'year=%s'" % (year) |
---|
76 | else: cond = customcond |
---|
77 | |
---|
78 | # 1. select items ARTICLE in the big bib file |
---|
79 | # put those in a dedicated author.bib file |
---|
80 | arg = \ |
---|
81 | cond,\ |
---|
82 | '"ARTICLE"',\ |
---|
83 | author+'.txt',\ |
---|
84 | author+'.bib',\ |
---|
85 | linkads+'.bib' |
---|
86 | cmd = "bib2bib --quiet %s -c '$type=%s' -oc %s -ob %s %s" % (arg) |
---|
87 | os.system(cmd) |
---|
88 | |
---|
89 | # modify the bib file to insert pdf links |
---|
90 | # the trick is to use the line adsurl and expect pdf to have the same name as ADS reference |
---|
91 | # ... then besides this, it is necessary to link pdfs or rename those |
---|
92 | # ... the online repository is indicated by addpdf |
---|
93 | if retrieve: |
---|
94 | if addpdf is not None: |
---|
95 | bibcontent = '' |
---|
96 | for line in open(linkads+'.bib'): |
---|
97 | bibcontent += line |
---|
98 | if 'adsurl' in line: |
---|
99 | line = line.replace('adsurl','localpdf') |
---|
100 | line = line.replace('http://cdsads.u-strasbg.fr/abs/',addpdf) |
---|
101 | line = line.replace('},','.pdf},') |
---|
102 | line = line.replace('%','_') |
---|
103 | bibcontent += line |
---|
104 | bibfile2 = open('temp','w') |
---|
105 | print >> bibfile2,bibcontent |
---|
106 | bibfile2.close() |
---|
107 | os.system('mv temp '+linkads+'.bib') |
---|
108 | |
---|
109 | # 2. make the html page from the author.bib file |
---|
110 | if customcond is None or len(listyear) > 1: |
---|
111 | header = '<a name="%.0f"></a>' % (year) |
---|
112 | header += "<h3>%.0f <a href=''>.</a> </h3>" % (year) |
---|
113 | if embedded: header += '<br>' |
---|
114 | else: |
---|
115 | header = '' |
---|
116 | |
---|
117 | header = '"'+header+'"' |
---|
118 | arg = \ |
---|
119 | bibstyle,\ |
---|
120 | header,\ |
---|
121 | author+'.bib' |
---|
122 | cmd = "bibtex2html -q \ |
---|
123 | --both \ |
---|
124 | -m ads.tex \ |
---|
125 | %s \ |
---|
126 | -nf adsurl 'ADS link' \ |
---|
127 | -nf localpdf 'PDF version' \ |
---|
128 | -r -d --revkeys \ |
---|
129 | -nofooter --nodoc \ |
---|
130 | --header %s -nokeywords \ |
---|
131 | %s" % (arg) |
---|
132 | os.system(cmd) |
---|
133 | |
---|
134 | # 3. load page content and delete intermediate HTML file |
---|
135 | htmlfile = open(author+'.html','r') |
---|
136 | htmlcontent = htmlcontent + htmlfile.read() |
---|
137 | htmlfile.close() |
---|
138 | os.system("rm -rf "+author+'.html') |
---|
139 | |
---|
140 | ## make a few corrections |
---|
141 | ## bibcontent = open(author+'.bib','r').read() |
---|
142 | ## bibcontent.replace('\grl','Yeah') |
---|
143 | |
---|
144 | find = re.compile(r'bib') |
---|
145 | htmlcontent = find.sub('Bibtex entry',htmlcontent) |
---|
146 | find = re.compile(r'Bibtex entry.html') |
---|
147 | htmlcontent = find.sub('bib.html',htmlcontent) |
---|
148 | |
---|
149 | find = re.compile(r'DOI') |
---|
150 | htmlcontent = find.sub('Journal website',htmlcontent) |
---|
151 | |
---|
152 | #find = re.compile(r'.pdf') |
---|
153 | #htmlcontent = find.sub('PDF version',htmlcontent) |
---|
154 | |
---|
155 | find = re.compile(r'<table>') |
---|
156 | htmlcontent = find.sub('<table border="0" cellspacing="15">',htmlcontent) |
---|
157 | find = re.compile(r'<td align="right">') |
---|
158 | htmlcontent = find.sub('<td align="center" width=17% style="font-size: 75%;">',htmlcontent) |
---|
159 | |
---|
160 | htmlcontent += '''<hr><p>Generated with |
---|
161 | <a href='https://www.lri.fr/~filliatr/bibtex2html/doc/manual.html'>BibTeX2HTML</a> |
---|
162 | and <a href='http://adsabs.harvard.edu/'>NASA ADS</a> |
---|
163 | and a bit of <a href='http://www.python.org/'>Python</a></p>''' |
---|
164 | if embedded: |
---|
165 | htmlfile = open('footer.html','r') |
---|
166 | htmlcontent += htmlfile.read() |
---|
167 | htmlfile.close() |
---|
168 | |
---|
169 | htmlmain = open(authorref+'.html','w') |
---|
170 | print >> htmlmain, htmlcontent |
---|
171 | htmlmain.close() |
---|
172 | |
---|
173 | ## move results to target directory and remove txt files |
---|
174 | if target is not None: |
---|
175 | target=target+"/" |
---|
176 | arg = target,\ |
---|
177 | authorref+"*.html",\ |
---|
178 | authorref+"*.bib",\ |
---|
179 | authorref+"*.txt",\ |
---|
180 | target,\ |
---|
181 | target+linkads+".bib",\ |
---|
182 | "*.css",\ |
---|
183 | target |
---|
184 | os.system( "mkdir -p %s ; mv %s %s %s %s ; mv %s ./ 2> /dev/null ; cp %s %s 2> /dev/null" % (arg) ) |
---|