aboutsummaryrefslogtreecommitdiffstats
path: root/lib/bib-strip.awk
blob: cecca3ecbd093e84471104d6b822b179cc7a6e51 (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
# bib-strip.awk - remove entries by key, preserving all other bytes
#
# Requires bib-parse.awk. Variables (set with -v):
#   keys    - comma-separated list of entry keys to remove
#   keyfile - file with one key per line, for key lists too large to
#             pass on the command line; merged with keys
#
# Unlike bib-select.awk, which re-emits entries canonically, this
# splices the matched entries' source spans out of the input and
# leaves everything else - comments, formatting, @string blocks -
# byte-for-byte intact. Used by bib-add -f so that replacing one
# entry never rewrites the rest of the database.
#
# This END block runs after bib-parse.awk's (END blocks execute in
# the order their files are given to awk), so the spans recorded by
# the hooks below are complete by the time output happens.

BEGIN {
  bib_strip_n = split(keys, bib_strip_k, ",")
  for (bib_strip_i = 1; bib_strip_i <= bib_strip_n; bib_strip_i++)
    BIB_DROP[bib_strip_k[bib_strip_i]] = 1
  if (keyfile != "") {
    while ((getline bib_strip_line < keyfile) > 0)
      BIB_DROP[bib_strip_line] = 1
    close(keyfile)
  }
}

function bib_pass(raw) { }

function bib_entry(type, key) {
  if (key in BIB_DROP) {
    BIB_NSPAN++
    BIB_SPAN_S[BIB_NSPAN] = BIB_START
    BIB_SPAN_E[BIB_NSPAN] = BIB_END
  }
}

END {
  i = 1
  for (j = 1; j <= BIB_NSPAN; j++) {
    printf "%s", substr(bib_buf, i, BIB_SPAN_S[j] - i)
    i = BIB_SPAN_E[j]
    # swallow the whitespace that followed the removed entry
    while (i <= length(bib_buf) && substr(bib_buf, i, 1) ~ /[ \t\r\n]/)
      i++
  }
  printf "%s", substr(bib_buf, i)
}