Fan*_*ien 5 haskell filter pandoc hakyll
我很抱歉提出这样的问题.但我对Haskell真的很新.我在互联网上搜索了一整天,但没有找到任何例子.
我有一个用python(tikzcd.py)编写的pandoc过滤器.我想使用该过滤器来处理我的博客帖子.
我想我需要使用unixFilter或者pandocCompileWithTransform我对Haskell的知识真的不足以自己找到解决方案.
那么,有人能为我提供一个例子吗?
- - - - - -更新 - - - - - - - -
@Michael使用pandocCompileWithTransformM和提供解决方案unixFilter.有用.但有一个问题.
从命令行使用过滤器时,我要做的是
pandoc -t json -READEROPTIONS input.markdown | ./filter.py | pandoc -f JSON -WRITEROPTIONS -o output.html
Run Code Online (Sandbox Code Playgroud)
或者等价的Run Code Online (Sandbox Code Playgroud)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
Run Code Online (Sandbox Code Playgroud)
不同之处在于传递给的文本filter.py是不同的:一个是直接从markdown生成的内容,另一个是从markdown生成的HTML文本.如你所知,来回转换东西总会产生意想不到的问题.所以我认为可能有更好的解决方案.
PS.我盯着学习Haskell.我希望有一天我能自己解决这个问题.谢谢!
最后我想你会同时使用两者。使用此https://github.com/listx/listx_blog/blob/master/blog.hs作为模型,以下内容将具有与transformer其中相同的形状。在第 69-80 行transformer用于“posts” ——即作为 的第三个参数,这是一个这里你需要添加 python 过滤器的绝对路径——或者名称(如果它在 $PATH 中)——并且读取器和写入器选项(例如和 )pandocCompilerWithTransformM(Pandoc -> Compiler Pandoc)defaultHakyllReaderOptionsdefaultHakyllWriterOptions
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
Run Code Online (Sandbox Code Playgroud)
类似地,(transformer "/usr/local/bin/myfilter.py" defaultHakyllReaderOptions defaultHakyllWriterOptions)可以在本示例要点(return . pandocTransform)的第 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
Run Code Online (Sandbox Code Playgroud)
该块的三行do相当于带有 pandoc -t json | filter.py | pandoc -f json任何附加参数的 UNIX 管道输入的阶段。
我想也许你是对的,这里有一个额外的 pandoc 层来回。这些pandocCompilerWithTransform(M)函数用于直接 Pandoc-> Pandoc 函数 - 它将应用于 hakyll 带来的 Pandoc。我认为我们应该放弃这个并直接使用 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
Run Code Online (Sandbox Code Playgroud)
我希望这些原则能够从这些变化中浮现出来!这应该与前面的几乎相同transform;我们使用 pandoc 库来代替对pandoc可执行文件的调用。