forked from sdiehl/kaleidoscope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocessor.hs
46 lines (38 loc) · 1.42 KB
/
preprocessor.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
{-# LANGUAGE OverloadedStrings #-}
import Text.Read
import Control.Monad.State
import Text.Pandoc
slice :: Int -> Int -> [a] -> [a]
slice from to xs = take (to - from + 1) (drop from xs)
doSlice :: Block -> IO Block
doSlice cb@(CodeBlock (id, classes, namevals) contents) = do
res <- return $ do
upper <- readMaybe =<< lookup "upper" namevals
lower <- readMaybe =<< lookup "lower" namevals
file <- lookup "slice" namevals
return (upper, lower, file)
case res of
Nothing -> return cb
Just (upper, lower, f) -> do
contents <- readFile f
let lns = unlines $ slice lower upper (lines contents)
return (CodeBlock (id, classes, namevals) lns)
doSlice x = return x
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
doHtml :: Block -> IO Block
doHtml cb@(CodeBlock (id, classes, namevals) contents) =
case lookup "literal" namevals of
Just f -> return . (RawBlock "html") =<< readFile f
Nothing -> return cb
doHtml x = return x
main :: IO ()
main = getContents >>= return . readMarkdown def
>>= bottomUpM doInclude
>>= bottomUpM doSlice
>>= bottomUpM doHtml
>>= putStrLn . writeMarkdown def