aboutsummaryrefslogtreecommitdiff
path: root/bin/compile-book
blob: 1c57b2eef6da3a7d8e55f29f7beb108fe64bf7b9 (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
#!/usr/bin/env bash
#
# Book compiler
#

# Parameters
BASENAME="`basename $0 | sed -e 's/\(.\)/\U\1/'`"
DIRNAME="`dirname $0`"
BASEDIR="$DIRNAME/.."
CONTENT="content/sections"
OUTPUT="${1:-book}"
REVISION="$2"
DATE="$3"
LANG="`grep lang _common.yml _book.yml _notes.yml 2> /dev/null | tail -1 | cut -d : -f 3 | sed -e 's/"//g' -e 's/ //g'`"

# Set the language
if [ -z "$LANG" ]; then
  LANG="en"
fi

# Update _output.yml with language information
sed -e "s|%%lang%%|$LANG|g" $BASEDIR/templates/_output.yml > _output.yml

# Set structure and templates
STRUCTURE="structure/book/$LANG"
TEMPLATES="templates/book/$LANG"

# Remove any dangling output files
rm -f $OUTPUT.md $OUTPUT.Rmd

# Bookup config
cat _book.yml   >> $OUTPUT.md ; echo "" >> $OUTPUT.md
cat _common.yml >> $OUTPUT.md ; echo "" >> $OUTPUT.md

# Bibliography
$BASEDIR/bin/biblio-yml  > _biblio.yml
cat _biblio.yml         >> $OUTPUT.md

# YAML headers
for file in $BASEDIR/$STRUCTURE/00*.yml; do
  cat $file >> $OUTPUT.md
  echo "---" >> $OUTPUT.md
done

# Markdown headers
for file in $BASEDIR/$STRUCTURE/00*.md; do
  cat $file >> $OUTPUT.md
  echo "" >> $OUTPUT.md
done

# Sections
if [ -d "$CONTENT" ]; then
  find $CONTENT -type f | grep '\.md$' | sort | while read file; do
    cat $file >> $OUTPUT.md
    echo "" >> $OUTPUT.md
    cat $BASEDIR/$TEMPLATES/references.md >> $OUTPUT.md
    echo "" >> $OUTPUT.md
  done
fi

# Footers
for file in $BASEDIR/$STRUCTURE/99*.md; do
  cat $file >> $OUTPUT.md
  echo "" >> $OUTPUT.md
done

# Revision information
sed -i -e "s|%%revision%%|$REVISION|g" -e "s|%%date%%|$DATE|g" $OUTPUT.md

# Post-processing
if [ -e "snippets/terminology.sed" ]; then
  sed -i -f snippets/terminology.sed $OUTPUT.md
fi

# Symlink as a Bookdown source
ln -sf $OUTPUT.md $OUTPUT.Rmd