git.iko.soy/typeable/pandoc
on master help sign in
Org reader: add basic file inclusion mechanism

Support for the `#+INCLUDE:` file inclusion mechanism was added. Recognized include types are *example*, *export*, *src*, and normal org file inclusion. Advanced features like line numbers and level selection are not implemented yet. Closes: #3510

commitaf4bf91c5925b5c6a7431cef8a7997c16d4c7b2b authorAlbert Krewinkel <albert@zeitkraut.de> dateSun 14 May 2017, 12:45 (9 years ago) parent9d295f4
8 files changed+73−5
pandoc.cabal+3 src/Text/Pandoc/Readers/Org/Blocks.hs+32−4 src/Text/Pandoc/Readers/Org/ParserState.hs+10−1 src/Text/Pandoc/Readers/Org/Parsing.hs+1 test/command/3510-export.latex+1 test/command/3510-src.hs+1 test/command/3510-subdoc.org+5 test/command/3510.md+20
pandoc.cabal+3
@@ -138,6 +138,9 @@ Extra-Source-Files:
138138 test/command/abbrevs
139139 test/command/sub-file-chapter-1.tex
140140 test/command/sub-file-chapter-2.tex
141+ test/command/3510-subdoc.org
142+ test/command/3510-export.latex
143+ test/command/3510-src.hs
141144 test/docbook-reader.docbook
142145 test/docbook-xref.docbook
143146 test/html-reader.html
src/Text/Pandoc/Readers/Org/Blocks.hs+32−4
@@ -1,6 +1,3 @@
1−{-# LANGUAGE FlexibleContexts #-}
2−{-# LANGUAGE RecordWildCards #-}
3−{-# LANGUAGE ViewPatterns #-}
41{-
52Copyright (C) 2014-2017 Albert Krewinkel <tarleb+pandoc@moltkeplatz.de>
63
@@ -18,7 +15,9 @@ You should have received a copy of the GNU General Public License
1815along with this program; if not, write to the Free Software
1916Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
2017-}
21−
18+{-# LANGUAGE FlexibleContexts #-}
19+{-# LANGUAGE RecordWildCards #-}
20+{-# LANGUAGE ViewPatterns #-}
2221{- |
2322 Module : Text.Pandoc.Readers.Org.Options
2423 Copyright : Copyright (C) 2014-2017 Albert Krewinkel
@@ -274,6 +273,7 @@ block = choice [ mempty <$ blanklines
274273 , figure
275274 , example
276275 , genericDrawer
276+ , include
277277 , specialLine
278278 , horizontalRule
279279 , list
@@ -717,6 +717,34 @@ exampleCode = B.codeBlockWith ("", ["example"], [])
717717specialLine :: PandocMonad m => OrgParser m (F Blocks)
718718specialLine = fmap return . try $ rawExportLine <|> metaLine <|> commentLine
719719
720+-- | Include the content of a file.
721+include :: PandocMonad m => OrgParser m (F Blocks)
722+include = try $ do
723+ metaLineStart <* stringAnyCase "include:" <* skipSpaces
724+ filename <- includeTarget
725+ blockType <- optionMaybe $ skipSpaces *> many1 alphaNum
726+ blocksParser <- case blockType of
727+ Just "example" -> do
728+ return $ pure . B.codeBlock <$> parseRaw
729+ Just "export" -> do
730+ format <- skipSpaces *> many (noneOf "\n\r\t ")
731+ return $ pure . B.rawBlock format <$> parseRaw
732+ Just "src" -> do
733+ language <- skipSpaces *> many (noneOf "\n\r\t ")
734+ let attr = (mempty, [language], mempty)
735+ return $ pure . B.codeBlockWith attr <$> parseRaw
736+ _ -> return $ pure . B.fromList <$> blockList
737+ anyLine
738+ insertIncludedFileF blocksParser ["."] filename
739+ where
740+ includeTarget :: PandocMonad m => OrgParser m FilePath
741+ includeTarget = do
742+ char '"'
743+ manyTill (noneOf "\n\r\t") (char '"')
744+
745+ parseRaw :: PandocMonad m => OrgParser m String
746+ parseRaw = many anyChar
747+
720748rawExportLine :: PandocMonad m => OrgParser m Blocks
721749rawExportLine = try $ do
722750 metaLineStart
src/Text/Pandoc/Readers/Org/ParserState.hs+10−1
@@ -66,7 +66,8 @@ import Text.Pandoc.Logging
6666import Text.Pandoc.Parsing (HasHeaderMap (..), HasIdentifierList (..),
6767 HasLogMessages (..),
6868 HasLastStrPosition (..), HasQuoteContext (..),
69− HasReaderOptions (..), ParserContext (..),
69+ HasReaderOptions (..), HasIncludeFiles (..),
70+ ParserContext (..),
7071 QuoteContext (..), SourcePos, Future,
7172 askF, asksF, returnF, runF, trimInlinesF)
7273
@@ -106,6 +107,7 @@ data OrgParserState = OrgParserState
106107 , orgStateExportSettings :: ExportSettings
107108 , orgStateHeaderMap :: M.Map Inlines String
108109 , orgStateIdentifiers :: Set.Set String
110+ , orgStateIncludeFiles :: [String]
109111 , orgStateLastForbiddenCharPos :: Maybe SourcePos
110112 , orgStateLastPreCharPos :: Maybe SourcePos
111113 , orgStateLastStrPos :: Maybe SourcePos
@@ -148,6 +150,12 @@ instance HasLogMessages OrgParserState where
148150 addLogMessage msg st = st{ orgLogMessages = msg : orgLogMessages st }
149151 getLogMessages st = reverse $ orgLogMessages st
150152
153+instance HasIncludeFiles OrgParserState where
154+ getIncludeFiles = orgStateIncludeFiles
155+ addIncludeFile f st = st { orgStateIncludeFiles = f : orgStateIncludeFiles st }
156+ dropLatestIncludeFile st =
157+ st { orgStateIncludeFiles = drop 1 $ orgStateIncludeFiles st }
158+
151159instance Default OrgParserState where
152160 def = defaultOrgParserState
153161
@@ -159,6 +167,7 @@ defaultOrgParserState = OrgParserState
159167 , orgStateExportSettings = def
160168 , orgStateHeaderMap = M.empty
161169 , orgStateIdentifiers = Set.empty
170+ , orgStateIncludeFiles = []
162171 , orgStateLastForbiddenCharPos = Nothing
163172 , orgStateLastPreCharPos = Nothing
164173 , orgStateLastStrPos = Nothing
src/Text/Pandoc/Readers/Org/Parsing.hs+1
@@ -71,6 +71,7 @@ module Text.Pandoc.Readers.Org.Parsing
7171 , ellipses
7272 , citeKey
7373 , gridTableWith
74+ , insertIncludedFileF
7475 -- * Re-exports from Text.Pandoc.Parsec
7576 , runParser
7677 , runParserT
test/command/3510-export.latexnew file+1
@@ -0,0 +1 @@
1+\emph{Hello}
test/command/3510-src.hsnew file+1
@@ -0,0 +1 @@
1+putStrLn outString
test/command/3510-subdoc.orgnew file+5
@@ -0,0 +1,5 @@
1+* Subsection
2+
3+Included text
4+
5+Lorem ipsum.
test/command/3510.mdnew file+20
@@ -0,0 +1,20 @@
1+See <http://orgmode.org/manual/Include-files.html>
2+```
3+% pandoc -f org -t native
4+Text
5+
6+#+include: "command/3510-subdoc.org"
7+
8+#+INCLUDE: "command/3510-src.hs" src haskell
9+#+INCLUDE: "command/3510-export.latex" export latex
10+
11+More text
12+^D
13+[Para [Str "Text"]
14+,Header 1 ("subsection",[],[]) [Str "Subsection"]
15+,Para [Str "Included",Space,Str "text"]
16+,Plain [Str "Lorem",Space,Str "ipsum."]
17+,CodeBlock ("",["haskell"],[]) "putStrLn outString\n"
18+,RawBlock (Format "latex") "\\emph{Hello}"
19+,Para [Str "More",Space,Str "text"]]
20+```