Attachment 'bibtex2wiki.py'

Download

   1 #!/usr/bin/python
   2 
   3 #
   4 # Parses a bibtex file, create a wiki-formatted list of bibtex entries
   5 #
   6 # Usage: bibtex2wiki <bibtexfile.bib> [linelists.txt]
   7 #
   8 #
   9 
  10 import sys
  11 
  12 def bibtex2wiki(infile, outfile):
  13     try:
  14         fin = open(infile, 'r')
  15     except IOError:
  16         print "File %s could not be found."
  17         sys.exit()
  18     try:
  19         fout = open(outfile, 'w')
  20     except IOError:
  21         print "Error opening outfile %s" % outfile
  22         sys.exit()
  23         
  24     outstring = ""    
  25     block = ""
  26     blockid = ""
  27     header = "\n<<Anchor(%s)>>\n{{{\n"
  28     footer = "}}}"
  29         
  30     def format_block(blockid, block):
  31         return header % blockid + block + footer
  32         
  33     for line in fin.xreadlines():
  34         #print line
  35         if line.strip().startswith('%'):
  36             # a bibtex comment
  37             continue 
  38         if line.strip().startswith('@'):
  39             # the start of a new block            
  40             if block and blockid:
  41                 # close the previous block
  42                 fout.writelines(format_block(blockid, block))
  43                 blockid, block = "", ""                
  44             try:
  45                 blockid, block = line.split('{', 1)[1].strip().strip(','), line
  46             except Exception:
  47                 print "ERROR: malformed block header in: '%s'" % line
  48                 sys.exit()
  49         elif block:
  50             # keep filling an existing block
  51             block += line
  52         else:
  53             # outside of any block - ignore
  54             pass
  55     if block and blockid:
  56         # handle last entry
  57         fout.writelines(format_block(blockid, block))
  58     fin.close()
  59     fout.close()
  60 
  61 if __name__ == "__main__":
  62     
  63     argv = sys.argv
  64     if len(argv) < 2:
  65         print """
  66         Usage: bibtex2wiki.py <bibtexfile> [linelistRefs.txt]
  67 
  68         This takes a valid bibtex file as input. It creates a readily formatted
  69         wiki-page with each bibtex entry as a HTML-linkable entry. Load the output file
  70         into the wiki using the wiki's Load command (found in the left-hand menu).
  71         """
  72 
  73         sys.exit()    
  74     infile = argv[1].strip()
  75     try:
  76         fil = open(infile, 'r')
  77     except IOError:
  78         print "File %s could not be found." % infile
  79         sys.exit()
  80     if len(argv) > 2:
  81         outfile = argv[2].strip()
  82     else:
  83         outfile = "linelistRefs.txt"
  84     bibtex2wiki(infile, outfile)
  85     print "... parsed %s. Created output file %s." % (infile, outfile)

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2014-10-10 10:00:07, 33.5 KB) [[attachment:VALD3_all.cfg]]
  • [get | view] (2015-05-20 18:50:41, 34.3 KB) [[attachment:VALD3_obs.cfg]]
  • [get | view] (2022-06-30 17:13:28, 373.9 KB) [[attachment:VALD_ref.bib]]
  • [get | view] (2011-02-14 09:38:07, 2.4 KB) [[attachment:bibtex2wiki.py]]
  • [get | view] (2010-10-28 15:04:24, 7.4 KB) [[attachment:publications_to_sources_map.dat]]
  • [get | view] (2011-01-22 12:38:06, 4.5 KB) [[attachment:sourceToRefMap]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.