forked from gbif/gbif-asciidoctor-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild
executable file
·224 lines (195 loc) · 7.6 KB
/
build
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/bin/bash -e
# Run:
# docker run --rm -it --user $(id -u):$(id -g) -v $PWD:/documents/ gbif/asciidoctor-toolkit:latest
# Enable the **/*.$PRIMARY_LANGUAGE.adoc below.
shopt -s globstar
# In case there are no translations.
shopt -s nullglob
if [[ -t 0 ]]; then
RED='\033[1;31m'
NC='\033[0m'
else
RED='▶▶▶ '
NC=''
fi
echo
echo -e "${RED}Primary language is $PRIMARY_LANGUAGE.${NC}"
# Check this is run from a reasonable directory.
# (Advanced users can run /bin/bash as a Docker target command.)
if [[ ! -e index.$PRIMARY_LANGUAGE.adoc ]]; then
echo >&2 "There is no index.$PRIMARY_LANGUAGE.adoc file in this directory."
echo >&2
echo >&2 "Check you are running this from the top-level of your document,"
echo >&2 "and that there is an $PRIMARY_LANGUAGE source document called index.$PRIMARY_LANGUAGE.adoc."
exit 1
fi
# Clean any old files
echo "Removing files ignored by Git"
git clean -f -X
# Set timestamp on index.$PRIMARY_LANGUAGE.adoc to latest commit time (shown in document footer)
touch --no-create --date @$(git rev-list --max-count=1 --pretty=%ct HEAD | tail -n 1) index.$PRIMARY_LANGUAGE.adoc
echo
# Produce the translated adoc source from the po-files.
if [[ -e po4a.conf ]]; then
echo -e "${RED}Translating sources${NC}"
for pri_adoc in *.$PRIMARY_LANGUAGE.adoc; do
grep -q $pri_adoc po4a.conf || echo >&2 "WARNING: Source file $pri_adoc not present in the translation configuration (po4a.conf)"
done
# Set up for a newly translatable document if necessary.
mkdir -p translations/
[[ -e translations/index.pot ]] || touch translations/index.pot
# --force is used because Git usage has probably made timestamps unrelated to actual modification time.
po4a -v po4a.conf --force
# If index.pot has only one changed line, it's the "POT-Creation-Date", and we don't want to commit that.
if [[ $(git diff --numstat translations/index.pot | cut -f 1) -eq 1 ]]; then
echo "No change to index.pot"
git checkout translations/index.pot
else
git add translations/index.pot
fi
for lang in translations/??.po translations/??-??.po translations/??-???.po; do
langcode=$(basename $lang .po)
# Replace include and image links to translated alternatives
for tdoc in $(grep -l -E -e '\.[a-z][a-z]\.adoc' **/*.$langcode.adoc); do
echo "Replacing includes in $tdoc for $langcode"
perl -pi -e 's/([A-Za-z0-9_-]+).'$PRIMARY_LANGUAGE'.adoc/\1.'$langcode'.adoc/' $tdoc
done
done
echo "Translating source files completed"
else
echo "No po4a.conf exists, this document will not be translated"
fi
echo
# Spell-check the document using Aspell
if [[ -e index.en.adoc ]]; then
echo -e "${RED}Checking document spelling (Oxford English dictionary)${NC}"
echo "(NB many names and technical terms are not in the dictionary.)"
set +e
for d in *.en.adoc; do
echo "Checking $d…"
# Lowercase words
aspell --ignore=3 -d en_GB-ize-w_accents -p /adoc/asciidoc.dict list < $d | grep -v '[A-Z]' | sed 's/^/ /'
# First letter uppercase
aspell --ignore=3 -d en_GB-ize-w_accents -p /adoc/asciidoc.dict list < $d | grep '^[A-Z]' | sed 's/^/ /'
# Subsequent letter uppercase
aspell --ignore=3 -d en_GB-ize-w_accents -p /adoc/asciidoc.dict list < $d | grep '^[^A-Z].*[A-Z]' | sed 's/^/ /'
done
set -e
fi
echo
improve_url=https://docs.gbif.org/contributing/
if git remote get-url origin &> /dev/null; then
contribute_url=https://github.com/gbif/$(git remote get-url origin | rev | cut -d/ -f1 | rev | cut -d. -f1)
issue_url=$contribute_url/issues/new
else
contribute_url=
issue_url=
fi
# Generate the output HTML and PDF.
rm -f **/*.??.html **/*.??.pdf *.??.asis \
**/*.??-??.html **/*.??-??.pdf *.??-??.asis \
**/*.??-???.html **/*.??-???.pdf *.??-???.asis
for lang in $PRIMARY_LANGUAGE translations/??.po translations/??-??.po translations/??-???.po; do
langcode=$(basename $lang .po)
echo -e "${RED}Building document in language $langcode${NC}"
mkdir -p $langcode
# Document title for PDF filename
title=$(grep -m 1 '^=[^=]' index.$langcode.adoc | rev | cut -d: -f2- | rev | unidecode | tr '[:upper:]`~!@"#$%^&*(){}[]/=?+_<>,.\| '"'" '[:lower:]-----------------------------'"-" | sed 's/--*/-/g; s/^-*//; s/-*$//;')
echo "Document title in $langcode is “$title”"
# Only add the BibTeX plugin if required
if grep -q :bibtex-file: index.$langcode.adoc; then
bibtex_plugin="-r asciidoctor-bibtex"
else
bibtex_plugin=
fi
echo -e "${RED}Generating $langcode HTML${NC}"
asciidoctor \
-a icons=font \
-a lang=$langcode \
-a imagesdir=../ \
-a sectanchors \
-a idprefix=@ \
-a idseparator=-@ \
-a pdf_filename=$title.$langcode.pdf \
-a improve_url=$improve_url@ \
-a contribute_url=$contribute_url@ \
-a issue_url=$issue_url@ \
-a stylesheet=/adoc/gbif-stylesheet/stylesheets/gbif.css@ \
-a google-analytics-account=UA-42057855-10 \
-r asciidoctor-diagram \
$bibtex_plugin \
-r asciidoctor-question \
-r /adoc/asciidoctor-extensions-lab/lib/git-metadata-preprocessor.rb \
-r /adoc/asciidoctor-extensions-lab/lib/google-analytics-docinfoprocessor.rb \
-r /adoc/gbif-extensions/lib/glossary.rb \
-r /adoc/gbif-extensions/lib/license-url-docinfoprocessor.rb \
-r /adoc/gbif-extensions/lib/term.rb \
-r /adoc/gbif-extensions/lib/translate-labels.rb \
-r /adoc/gbif-extensions/lib/translation-links.rb \
-r /adoc/GbifHtmlConverter.rb \
-T /adoc/gbif-templates/ \
-o $langcode/index.$langcode.html \
--trace \
--verbose \
index.$langcode.adoc
if [[ -n $SKIP_PDF ]]; then
echo -e "${RED}Not generating $langcode PDF${NC}"
else
brotli --keep --force --output=$langcode/index.$langcode.html.brotli --best $langcode/index.$langcode.html &
echo -e "${RED}Generating $langcode PDF${NC}"
asciidoctor-pdf \
-a icons=font \
-a pagenums \
-a lang=$langcode \
-a idprefix=@ \
-a idseparator=-@ \
-a title-page \
-a pdf-theme=/adoc/gbif-theme/gbif-theme.yml \
-a pdf-fontsdir='/adoc/fonts;GEM_FONTS_DIR' \
-r asciidoctor-diagram \
$bibtex_plugin \
-r asciidoctor-question \
-r /adoc/asciidoctor-extensions-lab/lib/git-metadata-preprocessor.rb \
-r /adoc/gbif-extensions/lib/glossary.rb \
-r /adoc/gbif-extensions/lib/term.rb \
-r /adoc/gbif-extensions/lib/translate-labels.rb \
-o $langcode/$title.$langcode.pdf \
--trace \
--verbose \
index.$langcode.adoc
brotli --verbose --keep --force --output=$langcode/$title.$langcode.pdf.brotli --best $langcode/$title.$langcode.pdf &
fi
cat > index.$langcode.asis <<-EOF
Status: 303 See Other
Location: ./$langcode/
Content-Type: text/html; charset=UTF-8
Content-Language: $langcode
Vary: negotiate,accept-language
See <a href="./$langcode/">$langcode</a>.
EOF
echo "$langcode completed"
echo
done
if [[ -n $SKIP_PDF ]]; then
echo -e "${RED}Not compressing files${NC}"
else
echo -e "${RED}Compressing files${NC}"
echo "Compressing SVG files with Brotli compression"
find . -type f -iname '*.svg' -exec brotli --verbose --keep --force --suffix=.brotli --best '{}' \;
echo "Compressing PNG files to WebP format"
find . -type f -iname '*.png' -exec cwebp -short -near_lossless 10 -o '{}.webp' '{}' \;
echo "Compressing JPG files to WebP format"
find . -type f -iname '*.jp*g' -exec cwebp -short -o '{}.webp' '{}' \;
echo "Waiting for HTML and PDF compression (with Brotli) to complete"
fi
echo
wait
echo -e "${RED}Document build completed.${NC}"
echo
# Make translation template
# po4a-gettextize -f asciidoc -M utf-8 -m index.adoc -p translations/index.pot
# Update translation
# po4a-updatepo -f asciidoc -M utf-8 -m index.adoc -p translations/da.po
# po4a-normalize -f asciidoc -M utf-8 translations/da.po
# Translate
# po4a-translate -f asciidoc -M utf-8 -m index.adoc -p translations/da.po -k 0 -l index.da.adoc