Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pdf build scripts and Github Actions release #173

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions .github/workflows/release-pdf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Release PDF
on:
push:
branches:
- master
paths:
- .github/workflows/release-pdf.yml
- docs/release-notes.md
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Install fonts
run: |
mkdir -p ~/.fonts
mkdir -p ~/.fonts/palatino-linotype
mkdir -p ~/.fonts/source-han-serif
wget https://github.com/dolbydu/font/raw/master/Serif/Palatino/Palatino%20Linotype%20Bold%20Italic.ttf -O ~/.fonts/palatino-linotype/Palatino-Linotype-Bold-Italic.ttf
wget https://github.com/dolbydu/font/raw/master/Serif/Palatino/Palatino%20Linotype%20Bold.ttf -O ~/.fonts/palatino-linotype/Palatino-Linotype-Bold.ttf
wget https://github.com/dolbydu/font/raw/master/Serif/Palatino/Palatino%20Linotype%20Italic.ttf -O ~/.fonts/palatino-linotype/Palatino-Linotype-Italic.ttf
wget https://github.com/dolbydu/font/raw/master/Serif/Palatino/Palatino%20Linotype.ttf -O ~/.fonts/palatino-linotype/Palatino-Linotype.ttf
wget https://github.com/adobe-fonts/source-han-serif/raw/release/SubsetOTF/CN/SourceHanSerifCN-Bold.otf -O ~/.fonts/source-han-serif/SourceHanSerifCN-Bold.otf
wget https://github.com/adobe-fonts/source-han-serif/raw/release/SubsetOTF/CN/SourceHanSerifCN-ExtraLight.otf -O ~/.fonts/source-han-serif/SourceHanSerifCN-ExtraLight.otf
wget https://github.com/adobe-fonts/source-han-serif/raw/release/SubsetOTF/CN/SourceHanSerifCN-Heavy.otf -O ~/.fonts/source-han-serif/SourceHanSerifCN-Heavy.otf
wget https://github.com/adobe-fonts/source-han-serif/raw/release/SubsetOTF/CN/SourceHanSerifCN-Light.otf -O ~/.fonts/source-han-serif/SourceHanSerifCN-Light.otf
wget https://github.com/adobe-fonts/source-han-serif/raw/release/SubsetOTF/CN/SourceHanSerifCN-Medium.otf -O ~/.fonts/source-han-serif/SourceHanSerifCN-Medium.otf
wget https://github.com/adobe-fonts/source-han-serif/raw/release/SubsetOTF/CN/SourceHanSerifCN-Regular.otf -O ~/.fonts/source-han-serif/SourceHanSerifCN-Regular.otf
wget https://github.com/adobe-fonts/source-han-serif/raw/release/SubsetOTF/CN/SourceHanSerifCN-SemiBold.otf -O ~/.fonts/source-han-serif/SourceHanSerifCN-SemiBold.otf
fc-cache -f -v ~/.fonts
echo "Fonts installed"
- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y texlive-xetex texlive-lang-chinese texlive-latex-recommended librsvg2-bin
wget https://github.com/jgm/pandoc/releases/download/2.14.1/pandoc-2.14.1-1-amd64.deb
sudo dpkg -i pandoc-2.14.1-1-amd64.deb
sudo npm install -g --save-dev --save-exact prettier
xelatex --version
pandoc --version
prettier --version
echo "Dependencies installed"
- name: Clone repository
uses: actions/checkout@v2
- name: Build pdf
run: |
cd docs
python3 compile.py
- name: Set up release env
id: setup
run: |
echo "::set-output name=version::$(head -n 1 docs/release-notes.md)"
echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"
echo "::set-output name=date::$(date +%Y-%m-%d)"
- name: Create pdf release
id: create_release
uses: actions/create-release@latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.setup.outputs.version }}-${{ steps.setup.outputs.sha_short }}
release_name: Release ${{ steps.setup.outputs.date }}
body_path: docs/release-notes.md
draft: false
prerelease: true
- name: Upload pdf release
id: upload_release
uses: actions/upload-release-asset@latest
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: docs/build/target/release.pdf
asset_name: dive-into-dl-pytorch.pdf
asset_content_type: application/pdf
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
data/pikachu*
# Pascal VOC2012数据集, 约2G
data/VOCdevkit*

docs/build
96 changes: 96 additions & 0 deletions docs/compile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import os
import re
import glob
from utils import get_content_files, downgrade_heading, create_empty_page, pure_markdown


def init():
os.system("rm -rf build")
os.system("mkdir -p build/docs")
os.system("mkdir -p build/target")
os.system("cp -r img tex -t build")
os.system("cp _sidebar.md -t build")
for file in get_content_files():
print("Found", file)
os.system("cp {} build/docs".format(file)) # move all markdown files to build/docs
os.chdir("build")
print("Inited.", end="\n\n")


def create_chapter():
files = []
with open("_sidebar.md", "r") as f:
for line in f.readlines():
# 提取页面文件
res = re.findall(r"\]\((.+)\)$", line)

if len(res) > 0: # 存在对应 markdown 文件
filename = "docs/{}".format(re.sub(r"^(.+)\/", "", res[0]))
if line[0] == " ":
# 将子章节内的标题统一降一级
downgrade_heading(filename)

else: # 不存在对应 markdown 文件
# 提取页面标题
title = re.findall(r"\*\s?\[?(.+?)\]?\(?\)?$", line)[0].replace("\\", "")
filename = "docs/{}.md".format(title.replace(" ", ""))

# this is a work around for not including this chapter
if title == "简介":
continue

if line[0] == " ":
# 针对子章节生成提示
create_empty_page(filename, title, is_chapter=False)
else:
# 针对章节生成标题
create_empty_page(filename, title, is_chapter=True)
files.append(filename)
return files


def format():
print("Formating docs...")
for filename in glob.glob("docs/*.md"):
pure_markdown(filename)
os.system("prettier --write docs")


def build(files):
print("Building...")
title = r"《动手学深度学习》PyTorch 实现"
toc_title = r"目录"
author = r"原书作者:阿斯顿・张、李沐、扎卡里C.立顿、\\ 亚历山大J.斯莫拉、以及其他社区贡献者 \thanks{GitHub 地址: https://github.com/d2l-ai/d2l-zh}"
date = r"\today"

os.system("pandoc {} -o target/release.pdf \
--from=markdown+link_attributes+footnotes+blank_before_header \
--to=pdf \
--toc \
--toc-depth=2 \
--resource-path=./docs \
--listings \
--pdf-engine=xelatex \
--template=tex/custom_template.tex \
-V documentclass=book \
-V mathspec \
-V graphicx \
-V colorlinks \
-V title='{}' \
-V toc-title='{}' \
-V author='{}' \
-V date='{}' \
--include-before-body=tex/cover.tex".format(" ".join(files), title, toc_title, author, date)
)

print("Done.", end="\n\n")


def main():
init()
files = create_chapter()
format()
build(files)

if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions docs/release-notes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
v0.0.1

This release is auto generated by Github Actions.
5 changes: 5 additions & 0 deletions docs/tex/cover.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
本项目将《动手学深度学习》 原书中 MXNet 代码实现改为 PyTorch 实现。项目地址:\href{https://github.com/ShusenTang/Dive-into-DL-PyTorch}{https://github.com/ShusenTang/Dive-into-DL-PyTorch} 此书的中英版本存在一些不同,针对此书英文版的 PyTorch 重构可参考这个项目。

There are some differences between the Chinese and English versions of this book. For the PyTorch modifying of the English version, you can refer to this repo.

\thispagestyle{empty}
Loading