[英]Internal links in HTML slides made from markdown with pandoc
根据pandoc(1) , pandoc 支持 HTML 幻灯片中的内部链接。 但是当我点击一个时,我什么也没发生。
一个最小的例子:
% A minimal example
% moi
% 2015-04-04
# Section 1
la la la
# Section 2
cf. [Section 1](#section-1)
我将上述内容另存为example.md
。 然后在 bash 我运行
file=example && \
pandoc -fmarkdown -tslidy --standalone --self-contained -o$file.html $file.md
在 Web 浏览器中打开生成的 HTML 幻灯片后,我单击幻灯片“第 2 节”上的“第 1 节”,但没有任何反应。 我已经在多个设备上的多个浏览器中尝试过:运行 Arch Linux 的 Macbook 上的 xombrero,运行 Android 的 Moto X 上的 Chrome 和运行 Windows 8.1 的索尼笔记本电脑上的 Chrome。 结果是一样的。 我正在使用 pandoc 版本 1.13.2。
pandoc 为内部参考生成的链接与相关幻灯片的链接不同:在本示例中,前者以#section-1
结尾,后者以#(2)
结尾。 我想这就是为什么单击内部链接不会返回到相关幻灯片的原因。 是否有某种方法可以实现内部链接确实转到其相关幻灯片?
这是相关的 HTML:
<body>
<div class="slide titlepage">
<h1 class="title">A minimal example</h1>
<p class="author">
moi
</p>
<p class="date">2015-04-04</p>
</div>
<div id="section-1" class="slide section level1">
<h1>Section 1</h1>
<p>la la la</p>
</div>
<div id="section-2" class="slide section level1">
<h1>Section 2</h1>
<p>cf. <a href="#section-1">Section 1</a></p>
</div>
</body>
谢谢你的帮助!
您的问题不在于 Pandoc,而在于 Slidy。 Pandoc 正在为普通 HTML 页面创建正确的 HTML,但 Slidy 幻灯片软件不支持转到<div>
- 仅转到幻灯片编号。
如果您将链接更改为cf. [Section 1](#(2))
cf. [Section 1](#(2))
('2' 是带有'Section 1' 的幻灯片的编号)然后它会正常工作。
顺便说一句 - 它在 Pandoc 创建的reveal.js 幻灯片中完美运行。
虽然这个问题是五年多前提出的,但我最近遇到了同样的问题,并在 Python 中创建了一个后处理脚本,这对我有用。 本质上,它正在读取 Pandoc -> Slidy html 输出,扫描内部链接并将它们替换为定义链接 ID 的幻灯片编号。
def Fix_Internal_Slidy_Links(infilename, outfilename):
"""Replaces all internal link targets with targets of the respective slidy page number
"""
page_pattern = ' class=\"slide';
id_pattern = ' id=\"';
internal_link_pattern = '<a href=\"#';
id_dict = dict();
whole_text = [];
cur_page = 0;
#
# First read all ids and associate them with the current page in id_dict
with open(infilename, 'r', encoding='utf-8') as filecontent:
for idx_cur_line, cur_line in enumerate(filecontent):
whole_text += [cur_line];
if (page_pattern in cur_line):
cur_page += 1;
#
if (id_pattern in cur_line):
while (id_pattern in cur_line):
startidx = cur_line.index(id_pattern);
cur_line = cur_line[startidx+len(id_pattern):];
lineparts = cur_line.split('"');
# Check if the current id is properly ended
if (len(lineparts) > 1):
id_dict.update([(lineparts[0], cur_page)]);
#
# Then process the code again and replace all internal links known in id_dict
with open(outfilename, 'w', encoding='utf-8') as filecontent:
for cur_line in whole_text:
if (internal_link_pattern in cur_line):
temp_line = '';
offset = 0;
while (internal_link_pattern in cur_line):
startidx = cur_line.index(internal_link_pattern);
# Extract name
temp_line += cur_line[offset:startidx+len(internal_link_pattern)];
cur_line = cur_line[startidx+len(internal_link_pattern):];
lineparts = cur_line.split('"');
if (len(lineparts) < 2):
# It seems that the id is not properly finished
break;
#
link = lineparts[0];
try:
# Create a link to the page assigned to that id
replacement_link = '(' + str(id_dict[link]) + ')"';
except:
# The link reference is not known in id_dict so do not change it
replacement_link = lineparts[0] + '"';
#
temp_line += replacement_link;
cur_line = cur_line[len(lineparts[0])+1:];
#
cur_line = temp_line + cur_line;
#
filecontent.write(cur_line);
#
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.