我可以自动将pandoc的默认模板嵌入到我的应用程序中吗? [英] Can I automatically embed pandoc's default templates in my application?

查看:189
本文介绍了我可以自动将pandoc的默认模板嵌入到我的应用程序中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pandoc附带了几个默认模板,这些模板随< pandoc 包。但是,如果我编写的应用程序使用 pandoc 作为库,那么这些默认模板不会包含在二进制文件中。我仍然可以在我的机器上使用它们:

 模块Main其中
import Text.Pandoc(getDefaultTemplate)

main = getDefaultTemplate Nothinglatex>> = print

default.latex 模板。但是,它不是可移植的,因为它确实指向我系统上的某个文件:

  $ cd path / to / example /项目
$堆栈建立
$ scp路径/ to / binary远程:远程/路径
$ ssh远程:远程/路径/二进制
例如:找不到数据文件/ home /Zeta/.stack/snapshots/.../pandoc-1.16.0.2/data/templates/default.latex

因为 pandoc的debian软件包不包含这些文件,所以以某种方式能够嵌入它们。事实上,有一个标志 -f embed_data_files 。我试图在本地 stack.yaml 中启用它:

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

但是这并没有改变任何东西,编译后的二进制文件仍然抱怨缺少数据文件。

所以,有没有办法自动包含pandoc的模板文件?

解决方案

事实证明, 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.

这篇关于我可以自动将pandoc的默认模板嵌入到我的应用程序中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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