def writeHTML(f, sections, sectionType, fieldName, choices, tag, config, cache_url_path, section_urls={}): """sections: list of (sectionname, [list of BibTeXEntry])''' sectionType: str fieldName: str choices: list of (choice, url)""" title = config.TAG_TITLES[tag] short_title = config.TAG_SHORT_TITLES[tag] # secStr = [] for s, _ in sections: hts = re.sub(r'\s+', ' ', s.strip()) hts = s.replace(" ", " ") secStr.append("<p class='l2'><a href='#%s'>%s</a></p>\n"% ((BibTeX.url_untranslate(s),hts))) secStr = "".join(secStr) # tagListStr = [] st = config.TAG_SHORT_TITLES.keys() st.sort() root = "../"*pathLength(config.TAG_DIRECTORIES[tag]) if root == "": root = "." for t in st: name = config.TAG_SHORT_TITLES[t] if t == tag: tagListStr.append(name) else: url = BibTeX.smartJoin(root, config.TAG_DIRECTORIES[t], "date.html") tagListStr.append("<a href='%s'>%s</a>"%(url, name)) tagListStr = " | ".join(tagListStr) # choiceStr = [] for choice, url in choices: if url: choiceStr.append("<a href='%s'>%s</a>"%(url, choice)) else: choiceStr.append(choice) choiceStr = (" | ".join(choiceStr)) fields = { 'command_line' : "", 'sectiontypes' : sectionType, 'choices' : choiceStr, 'field': fieldName, 'sections' : secStr, 'otherbibs' : tagListStr, 'title': title, 'short_title': short_title, "root" : root, } header, footer = getTemplate(config.TEMPLATE_FILE) print >>f, header%fields writeBody(f, sections, section_urls, cache_path=cache_url_path, base_url=root) print >>f, footer%fields
def html(bibfile): global str bib = BibTeX.BibTeX(); bib.parseFile(bibfile); bib.resolveAbbrev(); ## generate HTML append_nl( "<html>" ); append_nl( "<head>" ); append_nl( " <title>Bibliography %s</title>" % ( bibfile,) ); append_nl( """ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">""" ); append_nl( "</head>" ); append_nl( "<body>" ); for be in bib: hb = HBibEntry(be); hb.display(); if highlight: str = string.replace(str, highlight, """<font color="ff0000">%s</font>""" % highlight); append_nl( "<hr>" ); append_nl( "<p>Generated by bib2html at %s. bib2html by Peter Corke</p>" % time.asctime() ); append_nl( "</body>" ); append_nl( "</html>" ); return str
def index_bib_files_in_directory(output_handle, dirname, basenames): for basename in basenames: if os.path.splitext(basename)[1] != '.bib': continue filename = os.path.join(dirname, basename) print 'Checking', filename try: BibTeX.parseFile(filename) except: traceback.print_exc() print 'Skipping', filename, 'because of errors' continue with open(filename) as input_handle: for line in input_handle: output_handle.write(line)
def writeBody(f, sections, section_urls, cache_path, base_url): '''f: an open file sections: list of (sectionname, [list of BibTeXEntry]) section_urls: map from sectionname to external url''' for s, entries in sections: u = section_urls.get(s) sDisp = re.sub(r'\s+', ' ', s.strip()) sDisp = sDisp.replace(" ", " ") if u: print >> f, ('<li><h3><a name="%s"></a><a href="%s">%s</a></h3>' % ((BibTeX.url_untranslate(s), u, sDisp))) else: print >> f, ('<li><h3><a name="%s">%s</a></h3>' % (BibTeX.url_untranslate(s), sDisp)) print >> f, "<ul class='expand'>" for e in entries: print >> f, e.to_html(cache_path=cache_path, base_url=base_url) print >> f, "</ul></li>"
def writeBody(f, sections, section_urls, cache_path, base_url): '''f: an open file sections: list of (sectionname, [list of BibTeXEntry]) section_urls: map from sectionname to external url''' for s, entries in sections: u = section_urls.get(s) sDisp = re.sub(r'\s+', ' ', s.strip()) sDisp = sDisp.replace(" ", " ") if u: print >>f, ('<li><h3><a name="%s"></a><a href="%s">%s</a></h3>'%( (BibTeX.url_untranslate(s), u, sDisp))) else: print >>f, ('<li><h3><a name="%s">%s</a></h3>'%( BibTeX.url_untranslate(s),sDisp)) print >>f, "<ul class='expand'>" for e in entries: print >>f, e.to_html(cache_path=cache_path, base_url=base_url) print >>f, "</ul></li>"
def html(bibfile, keylist=None, rootdir=None): global str str = '' bib = BibTeX.BibTeX() bib.parseFile(bibfile) bib.resolveAbbrev() bibentries = [] if keylist is not None: for key in keylist: try: bibentries.append(bib.__getitem__(key)) except ValueError: pass else: bibentries = bib ## generate HTML # append_nl( "<html>" ); # append_nl( "<head>" ); # append_nl( " <title>Bibliography %s</title>" % ( bibfile,) ); # append_nl( """ <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">""" ); # append_nl( "</head>" ); # append_nl( "<body>" ); for be in bibentries: hb = HBibEntry(be, rootdir) hb.display() if highlight: str = string.replace(str, highlight, """<font color="ff0000">%s</font>""" % highlight) # append_nl( "<hr>" ); # append_nl( "<p>Generated by bib2html at %s. bib2html by Peter Corke</p>" % time.asctime() ); # append_nl( "</body>" ); # append_nl( "</html>" ); return str
return False URLTYPES=[ "pdf", "ps", "txt", "ps_gz", "html" ] if __name__ == '__main__': # First download the bibliography file. import BibTeX suggest = False if sys.argv[1] == 'suggest': suggest = True del sys.argv[1] config.load(sys.argv[1]) if config.CACHE_UMASK != None: os.umask(config.CACHE_UMASK) bib = BibTeX.parseFile(config.MASTER_BIB) remove_old() print "Downloading missing ranks." for ent in bib.entries: getCite(ent['title'], cache=True, update=True) if suggest: for ent in bib.entries: haveOne = False for utype in URLTYPES: if ent.has_key("www_%s_url"%utype): haveOne = True break if haveOne: continue
def writePageSet(config, bib, tag): if tag: bib_entries = [ b for b in bib.entries if tag in b.get('www_tags', "").split() ] else: bib_entries = bib.entries[:] if not bib_entries: print >> sys.stderr, "No entries with tag %r; skipping" % tag return tagdir = config.TAG_DIRECTORIES[tag] outdir = os.path.join(config.OUTPUT_DIR, tagdir) cache_url_path = BibTeX.smartJoin("../" * pathLength(tagdir), config.CACHE_DIR) if not os.path.exists(outdir): os.makedirs(outdir, 0755) ##### Sorted views: ## By topic. entries = BibTeX.sortEntriesBy(bib_entries, "www_section", "ZZZZZZZZZZZZZZ") entries = BibTeX.splitSortedEntriesBy(entries, "www_section") if entries[-1][0].startswith("<span class='bad'>"): entries[-1] = ("Miscellaneous", entries[-1][1]) entries = [(s, BibTeX.sortEntriesByDate(ents)) for s, ents in entries] f = open(os.path.join(outdir, "topic.html"), 'w') writeHTML(f, entries, "Topics", "topic", (("By topic", None), ("By date", "./date.html"), ("By author", "./author.html")), tag=tag, config=config, cache_url_path=cache_url_path) f.close() ## By date. entries = BibTeX.sortEntriesByDate(bib_entries) entries = BibTeX.splitSortedEntriesBy(entries, 'year') for idx in -1, -2: try: if entries[idx][0].startswith("<span class='bad'>"): entries[idx] = ("Unknown", entries[idx][1]) elif entries[idx][0].startswith("forthcoming"): entries[idx] = ("Forthcoming", entries[idx][1]) except IndexError: continue sections = [ent[0] for ent in entries] first_year = int(entries[0][1][0]['year']) try: last_year = int(entries[-1][1][0].get('year')) except ValueError: last_year = int(entries[-2][1][0].get('year')) years = map(str, range(first_year, last_year + 1)) if entries[-1][0] == 'Unknown': years.append("Unknown") f = open(os.path.join(outdir, "date.html"), 'w') writeHTML(f, entries, "Years", "date", (("By topic", "./topic.html"), ("By date", None), ("By author", "./author.html")), tag=tag, config=config, cache_url_path=cache_url_path) f.close() ## By author entries, url_map = BibTeX.splitEntriesByAuthor(bib_entries) f = open(os.path.join(outdir, "author.html"), 'w') writeHTML(f, entries, "Authors", "author", ( ("By topic", "./topic.html"), ("By date", "./date.html"), ("By author", None), ), tag=tag, config=config, cache_url_path=cache_url_path, section_urls=url_map) f.close() ## The big BibTeX file entries = bib_entries[:] entries = [(ent.key, ent) for ent in entries] entries.sort() entries = [ent[1] for ent in entries] ## Finding the root directory is done by writeHTML(), but ## the BibTeX file doesn't use that, so repeat the code here root = "../" * pathLength(config.TAG_DIRECTORIES[tag]) if root == "": root = "." header, footer = getTemplate(config.BIBTEX_TEMPLATE_FILE) f = open(os.path.join(outdir, "bibtex.html"), 'w') print >> f, header % { 'command_line': "", 'title': config.TAG_TITLES[tag], 'root': root } for ent in entries: print >> f, ( ("<tr><td class='bibtex'><a name='%s'>%s</a>" "<pre class='bibtex'>%s</pre></td></tr>") % (BibTeX.url_untranslate(ent.key), ent.key, ent.format(90, 8, 1))) print >> f, footer f.close() f = open(os.path.join(outdir, "bibtex.json"), 'w') json.dump(entries, f, default=jsonDumper) f.close()
print >> f, header % { 'command_line': "", 'title': config.TAG_TITLES[tag], 'root': root } for ent in entries: print >> f, ( ("<tr><td class='bibtex'><a name='%s'>%s</a>" "<pre class='bibtex'>%s</pre></td></tr>") % (BibTeX.url_untranslate(ent.key), ent.key, ent.format(90, 8, 1))) print >> f, footer f.close() f = open(os.path.join(outdir, "bibtex.json"), 'w') json.dump(entries, f, default=jsonDumper) f.close() if __name__ == '__main__': if len(sys.argv) == 2: print "Loading from %s" % sys.argv[1] else: print >> sys.stderr, "Expected a single configuration file as an argument" sys.exit(1) config.load(sys.argv[1]) bib = BibTeX.parseFile(config.MASTER_BIB) for tag in config.TAG_DIRECTORIES.keys(): writePageSet(config, bib, tag)
def getCacheFname(key, ftype, section): return BibTeX.smartJoin(config.OUTPUT_DIR, config.CACHE_DIR, section, "%s.%s" % (key, ftype))
def writePageSet(config, bib, tag): if tag: bib_entries = [ b for b in bib.entries if tag in b.get('www_tags', "").split() ] else: bib_entries = bib.entries[:] if not bib_entries: print >>sys.stderr, "No entries with tag %r; skipping"%tag return tagdir = config.TAG_DIRECTORIES[tag] outdir = os.path.join(config.OUTPUT_DIR, tagdir) cache_url_path = BibTeX.smartJoin("../"*pathLength(tagdir), config.CACHE_DIR) if not os.path.exists(outdir): os.makedirs(outdir, 0755) ##### Sorted views: ## By topic. entries = BibTeX.sortEntriesBy(bib_entries, "www_section", "ZZZZZZZZZZZZZZ") entries = BibTeX.splitSortedEntriesBy(entries, "www_section") if entries[-1][0].startswith("<span class='bad'>"): entries[-1] = ("Miscellaneous", entries[-1][1]) entries = [ (s, BibTeX.sortEntriesByDate(ents)) for s, ents in entries ] f = open(os.path.join(outdir,"topic.html"), 'w') writeHTML(f, entries, "Topics", "topic", (("By topic", None), ("By date", "./date.html"), ("By author", "./author.html") ), tag=tag, config=config, cache_url_path=cache_url_path) f.close() ## By date. entries = BibTeX.sortEntriesByDate(bib_entries) entries = BibTeX.splitSortedEntriesBy(entries, 'year') for idx in -1, -2: if entries[idx][0].startswith("<span class='bad'>"): entries[idx] = ("Unknown", entries[idx][1]) elif entries[idx][0].startswith("forthcoming"): entries[idx] = ("Forthcoming", entries[idx][1]) sections = [ ent[0] for ent in entries ] first_year = int(entries[0][1][0]['year']) try: last_year = int(entries[-1][1][0].get('year')) except ValueError: last_year = int(entries[-2][1][0].get('year')) years = map(str, range(first_year, last_year+1)) if entries[-1][0] == 'Unknown': years.append("Unknown") f = open(os.path.join(outdir,"date.html"), 'w') writeHTML(f, entries, "Years", "date", (("By topic", "./topic.html"), ("By date", None), ("By author", "./author.html") ), tag=tag, config=config, cache_url_path=cache_url_path) f.close() ## By author entries, url_map = BibTeX.splitEntriesByAuthor(bib_entries) f = open(os.path.join(outdir,"author.html"), 'w') writeHTML(f, entries, "Authors", "author", (("By topic", "./topic.html"), ("By date", "./date.html"), ("By author", None), ), tag=tag, config=config, cache_url_path=cache_url_path, section_urls=url_map) f.close() ## The big BibTeX file entries = bib_entries[:] entries = [ (ent.key, ent) for ent in entries ] entries.sort() entries = [ ent[1] for ent in entries ] ## Finding the root directory is done by writeHTML(), but ## the BibTeX file doesn't use that, so repeat the code here root = "../"*pathLength(config.TAG_DIRECTORIES[tag]) if root == "": root = "." header,footer = getTemplate(config.BIBTEX_TEMPLATE_FILE) f = open(os.path.join(outdir,"bibtex.html"), 'w') print >>f, header % { 'command_line' : "", 'title': config.TAG_TITLES[tag], 'root': root } for ent in entries: print >>f, ( ("<tr><td class='bibtex'><a name='%s'>%s</a>" "<pre class='bibtex'>%s</pre></td></tr>") %(BibTeX.url_untranslate(ent.key), ent.key, ent.format(90,8,1))) print >>f, footer f.close() f = open(os.path.join(outdir,"bibtex.json"), 'w') json.dump(entries, f, default=jsonDumper) f.close()
def emitKnown(f, ent, matches): print >>f, "%% Candidates are:", ", ".join([e.key for g,e in matches]) print >>f, "%%" print >>f, "%"+(ent.format(77,4,1,invStrings).replace("\n", "\n%")) if __name__ == '__main__': if len(sys.argv) != 3: print "reconcile.py expects 2 arguments" sys.exit(1) config.load(sys.argv[1]) print "========= Scanning master ==========" master = MasterBibTeX() master = BibTeX.parseFile(config.MASTER_BIB, result=master) master.buildIndex() print "========= Scanning new file ========" try: fn = sys.argv[2] input = BibTeX.parseFile(fn) except BibTeX.ParseError, e: print "Error parsing %s: %s"%(fn,e) sys.exit(1) f = open('tmp.bib', 'w') keys = input.newStrings.keys() keys.sort() for k in keys: v = input.newStrings[k]
def emitKnown(f, ent, matches): print >> f, "%% Candidates are:", ", ".join([e.key for g, e in matches]) print >> f, "%%" print >> f, "%" + (ent.format(77, 4, 1, invStrings).replace("\n", "\n%")) if __name__ == '__main__': if len(sys.argv) != 3: print "reconcile.py expects 2 arguments" sys.exit(1) config.load(sys.argv[1]) print "========= Scanning master ==========" master = MasterBibTeX() master = BibTeX.parseFile(config.MASTER_BIB, result=master) master.buildIndex() print "========= Scanning new file ========" try: fn = sys.argv[2] input = BibTeX.parseFile(fn) except BibTeX.ParseError, e: print "Error parsing %s: %s" % (fn, e) sys.exit(1) f = open('tmp.bib', 'w') keys = input.newStrings.keys() keys.sort() for k in keys: v = input.newStrings[k]
def authorsParseTo(self,authors,result): pa = BibTeX.parseAuthor(authors) self.assertEquals(["|".join(["+".join(item) for item in [a.first,a.von,a.last,a.jr]]) for a in pa], result)