aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ref2bib.awk
blob: 9384677bbbb314be1eead519fe4cc0961472604d (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
# ref2bib.awk - convert refer records to bibtex entries
#
# Standalone (does not use bib-parse.awk). Records are separated by
# blank lines. Output keys are FIXME; pipe through bib-key.

BEGIN {
  RS = ""
  FS = "\n"
}

function r_trim(t) {
  sub(/^[ \t\r]+/, "", t)
  sub(/[ \t\r]+$/, "", t)
  return t
}

function r_emit(name, v) {
  if (v != "")
    printf "  %s = {%s},\n", name, v
}

# join arr[1..n] with " and ", as bibtex name lists expect
function r_join(arr, n,    s, i) {
  s = arr[1]
  for (i = 2; i <= n; i++)
    s = s " and " arr[i]
  return s
}

{
  split("", val)
  na = 0
  ne = 0
  split("", A)
  split("", E)
  lasttag = ""
  for (i = 1; i <= NF; i++) {
    line = $i
    if (substr(line, 1, 1) == "%") {
      tag = substr(line, 2, 1)
      v = r_trim(substr(line, 3))
      if (tag == "A")
        A[++na] = v
      else if (tag == "E")
        E[++ne] = v
      else
        val[tag] = v
      lasttag = tag
    } else if (lasttag == "A")
      A[na] = A[na] " " r_trim(line)
    else if (lasttag == "E")
      E[ne] = E[ne] " " r_trim(line)
    else if (lasttag != "")
      val[lasttag] = val[lasttag] " " r_trim(line)
  }
  if (na == 0 && ne == 0 && !("T" in val))
    next

  # guess an entry type from the fields present
  if ("J" in val)
    type = "article"
  else if ("B" in val)
    type = (val["B"] ~ /[Pp]roceedings|[Cc]onference|[Ss]ymposium|[Ww]orkshop/) \
        ? "inproceedings" : "incollection"
  else if ("R" in val)
    type = "techreport"
  else if ("I" in val)
    type = "book"
  else
    type = "misc"

  if (out_n++)
    print ""
  printf "@%s{FIXME,\n", type

  if (na > 0)
    r_emit("author", r_join(A, na))
  if (ne > 0)
    r_emit("editor", r_join(E, ne))

  r_emit("title", val["T"])
  r_emit("journal", val["J"])
  r_emit("booktitle", val["B"])

  d = val["D"]
  if (match(d, /[0-9][0-9][0-9][0-9]/)) {
    r_emit("year", substr(d, RSTART, 4))
    m = r_trim(substr(d, 1, RSTART - 1) substr(d, RSTART + 4))
    if (m != "")
      r_emit("month", m)
  } else
    r_emit("year", d)

  r_emit("volume", val["V"])
  r_emit("number", val["N"])
  p = val["P"]
  gsub(/-+/, "--", p)
  r_emit("pages", p)
  r_emit(type == "techreport" ? "institution" : "publisher", val["I"])
  r_emit("address", val["C"])
  if ("R" in val) {
    if ("N" in val)
      r_emit("note", val["R"])
    else
      r_emit("number", val["R"])
  }
  r_emit("keywords", val["K"])
  r_emit("abstract", val["X"])
  r_emit("note", val["O"])
  print "}"
}