最后,我认为您会同时使用两者。使用此https://github.com/listx/listx_blog/blob/master/blog.hs作为模型,以下将具有与其中相同的transformer
形状。transformer
在第 69-80 行用于 'posts' - 这是作为 的第三个参数pandocCompilerWithTransformM
,这是一个(Pandoc -> Compiler Pandoc)
在这里你需要添加绝对路径到你的 python 过滤器 - 或者如果它在 $PATH 中的名称 - 和读取器和写入器选项(例如defaultHakyllReaderOptions
and 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)
使用,在本示例要点的第 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。这些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
可执行文件的调用。