-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract-bib-from-md.sh
More file actions
executable file
·59 lines (53 loc) · 1.13 KB
/
extract-bib-from-md.sh
File metadata and controls
executable file
·59 lines (53 loc) · 1.13 KB
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
#!/bin/bash
DIR='.'
if [[ ".$1" = '.--dir' ]]
then
DIR="$2"
shift 2
fi
if [[ ".$1" = '.--' ]]
then
shift
fi
if [[ ! -d "${DIR}" ]]
then
echo "Not a directory: [${DIR}]" >&2
exit 1
fi
( cat "$@" ; echo ) \
| sed -E \
-e '/^[*] [*]/!d' \
-e 's/^[*] [*]//' \
-e 's/[*], */|/' \
-e 's/, *([ 0-9]*)$/|\1/' \
-e 's/[|]([^| ][^|A-Z]* ([^|, ]*))/|\2#\1/' \
-e 's/[|]([^|#]*)[|]/|\1#\1|/' \
-e 's/#/|/' \
| (
IFS='|'
while read -r TITLE PREFIX AUTHORS YEAR
do
CODE="${PREFIX}${YEAR}"
AUTHOR_LIST="$(echo "${AUTHORS}" | sed -e 's/ and /,/' | tr ',' '\012' | sed -e 's/^ */ - "/' -e 's/$/"/')"
read -r -d '' ENTRY <<EOT
---
title: "${TITLE}"
authors:
${AUTHOR_LIST}
year: ${YEAR}
language: "en"
---
*${TITLE}*, ${AUTHORS}, ${YEAR}
EOT
echo "--- Begin entry for ${CODE} ---"
echo "${ENTRY}"
echo "--- End entry for ${CODE} ---"
FILE="${DIR}/${CODE}.md"
echo "FILE: [${FILE}]"
if [[ -f "${FILE}" ]]
then
echo "${ENTRY}" | diff "${FILE}" -
fi
echo "${ENTRY}" > "${FILE}"
done
)