70 lines
2.5 KiB
Haskell
70 lines
2.5 KiB
Haskell
-- SPDX-FileCopyrightText: 2023 David Mosbach <david.mosbach@campus.lmu.de>
|
|
--
|
|
-- SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
{-# Language DuplicateRecordFields,
|
|
NoFieldSelectors,
|
|
OverloadedRecordDot,
|
|
OverloadedStrings,
|
|
DeriveGeneric #-}
|
|
|
|
module Index where
|
|
|
|
import Data.Yaml
|
|
import Control.Applicative hiding (empty)
|
|
import GHC.Generics (Generic)
|
|
import Data.Map
|
|
import Data.Maybe (fromMaybe, fromJust)
|
|
|
|
type Index = Map String Entry
|
|
|
|
data Entry = Entry {
|
|
graphFile :: String,
|
|
category :: Maybe String,
|
|
defScope :: Maybe String,
|
|
defDescription :: Maybe Description,
|
|
instDescription :: Maybe Description,
|
|
instances :: Value
|
|
} deriving (Show, Generic)
|
|
|
|
instance FromJSON Entry where
|
|
parseJSON (Object o) = Entry <$>
|
|
o .: "graph-file" <*>
|
|
o .:? "category" <*>
|
|
o .:? "definition-scope" <*>
|
|
o .:? "definition-description" <*>
|
|
o .:? "instance-description" <*>
|
|
o .: "instances"
|
|
parseJSON _ = error "Unexpected yaml"
|
|
|
|
type Title = String
|
|
type Content = String
|
|
|
|
data Description = Description {
|
|
fallbackLang :: Maybe String,
|
|
fallback :: (Maybe Title, Maybe Content),
|
|
translations :: Map String (Maybe Title, Maybe Content)
|
|
} deriving (Show, Generic)
|
|
|
|
instance FromJSON Description where
|
|
parseJSON (Object o) = Description <$>
|
|
o .:? "fallback-lang" <*>
|
|
o .: "fallback" <*>
|
|
o .: "translations"
|
|
|
|
english = "en-eu";
|
|
|
|
getDefDescription :: Entry -> (Maybe Title, Maybe Content)
|
|
getDefDescription entry = let description = fromJust entry.defDescription
|
|
def = description.fallback
|
|
in findWithDefault def english description.translations
|
|
getInstDescription :: Entry -> (Maybe Title, Maybe Content)
|
|
getInstDescription entry = let description = fromJust entry.instDescription
|
|
def = description.fallback
|
|
in findWithDefault def english description.translations
|
|
|
|
getEntryByFile :: String -> Index -> Entry
|
|
getEntryByFile file index = query (elems index) file where
|
|
query :: [Entry] -> String -> Entry
|
|
query [] _ = error $ "No entries left for " ++ file
|
|
query (x:xs) file = if x.graphFile == file then x else query xs file |