Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. import Text.Pandoc
  2. import Text.Pandoc.JSON
  3. import Text.Pandoc.Builder
  4. import Control.Monad.State
  5. import qualified Data.Map as M
  6.  
  7. main :: IO ()
  8. main = toJSONFilter go
  9. where
  10. go (Just (Format "latex")) p = bottomUp runLatex p
  11. go _ (Pandoc meta body) = Pandoc meta $ runOther body
  12. runLatex s@(Span (_id', cls, _props) val:xs)
  13. | "index" `elem` cls = concat
  14. [ [RawInline (Format "latex") "\\index{"]
  15. , val
  16. , [RawInline (Format "latex") "}"]
  17. , xs
  18. ]
  19. | otherwise = s
  20. runLatex x = x
  21. runOther :: [Block] -> [Block]
  22. runOther body = addIndex $ evalState (bottomUpM makeIds body) (1 :: Integer)
  23. addIndex :: [Block] -> [Block]
  24. addIndex bs = bs ++ toList idx
  25. where
  26. idx = header 1 (str "Index") <> definitionList (cat $ queryWith getIdx bs)
  27. makeIds s@(Span (_id', cls, props) val)
  28. | "index" `elem` cls = do
  29. curIdx <- get
  30. put $ curIdx + 1
  31. return $ Span ("index-id-no-" ++ show curIdx, cls, props) val
  32. | otherwise = return s
  33. makeIds x = return x
  34. getIdx :: Inline -> [(Inlines, Inlines)]
  35. getIdx (Span (id', cls, _props) val)
  36. | "index" `elem` cls
  37. = [(strong (fromList val), link ('#' : id') [] (str "idx"))]
  38. | otherwise
  39. = []
  40. getIdx _ = []
  41. cat = M.toList . M.map (return . plain) . M.fromListWith
  42. (\x acc -> acc <> str "," <> space <> x)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement