Can I automatically embed pandoc's default templates in my application?

China☆狼群 提交于 2019-12-23 21:26:36

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/35491660/can-i-automatically-embed-pandocs-default-templates-in-my-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!