首先放上成品的倉庫位址:Gtihub-ACM_Template_Library,歡迎Star哦~
效果展示:
1.建立Github Actions
首先建立一個Github Actions的YML檔案(可以通過Github模闆生成),然後可以看到以下内容:
# This is a basic workflow to help you get started with Actions
name: Build PDF
# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the "master" branch
push:
branches: [ "master" ]
tags:
- 'v*'
pull_request:
branches: [ "master" ]
# Allows you to run this workflow manually from the Actions tab
workflow_dispatch:
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: ubuntu-latest
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses:
其中"name"是整個腳本的名稱,"on"是觸發條件,"runs-on"是運作系統環境…
具體更多的原理可以參考别的大佬的部落格,這裡不再贅述。假設讀者已經掌握了Github Actions的基本操作。
2.編寫Makrdown轉Latex的流程
(1).建立臨時檔案(避免污染工作區)
# Runs a single command using the runners shell
- name: Create Temp Files
run: |
echo JXNU Asscoiation of Computer Science
pwd
mkdir $HOME/build/
mkdir $HOME/build/1/
mkdir $HOME/build/2/
mkdir $HOME/build/3/
mkdir $HOME/build/4/
mkdir $HOME/build/5/
cp 1-數學.md $HOME/build/1/
cp 2-資料結構.md $HOME/build/2/
cp 3-字元串.md $HOME/build/3/
cp 4-計算幾何.md $HOME/build/4/
cp 5-圖論.md $HOME/build/5/
mkdir -p $HOME/build/1/pandoc/
mkdir -p $HOME/build/2/pandoc/
mkdir -p $HOME/build/3/pandoc/
mkdir -p $HOME/build/4/pandoc/
mkdir -p $HOME/build/5/pandoc/
cp ./pandoc/gen.sh -p $HOME/build/1/pandoc/
cp ./pandoc/gen.sh -p $HOME/build/2/pandoc/
cp ./pandoc/gen.sh -p $HOME/build/3/pandoc/
cp ./pandoc/gen.sh -p $HOME/build/4/pandoc/
cp ./pandoc/gen.sh -p $HOME/build/5/pandoc/
cp ./pandoc/minted.py -p $HOME/build/1/pandoc/
cp ./pandoc/minted.py -p $HOME/build/2/pandoc/
cp ./pandoc/minted.py -p $HOME/build/3/pandoc/
cp ./pandoc/minted.py -p $HOME/build/4/pandoc/
cp ./pandoc/minted.py -p $HOME/build/5/pandoc/
(2).Pandoc 安裝
我們将主要使用Pandoc進行Markdown轉Latex的操作,是以需要安裝Pandoc及一些附加的Python包:
- name: Install dependencies
run: |
wget https://github.com/jgm/pandoc/releases/download/2.10.1/pandoc-2.10.1-1-amd64.deb
sudo dpkg -i pandoc-2.10.1-1-amd64.deb
pandoc --version
pip install pandocfilters
(3).編譯生成Tex文檔
首先将需要使用的模闆拷貝到Pandoc的Template目錄,然後調用Pandoc生成Tex文檔。
- name: Copy Algo.latex to Template Dir
run: |
mkdir -p /home/runner/.pandoc/date/templates/
mkdir -p /home/runner/.pandoc/templates/
cp ./pandoc/algo.latex /home/runner/.pandoc/date/templates/
cp ./pandoc/algo.latex /home/runner/.pandoc/templates/
- name: Build Full version
run: |
mkdir -p $HOME/work/ACM_Algorithm_Library/ACM_Algorithm_Library/TexDocument/
cd $HOME/build/1/
mkdir complie
pandoc --template algo --filter ./pandoc/minted.py --pdf-engine=xelatex --no-highlight --pdf-engine-opt="-shell-escape" -o template.tex --from markdown -V mainfont="Source Han Serif CN" -V monofont="Source Code Pro" -V sansfont="Source Han Sans CN" -V CJKmainfont="Source Han Serif CN" -V secnumdepth=2 -V --number-sections --toc -V include-before="\renewcommand\labelitemi{$\bullet$}" -V header-includes="\usepackage{minted}" -V geometry="margin=2cm" *-*.md
pwd
cd $HOME/build/2/
mkdir complie
pandoc --template algo --filter ./pandoc/minted.py --pdf-engine=xelatex --no-highlight --pdf-engine-opt="-shell-escape" -o template.tex --from markdown -V mainfont="Source Han Serif CN" -V monofont="Source Code Pro" -V sansfont="Source Han Sans CN" -V CJKmainfont="Source Han Serif CN" -V secnumdepth=2 -V --number-sections --toc -V include-before="\renewcommand\labelitemi{$\bullet$}" -V header-includes="\usepackage{minted}" -V geometry="margin=2cm" *-*.md
pwd
cd $HOME/build/3/
mkdir complie
pandoc --template algo --filter ./pandoc/minted.py --pdf-engine=xelatex --no-highlight --pdf-engine-opt="-shell-escape" -o template.tex --from markdown -V mainfont="Source Han Serif CN" -V monofont="Source Code Pro" -V sansfont="Source Han Sans CN" -V CJKmainfont="Source Han Serif CN" -V secnumdepth=2 -V --number-sections --toc -V include-before="\renewcommand\labelitemi{$\bullet$}" -V header-includes="\usepackage{minted}" -V geometry="margin=2cm" *-*.md
pwd
cd $HOME/build/4/
mkdir complie
pandoc --template algo --filter ./pandoc/minted.py --pdf-engine=xelatex --no-highlight --pdf-engine-opt="-shell-escape" -o template.tex --from markdown -V mainfont="Source Han Serif CN" -V monofont="Source Code Pro" -V sansfont="Source Han Sans CN" -V CJKmainfont="Source Han Serif CN" -V secnumdepth=2 -V --number-sections --toc -V include-before="\renewcommand\labelitemi{$\bullet$}" -V header-includes="\usepackage{minted}" -V geometry="margin=2cm" *-*.md
pwd
cd $HOME/build/5/
mkdir complie
pandoc --template algo --filter ./pandoc/minted.py --pdf-engine=xelatex --no-highlight --pdf-engine-opt="-shell-escape" -o template.tex --from markdown -V mainfont="Source Han Serif CN" -V monofont="Source Code Pro" -V sansfont="Source Han Sans CN" -V CJKmainfont="Source Han Serif CN" -V secnumdepth=2 -V --number-sections --toc -V include-before="\renewcommand\labelitemi{$\bullet$}" -V header-includes="\usepackage{minted}" -V geometry="margin=2cm" *-*.md
pwd
cp $HOME/build/1/template.tex $HOME/work/ACM_Algorithm_Library/ACM_Algorithm_Library/TexDocument/part1.tex
cp $HOME/build/2/template.tex $HOME/work/ACM_Algorithm_Library/ACM_Algorithm_Library/TexDocument/part2.tex
cp $HOME/build/3/template.tex $HOME/work/ACM_Algorithm_Library/ACM_Algorithm_Library/TexDocument/part3.tex
cp $HOME/build/4/template.tex $HOME/work/ACM_Algorithm_Library/ACM_Algorithm_Library/TexDocument/part4.tex
cp $HOME/build/5/template.tex $HOME/work/ACM_Algorithm_Library/ACM_Algorithm_Library/TexDocument/part5.tex
cd $HOME/
ls
3.編譯生成PDF
編譯生成PDF需要Latex的支援,這裡利用latex-action的支援實作:
- name: Create PDF Build DIR
run: mkdir -p $HOME/work/ACM_Algorithm_Library/ACM_Algorithm_Library/PDFDocument/
- name: Compile LaTeX document
uses: xu-cheng/latex-action@v2
with:
root_file: |
./TexDocument/part1.tex
./TexDocument/part2.tex
./TexDocument/part3.tex
./TexDocument/part4.tex
./TexDocument/part5.tex
latexmk_use_xelatex: true
latexmk_shell_escape: true
extra_fonts: ./pandoc/font/*.otf
- name: Move built pdf to PDF dir
run: |
rm ./PDFDocument/*
mv part1.pdf ./PDFDocument/part1.pdf
mv part2.pdf ./PDFDocument/part2.pdf
mv part3.pdf ./PDFDocument/part3.pdf
mv part4.pdf ./PDFDocument/part4.pdf
mv part5.pdf ./PDFDocument/part5.pdf
cp ./TexDocument/Cover/ICPC.png ./
- name: Build Cover
uses: xu-cheng/latex-action@v2
with:
root_file: |
./TexDocument/Cover/cover1.tex
./TexDocument/Cover/cover2.tex
./TexDocument/Cover/cover3.tex
./TexDocument/Cover/cover4.tex
./TexDocument/Cover/cover5.tex
latexmk_use_xelatex: true
latexmk_shell_escape: true
extra_fonts: ./pandoc/font/*.otf
- name: Move built cover to PDF dir
run: |
mv cover1.pdf ./PDFDocument/
mv cover2.pdf ./PDFDocument/
mv cover3.pdf ./PDFDocument/
mv cover4.pdf ./PDFDocument/
mv cover5.pdf ./PDFDocument/
4.合并PDF與封面
- name: Merge PDF
run: |
sudo apt-get install poppler-utils
cd ./PDFDocument/
sudo pdfunite cover1.pdf part1.pdf part1-Math.pdf
sudo pdfunite cover2.pdf part2.pdf part2-DataStructure.pdf
sudo pdfunite cover3.pdf part3.pdf part3-String.pdf
sudo pdfunite cover4.pdf part4.pdf part4-Geometry.pdf
sudo pdfunite cover5.pdf part5.pdf part5-GraphTheory.pdf
rm cover1.pdf part1.pdf
rm cover2.pdf part2.pdf
rm cover3.pdf part3.pdf
rm cover4.pdf part4.pdf
rm cover5.pdf part5.pdf
cd $HOME
5.送出生成的PDF到倉庫
- name: Commit Changes
run: |
git config --global user.email "***"
git config --global user.name "***"
git add $HOME/work/ACM_Algorithm_Library/ACM_Algorithm_Library/TexDocument/*.tex
git add $HOME/work/ACM_Algorithm_Library/ACM_Algorithm_Library/PDFDocument/*.pdf
git commit -m "Generated Automatically by Workflow." -a
git config --global --add safe.directory '*'
- name: Push Changes
uses: ad-m/github-push-action@master
with:
branch: master
force: false
github_token: ${{ secrets.GITHUB_TOKEN }}