Skip to content

Instantly share code, notes, and snippets.

@mpickering
Created August 12, 2014 16:24
Show Gist options
  • Save mpickering/8bc9bb34c4e9b076b107 to your computer and use it in GitHub Desktop.
Save mpickering/8bc9bb34c4e9b076b107 to your computer and use it in GitHub Desktop.
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