# This Makefile has six features:
# 1. `make citation`: Get all bibliographies cited in the article, save as `citation.bib`
# 2. `make pinyin`: Convert Chinese bibliographies to Pinyin for romanization, save as `pinyin.bib`
# 3. `make trans`: Translate the title field of Chinese bibliographies to English, save as `trans.bib`
# 4. `make transall`: Translate all Chinese bibliographies into English, save as `transall.bib`
# 5. `make cnbib`: Merge bibliographies in Pinyin and translated English titles, save as `cnbib.bib`
# 6. `make docx`: Generate the Word document `main.docx` and the auxiliary file `main.md`
# `make` or `make all` will generate all of them
# `make clean` will remove all generated files except `cnbib.bib`
# Extract bibliographies cited in the article as the BibLaTeX format
CITE= pandoc --bibliography bibliography.bib \
--lua-filter getbib.lua \
--wrap none \
--to biblatex sections/*.md |.PHONY:allall:citationpinyintranscnbibdocx# Extract all bibliographies cited in the article as `citation.bib`
citation:$(CITE) perl -0777 -pe 's/(\}\n)(@)/\1\n\2/g'\
> citation.bib
# Extract Chinese bibliographies and then convert to Pinyin
pinyin:$(CITE) perl -0777 -pe \
's/@[\w\n\s\p{P}—=“”‘’]*\}\n\}//g'|\
xargs pinyin -s NORMAL -S -g |\
perl -pe 's/ ,/,/g; s/ 。/./g; s/ ;/;/g; \
s/ :/: /g; s/ !/!/g; s/ ?/?/g; \
s/( /(/g; s/ )/)/g; s/{ /{/g; s/ }/}/g'|\
perl -pe 's/(\})\s(@)/\n\1\n\n\2/g; \
s/(author\s.*?)\s(\w+\})/\1\2/g; \
s/(author\s=\s\{)(\w+)\s(.*)/\1\u\2, \u\3/g; \
s/(address\s=\s\{+)(\w+)(\}+)/\1\u\2\3/g; \
s/(,)\s([a-z]+\s=)/\1\n \2/g; \
s/(\}$$)/\n\1/g'\
> pinyin.bib
# Translate all Chinese bibliographies into English
# with Apple Translate, can be used offline, and
# require to create a Shortcuts named `Translate`
transall:$(CITE) perl -0777 -pe \
's/@[\w\n\s\p{P}—=“”‘’]*\}\n\}//g'|\
shortcuts run Translate |\
awk 'NF'| perl -pe '$$_=lc; s/(\w+\s=)/ \1/g; \
s/(author\s=\s\{)(\w+)\s(.*)/\1\u\2, \u\3/g; \
s/(address\s=\s\{+)(\w+)(\}+)/\1\u\2\3/g'|\
perl -0777 -pe 's/(\}\n)(@)/\1\n\2/g'\
> transall.bib
# Considering the slowness, only translate the title field
trans:$(CITE) perl -0777 -pe \
's/@[\w\n\s\p{P}—=“”‘’]*\}\n\}//g; \
s/(\})(\n@)/\1\n\2/g; s/(\stitle\s=\s\{)(.*)(\},*)/\2/g; \
s/.*[=@].*//g; s/\}//g'|\
perl -ne 'print if $$. > 1 || /\S/'|\
trans zh-CN:en -b > trans.bib
# Combine `pinyin.bib` and `trans.bib` line by line
cnbib: paste -d "\0" pinyin.bib trans.bib |\
perl -pe 's/(\stitle.*)(\},*)(.*)/\1 \(\3\)\2/g'\
> cnbib.bib
# Generate the Word document `main.docx` and the auxiliary file `main.md`
.SILENT:docx:$(shell ./make.sh)# Remove all generated files except `cnbib.bib`
.PHONY:cleanclean: rm -f citation.bib pinyin.bib trans.bib transall.bib main.md *.docx