HTML折叠中的内部链接由markdown与pandoc组成

时间:2015-04-05 06:03:00

标签: html markdown pandoc slidy

根据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幻灯片后,单击幻灯片“Section 2”上的“Section 1”,但没有任何反应。我在多个设备上尝试过多个浏览器:运行Arch Linux的Macbook上的xombrero,运行Android的Moto X上的Chrome和运行Windows 8.1的Sony笔记本电脑上的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>

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

你的问题不在于Pandoc,而在于Slidy。 Pandoc正在为普通的HTML页面创建正确的HTML,但Slidy幻灯片软件不支持转到<div> - 仅转到幻灯片编号。

如果您将链接更改为cf. [Section 1](#(2))('2'是带有'第1部分'的幻灯片编号),那么它将正常工作。

顺便说一句 - 它在Pandoc创建的reveal.js幻灯片中完美运行。

答案 1 :(得分:0)

尽管这个问题已在五年前提出,但我最近遇到了同样的问题,并在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);
#