且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以自动将pandoc的默认模板嵌入到我的应用程序中吗?

更新时间:2023-12-06 15:01:52

事实证明, pandoc 文件通过 hsb2hs 进行构建。不知何故,这个步骤在 stack build 期间失败了。我错过了错误消息。
$ b hsb2hs 及其主要依赖项 processing-tools 是堆栈的LTS的一部分,他们只在夜间堆栈版本中。以下是对 stack.yaml 的补充,解决了问题:

 #part stack.yaml:
extra-deps:
- preprocessor-tools-1.0.1
- hsb2hs-0.3.1
- pandoc-1.16

flags:
pandoc:
embed_data_files:true

对于那些使用Cabal的人来说,这有点等于

  cabal sandbox init 
cabal update
cabal install hsb2hs-0.3.1& amp ;&安培; cabal install pandoc-1.16 -f embed_data_files
cabal install --dependencies-only
cabal build

以下是我验证模板实际包含的内容:

  $ stack build 
$ grepusepackage \ {hyperref \}.stack-work / install / * / bin / example -a
\ usepackage [$ for(geometry)$$ geometry $$ sep $,$ endfor $] {geometry}
$ endif $
\ usepackage {hyperref}
$ if(colorlinks)$
\PassOptionsToPackage {usenames,dvipsnames} {color}%color由hyperref加载

该片段是 default.latex 的一部分,所以它真的包含在二进制文件中。


Pandoc comes with several default templates, which are distributed with the pandoc package. However, if I write an application that uses pandoc as a library, those default templates don't get included in the binary. I can still use them on my machine:

module Main where
import Text.Pandoc (getDefaultTemplate)

main = getDefaultTemplate Nothing "latex" >>= print

This will print the default.latex template. However, it's not portable, since it really refers to a file somewhere on my system:

$ cd path/to/example/project
$ stack build
$ scp path/to/binary remote:remote/path
$ ssh remote:remote/path/binary
example: Could not find data file /home/Zeta/.stack/snapshots/.../pandoc-1.16.0.2/data/templates/default.latex

Since pandoc's debian package does not include those files, it's somehow able to embed them. And indeed, there is a flag -f embed_data_files. I've tried to enable it in the local stack.yaml:

extra-deps: [pandoc-1.16]
flags: 
  pandoc:
    embed_data_files: true

But that didn't change anything, the compiled binary still complains about missing data files.

So, is there any way to automatically include pandoc's template files?

It turns out that pandoc injects its data files during its build via hsb2hs. Somehow that step failed during stack build I missed the error message.

Neither hsb2hs nor its main dependency processing-tools are part of stack's LTS, they're only in the nightly stackage versions. The following additions to stack.yaml fixed the problem:

# part of stack.yaml:
extra-deps:
- preprocessor-tools-1.0.1
- hsb2hs-0.3.1
- pandoc-1.16

flags:
  pandoc:
    embed_data_files: true

For those using Cabal, this is somewhat equal to

cabal sandbox init
cabal update
cabal install hsb2hs-0.3.1 && cabal install pandoc-1.16 -f embed_data_files
cabal install --dependencies-only
cabal build

Here's how I verified that the templates are actually included:

$ stack build
$ grep "usepackage\{hyperref\}" .stack-work/install/*/bin/example -a
\usepackage[$for(geometry)$$geometry$$sep$,$endfor$]{geometry}
$endif$
\usepackage{hyperref}
$if(colorlinks)$
\PassOptionsToPackage{usenames,dvipsnames}{color} % color is loaded by hyperref

That snippet is part of default.latex, so it's really included in the binary.