\n' + \
kpeter@743: '[' + outkey + '] | ')
kpeter@743: file.append('')
kpeter@743: file.append('\\anchor ' + outkey)
kpeter@743: for line in entry[2:]:
kpeter@743: file.append(line)
kpeter@743: file.append(' | \n
')
kpeter@743: file.append('')
kpeter@743:
kpeter@743: return file
kpeter@743:
kpeter@743:
kpeter@743: #
kpeter@743: # return 1 iff abbr is in line but not inside braces or quotes
kpeter@743: # assumes that abbr appears only once on the line (out of braces and quotes)
kpeter@743: #
kpeter@743: def verify_out_of_braces(line, abbr):
kpeter@743:
kpeter@743: phrase_split = delimiter_rex.split(line)
kpeter@743:
kpeter@743: abbr_rex = re.compile( '\\b' + abbr + '\\b', re.I)
kpeter@743:
kpeter@743: open_brace = 0
kpeter@743: open_quote = 0
kpeter@743:
kpeter@743: for phrase in phrase_split:
kpeter@743: if phrase == "{":
kpeter@743: open_brace = open_brace + 1
kpeter@743: elif phrase == "}":
kpeter@743: open_brace = open_brace - 1
kpeter@743: elif phrase == '"':
kpeter@743: if open_quote == 1:
kpeter@743: open_quote = 0
kpeter@743: else:
kpeter@743: open_quote = 1
kpeter@743: elif abbr_rex.search(phrase):
kpeter@743: if open_brace == 0 and open_quote == 0:
kpeter@743: return 1
kpeter@743:
kpeter@743: return 0
kpeter@743:
kpeter@743:
kpeter@743: #
kpeter@743: # a line in the form phrase1 # phrase2 # ... # phrasen
kpeter@743: # is returned as phrase1 phrase2 ... phrasen
kpeter@743: # with the correct punctuation
kpeter@743: # Bug: Doesn't always work with multiple abbreviations plugged in
kpeter@743: #
kpeter@743: def concat_line(line):
kpeter@743: # only look at part after equals
kpeter@743: field = field_rex.sub('\g<1>',line)
kpeter@743: rest = field_rex.sub('\g<2>',line)
kpeter@743:
kpeter@743: concat_line = field + ' ='
kpeter@743:
kpeter@743: pound_split = concatsplit_rex.split(rest)
kpeter@743:
kpeter@743: phrase_count = 0
kpeter@743: length = len(pound_split)
kpeter@743:
kpeter@743: for phrase in pound_split:
kpeter@743: phrase = phrase.strip()
kpeter@743: if phrase_count != 0:
kpeter@743: if phrase.startswith('"') or phrase.startswith('{'):
kpeter@743: phrase = phrase[1:]
kpeter@743: elif phrase.startswith('"'):
kpeter@743: phrase = phrase.replace('"','{',1)
kpeter@743:
kpeter@743: if phrase_count != length-1:
kpeter@743: if phrase.endswith('"') or phrase.endswith('}'):
kpeter@743: phrase = phrase[:-1]
kpeter@743: else:
kpeter@743: if phrase.endswith('"'):
kpeter@743: phrase = phrase[:-1]
kpeter@743: phrase = phrase + "}"
kpeter@743: elif phrase.endswith('",'):
kpeter@743: phrase = phrase[:-2]
kpeter@743: phrase = phrase + "},"
kpeter@743:
kpeter@743: # if phrase did have \#, add the \# back
kpeter@743: if phrase.endswith('\\'):
kpeter@743: phrase = phrase + "#"
kpeter@743: concat_line = concat_line + ' ' + phrase
kpeter@743:
kpeter@743: phrase_count = phrase_count + 1
kpeter@743:
kpeter@743: return concat_line
kpeter@743:
kpeter@743:
kpeter@743: #
kpeter@743: # substitute abbreviations into filecont
kpeter@743: # @param filecont_source - string of data from file
kpeter@743: #
kpeter@743: def bibtex_replace_abbreviations(filecont_source):
kpeter@743: filecont = filecont_source.splitlines()
kpeter@743:
kpeter@743: # These are defined in bibtex, so we'll define them too
kpeter@743: abbr_list = ['jan','feb','mar','apr','may','jun',
kpeter@743: 'jul','aug','sep','oct','nov','dec']
kpeter@743: value_list = ['January','February','March','April',
kpeter@743: 'May','June','July','August','September',
kpeter@743: 'October','November','December']
kpeter@743:
kpeter@743: abbr_rex = []
kpeter@743: total_abbr_count = 0
kpeter@743:
kpeter@743: front = '\\b'
kpeter@743: back = '(,?)\\b'
kpeter@743:
kpeter@743: for x in abbr_list:
kpeter@743: abbr_rex.append( re.compile( front + abbr_list[total_abbr_count] + back, re.I ) )
kpeter@743: total_abbr_count = total_abbr_count + 1
kpeter@743:
kpeter@743:
kpeter@743: abbrdef_rex = re.compile('\s*@string\s*{\s*('+ valid_name_chars +'*)\s*=(.*)',
kpeter@743: re.I)
kpeter@743:
kpeter@743: comment_rex = re.compile('@comment\s*{',re.I)
kpeter@743: preamble_rex = re.compile('@preamble\s*{',re.I)
kpeter@743:
kpeter@743: waiting_for_end_string = 0
kpeter@743: i = 0
kpeter@743: filecont2 = ''
kpeter@743:
kpeter@743: for line in filecont:
kpeter@743: if line == ' ' or line == '':
kpeter@743: continue
kpeter@743:
kpeter@743: if waiting_for_end_string:
kpeter@743: if re.search('}',line):
kpeter@743: waiting_for_end_string = 0
kpeter@743: continue
kpeter@743:
kpeter@743: if abbrdef_rex.search(line):
kpeter@743: abbr = abbrdef_rex.sub('\g<1>', line)
kpeter@743:
kpeter@743: if abbr_list.count(abbr) == 0:
kpeter@743: val = abbrdef_rex.sub('\g<2>', line)
kpeter@743: abbr_list.append(abbr)
kpeter@743: value_list.append(string.strip(val))
kpeter@743: abbr_rex.append( re.compile( front + abbr_list[total_abbr_count] + back, re.I ) )
kpeter@743: total_abbr_count = total_abbr_count + 1
kpeter@743: waiting_for_end_string = 1
kpeter@743: continue
kpeter@743:
kpeter@743: if comment_rex.search(line):
kpeter@743: waiting_for_end_string = 1
kpeter@743: continue
kpeter@743:
kpeter@743: if preamble_rex.search(line):
kpeter@743: waiting_for_end_string = 1
kpeter@743: continue
kpeter@743:
kpeter@743:
kpeter@743: # replace subsequent abbreviations with the value
kpeter@743: abbr_count = 0
kpeter@743:
kpeter@743: for x in abbr_list:
kpeter@743:
kpeter@743: if abbr_rex[abbr_count].search(line):
kpeter@743: if verify_out_of_braces(line,abbr_list[abbr_count]) == 1:
kpeter@743: line = abbr_rex[abbr_count].sub( value_list[abbr_count] + '\g<1>', line)
kpeter@743: # Check for # concatenations
kpeter@743: if concatsplit_rex.search(line):
kpeter@743: line = concat_line(line)
kpeter@743: abbr_count = abbr_count + 1
kpeter@743:
kpeter@743:
kpeter@743: filecont2 = filecont2 + line + '\n'
kpeter@743: i = i+1
kpeter@743:
kpeter@743:
kpeter@743: # Do one final pass over file
kpeter@743:
kpeter@743: # make sure that didn't end up with {" or }" after the substitution
kpeter@743: filecont2 = filecont2.replace('{"','{{')
kpeter@743: filecont2 = filecont2.replace('"}','}}')
kpeter@743:
kpeter@743: afterquotevalue_rex = re.compile('"\s*,\s*')
kpeter@743: afterbrace_rex = re.compile('"\s*}')
kpeter@743: afterbracevalue_rex = re.compile('(=\s*{[^=]*)},\s*')
kpeter@743:
kpeter@743: # add new lines to data that changed because of abbreviation substitutions
kpeter@743: filecont2 = afterquotevalue_rex.sub('",\n', filecont2)
kpeter@743: filecont2 = afterbrace_rex.sub('"\n}', filecont2)
kpeter@743: filecont2 = afterbracevalue_rex.sub('\g<1>},\n', filecont2)
kpeter@743:
kpeter@743: return filecont2
kpeter@743:
kpeter@743: #
kpeter@743: # convert @type( ... ) to @type{ ... }
kpeter@743: #
kpeter@743: def no_outer_parens(filecont):
kpeter@743:
kpeter@743: # do checking for open parens
kpeter@743: # will convert to braces
kpeter@743: paren_split = re.split('([(){}])',filecont)
kpeter@743:
kpeter@743: open_paren_count = 0
kpeter@743: open_type = 0
kpeter@743: look_next = 0
kpeter@743:
kpeter@743: # rebuild filecont
kpeter@743: filecont = ''
kpeter@743:
kpeter@743: at_rex = re.compile('@\w*')
kpeter@743:
kpeter@743: for phrase in paren_split:
kpeter@743: if look_next == 1:
kpeter@743: if phrase == '(':
kpeter@743: phrase = '{'
kpeter@743: open_paren_count = open_paren_count + 1
kpeter@743: else:
kpeter@743: open_type = 0
kpeter@743: look_next = 0
kpeter@743:
kpeter@743: if phrase == '(':
kpeter@743: open_paren_count = open_paren_count + 1
kpeter@743:
kpeter@743: elif phrase == ')':
kpeter@743: open_paren_count = open_paren_count - 1
kpeter@743: if open_type == 1 and open_paren_count == 0:
kpeter@743: phrase = '}'
kpeter@743: open_type = 0
kpeter@743:
kpeter@743: elif at_rex.search( phrase ):
kpeter@743: open_type = 1
kpeter@743: look_next = 1
kpeter@743:
kpeter@743: filecont = filecont + phrase
kpeter@743:
kpeter@743: return filecont
kpeter@743:
kpeter@743:
kpeter@743: #
kpeter@743: # make all whitespace into just one space
kpeter@743: # format the bibtex file into a usable form.
kpeter@743: #
kpeter@743: def bibtexwasher(filecont_source):
kpeter@743:
kpeter@743: space_rex = re.compile('\s+')
kpeter@743: comment_rex = re.compile('\s*%')
kpeter@743:
kpeter@743: filecont = []
kpeter@743:
kpeter@743: # remove trailing and excessive whitespace
kpeter@743: # ignore comments
kpeter@743: for line in filecont_source:
kpeter@743: line = string.strip(line)
kpeter@743: line = space_rex.sub(' ', line)
kpeter@743: # ignore comments
kpeter@743: if not comment_rex.match(line) and line != '':
kpeter@743: filecont.append(' '+ line)
kpeter@743:
kpeter@743: filecont = string.join(filecont, '')
kpeter@743:
kpeter@743: # the file is in one long string
kpeter@743:
kpeter@743: filecont = no_outer_parens(filecont)
kpeter@743:
kpeter@743: #
kpeter@743: # split lines according to preferred syntax scheme
kpeter@743: #
kpeter@743: filecont = re.sub('(=\s*{[^=]*)},', '\g<1>},\n', filecont)
kpeter@743:
kpeter@743: # add new lines after commas that are after values
kpeter@743: filecont = re.sub('"\s*,', '",\n', filecont)
kpeter@743: filecont = re.sub('=\s*([\w\d]+)\s*,', '= \g<1>,\n', filecont)
kpeter@743: filecont = re.sub('(@\w*)\s*({(\s*)[^,\s]*)\s*,',
kpeter@743: '\n\n\g<1>\g<2>,\n', filecont)
kpeter@743:
kpeter@743: # add new lines after }
kpeter@743: filecont = re.sub('"\s*}','"\n}\n', filecont)
kpeter@743: filecont = re.sub('}\s*,','},\n', filecont)
kpeter@743:
kpeter@743:
kpeter@743: filecont = re.sub('@(\w*)', '\n@\g<1>', filecont)
kpeter@743:
kpeter@743: # character encoding, reserved latex characters
kpeter@743: filecont = re.sub('{\\\&}', '&', filecont)
kpeter@743: filecont = re.sub('\\\&', '&', filecont)
kpeter@743:
kpeter@743: # do checking for open braces to get format correct
kpeter@743: open_brace_count = 0
kpeter@743: brace_split = re.split('([{}])',filecont)
kpeter@743:
kpeter@743: # rebuild filecont
kpeter@743: filecont = ''
kpeter@743:
kpeter@743: for phrase in brace_split:
kpeter@743: if phrase == '{':
kpeter@743: open_brace_count = open_brace_count + 1
kpeter@743: elif phrase == '}':
kpeter@743: open_brace_count = open_brace_count - 1
kpeter@743: if open_brace_count == 0:
kpeter@743: filecont = filecont + '\n'
kpeter@743:
kpeter@743: filecont = filecont + phrase
kpeter@743:
kpeter@743: filecont2 = bibtex_replace_abbreviations(filecont)
kpeter@743:
kpeter@743: # gather
kpeter@743: filecont = filecont2.splitlines()
kpeter@743: i=0
kpeter@743: j=0 # count the number of blank lines
kpeter@743: for line in filecont:
kpeter@743: # ignore blank lines
kpeter@743: if line == '' or line == ' ':
kpeter@743: j = j+1
kpeter@743: continue
kpeter@743: filecont[i] = line + '\n'
kpeter@743: i = i+1
kpeter@743:
kpeter@743: # get rid of the extra stuff at the end of the array
kpeter@743: # (The extra stuff are duplicates that are in the array because
kpeter@743: # blank lines were removed.)
kpeter@743: length = len( filecont)
kpeter@743: filecont[length-j:length] = []
kpeter@743:
kpeter@743: return filecont
kpeter@743:
kpeter@743:
kpeter@743: def filehandler(filepath):
kpeter@743: try:
kpeter@743: fd = open(filepath, 'r')
kpeter@743: filecont_source = fd.readlines()
kpeter@743: fd.close()
kpeter@743: except:
kpeter@743: print 'Could not open file:', filepath
kpeter@743: washeddata = bibtexwasher(filecont_source)
kpeter@743: outdata = bibtexdecoder(washeddata)
kpeter@743: print '/**'
kpeter@743: print '\page references References'
kpeter@743: print
kpeter@743: print '