Files
hello-algo/epub/templates/toc.xhtml.ejs
O 091afd38b4 add epub generator (#1831)
* add epub generator

* improve parser, keep images

* check epub after generate

* fix render math content error in block

* render \dots as ...

* render \lfoor and \rfloor

* use monospaced font to render code block

* render code block with syntax highlight

* adjust title render

* fix render LaTeX

* fix '!!! abstract' render

* render code block in flow

* include whole class when not specifiy function

* command line to build other language

* update README

* fix process python code example

* support build en, ja and zh-hant

* add '--all' option to build all version

* use branch docs to build epub

* fix title and toc render

* build epub file with name like 'hello-algo_{zh}_{cpp}.epub

* fix render LaTeX

* optimize style

* use math font

* fix extract code block

* add border for code block

* fix python code style

* fix page break

* try git pull first when build epub

* ajust title level of chapter section

* Update epub styles

* Update epub styles

* Update convers and fonts.

* Convert code comments and README into English.

* Update the output dir.

* Add code reviewers on the cover.

* Support multi language for the reviewer names.

* Update .gitignore

---------

Co-authored-by: krahets <krahets@163.com>
2025-12-30 08:23:31 +08:00

116 lines
3.9 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:epub="http://www.idpf.org/2007/ops" xml:lang="<%- lang %>" lang="<%- lang %>">
<head>
<title><%= title %></title>
<meta charset="UTF-8" />
<link rel="stylesheet" type="text/css" href="style.css" />
<style>
/* 隐藏目录中的列表编号 */
nav#toc ol {
list-style: none;
padding-left: 0;
}
nav#toc ol ol {
padding-left: 20px;
}
.toc-level-0 {
margin-left: 0;
font-weight: bold;
}
.toc-level-1 {
margin-left: 20px;
font-weight: normal;
}
</style>
</head>
<body>
<h1 class="h1"><%= tocTitle %></h1>
<nav id="toc" epub:type="toc">
<ol>
<%
// 构建层级结构
var hierarchicalContent = [];
var currentParent = null;
content.forEach(function(item, index) {
if (!item.excludeFromToc && !item.beforeToc) {
// 检查是否是顶级章节level 0 或没有 parentTitle
if (item.level === 0 || !item.parentTitle) {
// 开始新的父章节
currentParent = {
title: item.title,
href: item.href,
level: 0,
number: item.number,
children: []
};
hierarchicalContent.push(currentParent);
} else if (item.level === 1 && currentParent) {
// 添加到当前父章节的子项
currentParent.children.push({
title: item.title,
href: item.href,
level: 1,
number: item.number
});
} else {
// 其他情况,作为独立章节
hierarchicalContent.push({
title: item.title,
href: item.href,
level: item.level || 0,
number: item.number,
children: []
});
}
}
});
// 渲染嵌套的列表项
function renderTocItem(chapter) {
var title = chapter.title || 'Untitled';
var href = chapter.href;
var children = chapter.children || [];
var level = chapter.level || 0;
var number = chapter.number;
// 对于顶级章节level 0标题已经包含"第X章",不需要再加编号
// 只对子章节level 1添加编号
var displayTitle = (level === 1 && number ? number + ' ' : '') + title;
var result = ' <li class="table-of-content toc-level-' + level + '">\n';
result += ' <a href="' + href + '">' + displayTitle;
if (chapter.author && chapter.author.length) {
result += ' - <small class="toc-author">' + chapter.author.join(",") + '</small>';
}
if (chapter.url) {
result += '<span class="toc-link">' + chapter.url + '</span>';
}
result += '</a>\n';
// 如果有子章节,创建嵌套列表
if (children.length > 0) {
result += ' <ol>\n';
children.forEach(function(child) {
result += renderTocItem(child);
});
result += ' </ol>\n';
}
result += ' </li>\n';
return result;
}
// 渲染所有目录项
hierarchicalContent.forEach(function(chapter) {
%><%- renderTocItem(chapter) %><%
});
%>
</ol>
</nav>
</body>
</html>