Repository navigation
Expand file tree
/
Copy pathbuild_epub.sh
More file actions
executable file
·179 lines (166 loc) · 6.86 KB
/
Copy pathbuild_epub.sh
File metadata and controls
executable file
·179 lines (166 loc) · 6.86 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
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
#!/usr/bin/env bash
# Build EPUB 3 editions from the Markdown sources.
# Usage: ./build_epub.sh [all|zh-CN|zh-TW|en|ru|ta|vi|ja|ar]
# Note: `all` does NOT include ja or ar while their PDF pipelines are being
# validated. Build them explicitly with `./build_epub.sh ja|ar`.
set -euo pipefail
ROOT="$(cd "$(dirname "$0")" && pwd)"
SELECTION="${1:-all}"
for command in pandoc pdftoppm python3; do
if ! command -v "$command" >/dev/null 2>&1; then
echo "Error: $command is required." >&2
exit 1
fi
done
case "$SELECTION" in
all|zh-CN|zh-TW|en|ru|ta|vi|ja|ar) ;;
*)
echo "Usage: $0 [all|zh-CN|zh-TW|en|ru|ta|vi|ja|ar]" >&2
exit 2
;;
esac
TMP_DIR="$(mktemp -d "${TMPDIR:-/tmp}/ai-agent-book-epub.XXXXXX")"
trap 'rm -rf "$TMP_DIR"' EXIT
build_edition() {
local language="$1"
local directory title author pdf output title_label toc_label
local -a chapters
case "$language" in
zh-CN)
directory="book"
title="深入理解 AI Agent:设计原理与工程实践"
author="李博杰"
pdf="深入理解-AI-Agent-李博杰-v1.3.pdf"
output="深入理解-AI-Agent-李博杰-v1.3.epub"
title_label="扉页"
toc_label="目录"
chapters=(introduction.md chapter{1..10}.md afterword.md)
;;
zh-TW)
directory="book-zhtw"
title="深入理解 AI Agent:設計原理與工程實踐"
author="李博杰;正體中文翻譯:tigercosmos"
pdf="深入理解-AI-Agent-李博杰-v1.3-zhtw.pdf"
output="深入理解-AI-Agent-李博杰-v1.3-zhtw.epub"
title_label="扉頁"
toc_label="目錄"
chapters=(introduction.zhtw.md chapter{1..10}.zhtw.md afterword.zhtw.md)
;;
en)
directory="book-en"
title="AI Agents in Depth: Design Principles and Engineering Practice"
author="Bojie Li; English translation: Devaraj"
pdf="AI-Agents-in-Depth-Bojie-Li-v1.3.pdf"
output="AI-Agents-in-Depth-Bojie-Li-v1.3.epub"
title_label="Title Page"
toc_label="Table of Contents"
chapters=(introduction.md chapter{1..10}.md afterword.md)
;;
ru)
directory="book-ru"
title="Глубокое понимание AI Agent: принципы проектирования и инженерная практика"
author="Ли Боцзе (李博杰); русский перевод: ui99ru"
pdf="AI-Agents-in-Depth-ru.pdf"
output="AI-Agents-in-Depth-ru.epub"
title_label="Титульный лист"
toc_label="Содержание"
chapters=(introduction.md chapter{1..10}.md afterword.md)
;;
ta)
directory="book-ta"
title="AI Agents ஆழத்தில்: வடிவமைப்பு கோட்பாடுகள் மற்றும் பொறியியல் நடைமுறைகள்"
author="Bojie Li; தமிழ் மொழிபெயர்ப்பு: Devaraj"
pdf="AI-Agents-in-Depth-Bojie-Li-v1.3-ta.pdf"
output="AI-Agents-in-Depth-Bojie-Li-v1.3-ta.epub"
title_label="தலைப்புப் பக்கம்"
toc_label="பொருளடக்கம்"
chapters=(introduction.ta.md chapter{1..10}.ta.md afterword.ta.md)
;;
vi)
directory="book-vi"
title="Hiểu sâu về AI Agent: Nguyên lý thiết kế và thực hành kỹ thuật"
author="Lý Bác Kiệt; bản dịch tiếng Việt: Toàn Nguyễn"
pdf="AI-Agents-in-Depth-Bojie-Li-v1.3-vi.pdf"
output="AI-Agents-in-Depth-Bojie-Li-v1.3-vi.epub"
title_label="Trang tiêu đề"
toc_label="Mục lục"
chapters=(introduction.vi.md glossary.vi.md chapter{1..10}.vi.md afterword.vi.md)
;;
ja)
directory="book-ja"
title="AI Agent 徹底解説:設計原理とエンジニアリング実践"
author="李博杰;日本語訳:Ikko Eltociear Ashimine"
pdf="AI-Agents-in-Depth-Bojie-Li-v1.3-ja.pdf"
output="AI-Agents-in-Depth-Bojie-Li-v1.3-ja.epub"
title_label="扉"
toc_label="目次"
chapters=(introduction.ja.md chapter{1..10}.ja.md afterword.ja.md)
;;
ar)
directory="book-ar"
title="فهم وكلاء الذكاء الاصطناعي بعمق: مبادئ التصميم والممارسة الهندسية"
author="لي بوجي؛ الترجمة العربية: TheSyBuilder"
pdf="AI-Agents-in-Depth-ar.pdf"
output="AI-Agents-in-Depth-ar.epub"
title_label="صفحة العنوان"
toc_label="المحتويات"
chapters=(introduction.ar.md chapter{1..10}.ar.md afterword.ar.md)
;;
esac
local edition_dir="$ROOT/$directory"
local -a direction_args=()
if [ "$language" = "ar" ]; then
direction_args=(--metadata dir=rtl)
fi
local chapter
for chapter in "${chapters[@]}" "$pdf"; do
if [ ! -f "$edition_dir/$chapter" ]; then
echo "Error: $directory/$chapter not found." >&2
exit 1
fi
done
local cover="$TMP_DIR/cover-$language.jpg"
pdftoppm -f 1 -singlefile -jpeg -r 160 \
"$edition_dir/$pdf" "${cover%.jpg}"
echo "Building $language EPUB..."
(
cd "$edition_dir"
pandoc "${chapters[@]}" \
-o "$output" \
--from markdown+lists_without_preceding_blankline \
--to epub3 \
--standalone \
--toc \
--toc-depth=3 \
--number-sections \
--mathml \
--split-level=1 \
--highlight-style=kate \
--css="$ROOT/epub.css" \
--epub-cover-image="$cover" \
--metadata title="$title" \
--metadata author="$author" \
--metadata lang="$language" \
"${direction_args[@]}" \
--metadata identifier="https://github.com/bojieli/ai-agent-book#$language"
)
if [ "$language" = "ar" ]; then
python3 "$ROOT/flatten_epub_toc.py" \
"$edition_dir/$output" "$title_label" "$toc_label" rtl
else
python3 "$ROOT/flatten_epub_toc.py" \
"$edition_dir/$output" "$title_label" "$toc_label"
fi
if command -v epubcheck >/dev/null 2>&1; then
epubcheck "$edition_dir/$output"
else
echo "Built $directory/$output (install epubcheck to validate it)."
fi
}
if [ "$SELECTION" = "all" ]; then
for language in zh-CN zh-TW en ru ta vi; do
build_edition "$language"
done
else
build_edition "$SELECTION"
fi