@@ -37,8 +37,9 @@ import Text.Pandoc.Error
37import Text.Pandoc.Options
37import Text.Pandoc.Options
38import qualified Text.Pandoc.Parsing as P
38import qualified Text.Pandoc.Parsing as P
39import Text.Pandoc.Parsing hiding ( F, unF, askF, asksF, runF
39import Text.Pandoc.Parsing hiding ( F, unF, askF, asksF, runF
40+ , anyLine, blanklines, newline
40− , newline, orderedListMarker
41+ , orderedListMarker
41− , parseFromString, blanklines
42+ , parseFromString
43import Text.Pandoc.Readers.LaTeX (inlineCommand, rawLaTeXInline)
44import Text.Pandoc.Readers.LaTeX (inlineCommand, rawLaTeXInline)
44import Text.Pandoc.Readers.Org.ParserState
45import Text.Pandoc.Readers.Org.ParserState
@@ -49,10 +50,10 @@ import qualified Text.TeXMath.Readers.MathML.EntityMap as MathMLEntityMap
49import Control.Arrow (first)
50import Control.Arrow (first)
50import Control.Monad (foldM, guard, mplus, mzero, when)
51import Control.Monad (foldM, guard, mplus, mzero, when)
51import Control.Monad.Reader ( Reader, runReader )
52import Control.Monad.Reader ( Reader, runReader )
52−import Data.Char (isAlphaNum, isSpace, toLower)
53+import Data.Char (isAlphaNum, isSpace, toLower, toUpper)
53−import Data.List (intersperse, isPrefixOf, isSuffixOf)
54+import Data.List ( foldl', intersperse, isPrefixOf, isSuffixOf )
54import qualified Data.Map as M
55import qualified Data.Map as M
55−import Data.Maybe (fromMaybe, isJust)
56+import Data.Maybe ( fromMaybe, isNothing )
56import Network.HTTP (urlEncode)
57import Network.HTTP (urlEncode)
@@ -247,6 +248,12 @@ blanklines =
247 <* updateLastPreCharPos
248 <* updateLastPreCharPos
248 <* updateLastForbiddenCharPos
249 <* updateLastForbiddenCharPos
251+anyLine :: OrgParser String
254+ <* updateLastPreCharPos
255+ <* updateLastForbiddenCharPos
250-- | Succeeds when we're in list context.
257-- | Succeeds when we're in list context.
251inList :: OrgParser ()
258inList :: OrgParser ()
252inList = do
259inList = do
@@ -273,11 +280,9 @@ parseBlocks = mconcat <$> manyTill block eof
274block :: OrgParser (F Blocks)
281block :: OrgParser (F Blocks)
275block = choice [ mempty <$ blanklines
282block = choice [ mempty <$ blanklines
276− , optionalAttributes $ choice
283+ , table
277− [ orgBlock
284+ , orgBlock
278− , figure
285+ , figure
281 , example
286 , example
283 , specialLine
288 , specialLine
@@ -289,50 +294,73 @@ block = choice [ mempty <$ blanklines
289 , paraOrPlain
294 , paraOrPlain
290 ] <?> "block"
295 ] <?> "block"
293-- Block Attributes
299-- Block Attributes
296−-- | Parse optional block attributes (like #+TITLE or #+NAME)
302+-- | Attributes that may be added to figures (like a name or caption).
297−optionalAttributes :: OrgParser (F Blocks) -> OrgParser (F Blocks)
303+data BlockAttributes = BlockAttributes
298−optionalAttributes parser = try $
304+ { blockAttrName :: Maybe String
299− resetBlockAttributes *> parseBlockAttributes *> parser
305+ , blockAttrCaption :: Maybe (F Inlines)
306+ , blockAttrKeyValues :: [(String, String)]
309+stringyMetaAttribute :: (String -> Bool) -> OrgParser (String, String)
310+stringyMetaAttribute attrCheck = try $ do
312+ attrName <- map toUpper <$> many1Till nonspaceChar (char ':')
313+ guard $ attrCheck attrName
315+ attrValue <- anyLine
316+ return (attrName, attrValue)
318+blockAttributes :: OrgParser BlockAttributes
319+blockAttributes = try $ do
320+ kv <- many (stringyMetaAttribute attrCheck)
321+ let caption = foldl' (appendValues "CAPTION") Nothing kv
322+ let kvAttrs = foldl' (appendValues "ATTR_HTML") Nothing kv
323+ let name = lookup "NAME" kv
324+ caption' <- maybe (return Nothing)
325+ (fmap Just . parseFromString parseInlines)
327+ kvAttrs' <- parseFromString keyValues . (++ "\n") $ fromMaybe mempty kvAttrs
328+ return $ BlockAttributes
329+ { blockAttrName = name
330+ , blockAttrCaption = caption'
331+ , blockAttrKeyValues = kvAttrs'
301− resetBlockAttributes :: OrgParser ()
334+ attrCheck :: String -> Bool
302− resetBlockAttributes = updateState $ \s ->
335+ attrCheck attr =
303− s{ orgStateBlockAttributes = orgStateBlockAttributes def }
336+ case attr of
305−parseBlockAttributes :: OrgParser ()
338+ "CAPTION" -> True
306−parseBlockAttributes = do
339+ "ATTR_HTML" -> True
307− attrs <- many attribute
340+ _ -> False
308− mapM_ (uncurry parseAndAddAttribute) attrs
341+
342+ appendValues :: String -> Maybe String -> (String, String) -> Maybe String
343+ appendValues attrName accValue (key, value) =
346+ else case accValue of
347+ Just acc -> Just $ acc ++ ' ':value
348+ Nothing -> Just value
350+keyValues :: OrgParser [(String, String)]
352+ manyTill ((,) <$> key <*> value) newline
310− attribute :: OrgParser (String, String)
354+ key :: OrgParser String
311− attribute = try $ do
355+ key = try $ skipSpaces *> char ':' *> many1 nonspaceChar
312− key <- metaLineStart *> many1Till nonspaceChar (char ':')
356+
313− val <- skipSpaces *> anyLine
357+ value :: OrgParser String
314− return (map toLower key, val)
358+ value = skipSpaces *> manyTill anyChar endOfValue
316−parseAndAddAttribute :: String -> String -> OrgParser ()
360+ endOfValue :: OrgParser ()
317−parseAndAddAttribute key value = do
361+ endOfValue =
318− let key' = map toLower key
362+ lookAhead $ (() <$ try (many1 spaceChar <* key))
319− () <$ addBlockAttribute key' value
363+ <|> () <$ P.newline
321−lookupInlinesAttr :: String -> OrgParser (Maybe (F Inlines))
322−lookupInlinesAttr attr = try $ do
323− val <- lookupBlockAttribute attr
324− maybe (return Nothing)
325− (fmap Just . parseFromString parseInlines)
328−addBlockAttribute :: String -> String -> OrgParser ()
329−addBlockAttribute key val = updateState $ \s ->
330− let attrs = orgStateBlockAttributes s
331− in s{ orgStateBlockAttributes = M.insert key val attrs }
333−lookupBlockAttribute :: String -> OrgParser (Maybe String)
334−lookupBlockAttribute key =
335− M.lookup key . orgStateBlockAttributes <$> getState
@@ -346,6 +374,7 @@ updateIndent (_, blkType) indent = (indent, blkType)
347orgBlock :: OrgParser (F Blocks)
375orgBlock :: OrgParser (F Blocks)
348orgBlock = try $ do
376orgBlock = try $ do
377+ blockAttrs <- blockAttributes
349 blockProp@(_, blkType) <- blockHeaderStart
378 blockProp@(_, blkType) <- blockHeaderStart
350 ($ blockProp) $
379 ($ blockProp) $
351 case blkType of
380 case blkType of
@@ -356,7 +385,7 @@ orgBlock = try $ do
356 "example" -> withRaw' (return . exampleCode)
385 "example" -> withRaw' (return . exampleCode)
357 "quote" -> withParsed (fmap B.blockQuote)
386 "quote" -> withParsed (fmap B.blockQuote)
358 "verse" -> verseBlock
387 "verse" -> verseBlock
359− "src" -> codeBlock
388+ "src" -> codeBlock blockAttrs
360 _ -> withParsed (fmap $ divWithClass blkType)
389 _ -> withParsed (fmap $ divWithClass blkType)
362blockHeaderStart :: OrgParser (Int, String)
391blockHeaderStart :: OrgParser (Int, String)
@@ -410,20 +439,20 @@ followingResultsBlock =
410 *> blankline
439 *> blankline
411 *> block)
440 *> block)
413−codeBlock :: BlockProperties -> OrgParser (F Blocks)
442+codeBlock :: BlockAttributes -> BlockProperties -> OrgParser (F Blocks)
414−codeBlock blkProp = do
443+codeBlock blockAttrs blkProp = do
415 skipSpaces
444 skipSpaces
416 (classes, kv) <- codeHeaderArgs <|> (mempty <$ ignHeaders)
445 (classes, kv) <- codeHeaderArgs <|> (mempty <$ ignHeaders)
417− id' <- fromMaybe "" <$> lookupBlockAttribute "name"
418 leadingIndent <- lookAhead indentation
446 leadingIndent <- lookAhead indentation
419 content <- rawBlockContent (updateIndent blkProp leadingIndent)
447 content <- rawBlockContent (updateIndent blkProp leadingIndent)
420 resultsContent <- followingResultsBlock
448 resultsContent <- followingResultsBlock
449+ let id' = fromMaybe mempty $ blockAttrName blockAttrs
421 let includeCode = exportsCode kv
450 let includeCode = exportsCode kv
422 let includeResults = exportsResults kv
451 let includeResults = exportsResults kv
423 let codeBlck = B.codeBlockWith ( id', classes, kv ) content
452 let codeBlck = B.codeBlockWith ( id', classes, kv ) content
424− labelledBlck <- maybe (pure codeBlck)
453+ let labelledBlck = maybe (pure codeBlck)
425 (labelDiv codeBlck)
454 (labelDiv codeBlck)
426− <$> lookupInlinesAttr "caption"
455+ (blockAttrCaption blockAttrs)
427 let resultBlck = fromMaybe mempty resultsContent
456 let resultBlck = fromMaybe mempty resultsContent
428 return $ (if includeCode then labelledBlck else mempty)
457 return $ (if includeCode then labelledBlck else mempty)
429 <> (if includeResults then resultBlck else mempty)
458 <> (if includeResults then resultBlck else mempty)
@@ -579,47 +608,43 @@ drawerEnd = try $
579-- Figures
608-- Figures
582−-- Figures (Image on a line by itself, preceded by name and/or caption)
611+-- | Figures (Image on a line by itself, preceded by name and/or caption)
583figure :: OrgParser (F Blocks)
612figure :: OrgParser (F Blocks)
584figure = try $ do
613figure = try $ do
585− (cap, nam) <- nameAndCaption
614+ figAttrs <- blockAttributes
586 src <- skipSpaces *> selfTarget <* skipSpaces <* P.newline
615 src <- skipSpaces *> selfTarget <* skipSpaces <* P.newline
616+ guard . not . isNothing . blockAttrCaption $ figAttrs
587 guard (isImageFilename src)
617 guard (isImageFilename src)
588− return $ do
618+ let figName = fromMaybe mempty $ blockAttrName figAttrs
589− cap' <- cap
619+ let figCaption = fromMaybe mempty $ blockAttrCaption figAttrs
590− return $ B.para $ B.image src nam cap'
620+ let figKeyVals = blockAttrKeyValues figAttrs
621+ let attr = (mempty, mempty, figKeyVals)
622+ return $ (B.para . B.imageWith attr src (withFigPrefix figName) <$> figCaption)
594− maybeCap <- lookupInlinesAttr "caption"
595− maybeNam <- lookupBlockAttribute "name"
596− guard $ isJust maybeCap || isJust maybeNam
597− return ( fromMaybe mempty maybeCap
598− , withFigPrefix $ fromMaybe mempty maybeNam )
599 withFigPrefix cs =
624 withFigPrefix cs =
600− if "fig:" `isPrefixOf` cs
625+ if "fig:" `isPrefixOf` cs
602− else "fig:" ++ cs
627+ else "fig:" ++ cs
605-- Comments, Options and Metadata
630-- Comments, Options and Metadata
606specialLine :: OrgParser (F Blocks)
632specialLine :: OrgParser (F Blocks)
607specialLine = fmap return . try $ metaLine <|> commentLine
633specialLine = fmap return . try $ metaLine <|> commentLine
609metaLine :: OrgParser Blocks
635metaLine :: OrgParser Blocks
610−metaLine = try $ mempty
611− <$ (metaLineStart *> (optionLine <|> declarationLine))
636+metaLine = mempty <$ metaLineStart <* (optionLine <|> declarationLine)
613−commentLine :: OrgParser Blocks
614−commentLine = try $ commentLineStart *> anyLine *> pure mempty
616-- The order, in which blocks are tried, makes sure that we're not looking at
638-- The order, in which blocks are tried, makes sure that we're not looking at
617-- the beginning of a block, so we don't need to check for it
639-- the beginning of a block, so we don't need to check for it
618−metaLineStart :: OrgParser String
640+metaLineStart :: OrgParser ()
619−metaLineStart = try $ mappend <$> many spaceChar <*> string "#+"
641+metaLineStart = try $ skipSpaces <* string "#+"
643+commentLine :: OrgParser Blocks
644+commentLine = commentLineStart *> anyLine *> pure mempty
621−commentLineStart :: OrgParser String
646+commentLineStart :: OrgParser ()
622−commentLineStart = try $ mappend <$> many spaceChar <*> string "# "
647+commentLineStart = try $ skipSpaces <* string "# "
624declarationLine :: OrgParser ()
649declarationLine :: OrgParser ()
625declarationLine = try $ do
650declarationLine = try $ do
@@ -738,11 +763,12 @@ data OrgTable = OrgTable
739table :: OrgParser (F Blocks)
764table :: OrgParser (F Blocks)
740table = try $ do
765table = try $ do
766+ blockAttrs <- blockAttributes
741 lookAhead tableStart
767 lookAhead tableStart
743 rows <- tableRows
769 rows <- tableRows
744− cptn <- fromMaybe (pure "") <$> lookupInlinesAttr "caption"
770+ let caption = fromMaybe (return mempty) $ blockAttrCaption blockAttrs
745− return $ (<$> cptn) . orgToPandocTable . normalizeTable =<< rowsToTable rows
771+ return $ (<$> caption) . orgToPandocTable . normalizeTable =<< rowsToTable rows
747orgToPandocTable :: OrgTable
773orgToPandocTable :: OrgTable
748 -> Inlines
774 -> Inlines