我有一些 Calibre 创建的 epub,我想将它们制作成 markdown 以在 Obsidian 中使用。我发现了 Pandoc,我的简单转换尝试包括丢失斜体并传递 Calibre span
标签,这些标签在 Obsidian 中不会显示为斜体。
如果我关闭 raw_html
扩展,它不会传递所有 span
标记,但我也不会得到任何斜体。我想做的是转换 html:
<span class="italic">Some Words</span>
进入我最终的 Markdown 文件中的斜体文本。如果 Pandoc 能做到这一点,那就太好了。否则,我会在将 html 传递到 Pandoc 之前先对其进行转换,但是 Calibre 生成的许多 span
标签都堆叠了几层深,因此一个非常简单的解决方案会很棒。
Pandoc 直接处理这个问题还是我需要先处理 html?我不仅仅关心斜体,还有许多其他使用各种 Calibre span
标签的格式问题,这些标签可能更简单,例如粗体和一些标题。所以我正在尝试找出一种方法来处理所有这些问题。
更新:
由于我必须在网络上进行大量的探索和反复试验才能使其能够很好地满足我的需求,因此我认为发布我对 lua 过滤器的第一次尝试会很有用。这在从 EPUB 中提取(通过解压缩)的数百个 html 页面上效果很好,这些页面似乎是由名为 Calibre 的工具编写的。
我的电子书是冗长的引用资料,对图像的了解较少,因此我的图像处理程序足够简单,足以满足我的需求。我可以 grep 结果文件并修复任何链接问题。我的一套 Calibre EPUB 根据创建时间改变了它们的内部结构,所以我不断添加新的类名,因为随着时间的推移,似乎没有努力保持相同的命名。因此,随着时间的推移,当我转换更多文件时,我希望对此进行修改。我还预计 Markdown 将成为比 EPUB 更好的格式!
这是我的第一个 lua 代码,所以我希望它有点草率。另外,我在 bash 脚本中使用了 --wrap=none
-t commonmark
,该脚本对工作目录中的所有文件迭代 pandoc 转换。
-- Corrections for some Calibre oddities when using Pandoc to convert to markdown for Obsidian.
-- Note that I converting very old ebooks and that I don't know anything about Calibre.
function Span (span)
-- Make italic for: <span class="italic">(“Hello there!”)</span>
if span.classes:includes 'italic' then
return pandoc.Emph(span.content)
end
-- Make bold for: <span class="bold">(“Hello there!”)</span>
if span.classes:includes 'bold' then
return pandoc.Strong(span.content)
end
-- Unclear what purpose these serve...
if span.classes:includes 'calibre1' or span.classes:includes 'calibre2'
or span.classes:includes 'calibre3' or span.classes:includes 'calibre4' then
return pandoc.Strong(span.content)
end
-- My markdown reader (Obsidian) works with this when using commonmark output.
if span.classes:includes 'underline' then
span.attributes['style'] = 'text-decoration: double underline ;'
return span
end
end
function Image (img)
-- Fix calibre6 images.
if img.classes:includes 'calibre6' or img.classes:includes 'calibre9' then
return pandoc.Image(img.caption, img.src, nil, nil)
end
end
function Div (div)
-- Put a horizontal line in for the page break, just to see where they are.
if div.classes:includes 'mbp_pagebreak' then
return '---'
end
-- These seem to be hardcoded page delimiters put in by calibre for ebook readers?
if div.classes:includes 'calibre_4' or div.classes:includes 'calibre_13' then
return '---'
end
end
最后,我应该指出,我编写了一个脚本,该脚本通过第一行重命名所有新的 Markdown 文件。这使它们具有连贯的形状,可以添加到 Obsidian,Obsidian 使用文件系统来组织事物。经过一些编辑和重命名后,我的黑曜石保险库中有一部分旧引用书,可以在我的所有设备上轻松访问。
最佳答案
Pandoc 不解析 CSS,因此无法知道是否应该将其设置为斜体。一个好的解决方案是使用Lua filter修改pandoc的内部文档表示。 .
function Span (span)
if span.classes:includes 'italic' then
return pandoc.Emph(span.content)
end
end
此过滤器检查范围是否具有类 italic
,如果是,则将其转换为强调文本,通常以斜体输出。通过将过滤器保存到文件中并通过 --lua-filter
命令行选项传递该文件 pandoc 来使用过滤器。
您可能想要处理更多类(class);您可能想要使用的其他 pandoc 构造函数是 pandoc.Strong
和 pandoc.Underline
、etc. 。使用 --to=native
运行 pandoc 以查看 pandoc 在内部如何表示文档。
关于html - 使用Pandoc进行markdown输出时,奇数span标签该怎么办,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71743107/