我遇到了 pandoc 无法正确生成目录的问题。
以下是我的工作流程:
- 我创建了一个包含网页内容的 Markdown 文件。
- 我使用 Git 对我的项目进行版本控制。
- 我
git push
到我自己托管的 GitLab 服务器。 - 使用 CI/CD,我的服务器在 Ubuntu/bionic LXC 容器中运行 3 个脚本。
- 第一个提取元数据。
- 第二个调用 pandoc 生成带有页眉、页脚、目录和 CSS 的 HTML 页面。
- 第三个将新的 HTML 页面推送到我的生产服务器。
一切都运行正常,除了一个小细节:当 pandoc 生成 HTML 时,缺少目录。奇怪的是,当我在本地机器 (Fedora 34) 上运行它时,它可以正常工作。
这是我在第二个容器中运行的脚本(之前没有安装任何东西):
apt-get update
apt-get install -y python3 pandoc pandoc-citeproc
mkdir -p build
python3 .deploy/convert.py
您可以在下面找到脚本的结果。
笔记:在 LXC 容器中,用户是 root,不需要sudo
。
内容.deploy/convert.py
:
import sys
import os
import re
from pathlib import Path
if __name__ == '__main__':
with open('data/type', 'r') as f:
type = f.readline()
with open('data/number', 'r') as f:
number = f.readline()
with open('data/name', 'r') as f:
name = f.readline()
result = list(Path(".").rglob("*.md"))
for file in result:
parts = str(file).split('.')
parts.pop()
filename = '.'.join(parts)
if filename != 'README':
parts = str(filename).split('/')
parts.pop()
directory = '/'.join(parts)
os.system('mkdir -p build/' + directory)
# This is the important line!
os.system('pandoc --toc --metadata title=' + type + '-' + number +
' --metadata type=' + type +
' --metadata number=' + number +
' --metadata name="' + name +
'" -s --template .deploy/template.html ' +
str(file) +
' -o build/' + filename + '.html'
)
with open('build/' + filename + '.html', 'r') as f:
filedata = f.read()
filedata = re.sub('href="(.*)\.md"', 'href="\\1.html"', filedata)
with open('build/' + filename + '.html', 'w') as f:
f.write(filedata)
这 3 个容器按顺序调用,中间留有所谓的工件:它们是可以传递到下一个容器的文件。我检查了转换容器之前和之后的工件,唯一缺少的部分是目录。
我错过了什么?
以下是我从脚本中得到的结果。再次强调,即使您看到$
的不是#
,也要知道我是 root!
$ apt-get update
Hit:1 http://security.ubuntu.com/ubuntu bionic-security InRelease
Hit:2 http://archive.ubuntu.com/ubuntu bionic InRelease
Hit:3 http://archive.ubuntu.com/ubuntu bionic-updates InRelease
Hit:4 http://archive.ubuntu.com/ubuntu bionic-backports InRelease
Hit:5 https://packagecloud.io/github/git-lfs/ubuntu bionic InRelease
Reading package lists...
$ apt-get install -y python3 pandoc
Reading package lists...
Building dependency tree...
Reading state information...
python3 is already the newest version (3.6.7-1~18.04).
python3 set to manually installed.
The following package was automatically installed and is no longer required:
libfreetype6
Use 'apt autoremove' to remove it.
The following additional packages will be installed:
liblua5.1-0 libluajit-5.1-2 libluajit-5.1-common pandoc-data
Suggested packages:
texlive-latex-recommended texlive-xetex texlive-luatex pandoc-citeproc
texlive-latex-extra context wkhtmltopdf
The following NEW packages will be installed:
liblua5.1-0 libluajit-5.1-2 libluajit-5.1-common pandoc pandoc-data
0 upgraded, 5 newly installed, 0 to remove and 13 not upgraded.
Need to get 7103 kB of archives.
After this operation, 53.7 MB of additional disk space will be used.
Get:1 http://archive.ubuntu.com/ubuntu bionic/universe amd64 liblua5.1-0 amd64 5.1.5-8.1build2 [100 kB]
Get:2 http://archive.ubuntu.com/ubuntu bionic/universe amd64 libluajit-5.1-common all 2.1.0~beta3+dfsg-5.1 [44.3 kB]
Get:3 http://archive.ubuntu.com/ubuntu bionic/universe amd64 libluajit-5.1-2 amd64 2.1.0~beta3+dfsg-5.1 [227 kB]
Get:4 http://archive.ubuntu.com/ubuntu bionic/universe amd64 pandoc-data all 1.19.2.4~dfsg-1build4 [40.1 kB]
Get:5 http://archive.ubuntu.com/ubuntu bionic/universe amd64 pandoc amd64 1.19.2.4~dfsg-1build4 [6692 kB]
debconf: unable to initialize frontend: Dialog
debconf: (TERM is not set, so the dialog frontend is not usable.)
debconf: falling back to frontend: Readline
debconf: unable to initialize frontend: Readline
debconf: (This frontend requires a controlling tty.)
debconf: falling back to frontend: Teletype
Fetched 7103 kB in 1s (5904 kB/s)
dpkg-preconfigure: unable to re-open stdin:
Selecting previously unselected package liblua5.1-0:amd64.
(Reading database ... 29084 files and directories currently installed.)
Preparing to unpack .../liblua5.1-0_5.1.5-8.1build2_amd64.deb ...
Unpacking liblua5.1-0:amd64 (5.1.5-8.1build2) ...
Selecting previously unselected package libluajit-5.1-common.
Preparing to unpack .../libluajit-5.1-common_2.1.0~beta3+dfsg-5.1_all.deb ...
Unpacking libluajit-5.1-common (2.1.0~beta3+dfsg-5.1) ...
Selecting previously unselected package libluajit-5.1-2:amd64.
Preparing to unpack .../libluajit-5.1-2_2.1.0~beta3+dfsg-5.1_amd64.deb ...
Unpacking libluajit-5.1-2:amd64 (2.1.0~beta3+dfsg-5.1) ...
Selecting previously unselected package pandoc-data.
Preparing to unpack .../pandoc-data_1.19.2.4~dfsg-1build4_all.deb ...
Unpacking pandoc-data (1.19.2.4~dfsg-1build4) ...
Selecting previously unselected package pandoc.
Preparing to unpack .../pandoc_1.19.2.4~dfsg-1build4_amd64.deb ...
Unpacking pandoc (1.19.2.4~dfsg-1build4) ...
Setting up libluajit-5.1-common (2.1.0~beta3+dfsg-5.1) ...
Setting up pandoc-data (1.19.2.4~dfsg-1build4) ...
Setting up libluajit-5.1-2:amd64 (2.1.0~beta3+dfsg-5.1) ...
Setting up liblua5.1-0:amd64 (5.1.5-8.1build2) ...
Setting up pandoc (1.19.2.4~dfsg-1build4) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1.5) ...
$ mkdir -p build
$ python3 .deploy/convert.py
Uploading artifacts for successful job 00:01
Uploading artifacts...
Runtime platform arch=amd64 os=linux pid=1620 revision=bd40e3da version=14.9.1
build: found 2 matching files and directories
untracked: found 4 files
Uploading artifacts as "archive" to coordinator... 201 Created id=3380 responseStatus=201 Created token=WHTmbFcM
Cleaning up file based variables 00:00
Job succeeded
答案1
在 pandoc 1 中,目录的占位符是$toc$
,但是从 pandoc 2 开始,它变成了$table-of-contents$
。
不知何故,容器安装了 pandoc 1,它与脚本的最新版本不兼容。