|
| 1 | +#! /usr/bin/env runghc |
| 2 | + |
| 3 | + |
| 4 | +-- Project-specific Haskell port of Nick Ulle's pandoc-minted.py: |
| 5 | +-- https://github.com/nick-ulle/pandoc-minted |
| 6 | + |
| 7 | + |
| 8 | +import Data.List (intercalate) |
| 9 | +import Text.Pandoc.Generic (topDown) |
| 10 | +import Text.Pandoc.JSON |
| 11 | + |
| 12 | + |
| 13 | +data Minted = MintedInline (String, String) String |
| 14 | + | MintedBlock (String, String) String |
| 15 | + |
| 16 | + |
| 17 | +instance Show Minted where |
| 18 | + show (MintedInline (attrs, language) contents) = |
| 19 | + "\\mintinline[" ++ attrs ++ "]{" ++ language ++ "}{" ++ contents ++ "}" |
| 20 | + show (MintedBlock (attrs, language) contents) = |
| 21 | + unlines [ "\\begin{minted}[" ++ attrs ++ "]{" ++ language ++ "}" |
| 22 | + , contents |
| 23 | + , "\\end{minted}" |
| 24 | + ] |
| 25 | + |
| 26 | + |
| 27 | +main :: IO () |
| 28 | +main = toJSONFilter minted |
| 29 | + |
| 30 | + |
| 31 | +minted :: Pandoc -> Pandoc |
| 32 | +minted = topDown (concatMap mintinline) . |
| 33 | + topDown (concatMap mintedBlock) |
| 34 | + |
| 35 | + |
| 36 | +mintinline :: Inline -> [Inline] |
| 37 | +mintinline (Code attr contents) = |
| 38 | + let |
| 39 | + latex = show $ MintedInline (unpackCode attr "text") contents |
| 40 | + in |
| 41 | + [ RawInline (Format "latex") latex ] |
| 42 | +mintinline x = [x] |
| 43 | + |
| 44 | + |
| 45 | +mintedBlock :: Block -> [Block] |
| 46 | +mintedBlock (CodeBlock attr contents) = |
| 47 | + let |
| 48 | + latex = show $ MintedBlock (unpackCode attr "text") contents |
| 49 | + in |
| 50 | + [ RawBlock (Format "latex") latex ] |
| 51 | +mintedBlock x = [x] |
| 52 | + |
| 53 | + |
| 54 | +unpackCode :: Attr -> String -> (String, String) |
| 55 | +unpackCode (_, [], kvs) defaultLanguage = |
| 56 | + (unpackAttrs kvs, defaultLanguage) |
| 57 | +unpackCode (identifier, "sourceCode" : _, kvs) defaultLanguage = |
| 58 | + unpackCode (identifier, ["idris"], kvs) defaultLanguage |
| 59 | +unpackCode (_, language : _, kvs) _ = |
| 60 | + (unpackAttrs kvs, language) |
| 61 | + |
| 62 | + |
| 63 | +unpackAttrs :: [(String, String)] -> String |
| 64 | +unpackAttrs kvs = intercalate ", " [ k ++ "=" ++ v | (k, v) <- kvs ] |
0 commit comments