0 | ||| Communication with Pandoc
 1 | module Goop.Pandoc.Process
 2 |
 3 | import System
 4 | import System.Term
 5 | import System.File
 6 | import Text.PrettyPrint.Bernardy
 7 | import JSON.Simple
 8 |
 9 | import Goop.Pandoc.JSON
10 | import Goop.Util.IO
11 |
12 | public export
13 | data InputFormat
14 |   = GFM
15 |
16 | export
17 | Interpolation InputFormat where
18 |   interpolate GFM = "gfm"
19 |
20 | export
21 | parseDocument : HasIO io => (fmt : InputFormat) -> (path : String) -> io Pandoc
22 | parseDocument fmt path = do
23 |   let pandoc_part = "pandoc -f \{fmt} -t json \{path}"
24 |   -- FIXME: This is working around a limitation in json-simple
25 |   let jq_part =
26 |     "jq 'walk(if type == \"object\" and keys == [\"t\"] then . + {\"c\": null} else . end)'"
27 |   let command = "\{pandoc_part} | \{jq_part}"
28 |   Right pipe <- popen command Read
29 |     | Left err => fail "Pandoc Call: \{show err}"
30 |   Right output <- fRead pipe
31 |     | Left err => fail "Pandoc Pipe: \{show err}"
32 |   let res : DecodingResult Pandoc = decode output
33 |   Right pandoc <- pure res
34 |     | Left err => fail "Parsing pandoc output: \{show err}"
35 |   pure pandoc
36 |
37 | main : IO ()
38 | main = do
39 |   [_, path] <- getArgs
40 |     | xs => fail "Invalid augments: \{show xs}"
41 |   setupTerm
42 |   cols <- getTermCols
43 |   putStrLn "Parsing document from \{path} as gfm"
44 |   document <- parseDocument GFM path
45 |   putStrLn $ render (Opts (cast cols)) (pretty document)
46 |