使用 Pandoc 将 Markdown 转换为 PDF 时使用 CSS [英] Using CSS when converting Markdown to PDF with Pandoc

查看:59
本文介绍了使用 Pandoc 将 Markdown 转换为 PDF 时使用 CSS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 OS X 上试用 Pandoc,迄今为止的结果令人印象深刻.然而,一个阻塞问题是让 CSS 样式适用于内联代码示例.我正在从 Markdown 转换为 PDF.

I'm trying out Pandoc on OS X, and results thus far are impressive. One blocking problem, however, is getting CSS styles to work on inline code samples. I'm converting from Markdown to PDF.

我的源中有这个字符串:

I have this string in my source:

* Create a simple HTML document (<span class="filename">simple.html</span>) and load it into the browser via the file system

我也试过这个:

* Create a simple HTML document (`simple.html`{.filename}) and load it into the browser via the file system

我想应用类文件名";在每种情况下都添加到随附的文本,但它似乎对输出没有任何作用.但是手册说:

I'd like to apply the class "filename" to the enclosed text in each case, but it doesn't seem to do anything to the output. However the manual says:

一些输出格式可以使用这些信息来进行语法高亮.目前,使用此信息的唯一输出格式是 HTML 和 LaTeX.

Some output formats can use this information to do syntax highlighting. Currently, the only output formats that uses this information are HTML and LaTeX.

这是我的命令:

pandoc 
    --output ./output.pdf 
    --css source/styles.css 
    source/en/docs/input.md

我正在转换为 PDF,它是由 Pandoc 在内部由 LaTeX 编写的.我可以让它工作吗?或者,我可以使用使用 LaTeX 命令定义的样式吗?- 它不一定是 CSS.但是,它必须是一个样式系统 - 每次都更改 italic/font/color 属性是不可行的.

I'm converting to PDF, which is written by LaTeX by Pandoc internally. Can I get this to work? Or, can I use a style defined using a LaTeX command? - it doesn't have to be CSS. However, it must be a style system - it's not workable to change italic/font/colour attributes on each occasion.

我尝试将输出临时发送到 HTML,在这种情况下,样式是直接从特定样式资源导入的.所以,我的样式表规范和跨度标记是正确的,至少对于一种输出格式是正确的.

I've tried sending output temporarily to HTML, and in that situation the styles are imported directly from the specific style asset. So, my stylesheet specification and span markup is correct, at least for one output format.

一些事后的想法:

  • 解决方案不一定是 Pandoc 或 Markdown.但是,它确实需要是一种可以可靠地转换为 PDF 的简单的基于文本的标记语言,因为我想将文档文件存储在 Git 上以便于分叉和合并.我不喜欢 HTML,因为它很冗长,而且转换它的引擎也不是很好(尽管我的格式要求不高).
  • Pandoc 的 HTML 输出很好,所以如果我能找到将(简单)HTML/CSS 可靠地转换为 PDF 的东西,我会没事的.当然,Pandoc 应该能够做到这一点,但内联样式(用于代码片段的背景颜色)不会被渲染.这可能是个小问题,因为我必须重新引入分页符之类的东西,这在 HTML 到 PDF 转换器中可能很重要.

推荐答案

我根本不知道 LaTeX,但是我用 这个有用的手册.首先,创建一个样式:

I don't know LaTeX at all, but have hacked this solution using this helpful manual. First, create a style:

definecolor{silver}{RGB}{230,230,230}


ewcommand{inlinecodeblock}[1]{
    colorbox{silver}{
        	exttt{#1}
    }
}

下面是如何使用它:

Some inlinecodeblock{inline code}, and some widgets, go here

这将创建一个具有背景颜色和等宽字体的样式.根据我的喜好,边距和填充有点大,但这是一个非常有用的开始.这是它的样子:

This creates a style with a background colour and a monospaced font. The margin and padding are a bit large for my preferences, but it's a very useable start. Here's what it looks like:

缺点是,如果我希望输出到支持适当样式的格式(例如 HTML),那么这些都会丢失.此外,我的解决方案仅适用于 LaTeX/PDF.因此,如果您可以解决这些问题,请添加更好的答案!

The disadvantage is that if I wish to output to a format that supports styles proper (such as HTML) then these are lost. Also, my solution only works with LaTeX/PDF. Thus, if you can fix these issues, please add a better answer!

附录:我有一个更好的方法,那就是:

Addendum: I have a better approach, which is thus:


ewcommand{inlinecodeblock}[1]{
    fboxsep 1pt
    fboxrule 0pt
    colorbox{silver}{strut{	exttt{#1}}}
}

这避免了水平填充过多的问题——我认为是 colorbox 参数中的换行符造成的.我在 strut 中添加了,它使高亮保持相同的高度,无论文本是否有下降.

This avoids the problem of excess horizontal padding - I think it was the line break in the colorbox parameter that did it. I've added in strut, which keeps highlights the same height regardless of whether the text has descenders.

虽然它并不完美 - 框外的水平边距仍然太多,框后的逗号仍然会孤立到下一行.我可能会放弃 LaTeX,从 Pandoc 渲染成 HTML,然后使用 wkhtmltopdf 渲染最终文档.

It's not perfect though - there's still too much horizontal margin outside the box, and a comma after a box will still orphan onto the next line. I may give up with LaTeX, and render to HTML from Pandoc, and then use wkhtmltopdf to render the final document.

这篇关于使用 Pandoc 将 Markdown 转换为 PDF 时使用 CSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆