summaryrefslogtreecommitdiff
path: root/biblio/bibtex/utils/refer-tools/tex2refer
blob: e75311e1815880ab400139ba069d13c7c926f260 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
Article 2347 of comp.text.tex:
Path: ai-lab!snorkelwacker!usc!wuarchive!uunet!mcsun!unido!tub!fauern!fauern!immd2.informatik.uni-erlangen.de!fritzke
From: fritzke@immd2.informatik.uni-erlangen.de (B. Fritzke)
Newsgroups: comp.text.tex
Subject: BIBTEX to REFER conversion program (here it is)
Message-ID: <3065@medusa.informatik.uni-erlangen.de>
Date: 13 Aug 90 11:13:07 GMT
Organization: Universitaet Erlangen, CS-Dep. IMMD II
Lines: 139

Recently I posted a request for a program to convert bibliographic 
references in the bibtex format to the refer format.

Beneath a lot of 'me too!'-messages I got an answer with an 
awk script, that could do part of the job. After some iterations
of improvement there now exist a pretty good version, that fits
at least my needs.

Because many people seem to be interested in the program, I post it
to this newsgroup.

How to make it run:
  1. put the stuff after the cut-line in a file called tex2refer (or t2r or so)
  2. Make that file executable by typing
       chmod u+x tex2refer
     on your Unix system

Bernd Fritzke

Bernd Fritzke ------>  e-mail: fritzke@immd2.informatik.uni-erlangen.de
University of Erlangen, CS IMMD II, Martensstr. 3,  8520 Erlangen (FRG)

--------------------------------- cut here -----------------------------------
#! /bin/sh
#
# tex2refer - converts bibtex entries to refer entries
#
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
# This software comes on a 'as is'-basis.
# No guarantee for the correctness is given and no 'service' is provided 
# !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#
# This program (an awk skript) converts bibliographic references from the
# bibtex-format to the refer-format.
# it reads from stdin and writes to stdout:
#
# usage: tex2refer < file.bib > file.refer
#
# Be aware, that some information is neccessarily lost, because 
#
#   * several bibtex field names are mapped to the same refer filed name
#      e.g. publisher, organization and school are all mapped to %I
#   * refer doesn't support types for references (like @inproceedings, @article)
#     (therefore the inverse mapping refer2tex is mostly based on heuristics)
#
# In this program are only the more important (I.M.H.O.) field names covered.
# If tex2refer encounters unknown field names, it will ignore them but store their
# names in a list, which is displayed after the conversion process.
#
# With this list the program can easily be extended by adding entries to the 
# associative array 'refer'
#
#
# Thanks to Lee, who provided the main part of the program and added
# some useful comments for readability
#
# Bernd Fritzke (fritzke@immd2.informatik.uni-erlangen.de)
# August 1990
#

gawk ' #gnu awk, but works probably also with other versions
BEGIN {
    FS = " "
    refer["author"] = "%A"
    refer["AUTHOR"] = "%A"
    refer["address"] = "%C"
    refer["ADDRESS"] = "%C"
    refer["year"] = "%D"
    refer["YEAR"] = "%D"
    refer["publisher"] = "%I"
    refer["PUBLISHER"] = "%I"
    refer["journal"] = "%J"
    refer["JOURNAL"] = "%J"
    refer["keywords"] = "%K"
    refer["KEYWORDS"] = "%K"
    refer["pages"] = "%P"
    refer["PAGES"] = "%P"
    refer["title"] = "%T"
    refer["TITLE"] = "%T"
    refer["volume"] = "%V"
    refer["VOLUME"] = "%V"
    refer["city"] = "%C"
    refer["CITY"] = "%C"
    refer["booktitle"] = "%B"
    refer["BOOKTITLE"] = "%B"
    refer["note"] = "%o"
    refer["NOTE"] = "%o"
    refer["organization"] = "%I"
    refer["ORGANIZATION"] = "%I"
    refer["school"] = "%I"
    refer["SCHOOL"] = "%I"
}

/^@/	{next}   # reference type (not supported by refer) next line
# ---> "next" makes awk goto the start of the awk script & read the next
#	line of input, so here it is making it ignore lines starting
#	with an @ sign.

# ensure that an = signs is surrounded by space:
/\=/ { # "=" must be preceeded by `\` , also in line below 
    gsub(/\=/, " & ") # this may cause NF to be updated...
    # Warning -- do not put single quotes in comments!  This does not
    # work reliably in a shell script.
}

($1 in refer && $2 == "=") { # Begin of a bibtex field definition
    gsub(/[{}]/, "")  # deleting curly brackets
    gsub(/,$/, "")    # deleting commas at end of line
    printf "%s ", refer[$1]
    for (i = 3; i <= NF; i++) { # Loop over the keywords ($2 is "=")
	printf " %s", $i        # and print them
    }
    printf "\n" # newline on the end of the refer entry
}

(!($1 in refer) && $2 == "=") { #collect unknown keywords
    unknown[$1] = 1
    next
}

/[^	 ]/ {
    if ($2 != "=") { # This is not a first line of an entry
	# In this case, we are dealing with a continuation line.
	gsub(/[{}]/, "")  # deleting curly braces
	gsub(/,$/, "")    # deleting commas at end of line
	printf "   " 
	for (i = 1; i <= NF; i++) { # Loop over all the keywords 
	    printf " %s", $i        # and print them
	}
	printf "\n" # newline on the end of the refer entry
    }
}

END {
	for (a in unknown) {
	    print "------- unknown keyword:    " a 
	}
}
' ${@+"$@"}