提交 11d92bb6 编写于 作者: A Aston Zhang

concat book

上级 c854a2a5
MD="mdd"
CH="ch.md"
[ -e $MD ] && rm -rf $MD
mkdir $MD
# Collect files.
cp index.md $MD/
cp -R img $MD/
for f in chapter*/*; do
......@@ -13,6 +15,7 @@ for f in chapter*/*; do
fi
done
# ipynb to md.
for f in $MD/chapter*/*ipynb; do
base=$(basename $f)
jupyter nbconvert --to markdown $f --output "${base%%.*}.md"
......@@ -20,14 +23,47 @@ for f in $MD/chapter*/*ipynb; do
done
for f in $MD/chapter*/*md; do
dir=$(dirname "$f")
# Remove inner link.
sed -i 's/\[\([^]]*\)\]([^\)]*.md)/\1/' $f
# Refer pdf instead of svg.
sed -i s/\.svg/\.pdf/ $f
# Refer img in the same level.
sed -i 's/\](..\/img/\](img/' $f
if [ "$f" != "$dir/index.md" ]; then
sed -i s/#\ /##\ / $f
fi
done
# Convert svg to pdf.
for f in $MD/img/*svg; do
rsvg-convert -f pdf -o "${f%%.*}.pdf" $f
rm $f
done
# Concat sections in each chapter.
for f in $MD/chapter*/index.md; do
sections=$(python -c 'import mdd_utils; print(mdd_utils.get_sections())' $f)
dir=$(dirname "$f")
chapter=$dir/$CH
cat $f $sections > $chapter
perl -i -0777 -pe 's/```eval_rst[^`]+```//ge' $chapter
done
chapters=$(python -c 'import mdd_utils; print(mdd_utils.get_chapters())' $MD/index.md)
i=1
for chapter in $chapters; do
# Move matplotlib plots outside.
mv $MD/$chapter/*_files $MD/
# Move ch.md to ../ch0x.md
mv $MD/$chapter/$CH $MD/ch$(printf %02d $i).md
rm -rf $MD/$chapter
i=$((i + 1))
done
rm $MD/index.md
# zip files.
[ -e "$MD.zip" ] && rm "$MD.zip"
zip -r "$MD.zip" $MD
[ -e $MD ] && rm -rf $MD
import os
import sys
def get_sections():
assert len(sys.argv) == 2
index_md = sys.argv[1]
dirname = os.path.dirname(index_md)
start = False
sections = []
with open(index_md) as f:
for line in f:
line = line.rstrip().lstrip()
if ':maxdepth:' in line:
start = True
continue
elif line == '```':
break
if start and len(line) > 1:
sections.append(os.path.join(dirname, line + '.md'))
return ' '.join(sections)
def get_chapters():
assert len(sys.argv) == 2
index_md = sys.argv[1]
start = False
chapters = []
with open(index_md) as f:
for line in f:
line = line.rstrip().lstrip()
if ':maxdepth:' in line:
start = True
continue
elif line == '```':
break
if start and len(line) > 1:
chapters.append(line.split('/')[0])
return ' '.join(chapters)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册