    {-# LANGUAGE OverloadedStrings #-}
    {- |
       Module      : Text.Pandoc.Readers.LaTeX.Lang
       Copyright   : Copyright (C) 2018-2024 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
      ( setDefaultLanguage
      , polyglossiaLangToBCP47
      , babelLangToBCP47
      , enquoteCommands
      , inlineLanguageCommands
      )
    where
    import qualified Data.Map as M
    import Data.Text (Text)
    import qualified Data.Text as T
    import Text.Pandoc.Shared (extractSpaces)
    import Text.Collate.Lang (Lang(..), renderLang)
    import Text.Pandoc.Class (PandocMonad(..))
    import Text.Pandoc.Translations (setTranslations)
    import Text.Pandoc.Readers.LaTeX.Parsing
    import Text.Pandoc.Parsing (updateState, option, getState, QuoteContext(..),
                                withQuoteContext)
    import Text.Pandoc.Builder (Blocks, Inlines, setMeta, str, spanWith,
                                singleQuoted, doubleQuoted)

    enquote :: PandocMonad m
            => LP m Inlines
            -> Bool -> Maybe Text -> LP m Inlines
    enquote :: forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> Bool -> Maybe Text -> LP m Inlines
    enquote LP m Inlines
    tok Bool
    starred Maybe Text
    mblang = do
      LP m ()
    forall (m :: * -> *). PandocMonad m => LP m ()
    skipopts
      let lang :: Maybe Lang
    lang = Maybe Text
    mblang Maybe Text -> (Text -> Maybe Lang) -> Maybe Lang
    forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= Text -> Maybe Lang
    babelLangToBCP47
      let langspan :: Inlines -> Inlines
    langspan = case Maybe Lang
    lang of
                          Maybe Lang
    Nothing -> Inlines -> Inlines
    forall a. a -> a
    id
                          Just Lang
    l  -> Attr -> Inlines -> Inlines
    spanWith (Text
    "",[],[(Text
    "lang", Lang -> Text
    renderLang Lang
    l)])
      quoteContext <- LaTeXState -> QuoteContext
    sQuoteContext (LaTeXState -> QuoteContext)
    -> ParsecT TokStream LaTeXState m LaTeXState
    -> ParsecT TokStream LaTeXState m QuoteContext
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT TokStream LaTeXState m LaTeXState
    forall (m :: * -> *) s u. Monad m => ParsecT s u m u
    getState
      if starred || quoteContext == InDoubleQuote
         then singleQuoted . langspan <$> withQuoteContext InSingleQuote tok
         else doubleQuoted . langspan <$> withQuoteContext InDoubleQuote tok

    enquoteCommands :: PandocMonad m
                    => LP m Inlines -> M.Map Text (LP m Inlines)
    enquoteCommands :: forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> Map Text (LP m Inlines)
    enquoteCommands LP m Inlines
    tok = [(Text, LP m Inlines)] -> Map Text (LP m Inlines)
    forall k a. Ord k => [(k, a)] -> Map k a
    M.fromList
      [ (Text
    "enquote*", LP m Inlines -> Bool -> Maybe Text -> LP m Inlines
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> Bool -> Maybe Text -> LP m Inlines
    enquote LP m Inlines
    tok Bool
    True Maybe Text
    forall a. Maybe a
    Nothing)
      , (Text
    "enquote", LP m Inlines -> Bool -> Maybe Text -> LP m Inlines
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> Bool -> Maybe Text -> LP m Inlines
    enquote LP m Inlines
    tok Bool
    False Maybe Text
    forall a. Maybe a
    Nothing)
      -- foreignquote is supposed to use native quote marks
      , (Text
    "foreignquote*", LP m [Tok]
    forall (m :: * -> *). PandocMonad m => LP m [Tok]
    braced LP m [Tok] -> ([Tok] -> LP m Inlines) -> LP m Inlines
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> (a -> ParsecT TokStream LaTeXState m b)
    -> ParsecT TokStream LaTeXState m b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= LP m Inlines -> Bool -> Maybe Text -> LP m Inlines
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> Bool -> Maybe Text -> LP m Inlines
    enquote LP m Inlines
    tok Bool
    True (Maybe Text -> LP m Inlines)
    -> ([Tok] -> Maybe Text) -> [Tok] -> LP m Inlines
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> Maybe Text
    forall a. a -> Maybe a
    Just (Text -> Maybe Text) -> ([Tok] -> Text) -> [Tok] -> Maybe Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Tok] -> Text
    untokenize)
      , (Text
    "foreignquote", LP m [Tok]
    forall (m :: * -> *). PandocMonad m => LP m [Tok]
    braced LP m [Tok] -> ([Tok] -> LP m Inlines) -> LP m Inlines
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> (a -> ParsecT TokStream LaTeXState m b)
    -> ParsecT TokStream LaTeXState m b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= LP m Inlines -> Bool -> Maybe Text -> LP m Inlines
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> Bool -> Maybe Text -> LP m Inlines
    enquote LP m Inlines
    tok Bool
    False (Maybe Text -> LP m Inlines)
    -> ([Tok] -> Maybe Text) -> [Tok] -> LP m Inlines
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> Maybe Text
    forall a. a -> Maybe a
    Just (Text -> Maybe Text) -> ([Tok] -> Text) -> [Tok] -> Maybe Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Tok] -> Text
    untokenize)
      -- hypehnquote uses regular quotes
      , (Text
    "hyphenquote*", LP m [Tok]
    forall (m :: * -> *). PandocMonad m => LP m [Tok]
    braced LP m [Tok] -> ([Tok] -> LP m Inlines) -> LP m Inlines
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> (a -> ParsecT TokStream LaTeXState m b)
    -> ParsecT TokStream LaTeXState m b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= LP m Inlines -> Bool -> Maybe Text -> LP m Inlines
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> Bool -> Maybe Text -> LP m Inlines
    enquote LP m Inlines
    tok Bool
    True (Maybe Text -> LP m Inlines)
    -> ([Tok] -> Maybe Text) -> [Tok] -> LP m Inlines
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> Maybe Text
    forall a. a -> Maybe a
    Just (Text -> Maybe Text) -> ([Tok] -> Text) -> [Tok] -> Maybe Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Tok] -> Text
    untokenize)
      , (Text
    "hyphenquote", LP m [Tok]
    forall (m :: * -> *). PandocMonad m => LP m [Tok]
    braced LP m [Tok] -> ([Tok] -> LP m Inlines) -> LP m Inlines
    forall a b.
    ParsecT TokStream LaTeXState m a
    -> (a -> ParsecT TokStream LaTeXState m b)
    -> ParsecT TokStream LaTeXState m b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= LP m Inlines -> Bool -> Maybe Text -> LP m Inlines
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> Bool -> Maybe Text -> LP m Inlines
    enquote LP m Inlines
    tok Bool
    False (Maybe Text -> LP m Inlines)
    -> ([Tok] -> Maybe Text) -> [Tok] -> LP m Inlines
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> Maybe Text
    forall a. a -> Maybe a
    Just (Text -> Maybe Text) -> ([Tok] -> Text) -> [Tok] -> Maybe Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Tok] -> Text
    untokenize)
      ]

    foreignlanguage :: PandocMonad m => LP m Inlines -> LP m Inlines
    foreignlanguage :: forall (m :: * -> *). PandocMonad m => LP m Inlines -> LP m Inlines
    foreignlanguage LP m Inlines
    tok = do
      babelLang <- [Tok] -> Text
    untokenize ([Tok] -> Text)
    -> ParsecT TokStream LaTeXState m [Tok]
    -> ParsecT TokStream LaTeXState m Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT TokStream LaTeXState m [Tok]
    forall (m :: * -> *). PandocMonad m => LP m [Tok]
    braced
      case babelLangToBCP47 babelLang of
           Just Lang
    lang -> Attr -> Inlines -> Inlines
    spanWith (Text
    "", [], [(Text
    "lang",  Lang -> Text
    renderLang Lang
    lang)]) (Inlines -> Inlines) -> LP m Inlines -> LP m Inlines
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> LP m Inlines
    tok
           Maybe Lang
    _ -> LP m Inlines
    tok

    inlineLanguageCommands :: PandocMonad m
                           => LP m Inlines -> M.Map Text (LP m Inlines)
    inlineLanguageCommands :: forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> Map Text (LP m Inlines)
    inlineLanguageCommands LP m Inlines
    tok =
      [(Text, LP m Inlines)] -> Map Text (LP m Inlines)
    forall k a. Ord k => [(k, a)] -> Map k a
    M.fromList ([(Text, LP m Inlines)] -> Map Text (LP m Inlines))
    -> [(Text, LP m Inlines)] -> Map Text (LP m Inlines)
    forall a b. (a -> b) -> a -> b
    $
        (Text
    "foreignlanguage", LP m Inlines -> LP m Inlines
    forall (m :: * -> *). PandocMonad m => LP m Inlines -> LP m Inlines
    foreignlanguage LP m Inlines
    tok) (Text, LP m Inlines)
    -> [(Text, LP m Inlines)] -> [(Text, LP m Inlines)]
    forall a. a -> [a] -> [a]
    :
        ((Text, Text -> Lang) -> (Text, LP m Inlines)
    forall {a}.
    (Semigroup a, IsString a) =>
    (a, Text -> Lang) -> (a, LP m Inlines)
    mk ((Text, Text -> Lang) -> (Text, LP m Inlines))
    -> [(Text, Text -> Lang)] -> [(Text, LP m Inlines)]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Map Text (Text -> Lang) -> [(Text, Text -> Lang)]
    forall k a. Map k a -> [(k, a)]
    M.toList Map Text (Text -> Lang)
    polyglossiaLangToBCP47)
      where
        mk :: (a, Text -> Lang) -> (a, LP m Inlines)
    mk (a
    polyglossia, Text -> Lang
    bcp47Func) =
          (a
    "text" a -> a -> a
    forall a. Semigroup a => a -> a -> a
    <> a
    polyglossia, LP m Inlines -> (Text -> Lang) -> LP m Inlines
    forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> (Text -> Lang) -> LP m Inlines
    inlineLanguage LP m Inlines
    tok Text -> Lang
    bcp47Func)

    inlineLanguage :: PandocMonad m
                   => LP m Inlines -> (Text -> Lang) -> LP m Inlines
    inlineLanguage :: forall (m :: * -> *).
    PandocMonad m =>
    LP m Inlines -> (Text -> Lang) -> LP m Inlines
    inlineLanguage LP m Inlines
    tok Text -> Lang
    bcp47Func = do
      o <- Text
    -> ParsecT TokStream LaTeXState m Text
    -> ParsecT TokStream LaTeXState m Text
    forall s (m :: * -> *) t a u.
    Stream s m t =>
    a -> ParsecT s u m a -> ParsecT s u m a
    option Text
    "" (ParsecT TokStream LaTeXState m Text
     -> ParsecT TokStream LaTeXState m Text)
    -> ParsecT TokStream LaTeXState m Text
    -> ParsecT TokStream LaTeXState m Text
    forall a b. (a -> b) -> a -> b
    $ (Char -> Bool) -> Text -> Text
    T.filter (\Char
    c -> Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Char
    '[' Bool -> Bool -> Bool
    && Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Char
    ']')
                    (Text -> Text)
    -> ParsecT TokStream LaTeXState m Text
    -> ParsecT TokStream LaTeXState m Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT TokStream LaTeXState m Text
    forall (m :: * -> *). PandocMonad m => LP m Text
    rawopt
      let lang = Lang -> Text
    renderLang (Lang -> Text) -> Lang -> Text
    forall a b. (a -> b) -> a -> b
    $ Text -> Lang
    bcp47Func Text
    o
      extractSpaces (spanWith ("", [], [("lang", lang)])) <$> tok

    setDefaultLanguage :: PandocMonad m => LP m Blocks
    setDefaultLanguage :: forall (m :: * -> *). PandocMonad m => LP m Blocks
    setDefaultLanguage = do
      o <- Text
    -> ParsecT TokStream LaTeXState m Text
    -> ParsecT TokStream LaTeXState m Text
    forall s (m :: * -> *) t a u.
    Stream s m t =>
    a -> ParsecT s u m a -> ParsecT s u m a
    option Text
    "" (ParsecT TokStream LaTeXState m Text
     -> ParsecT TokStream LaTeXState m Text)
    -> ParsecT TokStream LaTeXState m Text
    -> ParsecT TokStream LaTeXState m Text
    forall a b. (a -> b) -> a -> b
    $ (Char -> Bool) -> Text -> Text
    T.filter (\Char
    c -> Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Char
    '[' Bool -> Bool -> Bool
    && Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Char
    ']')
                    (Text -> Text)
    -> ParsecT TokStream LaTeXState m Text
    -> ParsecT TokStream LaTeXState m Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> ParsecT TokStream LaTeXState m Text
    forall (m :: * -> *). PandocMonad m => LP m Text
    rawopt
      polylang <- untokenize <$> braced
      case M.lookup polylang polyglossiaLangToBCP47 of
           Maybe (Text -> Lang)
    Nothing -> Blocks -> ParsecT TokStream LaTeXState m Blocks
    forall a. a -> ParsecT TokStream LaTeXState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Blocks
    forall a. Monoid a => a
    mempty -- TODO mzero? warning?
           Just Text -> Lang
    langFunc -> do
             let l :: Lang
    l = Text -> Lang
    langFunc Text
    o
             Lang -> ParsecT TokStream LaTeXState m ()
    forall (m :: * -> *). PandocMonad m => Lang -> m ()
    setTranslations Lang
    l
             (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
    forall (m :: * -> *) u s. Monad m => (u -> u) -> ParsecT s u m ()
    updateState ((LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ())
    -> (LaTeXState -> LaTeXState) -> ParsecT TokStream LaTeXState m ()
    forall a b. (a -> b) -> a -> b
    $ Text -> Inlines -> LaTeXState -> LaTeXState
    forall a b. (HasMeta a, ToMetaValue b) => Text -> b -> a -> a
    forall b. ToMetaValue b => Text -> b -> LaTeXState -> LaTeXState
    setMeta Text
    "lang" (Inlines -> LaTeXState -> LaTeXState)
    -> Inlines -> LaTeXState -> LaTeXState
    forall a b. (a -> b) -> a -> b
    $ Text -> Inlines
    str (Lang -> Text
    renderLang Lang
    l)
             Blocks -> ParsecT TokStream LaTeXState m Blocks
    forall a. a -> ParsecT TokStream LaTeXState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Blocks
    forall a. Monoid a => a
    mempty

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

    simpleLang :: Text -> Lang
    simpleLang :: Text -> Lang
    simpleLang Text
    l = Text
    -> Maybe Text
    -> Maybe Text
    -> [Text]
    -> [(Text, [(Text, Text)])]
    -> [Text]
    -> Lang
    Lang Text
    l Maybe Text
    forall a. Maybe a
    Nothing Maybe Text
    forall a. Maybe a
    Nothing [] [] []

    babelLangToBCP47 :: T.Text -> Maybe Lang
    babelLangToBCP47 :: Text -> Maybe Lang
    babelLangToBCP47 Text
    s =
      case Text
    s of
           Text
    "austrian" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text
    -> Maybe Text
    -> Maybe Text
    -> [Text]
    -> [(Text, [(Text, Text)])]
    -> [Text]
    -> Lang
    Lang Text
    "de" Maybe Text
    forall a. Maybe a
    Nothing (Text -> Maybe Text
    forall a. a -> Maybe a
    Just Text
    "AT") [Text
    "1901"] [] []
           Text
    "naustrian" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text
    -> Maybe Text
    -> Maybe Text
    -> [Text]
    -> [(Text, [(Text, Text)])]
    -> [Text]
    -> Lang
    Lang Text
    "de" Maybe Text
    forall a. Maybe a
    Nothing (Text -> Maybe Text
    forall a. a -> Maybe a
    Just Text
    "AT") [] [] []
           Text
    "swissgerman" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text
    -> Maybe Text
    -> Maybe Text
    -> [Text]
    -> [(Text, [(Text, Text)])]
    -> [Text]
    -> Lang
    Lang Text
    "de" Maybe Text
    forall a. Maybe a
    Nothing (Text -> Maybe Text
    forall a. a -> Maybe a
    Just Text
    "CH") [Text
    "1901"] [] []
           Text
    "nswissgerman" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text
    -> Maybe Text
    -> Maybe Text
    -> [Text]
    -> [(Text, [(Text, Text)])]
    -> [Text]
    -> Lang
    Lang Text
    "de" Maybe Text
    forall a. Maybe a
    Nothing (Text -> Maybe Text
    forall a. a -> Maybe a
    Just Text
    "CH") [] [] []
           Text
    "german" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text
    -> Maybe Text
    -> Maybe Text
    -> [Text]
    -> [(Text, [(Text, Text)])]
    -> [Text]
    -> Lang
    Lang Text
    "de" Maybe Text
    forall a. Maybe a
    Nothing (Text -> Maybe Text
    forall a. a -> Maybe a
    Just Text
    "DE") [Text
    "1901"] [] []
           Text
    "ngerman" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text
    -> Maybe Text
    -> Maybe Text
    -> [Text]
    -> [(Text, [(Text, Text)])]
    -> [Text]
    -> Lang
    Lang Text
    "de" Maybe Text
    forall a. Maybe a
    Nothing (Text -> Maybe Text
    forall a. a -> Maybe a
    Just Text
    "DE") [] [] []
           Text
    "lowersorbian" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text
    -> Maybe Text
    -> Maybe Text
    -> [Text]
    -> [(Text, [(Text, Text)])]
    -> [Text]
    -> Lang
    Lang Text
    "dsb" Maybe Text
    forall a. Maybe a
    Nothing Maybe Text
    forall a. Maybe a
    Nothing [] [] []
           Text
    "uppersorbian" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text
    -> Maybe Text
    -> Maybe Text
    -> [Text]
    -> [(Text, [(Text, Text)])]
    -> [Text]
    -> Lang
    Lang Text
    "hsb" Maybe Text
    forall a. Maybe a
    Nothing Maybe Text
    forall a. Maybe a
    Nothing [] [] []
           Text
    "polytonicgreek" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text
    -> Maybe Text
    -> Maybe Text
    -> [Text]
    -> [(Text, [(Text, Text)])]
    -> [Text]
    -> Lang
    Lang Text
    "el" Maybe Text
    forall a. Maybe a
    Nothing Maybe Text
    forall a. Maybe a
    Nothing [Text
    "polyton"] [] []
           Text
    "polutonikogreek" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text
    -> Maybe Text
    -> Maybe Text
    -> [Text]
    -> [(Text, [(Text, Text)])]
    -> [Text]
    -> Lang
    Lang Text
    "el" Maybe Text
    forall a. Maybe a
    Nothing Maybe Text
    forall a. Maybe a
    Nothing [Text
    "polyton"] [] []
           Text
    "slovene" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text -> Lang
    simpleLang Text
    "sl"
           Text
    "australian" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text
    -> Maybe Text
    -> Maybe Text
    -> [Text]
    -> [(Text, [(Text, Text)])]
    -> [Text]
    -> Lang
    Lang Text
    "en" Maybe Text
    forall a. Maybe a
    Nothing (Text -> Maybe Text
    forall a. a -> Maybe a
    Just Text
    "AU") [] [] []
           Text
    "canadian" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text
    -> Maybe Text
    -> Maybe Text
    -> [Text]
    -> [(Text, [(Text, Text)])]
    -> [Text]
    -> Lang
    Lang Text
    "en" Maybe Text
    forall a. Maybe a
    Nothing (Text -> Maybe Text
    forall a. a -> Maybe a
    Just Text
    "CA") [] [] []
           Text
    "british" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text
    -> Maybe Text
    -> Maybe Text
    -> [Text]
    -> [(Text, [(Text, Text)])]
    -> [Text]
    -> Lang
    Lang Text
    "en" Maybe Text
    forall a. Maybe a
    Nothing (Text -> Maybe Text
    forall a. a -> Maybe a
    Just Text
    "GB") [] [] []
           Text
    "newzealand" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text
    -> Maybe Text
    -> Maybe Text
    -> [Text]
    -> [(Text, [(Text, Text)])]
    -> [Text]
    -> Lang
    Lang Text
    "en" Maybe Text
    forall a. Maybe a
    Nothing (Text -> Maybe Text
    forall a. a -> Maybe a
    Just Text
    "NZ") [] [] []
           Text
    "american" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text
    -> Maybe Text
    -> Maybe Text
    -> [Text]
    -> [(Text, [(Text, Text)])]
    -> [Text]
    -> Lang
    Lang Text
    "en" Maybe Text
    forall a. Maybe a
    Nothing (Text -> Maybe Text
    forall a. a -> Maybe a
    Just Text
    "US") [] [] []
           Text
    "classiclatin" -> Lang -> Maybe Lang
    forall a. a -> Maybe a
    Just (Lang -> Maybe Lang) -> Lang -> Maybe Lang
    forall a b. (a -> b) -> a -> b
    $ Text
    -> Maybe Text
    -> Maybe Text
    -> [Text]
    -> [(Text, [(Text, Text)])]
    -> [Text]
    -> Lang
    Lang Text
    "la" Maybe Text
    forall a. Maybe a
    Nothing Maybe Text
    forall a. Maybe a
    Nothing [Text
    "x-classic"] [] []
           Text
    _ -> ((Text -> Lang) -> Text -> Lang
    forall a b. (a -> b) -> a -> b
    $ Text
    "") ((Text -> Lang) -> Lang) -> Maybe (Text -> Lang) -> Maybe Lang
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f 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