git.iko.soy/typeable/pandoc
on master help sign in
Org reader: read LaTeX_header as header-includes

LaTeX-specific header commands can be defined in `#+LaTeX_header` lines. They are parsed as format-specific inlines to ensure that they will only show up in LaTeX output.

commita2574883432c2375661caa4bee19a48967cf49db authorAlbert Krewinkel <albert@zeitkraut.de> dateMon 29 Aug 2016, 14:10 (10 years ago) parent75df104
2 files changed+38−9
src/Text/Pandoc/Readers/Org/Meta.hs+31−9 tests/Tests/Readers/Org.hs+7
src/Text/Pandoc/Readers/Org/Meta.hs+31−9
@@ -1,4 +1,5 @@
11{-# LANGUAGE FlexibleContexts #-}
2+{-# LANGUAGE TupleSections #-}
23{-
34Copyright (C) 2014-2016 Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
45
@@ -56,9 +57,9 @@ metaLine = mempty <$ metaLineStart <* (optionLine <|> declarationLine)
5657declarationLine :: OrgParser ()
5758declarationLine = try $ do
5859 key <- map toLower <$> metaKey
59− value <- metaValue key
60+ (key', value) <- metaValue key
6061 updateState $ \st ->
61− let meta' = B.setMeta key <$> value <*> pure nullMeta
62+ let meta' = B.setMeta key' <$> value <*> pure nullMeta
6263 in st { orgStateMeta = meta' <> orgStateMeta st }
6364
6465metaKey :: OrgParser String
@@ -66,13 +67,17 @@ metaKey = map toLower <$> many1 (noneOf ": \n\r")
6667 <* char ':'
6768 <* skipSpaces
6869
69−metaValue :: String -> OrgParser (F MetaValue)
70−metaValue key = do
71− case key of
72− "author" -> metaInlinesCommaSeparated
73− "title" -> metaInlines
74− "date" -> metaInlines
75− _ -> metaString
70+metaValue :: String -> OrgParser (String, (F MetaValue))
71+metaValue key =
72+ let inclKey = "header-includes"
73+ in case key of
74+ "author" -> (key,) <$> metaInlinesCommaSeparated
75+ "title" -> (key,) <$> metaInlines
76+ "date" -> (key,) <$> metaInlines
77+ "header-includes" -> (key,) <$> accumulatingList key metaInlines
78+ "latex_header" -> (inclKey,) <$>
79+ accumulatingList inclKey (metaExportSnippet "latex")
80+ _ -> (key,) <$> metaString
7681
7782metaInlines :: OrgParser (F MetaValue)
7883metaInlines = fmap (MetaInlines . B.toList) <$> inlinesTillNewline
@@ -88,6 +93,23 @@ metaInlinesCommaSeparated = do
8893metaString :: OrgParser (F MetaValue)
8994metaString = return . MetaString <$> anyLine
9095
96+-- | Read an format specific meta definition
97+metaExportSnippet :: String -> OrgParser (F MetaValue)
98+metaExportSnippet format =
99+ return . MetaInlines . B.toList . B.rawInline format <$> anyLine
100+
101+-- | Accumulate the result of the @parser@ in a list under @key@.
102+accumulatingList :: String
103+ -> OrgParser (F MetaValue)
104+ -> OrgParser (F MetaValue)
105+accumulatingList key p = do
106+ value <- p
107+ meta' <- orgStateMeta <$> getState
108+ return $ (\m v -> MetaList (curList m ++ [v])) <$> meta' <*> value
109+ where curList m = case lookupMeta key m of
110+ Just (MetaList ms) -> ms
111+ Just x -> [x]
112+ _ -> []
91113
92114--
93115-- export options
tests/Tests/Readers/Org.hs+7
@@ -496,6 +496,13 @@ tests =
496496 ] =?>
497497 (mempty::Blocks)
498498
499+ , "LaTeX_headers options are translated to header-includes" =:
500+ "#+LaTeX_header: \\usepackage{tikz}" =?>
501+ let latexInlines = rawInline "latex" "\\usepackage{tikz}"
502+ inclList = MetaList [MetaInlines (toList latexInlines)]
503+ meta = setMeta "header-includes" inclList nullMeta
504+ in Pandoc meta mempty
505+
499506 , "later meta definitions take precedence" =:
500507 unlines [ "#+AUTHOR: this will not be used"
501508 , "#+author: Max"