首页 > 其他分享 >利用Github Actions自动将Markdown文件转为Latex文档并生成PDF(制作一个支持自动编译的代码模板库)

利用Github Actions自动将Markdown文件转为Latex文档并生成PDF(制作一个支持自动编译的代码模板库)

时间:2022-10-28 10:37:11浏览次数:76  
标签:Latex Markdown pandoc -- tex 自动 build pdf HOME


首先放上成品的仓库地址:​​Gtihub-ACM_Template_Library​​,欢迎Star哦~

效果展示:

利用Github Actions自动将Markdown文件转为Latex文档并生成PDF(制作一个支持自动编译的代码模板库)_github


利用Github Actions自动将Markdown文件转为Latex文档并生成PDF(制作一个支持自动编译的代码模板库)_ubuntu_02


利用Github Actions自动将Markdown文件转为Latex文档并生成PDF(制作一个支持自动编译的代码模板库)_linux_03

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​​的支持实现:

特别注意,由于使用了一些自己的字体,所以需要指定​​extra_fonts​​参数。

- 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与封面

这个操作基于​​poppler-utils​​实现,因此也需要先安装后使用。

- 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 }}


标签:Latex,Markdown,pandoc,--,tex,自动,build,pdf,HOME
From: https://blog.51cto.com/u_12372287/5803544

相关文章