Skip to content

Commit 19ef96b

Browse files
committed
pandoc-minted: Python -> Haskell
1 parent b96c785 commit 19ef96b

File tree

2 files changed

+64
-109
lines changed

2 files changed

+64
-109
lines changed

src/pandoc-minted.hs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 ]

src/pandoc-minted.py

Lines changed: 0 additions & 109 deletions
This file was deleted.

0 commit comments

Comments
 (0)