blob: c9087e7794e3263b92eb95f6fe823e4b699c7f38 (
plain)
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
|
{-# LANGUAGE TemplateHaskell #-}
module Hakyll.TeX.TikZ
( TikzTemplate
, tikzFilter
, tikzBlockFilter
-- * Using the default TikZ template
, tikzTemplate
, tikzFilter_
, tikzBlockFilter_
) where
import Network.URI.Encode qualified as URI
import Text.Pandoc.Definition
import Text.Pandoc.Walk (walkM)
import Hakyll
import Prolog
import Hakyll.TeX.LaTeX (rubberPipe)
type TikzTemplate = Template
tikzFilter :: TikzTemplate -> Pandoc -> Compiler Pandoc
tikzFilter = walkM . tikzBlockFilter
-- | Requires `rubber` and `poppler_utils`
tikzBlockFilter :: TikzTemplate -> Block -> Compiler Block
tikzBlockFilter tmpl (CodeBlock info@(_, "tikzpicture":_, _) contents) = do
let imageBlock fname = Para [ Image info [] (toSL fname, "") ]
makeItem (toSL contents) >>= applyTemplate tmpl (bodyField "body") >>=
traverse (convSL $ rubberPipe [ "--pdf" ] >=> unixFilterLBS "pdftocairo" [ "-svg", "-", "-" ]) >>=
\(Item _ b) -> pure $ imageBlock $ ("data:image/svg+sml;utf8," <>) $ URI.encode $ filter (/= '\n') b
tikzBlockFilter _ x = pure x
tikzTemplate :: TikzTemplate
tikzTemplate = $(embedTemplate "templates/tikz.tex")
tikzBlockFilter_ :: Block -> Compiler Block
tikzBlockFilter_ = tikzBlockFilter tikzTemplate
tikzFilter_ :: Pandoc -> Compiler Pandoc
tikzFilter_ = tikzFilter tikzTemplate
|