Skip to content

Instantly share code, notes, and snippets.

@trans
Created August 14, 2014 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trans/15d673ef6ba61018430f to your computer and use it in GitHub Desktop.
Save trans/15d673ef6ba61018430f to your computer and use it in GitHub Desktop.
Index filter for Pandoc
#!/usr/bin/env runhaskell
import Text.Pandoc
import Text.Pandoc.Shared
import Text.Pandoc.JSON
import Text.Parsec
import Text.Regex
import Control.Applicative
import Data.Monoid
import Data.List
import Data.String
main :: IO ()
main = toJSONFilter index
index :: Maybe Format -> Inline -> [Inline]
index (Just (Format f)) (Code a c)
| (take 2 c) == "# " = drawIndex f c
| otherwise = [Code a c]
index _ x = [x]
rawHtml :: [String] -> Inline
rawHtml [t] = RawInline (Format "html") ("<a id=" ++ (intercalate ":" [t]) ++ " class=\"index\"></a>")
rawLaTeX :: [String] -> Inline
rawLaTeX [t] = RawInline (Format "latex") ("\\index{" ++ (intercalate "!" [t]) ++ "}")
drawIndex :: String -> String -> [Inline]
drawIndex f c =
case f of
"html" -> [rawHtml (terms c)]
"latex" -> [rawLaTeX (terms c)]
_ -> []
terms :: String -> [String]
terms c = splitRegex (mkRegex "\\s*,\\s*") (drop 2 c)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment