-
Notifications
You must be signed in to change notification settings - Fork 23
204 lines (182 loc) · 7.31 KB
/
generate-pdf.yml
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
name: Generate PDF for Textbook
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build_and_generate_pdf:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Install Pandoc, Latex necessities, and pdftk
run: |
sudo apt-get install -y pandoc texlive texlive-latex-extra poppler-utils texlive-extra-utils pdftk
- name: Install Python
uses: actions/setup-python@v4
with:
python-version: '3.11'
- name: Generate PDFs from the textbook Markdown files
run: |
mkdir -p pdf_output
file_list=(
"index.md"
"principles/index.md"
"principles/principles.md"
"memory-safety/index.md"
"memory-safety/x86.md"
"memory-safety/vulnerabilities.md"
"memory-safety/mitigations.md"
"crypto/index.md"
"crypto/intro.md"
"crypto/symmetric.md"
"crypto/hashes.md"
"crypto/macs.md"
"crypto/prng.md"
"crypto/key-exchange.md"
"crypto/public-key.md"
"crypto/signatures.md"
"crypto/certificates.md"
"crypto/passwords.md"
"crypto/case-studies.md"
"crypto/bitcoin.md"
"web/index.md"
"web/sqli.md"
"web/intro.md"
"web/sop.md"
"web/cookies.md"
"web/csrf.md"
"web/xss.md"
"web/ui-attacks.md"
"web/captchas.md"
"network/index.md"
"network/intro.md"
"network/arp.md"
"network/dhcp.md"
"network/wpa.md"
"network/bgp.md"
"network/transport.md"
"network/tls.md"
"network/dns.md"
"network/dnssec.md"
"network/dos.md"
"network/firewalls.md"
"network/intrusion-detection.md"
"network/abusing-intrusion-detection.md"
"network/malware.md"
"network/tor.md"
)
for md_file in "${file_list[@]}"; do
pdf_file_name="pdf_output/$(echo "$md_file" | sed 's/\//_/g' | sed 's/.md//g').pdf"
python3 pdf-generation/generate-pdf-edits.py "$md_file" | pandoc -V geometry:margin=1in -V colorlinks=true -V linkcolor=blue -V urlcolor=blue --pdf-engine=pdflatex -o "$pdf_file_name"
if [ ! -f "$pdf_file_name" ]; then
echo "Error: PDF file $pdf_file_name not created!" >&2
exit 1
fi
done
pdftk \
"pdf_output/index.pdf" \
"pdf_output/principles_index.pdf" \
"pdf_output/principles_principles.pdf" \
"pdf_output/memory-safety_index.pdf" \
"pdf_output/memory-safety_x86.pdf" \
"pdf_output/memory-safety_vulnerabilities.pdf" \
"pdf_output/memory-safety_mitigations.pdf" \
"pdf_output/crypto_index.pdf" \
"pdf_output/crypto_intro.pdf" \
"pdf_output/crypto_symmetric.pdf" \
"pdf_output/crypto_hashes.pdf" \
"pdf_output/crypto_macs.pdf" \
"pdf_output/crypto_prng.pdf" \
"pdf_output/crypto_key-exchange.pdf" \
"pdf_output/crypto_public-key.pdf" \
"pdf_output/crypto_signatures.pdf" \
"pdf_output/crypto_certificates.pdf" \
"pdf_output/crypto_passwords.pdf" \
"pdf_output/crypto_case-studies.pdf" \
"pdf_output/crypto_bitcoin.pdf" \
"pdf_output/web_index.pdf" \
"pdf_output/web_sqli.pdf" \
"pdf_output/web_intro.pdf" \
"pdf_output/web_sop.pdf" \
"pdf_output/web_cookies.pdf" \
"pdf_output/web_csrf.pdf" \
"pdf_output/web_xss.pdf" \
"pdf_output/web_ui-attacks.pdf" \
"pdf_output/web_captchas.pdf" \
"pdf_output/network_index.pdf" \
"pdf_output/network_intro.pdf" \
"pdf_output/network_arp.pdf" \
"pdf_output/network_dhcp.pdf" \
"pdf_output/network_wpa.pdf" \
"pdf_output/network_bgp.pdf" \
"pdf_output/network_transport.pdf" \
"pdf_output/network_tls.pdf" \
"pdf_output/network_dns.pdf" \
"pdf_output/network_dnssec.pdf" \
"pdf_output/network_dos.pdf" \
"pdf_output/network_firewalls.pdf" \
"pdf_output/network_intrusion-detection.pdf" \
"pdf_output/network_abusing-intrusion-detection.pdf" \
"pdf_output/network_malware.pdf" \
"pdf_output/network_tor.pdf" \
cat output "textbook_full_original.pdf"
- name: Inject page numbers back into the pdf
run: |
mkdir -p pdf_numbering_output
num_pages=$(pdftk textbook_full_original.pdf dump_data | grep NumberOfPages | awk '{print $2}')
sed -i "s/161/$num_pages/" pdf-generation/numbering.tex
# Don't continue if the sed failed
if [ $? -ne 0 ]; then
echo "Error: sed command failed" >&2
exit 1
fi
pdflatex pdf-generation/numbering.tex
pdftk numbering.pdf burst output pdf_numbering_output/number_%03d.pdf
pdftk textbook_full_original.pdf burst output pdf_numbering_output/page_%03d.pdf
for i in $(seq -f %03g 1 $num_pages) ; do pdftk pdf_numbering_output/page_$i.pdf background pdf_numbering_output/number_$i.pdf output pdf_numbering_output/new_$i.pdf; done
pdftk pdf_numbering_output/new_???.pdf output textbook_full.pdf
cp textbook_full.pdf textbook_full_cpy.pdf
sed -i "s/$num_pages/161/" pdf-generation/numbering.tex
- name: Upload PDF as an artifact
uses: actions/upload-artifact@v4
with:
name: textbook-full-copy
path: textbook_full_cpy.pdf
- name: Remove temporary files
run: |
rm -rf pdf_output
rm -rf pdf_numbering_output
rm textbook_full_original.pdf
rm textbook_full_cpy.pdf
rm numbering.pdf
rm numbering.aux
- name: Check if PDF files are created
run: |
if [ -f "textbook_full.pdf" ]; then echo "textbook_full.pdf here"; else echo "textbook_full.pdf gone"; fi
- name: Check git status
run: git status
- name: Check git diff
run: git diff --stat
- name: Commit changes to a new branch
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "GitHub Actions"
git checkout -b update-textbook-full-pdf
git add -f --renormalize textbook_full.pdf
git commit -m "Update PDF of textbook on site after a merge to main"
git push -f origin update-textbook-full-pdf
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Create a Pull Request
uses: peter-evans/create-pull-request@v5
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: "Update PDF of textbook on site"
branch: update-textbook-full-pdf
title: "Update full PDF of textbook"
body: "This PR updates the full PDF of the textbook."
base: main