#!/usr/bin/python ##****p* utils/fix_bib.py ## NAME ## fix_bib.py -- fix formatting in a bib file ## SYNOPSIS ## python fix_bib.py in the directory where biblio.bib resides ## DESCRIPTION ## based on values of options, ## open, read lines and close original file ## ## replace 'note' with 'availableas' keyword ## ## indentation: ## loop over lines, ## find lines with a '=' before the indent column, ## insert spaces between the '=' and the word preceding it, ## so that the '=' ends at the indent column ## remove trailing newline character '\n' from all lines, ## open new file and write all new_lines to it ## ## capitalization keywords: (title, publisher,booktitle,address,number,abstract: ## loop over lines, ## find line that start with any of the above keywords, ## find first left and last right curly bracket, ## add a pair of curly brackets around them ## remove trailing newline character '\n' from all lines, ## open new file and write all new_lines to it ## USES ## string ## MODIFICATION HISTORY ## 2008-07-10 -- WvH : - replace 'note' with 'availableas' ## - add curly braces around titles, publisher, number,... ## TODO ## add command-line options? ## COPYRIGHT ## Copyright (c) 2008 Wim Van Hoydonck ## AUTHOR ## Wim Van Hoydonck ## CREATION DATE ## 2008-06-24 ## SOURCE import string orig_filename = "test.bib" new_filename = "test_out.bib" indent = 17 spaces = indent*" " do_indent = True capitalize = ["title", "publisher", "journal", "booktitle", "address", "number", "abstract"] notekeyw = "note" # as python starts counting from 0, indent should be 1 less indent -= 1 # open, read lines and close original file bib_file = open(orig_filename,'r') bib_lines = bib_file.readlines() bib_file.close() # init new_lines as array new_lines = [] new_lines2 = [] # replace 'note' by 'availableas' # which is not a bibtex keyword for line in bib_lines: sline = string.strip(line) idx = sline.find(notekeyw) if ( idx == 0 ): lidx,ridx = line.find(notekeyw) , line.rfind(notekeyw) eidx = line.find(" =") # prepend line with a comment mark line = line[0:lidx]+"availableas"+line[ridx+4:] new_lines2.append(line) new_lines = new_lines2 new_lines2 = [] # indent lines properly for line in new_lines: # find lines with a '=' before the 17th column, idx = line.find("=") if (0 < idx < indent) and (line[0] != "%"): # insert spaces between the '=' and the word preceding it, # so that the '=' ends at the 17th column line = line[0:idx] + spaces[0:indent-idx] + line[idx:] # remove trailing newline character '\n' from all lines, new_lines2.append(line[0:-1]) new_lines = new_lines2 new_lines2 = [] # add a pair of curly braces around lines that start with certain keywords for line in new_lines: for keyw in capitalize: # remove leading blanks sline = string.strip(line) idx = sline.find(keyw) # keyword is first thing in line if ( idx == 0 ): # find position of first left curly bracket and last right curly bracket lidx,ridx = line.find("{") , line.rfind("}") #print lidx,ridx , keyw # if ( lidx < ridx ): line = line[0:lidx]+"{"+line[lidx:ridx]+"}"+line[ridx:] new_lines2.append(line) new_lines = new_lines2 # open new file and write all new_lines to it bib1_file = open(new_filename,'w') for line in new_lines: print >> bib1_file , line ##******