    {-# LANGUAGE OverloadedStrings #-}
    {- |
       Module      : Text.Pandoc.Readers.LaTeX.Lang
       Copyright   : Copyright (C) 2018-2020 John MacFarlane
       License     : GNU GPL, version 2 or above

       Maintainer  : John MacFarlane <jgm@berkeley.edu>
       Stability   : alpha
       Portability : portable

    Functions for parsing polyglossia and babel language specifiers to
    BCP47 'Lang'.
    -}
    module Text.Pandoc.Readers.LaTeX.Lang
      ( polyglossiaLangToBCP47
      , babelLangToBCP47
      )
    where
    import qualified Data.Map as M
    import qualified Data.Text as T
    import Text.Pandoc.BCP47 (Lang(..))

    polyglossiaLangToBCP47 :: M.Map T.Text (T.Text -> Lang)
    polyglossiaLangToBCP47 :: Map Text (Text -> Lang)
    polyglossiaLangToBCP47 = [(Text, Text -> Lang)] -> Map Text (Text -> Lang)
    forall k a. Ord k => [(k, a)] -> Map k a
    M.fromList
      [ ("arabic", \o :: Text
    o -> case (Char -> Bool) -> Text -> Text
    T.filter (Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    /=' ') Text
    o of
           "locale=algeria"    -> Text -> Text -> Text -> [Text] -> Lang
    Lang "ar" "" "DZ" []
           "locale=mashriq"    -> Text -> Text -> Text -> [Text] -> Lang
    Lang "ar" "" "SY" []
           "locale=libya"      -> Text -> Text -> Text -> [Text] -> Lang
    Lang "ar" "" "LY" []
           "locale=morocco"    -> Text -> Text -> Text -> [Text] -> Lang
    Lang "ar" "" "MA" []
           "locale=mauritania" -> Text -> Text -> Text -> [Text] -> Lang
    Lang "ar" "" "MR" []
           "locale=tunisia"    -> Text -> Text -> Text -> [Text] -> Lang
    Lang "ar" "" "TN" []
           _                   -> Text -> Text -> Text -> [Text] -> Lang
    Lang "ar" "" "" [])
      , ("german", \o :: Text
    o -> case (Char -> Bool) -> Text -> Text
    T.filter (Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    /=' ') Text
    o of
           "spelling=old" -> Text -> Text -> Text -> [Text] -> Lang
    Lang "de" "" "DE" ["1901"]
           "variant=austrian,spelling=old"
                           -> Text -> Text -> Text -> [Text] -> Lang
    Lang "de" "" "AT" ["1901"]
           "variant=austrian" -> Text -> Text -> Text -> [Text] -> Lang
    Lang "de" "" "AT" []
           "variant=swiss,spelling=old"
                           -> Text -> Text -> Text -> [Text] -> Lang
    Lang "de" "" "CH" ["1901"]
           "variant=swiss" -> Text -> Text -> Text -> [Text] -> Lang
    Lang "de" "" "CH" []
           _ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "de" "" "" [])
      , ("lsorbian", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "dsb" "" "" [])
      , ("greek", \o :: Text
    o -> case (Char -> Bool) -> Text -> Text
    T.filter (Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    /=' ') Text
    o of
           "variant=poly"    -> Text -> Text -> Text -> [Text] -> Lang
    Lang "el" "" "polyton" []
           "variant=ancient" -> Text -> Text -> Text -> [Text] -> Lang
    Lang "grc" "" "" []
           _                 -> Text -> Text -> Text -> [Text] -> Lang
    Lang "el" "" "" [])
      , ("english", \o :: Text
    o -> case (Char -> Bool) -> Text -> Text
    T.filter (Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    /=' ') Text
    o of
           "variant=australian" -> Text -> Text -> Text -> [Text] -> Lang
    Lang "en" "" "AU" []
           "variant=canadian"   -> Text -> Text -> Text -> [Text] -> Lang
    Lang "en" "" "CA" []
           "variant=british"    -> Text -> Text -> Text -> [Text] -> Lang
    Lang "en" "" "GB" []
           "variant=newzealand" -> Text -> Text -> Text -> [Text] -> Lang
    Lang "en" "" "NZ" []
           "variant=american"   -> Text -> Text -> Text -> [Text] -> Lang
    Lang "en" "" "US" []
           _                    -> Text -> Text -> Text -> [Text] -> Lang
    Lang "en" "" "" [])
      , ("usorbian", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "hsb" "" "" [])
      , ("latin", \o :: Text
    o -> case (Char -> Bool) -> Text -> Text
    T.filter (Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    /=' ') Text
    o of
           "variant=classic" -> Text -> Text -> Text -> [Text] -> Lang
    Lang "la" "" "" ["x-classic"]
           _                 -> Text -> Text -> Text -> [Text] -> Lang
    Lang "la" "" "" [])
      , ("slovenian", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "sl" "" "" [])
      , ("serbianc", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "sr" "cyrl" "" [])
      , ("pinyin", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "zh" "Latn" "" ["pinyin"])
      , ("afrikaans", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "af" "" "" [])
      , ("amharic", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "am" "" "" [])
      , ("assamese", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "as" "" "" [])
      , ("asturian", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "ast" "" "" [])
      , ("bulgarian", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "bg" "" "" [])
      , ("bengali", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "bn" "" "" [])
      , ("tibetan", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "bo" "" "" [])
      , ("breton", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "br" "" "" [])
      , ("catalan", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "ca" "" "" [])
      , ("welsh", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "cy" "" "" [])
      , ("czech", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "cs" "" "" [])
      , ("coptic", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "cop" "" "" [])
      , ("danish", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "da" "" "" [])
      , ("divehi", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "dv" "" "" [])
      , ("esperanto", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "eo" "" "" [])
      , ("spanish", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "es" "" "" [])
      , ("estonian", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "et" "" "" [])
      , ("basque", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "eu" "" "" [])
      , ("farsi", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "fa" "" "" [])
      , ("finnish", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "fi" "" "" [])
      , ("french", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "fr" "" "" [])
      , ("friulan", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "fur" "" "" [])
      , ("irish", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "ga" "" "" [])
      , ("scottish", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "gd" "" "" [])
      , ("ethiopic", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "gez" "" "" [])
      , ("galician", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "gl" "" "" [])
      , ("hebrew", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "he" "" "" [])
      , ("hindi", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "hi" "" "" [])
      , ("croatian", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "hr" "" "" [])
      , ("magyar", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "hu" "" "" [])
      , ("armenian", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "hy" "" "" [])
      , ("interlingua", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "ia" "" "" [])
      , ("indonesian", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "id" "" "" [])
      , ("icelandic", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "is" "" "" [])
      , ("italian", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "it" "" "" [])
      , ("japanese", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "jp" "" "" [])
      , ("khmer", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "km" "" "" [])
      , ("kurmanji", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "kmr" "" "" [])
      , ("kannada", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "kn" "" "" [])
      , ("korean", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "ko" "" "" [])
      , ("lao", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "lo" "" "" [])
      , ("lithuanian", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "lt" "" "" [])
      , ("latvian", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "lv" "" "" [])
      , ("malayalam", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "ml" "" "" [])
      , ("mongolian", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "mn" "" "" [])
      , ("marathi", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "mr" "" "" [])
      , ("dutch", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "nl" "" "" [])
      , ("nynorsk", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "nn" "" "" [])
      , ("norsk", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "no" "" "" [])
      , ("nko", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "nqo" "" "" [])
      , ("occitan", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "oc" "" "" [])
      , ("panjabi", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "pa" "" "" [])
      , ("polish", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "pl" "" "" [])
      , ("piedmontese", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "pms" "" "" [])
      , ("portuguese", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "pt" "" "" [])
      , ("romansh", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "rm" "" "" [])
      , ("romanian", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "ro" "" "" [])
      , ("russian", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "ru" "" "" [])
      , ("sanskrit", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "sa" "" "" [])
      , ("samin", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "se" "" "" [])
      , ("slovak", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "sk" "" "" [])
      , ("albanian", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "sq" "" "" [])
      , ("serbian", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "sr" "" "" [])
      , ("swedish", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "sv" "" "" [])
      , ("syriac", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "syr" "" "" [])
      , ("tamil", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "ta" "" "" [])
      , ("telugu", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "te" "" "" [])
      , ("thai", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "th" "" "" [])
      , ("turkmen", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "tk" "" "" [])
      , ("turkish", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "tr" "" "" [])
      , ("ukrainian", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "uk" "" "" [])
      , ("urdu", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "ur" "" "" [])
      , ("vietnamese", \_ -> Text -> Text -> Text -> [Text] -> Lang
    Lang "vi" "" "" [])
      ]

    babelLangToBCP47 :: T.Text -> Maybe Lang
    babelLangToBCP47 :: Text -> Maybe Lang
    babelLangToBCP47 s :: Text
    s =
      case Text
    s of
           "austrian" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text -> Text -> Text -> [Text] -> Lang
    Lang "de" "" "AT" ["1901"]
           "naustrian" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text -> Text -> Text -> [Text] -> Lang
    Lang "de" "" "AT" []
           "swissgerman" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text -> Text -> Text -> [Text] -> Lang
    Lang "de" "" "CH" ["1901"]
           "nswissgerman" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text -> Text -> Text -> [Text] -> Lang
    Lang "de" "" "CH" []
           "german" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text -> Text -> Text -> [Text] -> Lang
    Lang "de" "" "DE" ["1901"]
           "ngerman" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text -> Text -> Text -> [Text] -> Lang
    Lang "de" "" "DE" []
           "lowersorbian" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text -> Text -> Text -> [Text] -> Lang
    Lang "dsb" "" "" []
           "uppersorbian" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text -> Text -> Text -> [Text] -> Lang
    Lang "hsb" "" "" []
           "polutonikogreek" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text -> Text -> Text -> [Text] -> Lang
    Lang "el" "" "" ["polyton"]
           "slovene" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text -> Text -> Text -> [Text] -> Lang
    Lang "sl" "" "" []
           "australian" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text -> Text -> Text -> [Text] -> Lang
    Lang "en" "" "AU" []
           "canadian" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text -> Text -> Text -> [Text] -> Lang
    Lang "en" "" "CA" []
           "british" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text -> Text -> Text -> [Text] -> Lang
    Lang "en" "" "GB" []
           "newzealand" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text -> Text -> Text -> [Text] -> Lang
    Lang "en" "" "NZ" []
           "american" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text -> Text -> Text -> [Text] -> Lang
    Lang "en" "" "US" []
           "classiclatin" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text -> Text -> Text -> [Text] -> Lang
    Lang "la" "" "" ["x-classic"]
           _ -> ((Text -> Lang) -> Lang) -> Maybe (Text -> Lang) -> Maybe Lang
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap ((Text -> Lang) -> Text -> Lang
    forall a b. (a -> b) -> a -> b
    $ "") (Maybe (Text -> Lang) -> Maybe Lang)
    -> Maybe (Text -> Lang) -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text -> Map Text (Text -> Lang) -> Maybe (Text -> Lang)
    forall k a. Ord k => k -> Map k a -> Maybe a
    M.lookup Text
    s Map Text (Text -> Lang)
    polyglossiaLangToBCP47