git.iko.soy/typeable/pandoc
on master help sign in
Org reader: keep unknown keyword lines as raw org

The lines of unknown keywords, like `#+SOMEWORD: value` are no longer read as metadata, but kept as raw `org` blocks. This ensures that more information is retained when round-tripping org-mode files; additionally, this change makes it possible to support non-standard org extensions via filters.

commit8d08197b8e9ae5c771b7d5e0c5b5a2971f4d07ea authorAlbert Krewinkel <albert@zeitkraut.de> dateMon 29 Jun 2020, 21:19 (6 years ago) parent1a45250
3 files changed+18−4
src/Text/Pandoc/Readers/Org/Blocks.hs+11 src/Text/Pandoc/Readers/Org/Meta.hs+2−2 test/Tests/Readers/Org/Meta.hs+5−2
src/Text/Pandoc/Readers/Org/Blocks.hs+11
@@ -76,6 +76,7 @@ block = choice [ mempty <$ blanklines
7676 , list
7777 , latexFragment
7878 , noteBlock
79+ , rawOrgLine
7980 , paraOrPlain
8081 ] <?> "block"
8182
@@ -559,6 +560,8 @@ include = try $ do
559560 | otherwise -> Para content
560561 _ -> blk
561562
563+-- | Parses a meta line which defines a raw block. Currently recognized:
564+-- @#+LATEX:@, @#+HTML:@, @#+TEXINFO:@, and @#+BEAMER@.
562565rawExportLine :: PandocMonad m => OrgParser m Blocks
563566rawExportLine = try $ do
564567 metaLineStart
@@ -567,6 +570,14 @@ rawExportLine = try $ do
567570 then B.rawBlock key <$> anyLine
568571 else mzero
569572
573+-- | Parses any meta line, i.e., a line starting with @#+@, into a raw
574+-- org block. This should be the last resort when trying to parse
575+-- keywords. Leading spaces are discarded.
576+rawOrgLine :: PandocMonad m => OrgParser m (F Blocks)
577+rawOrgLine = do
578+ line <- metaLineStart *> anyLine
579+ returnF $ B.rawBlock "org" $ ("#+" <> line)
580+
570581commentLine :: Monad m => OrgParser m Blocks
571582commentLine = commentLineStart *> anyLine *> pure mempty
572583
src/Text/Pandoc/Readers/Org/Meta.hs+2−2
@@ -57,13 +57,13 @@ removeMeta key meta' =
5757-- The order, in which blocks are tried, makes sure that we're not looking at
5858-- the beginning of a block, so we don't need to check for it
5959metaLine :: PandocMonad m => OrgParser m Blocks
60−metaLine = mempty <$ metaLineStart <* keywordLine
60+metaLine = try $ mempty <$ metaLineStart <* keywordLine
6161
6262keywordLine :: PandocMonad m => OrgParser m ()
6363keywordLine = try $ do
6464 key <- T.toLower <$> metaKey
6565 case Map.lookup key keywordHandlers of
66− Nothing -> () <$ anyLine -- discard unknown lines
66+ Nothing -> fail $ "Unknown keyword: " ++ T.unpack key
6767 Just hd -> hd
6868
6969metaKey :: Monad m => OrgParser m Text
test/Tests/Readers/Org/Meta.hs+5−2
@@ -214,8 +214,11 @@ tests =
214214 ]
215215
216216 , "Unknown keyword" =:
217− "#+UNKNOWN_KEYWORD: Chumbawamba" =?>
218− Pandoc nullMeta mempty
217+ T.unlines [ "#+UNKNOWN_KEYWORD: Chumbawamba"
218+ , "#+ANOTHER_UNKNOWN: Blur"
219+ ] =?>
220+ rawBlock "org" "#+UNKNOWN_KEYWORD: Chumbawamba" <>
221+ rawBlock "org" "#+ANOTHER_UNKNOWN: Blur"
219222 ]
220223
221224 , "Properties drawer" =: