-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dex.hs
118 lines (109 loc) · 3.33 KB
/
Dex.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}
module Text.Pandoc.Readers.Dex where
import Control.Monad.IO.Class (MonadIO (liftIO))
import Control.Monad.Reader (MonadReader (ask), ReaderT (runReaderT))
import Data.Foldable (Foldable (fold))
import qualified Data.Text as T
import qualified Err as Dex
import qualified Syntax as Dex
( Output (HtmlOut, TextOut),
Result (Result),
SourceBlock (sbContents),
SourceBlock' (..),
)
import qualified Text.Pandoc as Pandoc
import qualified Text.Pandoc.Builder as Pandoc
import qualified Text.Pandoc.Parsing as Pandoc
import qualified TopLevel as Dex
readDex ::
(MonadIO m, Pandoc.PandocMonad m, Pandoc.ToSources a) =>
Pandoc.ReaderOptions ->
Dex.EvalConfig ->
a ->
m Pandoc.Pandoc
readDex pandocOpts dexOpts s = flip runReaderT pandocOpts $ do
dexEnv <- liftIO Dex.loadCache
let source = T.unpack . Pandoc.sourcesToText . Pandoc.toSources $ s
(results, dexEnv') <-
liftIO $
Dex.runTopperM dexOpts dexEnv $
Dex.evalSourceText
source
Dex.storeCache dexEnv'
fold <$> traverse toPandoc results
class ToPandoc a where
toPandoc ::
( MonadReader Pandoc.ReaderOptions m,
Pandoc.PandocMonad m
) =>
a ->
m Pandoc.Pandoc
instance (ToPandoc a, ToPandoc b) => ToPandoc (a, b) where
toPandoc (a, b) = do
Pandoc.Pandoc meta blocks <- toPandoc a
Pandoc.Pandoc meta' blocks' <- toPandoc b
return $ Pandoc.Pandoc (meta <> meta') (blocks <> blocks')
instance ToPandoc Dex.Result where
toPandoc (Dex.Result outs err) = do
outsBlocks <- fold <$> traverse toPandoc outs
errBlocks <- toPandoc err
return $ outsBlocks <> errBlocks
instance ToPandoc (Dex.Except ()) where
toPandoc err = case err of
Dex.Failure er ->
pure
. Pandoc.Pandoc mempty
. Pandoc.toList
. Pandoc.codeBlock
. T.pack
$ Dex.pprint er
Dex.Success _x0 -> pure mempty
instance ToPandoc Dex.Output where
toPandoc out = case out of
Dex.TextOut s ->
pure
. Pandoc.Pandoc mempty
. Pandoc.toList
. Pandoc.blockQuote
. Pandoc.codeBlock
. T.pack
$ s
Dex.HtmlOut s -> do
pandocOpts <- ask
Pandoc.readHtml pandocOpts . T.pack $ s
-- Dex.PassInfo pn s -> undefined
-- Dex.EvalTime x ma -> undefined
-- Dex.TotalTime x -> undefined
-- Dex.BenchResult s x y ma -> undefined
-- Dex.MiscLog s -> undefined
_ ->
pure
. Pandoc.Pandoc mempty
. Pandoc.toList
. Pandoc.codeBlock
. T.pack
$ Dex.pprint out
instance ToPandoc Dex.SourceBlock where
toPandoc sourceBlock = case Dex.sbContents sourceBlock of
-- Dex.EvalUDecl ud -> undefined
-- Dex.Command cn wse -> undefined
-- Dex.DeclareForeign s uab -> undefined
-- Dex.GetNameType s -> undefined
-- Dex.ImportModule msn -> undefined
-- Dex.QueryEnv eq -> undefined
Dex.ProseBlock s -> do
pandocOpts <- ask
Pandoc.readCommonMark pandocOpts . T.pack $ s
-- Dex.CommentLine -> undefined
Dex.EmptyLines -> pure mempty
-- Dex.UnParseable b s -> undefined
_ ->
pure
. Pandoc.Pandoc mempty
. Pandoc.toList
. Pandoc.codeBlockWith ("", ["dex"], [])
. T.pack
$ Dex.pprint sourceBlock