aboutsummaryrefslogtreecommitdiffstats
path: root/lib/ref2bib.awk
blob: 422fdd71b19004fd92db83a3b4754e99ca55df86 (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
# 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
}

{
  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

  authors = ""
  for (i = 1; i <= na; i++)
    authors = (i == 1) ? A[i] : authors " and " A[i]
  r_emit("author", authors)
  editors = ""
  for (i = 1; i <= ne; i++)
    editors = (i == 1) ? E[i] : editors " and " E[i]
  r_emit("editor", editors)

  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 "}"
}