From cddae9e69c6098a08a2dcd45451ec741a36dcb54 Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Sat, 6 Jul 2019 09:32:40 -0400 Subject: [PATCH] Add getPostAttributeList --- rib.cabal | 1 - src/Reflex/Dom/Pandoc/Document.hs | 2 +- src/Rib/Types.hs | 16 +++++++++++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/rib.cabal b/rib.cabal index 4030152..b76ca9d 100644 --- a/rib.cabal +++ b/rib.cabal @@ -70,7 +70,6 @@ executable rib-example build-depends: base , clay - , data-default , directory , reflex-dom-core , rib diff --git a/src/Reflex/Dom/Pandoc/Document.hs b/src/Reflex/Dom/Pandoc/Document.hs index 6e3e789..fd81452 100644 --- a/src/Reflex/Dom/Pandoc/Document.hs +++ b/src/Reflex/Dom/Pandoc/Document.hs @@ -28,7 +28,7 @@ elPandocDoc (Pandoc _meta blocks) = mapM_ renderBlock blocks -- | Render the first level of heading elPandocHeading1 :: DomBuilder t m => Pandoc -> m () elPandocHeading1 (Pandoc _meta blocks) = forM_ blocks $ \case - Header 1 _ xs -> mapM_ renderInline xs + Header 1 _ xs -> elPandocInlines xs _ -> blank -- | Render list of Pandoc inlines diff --git a/src/Rib/Types.hs b/src/Rib/Types.hs index e7a9dd5..760ffdb 100644 --- a/src/Rib/Types.hs +++ b/src/Rib/Types.hs @@ -9,16 +9,19 @@ module Rib.Types , PostCategory(..) , PostFilePath(..) , getPostAttribute + , getPostAttributeList , getPostAttributeJson ) where import Data.Aeson (FromJSON, ToJSON, decode) import qualified Data.Map as Map import Data.Text (Text) +import Data.Maybe +import qualified Data.Text as T import GHC.Generics (Generic) import Development.Shake.Classes (Binary, Hashable, NFData) -import Text.Pandoc (Inline (Str), Meta (unMeta), MetaValue (MetaInlines), Pandoc (Pandoc)) +import Text.Pandoc import Text.Pandoc.UTF8 (fromStringLazy) -- | Represents a HTML page that will be generated @@ -44,6 +47,8 @@ data PostCategory newtype PostFilePath = PostFilePath FilePath deriving (Show, Eq, Hashable, Binary, NFData, Generic) +-- TODO: These functions should probably live in Pandoc Util module + -- Get the YAML metadata for the given key in a post -- -- This has to always return `[Inline]` unless we upgrade pandoc. See @@ -56,6 +61,15 @@ getPostAttribute k (Post (Pandoc meta _) _) = Just (MetaInlines inlines) -> Just inlines _ -> Nothing +-- Get the YAML metadata for a key that is a list of text values +getPostAttributeList :: String -> Post -> Maybe [Text] +getPostAttributeList k (Post (Pandoc meta _) _) = + case Map.lookup k (unMeta meta) of + Just (MetaList vals) -> Just $ catMaybes $ flip fmap vals $ \case + MetaInlines [Str val] -> Just $ T.pack val + _ -> Nothing + _ -> Nothing + -- Like getPostAttribute but expects the value to be JSON encoding of a type. getPostAttributeJson :: FromJSON a => String -> Post -> Maybe a getPostAttributeJson k p = do