From e7af795f7fb26a669c9ceaf510f41def9b9d83db Mon Sep 17 00:00:00 2001 From: Rhiannon Morris Date: Sat, 18 Jul 2020 11:29:07 +0200 Subject: [PATCH] allow single image (non-list) for images field --- make-pages/Info.hs | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/make-pages/Info.hs b/make-pages/Info.hs index 7c00e50..db4f3d9 100644 --- a/make-pages/Info.hs +++ b/make-pages/Info.hs @@ -95,7 +95,7 @@ instance FromYAML Info where <*> m .:? "tags" .!= [] <*> m .:? "nsfw-tags" .!= [] <*> m .:? "description" - <*> m .: "images" + <*> (m .: "images" >>= imageList) <*> m .:? "background" <*> m .:? "thumb" <*> m .:? "links" .!= [] @@ -106,18 +106,26 @@ instance FromYAML Artist where withUrl = YAML.withMap "full info" \m -> Artist <$> m .: "name" <*> m .:? "url" +imageList :: YAML.Node YAML.Pos -> YAML.Parser [Image] +imageList y = pure <$> unlabelledImage y <|> parseYAML y + instance FromYAML Image where - parseYAML y = do - Pair label rest <- parseYAML y - asStr label rest <|> asObj label rest - where - asStr label = YAML.withStr "path" \(Text.unpack -> path) -> - pure $ Image {label, path, nsfw = False, warning = Nothing} - asObj label = YAML.withMap "image info" \m -> do - path <- m .: "path" - nsfw <- m .:? "nsfw" .!= False - warning <- m .:? "warning" - pure $ Image {label, path, nsfw, warning} + parseYAML y = unlabelledImage y <|> labelled y where + labelled y' = do + Pair label rest <- parseYAML y' + i <- unlabelledImage rest + pure $ i {label} + +unlabelledImage :: YAML.Node YAML.Pos -> YAML.Parser Image +unlabelledImage y = asStr y <|> asObj y + where + asStr = YAML.withStr "path" \(Text.unpack -> path) -> + pure $ Image {label = "", path, nsfw = False, warning = Nothing} + asObj = YAML.withMap "image info" \m -> do + path <- m .: "path" + nsfw <- m .:? "nsfw" .!= False + warning <- m .:? "warning" + pure $ Image {label = "", path, nsfw, warning} instance FromYAML Link where parseYAML y = do