File: includes.hs

package info (click to toggle)
parallel-hashmap 1.4.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,872 kB
  • sloc: cpp: 20,492; ansic: 1,114; python: 492; makefile: 85; haskell: 56; perl: 43; sh: 23
file content (65 lines) | stat: -rw-r--r-- 2,049 bytes parent folder | download
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
{-# LANGUAGE OverloadedStrings #-}

import Text.Read
import Control.Monad.State
import Control.Monad
import Text.Pandoc
import Data.Monoid
import Control.Applicative

import Text.Pandoc.JSON
import Text.Pandoc.Walk

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

injectLatexMacros :: Maybe Format -> Pandoc -> IO Pandoc
injectLatexMacros (Just fmt) p = do
  macros <- readFile "latex_macros"
  let block =
        case fmt of
          Format "html" ->
            Div ("",[],[("style","display:none")]) . (:[])
              . Para . (:[]) . Math DisplayMath $ macros
          Format "latex" -> RawBlock "latex" macros
          Format "epub" -> RawBlock "latex" macros
          _ -> RawBlock "latex" macros
  return (Pandoc nullMeta [block] <> p)
injectLatexMacros _ _ = return mempty

main :: IO ()
main = toJSONFilter
        ((\fmt -> injectLatexMacros fmt
         >=> walkM doInclude
         >=> walkM doSlice
         >=> walkM doHtml) :: Maybe Format -> Pandoc -> IO Pandoc)