[BACK]Return to bib2xml.sh CVS log [TXT][DIR] Up to [local] / web

File: [local] / web / bib2xml.sh (download)

Revision 1.12, Sat Apr 30 17:05:35 2022 JST (2 years ago) by hako
Branch: MAIN
CVS Tags: HEAD
Changes since 1.11: +2 -2 lines

use [[:blank:]] instead of .* to identify booktitle and title

#!/bin/sh
# $Id: bib2xml.sh,v 1.12 2022/04/30 08:05:35 hako Exp $
# bib2xml converts a bibtex file to an xml file.
# by Hiroshi Hakoyama

readonly cmd=$(basename $0)
if [ $# -ne 2 ]; then
  echo "Usage: $cmd template.xml data.bib" 1>&2
  exit 1
fi

template=$1
bib_file=$2

# reverse_abbr_name() identifies the encoding of the author name,
# changes the separator style (A and B; A, B and C),
# and abbreviates the first name, if it is ASCII.
# Middle name is acceptable.
# Examples:
# Kodama, Sakie and Fujimori, Hiroka and Hakoyama, Hiroshi
# =>
# Kodama, S., Fujimori, H. and Hakoyama, H.
# Le Boeuf, Burney J => Le Boeuf, B. J.
# 児玉 紗希江 and 藤森 宏佳 and 箱山  洋
# =>
# 児玉紗希江, 藤森宏佳 and 箱山 洋
reverse_abbr_name() {
  encode=$(echo $1 | LC_COLLATE=C grep '[^ -~]')
  if [ -z "$encode" ]; then
    echo $1 | sed s/" and "/:/g | awk -F','  -v 'RS=:' -v 'OFS=,' '{print $1,$2}' | sed '$d' | sed s/$/./ | sed 's/, \(.\)[^ ]*/, \1./' | tr '\n' ',' | sed s/,$// | sed 's/\.,/\., /g' | sed 's/\(.*\)\., /\1\. and /'
  else
    echo $1 | sed s/" and "/,/g | sed s/" "//g | sed s/,/", "/g | sed 's/\(.*\)\, /\1\ and /'
  fi
}

# read fields from a bibtex entry 
pubType=$(grep '^@.*{' $bib_file | sed -e 's/^@\(.*\){.*/\1/')
citeKey=$(grep '^@.*{' $bib_file | sed -e 's/^@.*{\(.*\),/\1/')
fields="author abstract title journal volume pages year month day note keywords url affiliation booktitle address editor publisher chapter"
for i in $fields; do
  eval "$i=\"$(grep "^[[:blank:]]*$i = {" $bib_file | sed 's/^[^=]*= {\(.*\)}./\1/')\""
done
author=$(reverse_abbr_name "$author")
editor=$(reverse_abbr_name "$editor")

# write the fields to an xml template
commands="s@<\$citeKey/>@$citeKey@g;s@<\$pubType/>@$pubType@g;"
for i in $fields; do  
  command="s@<\$fields\.$i/>@$(eval echo '$'$i)@g;"
  commands=$commands$command
done
commands=$commands"/>NA</d;/>NA\./d;s@?\.@?@g;s@?\.@?@g"
sed -e "$commands" $template > ${citeKey}.xml

exit 0