git.iko.soy/typeable/pandoc
on master help sign in
Org reader: read description lines as inlines

`#+DESCRIPTION` lines are treated as text with markup. If multiple such lines are given, then all lines are read and separated by soft linebreaks. Closes: #6485

commit8dce28d9494d4aaa0409eaa719cce4f46e4bf353 authorAlbert Krewinkel <albert@zeitkraut.de> dateFri 26 Jun 2020, 15:50 (6 years ago) parent9e6e9a7
2 files changed+63−15
src/Text/Pandoc/Readers/Org/Meta.hs+46−10 test/Tests/Readers/Org/Meta.hs+17−5
src/Text/Pandoc/Readers/Org/Meta.hs+46−10
@@ -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
test/Tests/Readers/Org/Meta.hs+17−5
@@ -65,11 +65,23 @@ tests =
6565 meta = setMeta "date" (MetaInlines date) nullMeta
6666 in Pandoc meta mempty
6767
68− , "Description" =:
69− "#+DESCRIPTION: Explanatory text" =?>
70− let description = "Explanatory text"
71− meta = setMeta "description" (MetaString description) nullMeta
72− in Pandoc meta mempty
68+ , testGroup "Description"
69+ [ "Single line" =:
70+ "#+DESCRIPTION: Explanatory text" =?>
71+ let description = [Str "Explanatory", Space, Str "text"]
72+ meta = setMeta "description" (MetaInlines description) nullMeta
73+ in Pandoc meta mempty
74+
75+ , "Multiline" =:
76+ T.unlines [ "#+DESCRIPTION: /Short/ introduction"
77+ , "#+DESCRIPTION: to Org-mode"
78+ ] =?>
79+ let description = [ Emph [Str "Short"], Space, Str "introduction", SoftBreak
80+ , Str "to", Space, Str "Org-mode"
81+ ]
82+ meta = setMeta "description" (MetaInlines description) nullMeta
83+ in Pandoc meta mempty
84+ ]
7385
7486 , "Properties drawer" =:
7587 T.unlines [ " :PROPERTIES:"