Pandoc Enable file filter in haskell

I am using the haskell pandoc include filter

#!/usr/bin/env runhaskell
-- includes.hs
import Text.Pandoc.JSON

doInclude :: Block -> IO Block
doInclude cb@(CodeBlock (id, classes, namevals) contents) =
  case lookup "include" namevals of
       Just f     -> return . (CodeBlock (id, classes, namevals)) =<< readFile f
       Nothing    -> return cb
doInclude x = return x

main :: IO ()
main = toJSONFilter doInclude

with the following code snippet in markdown

~~~~ {include="tasks/mdbook.js"}
~~~~

This actually includes the file at markdown, but I would like it to also include code formatting, for example

```js
file content here
```

How can I update the above haskell code to do this? with something like

~~~~ {code="tasks/mdbook.js", format="js"}
~~~~
+4
source share
3 answers

Since the filter includesaves all the classescode blocks, you can include the contents of the file and apply code formatting to it simply with this:

~~~~ {.javascript include="tasks/mdbook.js"}
~~~~
+2
source

, , , , , - , . , , .

-. () , . , , , . zip- .

: https://github.com/josefs/CodeExtract

, :

~~~ {.haskell file="Hello.hs"}
main = putStrLn "Hello World!"
~~~

, , Hello.hs, .

, , . , :

~~~ {.haskell template="Hello.hs.tmpl" var="mainfkn"}
main = putStrLn str
~~~

~~~ {.haskell template="Hello.hs.tmpl" var="misc"}
str = "Hello World!"
~~~

, Hello.hs.tmpl, ( , pandoc):

~~~
module Main where
$mainfkn$
$misc$
~~~

Hello.hs, , . .

, , , .

+1

, . :

#!/usr/bin/env runhaskell
-- includes.hs
import Text.Pandoc.JSON

doInclude :: Block -> IO Block
doInclude cb@(CodeBlock (id, classes, namevals) contents) =
  case lookup "include" namevals of
       Just f     -> do
         let newAttrs = filter ((/= "include") . fst) namevals ++ [("code",f), ("format","js")]
         return . (CodeBlock (id, classes, newAttrs)) =<< readFile f
       Nothing    -> return cb
doInclude x = return x

main :: IO ()
main = toJSONFilter doInclude

newAttr .

+1
source

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


All Articles