#!/usr/bin/env python3 """ Format .bbl file to use Author Year. format instead of {Author} (Year). This script processes the bibliography file generated by BibTeX and reformats entries to display as "Author Year." instead of "{Author} (Year)." in the references section. Usage: python format_bbl.py """ import re import sys def format_bibliography(bbl_file): """ Format bibliography entries to Author Year. format. Also fixes Vietnamese name order from "Given Family" back to "Family Given". Args: bbl_file: Path to the .bbl file to format """ with open(bbl_file, 'r', encoding='utf-8') as f: content = f.read() # First, join multiline bibitem entries into single lines # Pattern: \bibitem[...](year)]{key} that may span multiple lines content = re.sub(r'\\bibitem\[([^\]]+)\]\{([^}]+)\}\s*\n\s*', r'\\bibitem[\1]{\2}\n', content) lines = content.split('\n') output = [] i = 0 # Vietnamese family names to detect viet_families = ['Cao', 'Đoàn', 'Hoàng', 'Mai', 'Nguyễn', 'Vũ', 'Vương'] while i < len(lines): line = lines[i] # Match bibitem line with format: \bibitem[{Author}(Year)]{key} # Handle various formats including fontencoding match = re.match(r'\\bibitem\[(.+?)\((\d+[a-z]?|{\\.+?})\)\]\{([^}]+)\}', line) # Also handle bibitem without year (editors) if not match: match = re.match(r'\\bibitem\[(.+?)\]\{([^}]+)\}', line) has_year = False else: has_year = True if match: if has_year: author_part = match.group(1).strip() year = match.group(2) key = match.group(3) else: author_part = match.group(1).strip() year = None key = match.group(2) # Write the bibitem line unchanged output.append(line) i += 1 # Next line should be the author - replace with Author Year. if i < len(lines): next_line = lines[i] # Skip if line is empty or starts with \newblock if next_line.strip() and not next_line.strip().startswith('\\newblock'): # Handle fontencoding patterns clean_line = re.sub(r'\{\\fontencoding\{[^}]+\}\\selectfont ([^}]+)\}', r'\1', next_line) clean_line = re.sub(r'^\{(.+)\}\.?\s*$', r'\1', clean_line.strip()) # Fix Vietnamese name order: "Given~Family" -> "Family Given" # and wrap Vietnamese names in T5 encoding for family in viet_families: # Match "Given~Family" or "Given Family" pattern = r'(\S+)~(' + re.escape(family) + r')\b' if re.search(pattern, clean_line): # Swap to "Family Given" and wrap in T5 clean_line = re.sub(pattern, r'{\\fontencoding{T5}\\selectfont \2 \1}', clean_line) # Also wrap any remaining Vietnamese family names in T5 if they appear standalone for family in viet_families: # Match "Family Given" pattern not already wrapped pattern = r'\b(' + re.escape(family) + r')\s+(\S+)\b' # Only wrap if not already in fontencoding and contains Vietnamese characters if re.search(pattern, clean_line) and 'fontencoding' not in clean_line: # Check if the second part contains Vietnamese characters match = re.search(pattern, clean_line) if match: given = match.group(2) # Check for Vietnamese characters if any(c in given for c in 'ạảãàáâậầấẩẫăặắằẳẵêệềếểễôộồốổỗơởỡớờợưựứừửữđíìịỉĩóòỏõọúùủũụýỳỷỹỵ'): clean_line = re.sub(pattern, r'{\\fontencoding{T5}\\selectfont \1 \2}', clean_line, count=1) # Add year with period if has_year and year and not clean_line.endswith(year + '.') and 'natexlab' not in clean_line: clean_line = clean_line.rstrip('.') + ' ' + year + '.' output.append(clean_line) i += 1 else: output.append(next_line) i += 1 else: output.append(line) i += 1 # Write back the formatted content with open(bbl_file, 'w', encoding='utf-8') as f: f.write('\n'.join(output)) print(f"Formatted bibliography in {bbl_file}") if __name__ == '__main__': if len(sys.argv) != 2: print("Usage: python format_bbl.py ") sys.exit(1) bbl_file = sys.argv[1] format_bibliography(bbl_file)