Created
August 14, 2014 15:55
-
-
Save trans/15d673ef6ba61018430f to your computer and use it in GitHub Desktop.
Index filter for Pandoc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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