@@ -1,4 +1,5 @@
11{-# LANGUAGE FlexibleContexts #-}
2+{-# LANGUAGE LambdaCase #-}
23{-# LANGUAGE TupleSections #-}
34{-# LANGUAGE OverloadedStrings #-}
45{- |
@@ -26,10 +27,10 @@ import Text.Pandoc.Builder (Blocks, Inlines)
2627import qualified Text.Pandoc.Builder as B
2728import Text.Pandoc.Class.PandocMonad (PandocMonad)
2829import Text.Pandoc.Definition
29−import Text.Pandoc.Shared (safeRead)
30+import Text.Pandoc.Shared (blocksToInlines, safeRead)
3031
3132import Control.Monad (mzero, void, when)
32−import Data.List (intersperse)
33+import Data.List (intercalate, intersperse)
3334import Data.Maybe (fromMaybe)
3435import qualified Data.Map as M
3536import qualified Data.Set as Set
@@ -80,6 +81,7 @@ metaValue key =
8081 "title" -> (key,) <$> metaInlines
8182 "subtitle" -> (key,) <$> metaInlines
8283 "date" -> (key,) <$> metaInlines
84+ "description" -> (key,) <$> accumulatingInlines key
8385 "nocite" -> (key,) <$> accumulatingList key metaInlines
8486 "header-includes" -> (key,) <$> accumulatingList key metaInlines
8587 "latex_header" -> (inclKey,) <$>
@@ -93,6 +95,8 @@ metaValue key =
9395 accumulatingList inclKey (metaExportSnippet "html")
9496 _ -> (key,) <$> metaString
9597
98+-- TODO Cleanup this mess
99+
96100metaInlines :: PandocMonad m => OrgParser m (F MetaValue)
97101metaInlines = fmap (MetaInlines . B.toList) <$> inlinesTillNewline
98102
@@ -115,18 +119,50 @@ metaExportSnippet :: Monad m => Text -> OrgParser m (F MetaValue)
115119metaExportSnippet format =
116120 return . MetaInlines . B.toList . B.rawInline format <$> anyLine
117121
122+accumulatingInlines :: PandocMonad m
123+ => Text
124+ -> OrgParser m (F MetaValue)
125+accumulatingInlines key = do
126+ value <- inlinesTillNewline
127+ accumulating appendValue (B.toList <$> value)
128+ where
129+ appendValue :: Meta -> [Inline] -> MetaValue
130+ appendValue m v = MetaInlines $ curInlines m <> v
131+
132+ curInlines m = case collectInlines <$> lookupMeta key m of
133+ Nothing -> []
134+ Just [] -> []
135+ Just xs -> xs <> [B.SoftBreak]
136+
137+ collectInlines :: MetaValue -> [Inline]
138+ collectInlines = \case
139+ MetaInlines inlns -> inlns
140+ MetaList ml -> intercalate [B.SoftBreak] $ map collectInlines ml
141+ MetaString s -> [B.Str s]
142+ MetaBlocks blks -> blocksToInlines blks
143+ MetaMap _map -> []
144+ MetaBool _bool -> []
145+
118146-- | Accumulate the result of the @parser@ in a list under @key@.
119147accumulatingList :: Monad m => Text
120148 -> OrgParser m (F MetaValue)
121149 -> OrgParser m (F MetaValue)
122−accumulatingList key p = do
123− value <- p
124− meta' <- orgStateMeta <$> getState
125− return $ (\m v -> MetaList (curList m ++ [v])) <$> meta' <*> value
126− where curList m = case lookupMeta key m of
127− Just (MetaList ms) -> ms
128− Just x -> [x]
129− _ -> []
150+accumulatingList key p = p >>= accumulating metaListAppend
151+ where
152+ metaListAppend m v = MetaList (curList m ++ [v])
153+
154+ curList m = case lookupMeta key m of
155+ Just (MetaList ms) -> ms
156+ Just x -> [x]
157+ _ -> []
158+
159+accumulating :: Monad m
160+ => (Meta -> a -> MetaValue)
161+ -> F a
162+ -> OrgParser m (F MetaValue)
163+accumulating acc value = do
164+ meta <- orgStateMeta <$> getState
165+ return $ acc <$> meta <*> value
130166
131167--
132168-- export options