Created
August 12, 2014 16:24
-
-
Save mpickering/8bc9bb34c4e9b076b107 to your computer and use it in GitHub Desktop.
This file contains 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
import Text.Pandoc.JSON | |
import Text.Parsec | |
import Control.Applicative | |
import Data.Monoid | |
main :: IO () | |
main = toJSONFilter index | |
index :: Maybe Format -> Inline -> [Inline] | |
index (Just (Format f)) c@(Code _ s) = | |
case parse parseIndex "" s of | |
Left _ -> [c] | |
Right (term, subterm) -> | |
case f of | |
"html" -> [rawHtml term subterm] | |
"latex" -> [rawLaTeX term subterm] | |
_ -> [] | |
index _ x = [x] | |
rawHtml :: String -> String -> Inline | |
rawHtml term subterm = RawInline (Format "html") ("<span id=" ++ term ++ ":" ++ subterm ++" class=\"index\"></a>") | |
rawLaTeX :: String -> String -> Inline | |
rawLaTeX term subterm = RawInline (Format "latex") ("\\index{"++term++"!"++subterm ++ "}") | |
parseIndex :: Parsec String () (String, String) | |
parseIndex = do | |
string "(#" | |
term <- spaces *> many1 (noneOf " ,") <* spaces | |
char ',' | |
subterm <- spaces *> many1 (noneOf " )") <* spaces | |
char ')' | |
return (term,subterm) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment