PigLatin.hs 999 B

123456789101112131415161718192021222324252627282930313233
  1. module PigLatin (plugin) where
  2. -- This plugin converts a page to pig latin if the 'language' metadata
  3. -- field is set to 'pig latin'. This demonstrates how to get access to
  4. -- metadata in a plugin.
  5. import Network.Gitit.Interface
  6. import Data.Char (toLower, toUpper, isLower, isUpper, isLetter)
  7. plugin :: Plugin
  8. plugin = PageTransform $ \doc -> do
  9. meta <- askMeta
  10. case lookup "language" meta of
  11. Just s | map toLower s == "pig latin" ->
  12. return $ processWith pigLatinStr doc
  13. _ -> return doc
  14. pigLatinStr :: Inline -> Inline
  15. pigLatinStr (Str "") = Str ""
  16. pigLatinStr (Str (c:cs)) | isLower c && isConsonant c =
  17. Str (cs ++ (c : "ay"))
  18. pigLatinStr (Str (c:cs)) | isUpper c && isConsonant c =
  19. Str (capitalize cs ++ (toLower c : "ay"))
  20. pigLatinStr (Str x@(c:_)) | isLetter c = Str (x ++ "yay")
  21. pigLatinStr x = x
  22. isConsonant :: Char -> Bool
  23. isConsonant c = c `notElem` "aeiouAEIOU"
  24. capitalize :: String -> String
  25. capitalize "" = ""
  26. capitalize (c:cs) = toUpper c : cs