- #! /usr/bin/env python
- #
- import sys
- import codecs
- import tempfile
- import logging
- logging.basicConfig()
- from zope.publisher.browser import TestRequest
- from mybibtex import BibtexParser
- from bibliograph.rendering.renderers.bibtex import BibtexRenderView
- from bibliograph.rendering.utility import BibtexRenderer
- from zope.publisher.browser import TestRequest
- from zope.component import queryAdapter
- from bibliograph.core.interfaces import IBibliography
- from bibliograph.core.interfaces import IBibliographicReference
- areas = { 'cv':('Computer Vision', 'Images/Vision-100.png'),
- 'ai':('Artificial Intelligence', 'Images/AI-100.png'),
- 'ml':('Machine Learning', 'Images/ML-80.png'),
- 'forensics':('Forensics', 'Images/Forensics-80.png'),
- 'access':('Accessibility', 'Images/Access-80.png'),
- 'cg':('Computer Graphics', 'Images/Graphics-80.png'),
- 'asl':('Sign Language', 'Images/ASL-100.png'),
- 'wavelets':('Wavelets', 'Images/Wavelet-100.png'),
- 'sport': ('Sport', 'Images/Sport-80.png'),
- 'uncert': ('Uncertanties', 'Images/Uncert-100.png'),
- 'virtcrd': ('Virtual Crowds', 'Images/Crowd-80.png'),
- 'med': ('Biomedical', 'Images/Med-64.png'),
- 'biblio': ('Bibliometrics', 'Images/Book-80.png')
- }
- areas_keys = areas.keys()
- #
- # input is the variable with all the text of the bibtex file
- # output is a list of dictionaries
- #
- def parseBibtex( source ):
- # Bibtex parser
- parser = BibtexParser()
- # fix encoding
- #// source = parser.checkEncoding(source)
- # fix latex special characters, among other things
- source = parser.preprocess(source) #.encode('utf-8'))
- # get list of dictionaries
- return parser.getEntries(source)
- def toBibtex (ref,
- valid_fields=[u'title', u'editor', u'author', u'journal', u'booktitle',
- u'publisher', u'school', u'issn', u'volume', u'number',
- u'pages', u'year', u'doi', u'pdf'],
- omit_fields=[]):
- omit = [each.lower() for each in omit_fields]
- fields = [each.lower() for each in valid_fields]
- ttable = [('&', r'\&'),
- ('~', r'\~'),
- ('—',r'---'),
- ('–', r'--'),
- ]
- bib_key = ref['pid']
- ref_type = ref['reference_type'].replace('Reference','')
- bibtex = u'\n@' + ref_type + u'{' + bib_key + u',\n' # '%s{%s,\n" % (ref_type, bib_key)
- ref_keys = ref.keys()
- for k in fields:
- if k in ref_keys and k not in omit:
- if type(ref[k]) == list:
- nv = ref[k][0]
- else:
- nv = ref[k]
- for a,b in ttable:
- nv = nv.replace(a,b)
- bibtex = bibtex + u' '
- bibtex = bibtex + k
- bibtex = bibtex + u' = {'
- # bibtex = bibtex + unicode(nv.encode('utf8'))
- # bibtex = bibtex + unicode(nv, encoding='utf8')
- bibtex = bibtex + unicode(nv, encoding='latin_1')
- bibtex = bibtex + u'},\n'
- # remove trailing command
- bibtex = bibtex[0:-2] + u"}\n"
- return bibtex
- def toHTML (ref,
- numb=0,
- valid_fields=[u'title', u'editor', u'author', u'journal', u'booktitle',
- u'publisher', u'school', u'issn', u'volume', u'number',
- u'pages', u'year', u'doi', u'pdf', u'abstract'],
- omit_fields=[]):
- omit = [each.lower() for each in omit_fields]
- fields = [each.lower() for each in valid_fields]
- ref_type = ref['reference_type'].replace('Reference','').lower()
- bib_key = ref['pid']
- bid = 'b' + bib_key
- ref_keys = ref.keys()
- html = u'<table width="100%" class="pub">\n'
- html += u'<col align="center" width=230px />\n'
- html += u'<col align="left" />\n'
- html += u'<col align="left" />\n'
- html += u'<tr>\n'
- html += u'<td class="keywords" align="center">\n'
- # html += u'<td class="keywords" align="center" padding="20px">\n'
- if 'keywords' in ref_keys:
- for k in ref['keywords']:
- lk = k.lower()
- if lk in areas_keys:
- html += u'<img class="keyw" src="%s" alt="%s" title="%s" align="middle" />\n' % (areas[lk][1], areas[lk][0], areas[lk][0])
- else:
- html += u'No keywords!\n'
- html += u'</td>\n'
- html += u'<td><div class="%s" name="%s" ><a name="%s">\n' % (ref_type, bib_key, bib_key)
- # if numb>0:
- # html += u'%d. \n' % numb
- for k in fields:
- if k in ref_keys and k not in omit:
- if type(ref[k]) == list:
- v = ref[k][0]
- else:
- v = ref[k]
- if numb>0 and k=='title':
- html += u'<span class="%s">%d. %s </span>\n' %(k, numb, unicode(v, encoding='utf8'))
- elif k=='issn':
- html += u'<span class="%s">ISSN: %s, </span>\n' %(k, unicode(v, encoding='utf8'))
- elif k=='volume':
- html += u'<span class="%s">Vol. %s, </span>\n' %(k, unicode(v, encoding='utf8'))
- elif k=='number':
- html += u'<span class="%s">Num. %s, </span>\n' %(k, unicode(v, encoding='utf8'))
- elif k=='pages':
- html += u'<span class="%s">pgs. %s, </span>\n' %(k, unicode(v, encoding='utf8'))
- elif k=='doi':
- html += u'<span class="%s"> <a href="http://dx.doi.org/%s">doi:%s</a> </span>\n' % (k, unicode(v, encoding='utf8'), unicode(v, encoding='utf8'))
- elif k == 'pdf':
- html += u'<span class="pdf"> <a href="papers/%s">PDF.</a> </span>\n' % unicode(v, encoding='utf8')
- elif k == 'abstract':
- html += u'<span class="linkabs"><a href="javascript:unhide(\'%s\');">Show/Hide Abstract.</a></span>\n' % (bid + '-abs')
- html += u'<div class="hidden" id="%s"><div class="abstract"><b>Abstract:</b> %s\n</div></div>\n' % (bid+'-abs', unicode(v, encoding='utf8'))
- else:
- html += u'<span class="%s"> %s </span>\n' %(k, unicode(v, encoding='utf8'))
- html += u'<span class="linkbib"><a href="javascript:unhide(\'%s\');">Show/Hide Bibtex.</a></span>\n' % bid
- html += u'<div class="hidden" id="%s"><pre>%s\n</pre></div>\n' % (bid, toBibtex(ref))
- html += u'\n</a></div></td>\n'
- html += u'</tr>\n'
- # html += u'<tr>\n'
- # html += u'<td>\n'
- # html += u'</td>\n'
- # html += u'<td>\n'
- # html += u'<div class="hidden" id="%s"><pre>%s\n</pre></div>\n' % (bid, toBibtex(ref))
- # html += u'</td>\n'
- # html += u'</tr>\n'
- html += u'</table>\n'
- return html
- def referencesByYear(bibs):
- # byYears = {first:0, last: 3000}
- byYears = {}
- for ref in bibs:
- if 'year' in ref.keys():
- year = int(ref['year'])
- if year not in byYears.keys():
- byYears[year] = [ ref ]
- else:
- byYears[year].append(ref)
- return byYears
- def preamble():
- a = """<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>
- <link href="publications.css" type="text/css" rel="stylesheet" />
- <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
- <script type="text/javascript">
- function unhide(divID) {
- var item = document.getElementById(divID);
- if (item) {
- item.className=(item.className=='hidden')?'unhidden':'hidden';
- }
- }
- </script>
- <title>Publications</title>
- </head><body>
- <div class="header1"><br />
- <table class="header1" cellpadding="0" cellspacing="0">
- <colgroup span="1" width="80%" /><colgroup span="1" width="20%" /><tbody>
- <tr>
- <td>
- <h1>Publications</h1>
- </td>
- <td><!--#include virtual="navigation.html"--> </td>
- </tr>
- </tbody>
- </table>
- </div>
- <p>
- The full CV, at the CNPq CV repository, is available
- <a href="http://lattes.cnpq.br/3575273076267374 ">here</a>
- (in portuguese).
- </p>"""
- return a
- def posamble():
- a = """<h2> </h2>
- <div>
- <address>Siome Klein Goldenstein:
- [myfirstname](at) ic unicamp br </address>
- <!-- hhmts start -->Lastmodified:
- <!--#flastmod file="publications.shtml" -->
- <!-- hhmts end -->
- </div>
- </body></html>"""
- return a
- def filterType( entry, valid_types ):
- vtypes = [each.lower() for each in valid_types]
- tp = entry['reference_type']
- tp = tp.lower()
- tp = tp.replace('reference','')
- return tp in vtypes
- def generateWebpage (bibs,
- pord=[ (['article'], 'Journal Publications', 'byYear'),
- (['patent'], 'Patents', None),
- (['book', 'inbook'], 'Books and Book Chapters', None),
- (['inproceedings'], 'Conferences and Workshops', 'byYear'),
- (['incollection'], 'Magazines', None),
- (['phdthesis','mastersthesis'], 'Thesis', None)
- ]
- ):
- webpg = preamble() + u'\n\n'
- webpg += u'<br /> <br /> <p style="text-align: center;"> '
- for ent in pord:
- webpg += u' '
- webpg += u' <a href="#' + unicode(ent[1]) + u'"> ' + unicode(ent[1]) + u'</a> '
- webpg += u' </p"> <br /> <br />'
- for ent in pord:
- fbib = filter(lambda x: filterType(x, ent[0]), bibs)
- print >>sys.stderr, '%s: has %d elements out of %d total' % (ent[1],len(fbib),len(bibs))
- sys.stderr.flush()
- webpg += u'<h2> <a name="' + unicode(ent[1]) + u'" > ' + unicode(ent[1]) + u' </a></h2>\n'
- # webpg += u'<ol reversed="reversed">'
- numb = len(fbib)
- byear = referencesByYear(fbib)
- years = byear.keys()
- years.sort(reverse=True)
- for y in years:
- if ent[2] == 'byYear':
- webpg += u'<h3> <span> '+ unicode(str(y)) + u'</span></h3>\n'
- # fp.write( '<h3> '+ y + '</h3>\n' )
- for r in byear[y]:
- # fp.write('\n' + toHTML(r) + '\n')
- webpg += u'\n' + toHTML(r, numb) + u'\n'
- numb -=1
- # webpg += u'</ol>'
- # fp.write ( posamble() )
- webpg += posamble()
- # fp.close()
- return webpg
- if __name__ == "__main__":
- if len(sys.argv) != 2:
- print '\n' + sys.argv[0] + ': requires name of the bibtex file!\n'
- sys.exit()
- fname = sys.argv[1]
- bibfile = codecs.open(fname,encoding='latin_1').read()
- bibs = parseBibtex( bibfile )
- s = generateWebpage( bibs )
- print s.encode('latin_1')
Raw Paste