Ian Jauslin
summaryrefslogtreecommitdiff
blob: b10d6c388cffe4d5024189e1907e1b7f1d4f5cab (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
base_SQL="SELECT title,author,journal,year,token,doi,arxiv,citeref,prauth,bibtex FROM bibliography"
base_SQL_noprauth="SELECT title,author,journal,year,token,doi,arxiv,citeref,bibtex FROM bibliography"

# add quotes
function SQL_addquotes {
  while read -r line; do
    # add quotes and escape string
    if [ -n "$line" ]; then
      line=$(escape_string "$line")
      line="${line/%=/= }"
      line="${line/ = /=\"}"
      line="${line/%/\"}"
    fi
    echo "$line"
  done
}

# SQL command for alphabetical ordering
function generate_SQL_alpha {
  echo -n "$base_SQL WHERE citeref='"

  # whether there is a citeref
  exists_citeref=0

  # sift through aux file
  grep -h "$aux_cmd" $aux | while read -r citerefs; do
    eval "citerefs=\${citerefs#$aux_cmd}"
    citerefs="${citerefs%\}}"

    # can be a comma separated list
    for citeref in $(echo -n "$citerefs" | tr ',' '\n'); do

      # replace the citeref with a ref_map if there is any
      if [ -n "$ref_map" ]; then
	citeref=$(map_citeref "$citeref")
      fi

      # Only query the db if there is no matching extra entry
      foundit=0
      if [ ${#extra} -gt 0 ]; then
	for entry in "${extra[@]}"; do
	  ref="${entry#*:}"
	  ref="${ref%%:*}"
	  if [ "$ref" = "$citeref" ]; then
	    foundit=1
	    break
	  fi
	done
      fi
      
      if [ "$foundit" = 0 ]; then
	echo -n "$citeref' OR citeref='"
	exists_citeref=1
      fi
    done
  done

  # if there are no citerefs close '
  [ $exists_citeref = 0 ] && echo -n "'"
}

# fetch entries by alphabetical order
function fetch_BBlog_entries_alpha {
  SQL="$(generate_SQL_alpha)"
  SQL="${SQL% OR citeref=\'} ORDER BY prauth,year"
  sqlite3 -line "$db" "$SQL" | SQL_addquotes
  # add an empty line
  echo ""
}

# fetch entries by order of appearance
function fetch_BBlog_entries_appearance {
  # keep track of citerefs to avoid repetitions
  citeref_list=""

  # sift through aux file
  grep -h "$aux_cmd" $aux | while read -r citerefs; do
    eval "citerefs=\${citerefs#$aux_cmd}"
    citerefs="${citerefs%\}}"

    # can be a comma separated list
    for citeref in $(echo -n "$citerefs" | tr ',' '\n'); do

      # replace the citeref with a ref_map if there is any
      if [ -n "$ref_map" ]; then
	citeref=$(map_citeref "$citeref")
      fi

      # check whether the reference was already cited
      citeref_list="$citeref_list;"
      if $(echo "$citeref_list" | grep -q ";$citeref;"); then
	# remove trailing ';'
	citeref_list="${citeref_list%;}"
      else
	citeref_list="$citeref_list$citeref"

	# Only query the db if there is no matching extra entry
	foundit=0
	if [ ${#extra} -gt 0 ]; then
	  for entry in "${extra[@]}"; do
	    ref="${entry#*:}"
	    ref="${ref%%:*}"
	    if [ "$ref" = "$citeref" ]; then
	      foundit=1
	      break
	    fi
	  done
	fi
	
	if [ "$foundit" = 0 ]; then
	  SQL="$base_SQL_noprauth WHERE citeref='$citeref'"
	  sqlite3 -line "$db" "$SQL" | SQL_addquotes
	else
	  echo "citeref=$citeref"
	fi

	# add an empty line
	echo ""
      fi
    done
  done
}