How to use Pandoc filter in Hakyll?

I apologize for such a question. But I'm really new to Haskell. I searched the Internet all day, but found no example.

I have a pandoc filter written in python ( tikzcd.py). I want to use this filter to process blog posts.

I think I need to use unixFilteror pandocCompileWithTransform, but my knowledge for Haskell is really insufficient to find a solution on my own.

So can anyone provide me an example?

----------- U - P - D - A - T - E - C ---------------

@Michael gives a solution using pandocCompileWithTransformMand unixFilter. It works. But there's a problem.

When using a filter from the command line I will do

pandoc -t json -READEROPTIONS input.markdown | ./filter.py | pandoc -f JSON -WRITEROPTIONS -o output.html

or equivalent

pandoc --filter ./filter.py -READEROPTIONS -WRITEROPTIONS -o html

This command is shorter, but it does not display the procedure.

pandocCompilerTransformM -

pandoc -t html -READEROPTIONS -WRITEROPTIONS input.mardown | pandoc -t JSON | ./filter.py | pandoc -f JSON -WRITEROPTIONS -o output.html

, , filter.py, : , , - , HTML, . , - . , .

PS. . , . !

+4
1

, , . 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), 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 unix- 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.

+4

Source: https://habr.com/ru/post/1584650/


All Articles