lang/langfilter/langfilter.hs

28 lines
683 B
Haskell

import Text.Pandoc.Definition
import Text.Pandoc.JSON
import Text.Pandoc.Builder
main :: IO ()
main = toJSONFilter \case
Code _ txt
| Just _ <- enclosed '/' '/' txt ->
Span (cls ["ipa", "ipa-broad"]) $ text' txt
| Just _ <- enclosed '[' ']' txt ->
Span (cls ["ipa", "ipa-narrow"]) $ text' txt
| Just txt' <- enclosed '{' '}' txt ->
Span (cls ["lang"]) $ text' txt'
i -> i
cls :: [String] -> Attr
cls cs = ("", cs, [])
text' :: String -> [Inline]
text' = toList . text
enclosed :: Char -> Char -> String -> Maybe String
enclosed o c txt
| length txt >= 2, head txt == o, last txt == c
= Just $ init $ tail txt
enclosed _ _ _ = Nothing