[英]How to use Pandoc filter within Hakyll?
我很抱歉提出这样的问题。 但我对Haskell真的很新。 我在互联网上搜索了一整天,但没有找到任何例子。
我有一个用python编写的pandoc过滤器( tikzcd.py
)。 我想使用该过滤器来处理我的博客帖子。
我想我需要使用unixFilter
或pandocCompileWithTransform
但我对Haskell的了解真的不足以找到解决方案。
那么,有人能为我提供一个例子吗?
- - - - - -更新 - - - - - - - -
@Michael使用pandocCompileWithTransformM
和unixFilter
提供解决方案。 有用。 但有一个问题。
从命令行使用过滤器时,我要做的是
pandoc -t json -READEROPTIONS input.markdown | ./filter.py | pandoc -f JSON -WRITEROPTIONS -o output.html
或者等价的pandoc --filter ./filter.py -READEROPTIONS -WRITEROPTIONS -o html
此命令较短但不显示过程。
但是使用pandocCompilerTransformM
,它可以做类似的事情
pandoc -t html -READEROPTIONS -WRITEROPTIONS input.mardown | pandoc -t JSON | ./filter.py | pandoc -f JSON -WRITEROPTIONS -o output.html
不同之处在于传递给filter.py
的文本是不同的:一个是从markdown直接生成的内容,另一个是从markdown生成的HTML文本。 如你所知,来回转换东西总会产生意想不到的问题。 所以我认为可能有更好的解决方案。
PS。 我盯着学习Haskell。 我希望有一天我能自己解决这个问题。 谢谢!
最后我想你会同时使用两者。 使用此https://github.com/listx/listx_blog/blob/master/blog.hs作为模型,以下内容与transformer
具有相同的形状。 transformer
在第69-80行用于'posts' - 这是pandocCompilerWithTransformM
的第三个参数,它是一个(Pandoc -> Compiler Pandoc)
这里你需要添加你的python过滤器的绝对路径 - 或者名称,如果它在$ PATH中 - 以及读者和作者选项(例如defaultHakyllReaderOptions
和defaultHakyllWriterOptions
)
import Text.Pandoc
import Hakyll
type Script = String
transformer
:: Script -- e.g. "/absolute/path/filter.py"
-> ReaderOptions -- e.g. defaultHakyllReaderOptions
-> WriterOptions -- e.g. defaultHakyllWriterOptions
-> (Pandoc -> Compiler Pandoc)
transformer script reader_opts writer_opts pandoc =
do let input_json = writeJSON writer_opts pandoc
output_json <- unixFilter script [] input_json
return $
-- either (error.show) id $ -- this line needs to be uncommented atm.
readJSON reader_opts output_json
类似地, (transformer "/usr/local/bin/myfilter.py" defaultHakyllReaderOptions defaultHakyllWriterOptions)
可能会在使用的地方使用(return . pandocTransform)
,在此示例gist的第125行
对于调试,您可以将所有内容外包给unixFilter
:
transform :: Script -> String -> Compiler String
transform script md = do json0 <- unixFilter pandoc input_args md
json1 <- unixFilter script [] json0
unixFilter pandoc output_args json1
where
pandoc = "pandoc"
input_args = words "-f markdown -t json" -- add others
output_args = words "-f json -t html" -- add others
do
块的三行相当于pandoc -t json | filter.py | pandoc -f json
中unix管道的各个阶段 pandoc -t json | filter.py | pandoc -f json
pandoc -t json | filter.py | pandoc -f json
与任何其他参数。
我想也许你说得对,这里有一层额外的pandoc来回。 pandocCompilerWithTransform(M)
函数用于直接Pandoc-> Pandoc函数 - 它将应用于Pandoc hakyll。 我认为我们应该免除这一点并直接使用Pandoc库。 使用unixCompile
可能是这样的。
transformXLVI :: Script -> ReaderOptions -> WriterOptions -> String -> Compiler Html
transformXLVI script ropts wopts = fmap fromJSON . unixFilter script [] . toJSON
where
toJSON = writeJSON wopts
-- . either (error . show) id -- for pandoc > 1.14
. readMarkdown ropts
fromJSON = writeHtml wopts
-- . either (error . show) id
. readJSON ropts
我希望这些原则正在从这些变化出现! 这应该与前面的transform
几乎相同; 我们使用pandoc库代替对pandoc
可执行文件的调用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.