module Data.Languages
( languagesForPath
, languages
, languagesByExtension
, languagesByFileName
, LanguageKey
, Language(..)
) where
import Control.Applicative
import qualified Data.Map.Strict as Map
import Data.Maybe
import Data.Text (Text)
import qualified Data.Text as Text
import Gen_Languages
import System.FilePath.Posix
languagesForPath :: FilePath -> [Language]
languagesForPath :: FilePath -> [Language]
languagesForPath FilePath
path = [Language]
languageForFileName [Language] -> [Language] -> [Language]
forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
<|> [Language]
languageForExtension
where
languageForFileName :: [Language]
languageForFileName = FilePath -> Map LanguageKey [LanguageKey] -> [Language]
languageFor (FilePath -> FilePath
takeFileName FilePath
path) Map LanguageKey [LanguageKey]
languagesByFileName
languageForExtension :: [Language]
languageForExtension = FilePath -> Map LanguageKey [LanguageKey] -> [Language]
languageFor (FilePath -> FilePath
takeExtension FilePath
path) Map LanguageKey [LanguageKey]
languagesByExtension
languageFor :: String -> Map.Map Text [LanguageKey] -> [Language]
languageFor :: FilePath -> Map LanguageKey [LanguageKey] -> [Language]
languageFor FilePath
k
= (LanguageKey -> [Language]) -> [LanguageKey] -> [Language]
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Maybe Language -> [Language]
forall a. Maybe a -> [a]
maybeToList (Maybe Language -> [Language])
-> (LanguageKey -> Maybe Language) -> LanguageKey -> [Language]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LanguageKey -> Map LanguageKey Language -> Maybe Language)
-> Map LanguageKey Language -> LanguageKey -> Maybe Language
forall a b c. (a -> b -> c) -> b -> a -> c
flip LanguageKey -> Map LanguageKey Language -> Maybe Language
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup Map LanguageKey Language
languages)
([LanguageKey] -> [Language])
-> (Map LanguageKey [LanguageKey] -> [LanguageKey])
-> Map LanguageKey [LanguageKey]
-> [Language]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [LanguageKey] -> Maybe [LanguageKey] -> [LanguageKey]
forall a. a -> Maybe a -> a
fromMaybe []
(Maybe [LanguageKey] -> [LanguageKey])
-> (Map LanguageKey [LanguageKey] -> Maybe [LanguageKey])
-> Map LanguageKey [LanguageKey]
-> [LanguageKey]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LanguageKey -> Map LanguageKey [LanguageKey] -> Maybe [LanguageKey]
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup (FilePath -> LanguageKey
Text.pack FilePath
k)