Debian Codegraph @

html/includes.hs

raw
1 {-# LANGUAGE OverloadedStrings #-}
2
3 import Text.Read
4 import Control.Monad.State
5 import Control.Monad
6 import Text.Pandoc
7 import Data.Monoid
8 import Control.Applicative
9
10 import Text.Pandoc.JSON
11 import Text.Pandoc.Walk
12
13 slice :: Int -> Int -> [a] -> [a]
14 slice from to xs = take (to - from + 1) (drop from xs)
15
16 doSlice :: Block -> IO Block
17 doSlice cb@(CodeBlock (id, classes, namevals) contents) = do
18 res <- return $ do
19 upper <- readMaybe =<< lookup "upper" namevals
20 lower <- readMaybe =<< lookup "lower" namevals
21 file <- lookup "slice" namevals
22 return (upper, lower, file)
23
24 case res of
25 Nothing -> return cb
26 Just (upper, lower, f) -> do
27 contents <- readFile f
28 let lns = unlines $ slice lower upper (lines contents)
29 return (CodeBlock (id, classes, namevals) lns)
30 doSlice x = return x
31
32 doInclude :: Block -> IO Block
33 doInclude cb@(CodeBlock (id, classes, namevals) contents) =
34 case lookup "include" namevals of
35 Just f -> return . (CodeBlock (id, classes, namevals)) =<< readFile f
36 Nothing -> return cb
37 doInclude x = return x
38
39 doHtml :: Block -> IO Block
40 doHtml cb@(CodeBlock (id, classes, namevals) contents) =
41 case lookup "literal" namevals of
42 Just f -> return . (RawBlock "html") =<< readFile f
43 Nothing -> return cb
44 doHtml x = return x
45
46 injectLatexMacros :: Maybe Format -> Pandoc -> IO Pandoc
47 injectLatexMacros (Just fmt) p = do
48 macros <- readFile "latex_macros"
49 let block =
50 case fmt of
51 Format "html" ->
52 Div ("",[],[("style","display:none")]) . (:[])
53 . Para . (:[]) . Math DisplayMath $ macros
54 Format "latex" -> RawBlock "latex" macros
55 Format "epub" -> RawBlock "latex" macros
56 _ -> RawBlock "latex" macros
57 return (Pandoc nullMeta [block] <> p)
58 injectLatexMacros _ _ = return mempty
59
60 main :: IO ()
61 main = toJSONFilter
62 ((\fmt -> injectLatexMacros fmt
63 >=> walkM doInclude
64 >=> walkM doSlice
65 >=> walkM doHtml) :: Maybe Format -> Pandoc -> IO Pandoc)