Posted By

iblis on 08/02/07


Tagged

convert bibtex bibliography


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

mabridge


Convert keywords to tags (and vice versa) in BiBTeX files


 / Published in: Perl
 

Convert (comma separated) keywords to (space separated) tags in BibTeX files for use in social bibliography sites (bibsonomy.org, citeulike.org)

  1. #!/usr/bin/perl -w
  2. use strict;
  3. use utf8;
  4. use encoding "utf8";
  5. use Getopt::Std;
  6. use Text::BibTeX;
  7. #
  8. getopt();
  9. our $opt_r;
  10. die "Usage : $0 [-r] source-file dest-file\n" if !defined( $ARGV[1] ) ;
  11. #
  12. my $oldfile = shift;
  13. open BIBFILE1, '<:utf8', $oldfile or die "Problem opening $oldfile\n";
  14. my $newfile = shift;
  15. open BIBFILE2, '>:utf8', $newfile or die "Problem opening $newfile\n";
  16. #
  17. while (my $entry = new Text::BibTeX::Entry $oldfile, \*BIBFILE1)
  18. {
  19. next if !$entry->parse_ok;
  20. my $keywords = $entry->get ('keywords');
  21. next if !defined($keywords);
  22. # my $title = $entry->get ('title');
  23. # print "$title:\n" if (defined($title));
  24. # print "$keywords\n-->\n";
  25. ##
  26. my $tags;
  27. if (!$opt_r) {
  28. my @list = split (",", $keywords);
  29. foreach (@list) {s/^ //g; s/ $//g; s/ /_/g};
  30. $tags = join(' ', @list);
  31. }
  32. else {
  33. # print "reverse\n";
  34. my @list = split (" ", $keywords);
  35. foreach (@list) {s/_/ /g};
  36. $tags = join(', ', @list);
  37. }
  38. # print "/", $tags, "/\n\n";
  39. ##
  40. $entry->set ('keywords', $tags);
  41. $entry->print (\*BIBFILE2);
  42. }

Report this snippet  

You need to login to post a comment.