@@ -17,7 +17,6 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18{-# LANGUAGE FlexibleContexts #-}
18{-# LANGUAGE FlexibleContexts #-}
19{-# LANGUAGE RecordWildCards #-}
19{-# LANGUAGE RecordWildCards #-}
20−{-# LANGUAGE ViewPatterns #-}
22 Module : Text.Pandoc.Readers.Org.Blocks
21 Module : Text.Pandoc.Readers.Org.Blocks
23 Copyright : Copyright (C) 2014-2017 Albert Krewinkel
22 Copyright : Copyright (C) 2014-2017 Albert Krewinkel
@@ -52,7 +51,7 @@ import Control.Monad (foldM, guard, mzero, void)
52import Data.Char (isSpace, toLower, toUpper)
51import Data.Char (isSpace, toLower, toUpper)
53import Data.Default (Default)
52import Data.Default (Default)
54import Data.List (foldl', isPrefixOf)
53import Data.List (foldl', isPrefixOf)
55−import Data.Maybe (fromMaybe, isNothing)
54+import Data.Maybe (fromMaybe, isJust, isNothing)
56import Data.Monoid ((<>))
55import Data.Monoid ((<>))
@@ -113,7 +112,7 @@ data BlockAttributes = BlockAttributes
114-- | Convert BlockAttributes into pandoc Attr
113-- | Convert BlockAttributes into pandoc Attr
115attrFromBlockAttributes :: BlockAttributes -> Attr
114attrFromBlockAttributes :: BlockAttributes -> Attr
116−attrFromBlockAttributes (BlockAttributes{..}) =
115+attrFromBlockAttributes BlockAttributes{..} =
118 ident = fromMaybe mempty $ lookup "id" blockAttrKeyValues
117 ident = fromMaybe mempty $ lookup "id" blockAttrKeyValues
119 classes = case lookup "class" blockAttrKeyValues of
118 classes = case lookup "class" blockAttrKeyValues of
@@ -142,7 +141,7 @@ blockAttributes = try $ do
142 Nothing -> return Nothing
141 Nothing -> return Nothing
143 Just s -> Just <$> parseFromString inlines (s ++ "\n")
142 Just s -> Just <$> parseFromString inlines (s ++ "\n")
144 kvAttrs' <- parseFromString keyValues . (++ "\n") $ fromMaybe mempty kvAttrs
143 kvAttrs' <- parseFromString keyValues . (++ "\n") $ fromMaybe mempty kvAttrs
145− return $ BlockAttributes
144+ return BlockAttributes
146 { blockAttrName = name
145 { blockAttrName = name
147 , blockAttrLabel = label
146 , blockAttrLabel = label
148 , blockAttrCaption = caption'
147 , blockAttrCaption = caption'
@@ -187,7 +186,7 @@ orgBlock = try $ do
187 blockAttrs <- blockAttributes
186 blockAttrs <- blockAttributes
188 blkType <- blockHeaderStart
187 blkType <- blockHeaderStart
189 ($ blkType) $
188 ($ blkType) $
190− case (map toLower blkType) of
189+ case map toLower blkType of
191 "export" -> exportBlock
190 "export" -> exportBlock
192 "comment" -> rawBlockLines (const mempty)
191 "comment" -> rawBlockLines (const mempty)
193 "html" -> rawBlockLines (return . B.rawBlock (lowercase blkType))
192 "html" -> rawBlockLines (return . B.rawBlock (lowercase blkType))
@@ -208,10 +207,10 @@ orgBlock = try $ do
208 lowercase = map toLower
207 lowercase = map toLower
210rawBlockLines :: Monad m => (String -> F Blocks) -> String -> OrgParser m (F Blocks)
209rawBlockLines :: Monad m => (String -> F Blocks) -> String -> OrgParser m (F Blocks)
211−rawBlockLines f blockType = (ignHeaders *> (f <$> rawBlockContent blockType))
210+rawBlockLines f blockType = ignHeaders *> (f <$> rawBlockContent blockType)
213parseBlockLines :: PandocMonad m => (F Blocks -> F Blocks) -> String -> OrgParser m (F Blocks)
212parseBlockLines :: PandocMonad m => (F Blocks -> F Blocks) -> String -> OrgParser m (F Blocks)
214−parseBlockLines f blockType = (ignHeaders *> (f <$> parsedBlockContent))
213+parseBlockLines f blockType = ignHeaders *> (f <$> parsedBlockContent)
216 parsedBlockContent :: PandocMonad m => OrgParser m (F Blocks)
215 parsedBlockContent :: PandocMonad m => OrgParser m (F Blocks)
217 parsedBlockContent = try $ do
216 parsedBlockContent = try $ do
@@ -239,8 +238,7 @@ rawBlockContent blockType = try $ do
239 stripIndent strs = map (drop (shortestIndent strs)) strs
238 stripIndent strs = map (drop (shortestIndent strs)) strs
241 shortestIndent :: [String] -> Int
240 shortestIndent :: [String] -> Int
242− shortestIndent = foldr min maxBound
243− . map (length . takeWhile isSpace)
241+ shortestIndent = foldr (min . length . takeWhile isSpace) maxBound
244 . filter (not . null)
242 . filter (not . null)
246 tabsToSpaces :: Int -> String -> String
244 tabsToSpaces :: Int -> String -> String
@@ -336,13 +334,13 @@ codeHeaderArgs = try $ do
336 language <- skipSpaces *> orgArgWord
334 language <- skipSpaces *> orgArgWord
337 (switchClasses, switchKv) <- switchesAsAttributes
335 (switchClasses, switchKv) <- switchesAsAttributes
338 parameters <- manyTill blockOption newline
336 parameters <- manyTill blockOption newline
339− return $ ( translateLang language : switchClasses
337+ return ( translateLang language : switchClasses
340− , originalLang language <> switchKv <> parameters
338+ , originalLang language <> switchKv <> parameters
343switchesAsAttributes :: Monad m => OrgParser m ([String], [(String, String)])
341switchesAsAttributes :: Monad m => OrgParser m ([String], [(String, String)])
344switchesAsAttributes = try $ do
342switchesAsAttributes = try $ do
345− switches <- skipSpaces *> (try $ switch `sepBy` (many1 spaceChar))
343+ switches <- skipSpaces *> try (switch `sepBy` many1 spaceChar)
346 return $ foldr addToAttr ([], []) switches
344 return $ foldr addToAttr ([], []) switches
348 addToAttr :: (Char, Maybe String, SwitchPolarity)
346 addToAttr :: (Char, Maybe String, SwitchPolarity)
@@ -350,7 +348,7 @@ switchesAsAttributes = try $ do
350 -> ([String], [(String, String)])
348 -> ([String], [(String, String)])
351 addToAttr ('n', lineNum, pol) (cls, kv) =
349 addToAttr ('n', lineNum, pol) (cls, kv) =
352 let kv' = case lineNum of
350 let kv' = case lineNum of
353− Just num -> (("startFrom", num):kv)
351+ Just num -> ("startFrom", num):kv
354 Nothing -> kv
352 Nothing -> kv
355 cls' = case pol of
353 cls' = case pol of
356 SwitchPlus -> "continuedSourceBlock":cls
354 SwitchPlus -> "continuedSourceBlock":cls
@@ -382,7 +380,7 @@ genericSwitch :: Monad m
382genericSwitch c p = try $ do
380genericSwitch c p = try $ do
383 polarity <- switchPolarity <* char c <* skipSpaces
381 polarity <- switchPolarity <* char c <* skipSpaces
384 arg <- optionMaybe p
382 arg <- optionMaybe p
385− return $ (c, arg, polarity)
383+ return (c, arg, polarity)
387-- | Reads a line number switch option. The line number switch can be used with
385-- | Reads a line number switch option. The line number switch can be used with
388-- example and source blocks.
386-- example and source blocks.
@@ -402,8 +400,8 @@ orgParamValue = try $
402 *> noneOf "\n\r" `many1Till` endOfValue
400 *> noneOf "\n\r" `many1Till` endOfValue
403 <* skipSpaces
401 <* skipSpaces
405− endOfValue = lookAhead $ (try $ skipSpaces <* oneOf "\n\r")
403+ endOfValue = lookAhead $ try (skipSpaces <* oneOf "\n\r")
406− <|> (try $ skipSpaces1 <* orgArgKey)
404+ <|> try (skipSpaces1 <* orgArgKey)
@@ -421,7 +419,7 @@ genericDrawer = try $ do
421 -- Include drawer if it is explicitly included in or not explicitly excluded
419 -- Include drawer if it is explicitly included in or not explicitly excluded
422 -- from the list of drawers that should be exported. PROPERTIES drawers are
420 -- from the list of drawers that should be exported. PROPERTIES drawers are
423 -- never exported.
421 -- never exported.
424− case (exportDrawers . orgStateExportSettings $ state) of
422+ case exportDrawers . orgStateExportSettings $ state of
425 _ | name == "PROPERTIES" -> return mempty
423 _ | name == "PROPERTIES" -> return mempty
426 Left names | name `elem` names -> return mempty
424 Left names | name `elem` names -> return mempty
427 Right names | name `notElem` names -> return mempty
425 Right names | name `notElem` names -> return mempty
@@ -455,7 +453,7 @@ figure = try $ do
455 Nothing -> mzero
453 Nothing -> mzero
456 Just imgSrc -> do
454 Just imgSrc -> do
457 guard (isImageFilename imgSrc)
455 guard (isImageFilename imgSrc)
458− let isFigure = not . isNothing $ blockAttrCaption figAttrs
456+ let isFigure = isJust $ blockAttrCaption figAttrs
459 return $ imageBlock isFigure figAttrs imgSrc
457 return $ imageBlock isFigure figAttrs imgSrc
461 selfTarget :: PandocMonad m => OrgParser m String
459 selfTarget :: PandocMonad m => OrgParser m String
@@ -490,8 +488,7 @@ endOfParagraph = try $ skipSpaces *> newline *> endOfBlock
491-- | Example code marked up by a leading colon.
489-- | Example code marked up by a leading colon.
492example :: Monad m => OrgParser m (F Blocks)
490example :: Monad m => OrgParser m (F Blocks)
494− returnF . exampleCode =<< unlines <$> many1 exampleLine
491+example = try $ returnF . exampleCode =<< unlines <$> many1 exampleLine
496 exampleLine :: Monad m => OrgParser m String
493 exampleLine :: Monad m => OrgParser m String
497 exampleLine = try $ exampleLineStart *> anyLine
494 exampleLine = try $ exampleLineStart *> anyLine
@@ -514,7 +511,7 @@ include = try $ do
514 filename <- includeTarget
511 filename <- includeTarget
515 blockType <- optionMaybe $ skipSpaces *> many1 alphaNum
512 blockType <- optionMaybe $ skipSpaces *> many1 alphaNum
516 blocksParser <- case blockType of
513 blocksParser <- case blockType of
517− Just "example" -> do
514+ Just "example" ->
518 return $ pure . B.codeBlock <$> parseRaw
515 return $ pure . B.codeBlock <$> parseRaw
519 Just "export" -> do
516 Just "export" -> do
520 format <- skipSpaces *> many (noneOf "\n\r\t ")
517 format <- skipSpaces *> many (noneOf "\n\r\t ")
@@ -580,8 +577,8 @@ orgTable :: PandocMonad m => OrgParser m (F Blocks)
580orgTable = try $ do
577orgTable = try $ do
581 -- don't allow a table on the first line of a list item; org requires that
578 -- don't allow a table on the first line of a list item; org requires that
582 -- tables start at first non-space character on the line
579 -- tables start at first non-space character on the line
583− let isFirstInListItem st = (orgStateParserContext st == ListItemState) &&
580+ let isFirstInListItem st = orgStateParserContext st == ListItemState &&
584− (orgStateLastPreCharPos st == Nothing)
581+ isNothing (orgStateLastPreCharPos st)
585 guard =<< not . isFirstInListItem <$> getState
582 guard =<< not . isFirstInListItem <$> getState
586 blockAttrs <- blockAttributes
583 blockAttrs <- blockAttributes
587 lookAhead tableStart
584 lookAhead tableStart
@@ -594,7 +591,7 @@ orgToPandocTable :: OrgTable
594 -> Inlines
591 -> Inlines
595 -> Blocks
592 -> Blocks
596orgToPandocTable (OrgTable colProps heads lns) caption =
593orgToPandocTable (OrgTable colProps heads lns) caption =
597− let totalWidth = if any (not . isNothing) (map columnRelWidth colProps)
594+ let totalWidth = if any isJust (map columnRelWidth colProps)
598 then Just . sum $ map (fromMaybe 1 . columnRelWidth) colProps
595 then Just . sum $ map (fromMaybe 1 . columnRelWidth) colProps
599 else Nothing
596 else Nothing
600 in B.table caption (map (convertColProp totalWidth) colProps) heads lns
597 in B.table caption (map (convertColProp totalWidth) colProps) heads lns
@@ -604,7 +601,7 @@ orgToPandocTable (OrgTable colProps heads lns) caption =
605 align' = fromMaybe AlignDefault $ columnAlignment colProp
602 align' = fromMaybe AlignDefault $ columnAlignment colProp
606 width' = fromMaybe 0 $ (\w t -> (fromIntegral w / fromIntegral t))
603 width' = fromMaybe 0 $ (\w t -> (fromIntegral w / fromIntegral t))
607− <$> (columnRelWidth colProp)
604+ <$> columnRelWidth colProp
608 <*> totalWidth
605 <*> totalWidth
609 in (align', width')
606 in (align', width')
@@ -630,7 +627,7 @@ tableAlignRow = try $ do
630columnPropertyCell :: Monad m => OrgParser m ColumnProperty
627columnPropertyCell :: Monad m => OrgParser m ColumnProperty
631columnPropertyCell = emptyCell <|> propCell <?> "alignment info"
628columnPropertyCell = emptyCell <|> propCell <?> "alignment info"
633− emptyCell = ColumnProperty Nothing Nothing <$ (try $ skipSpaces *> endOfCell)
630+ emptyCell = ColumnProperty Nothing Nothing <$ try (skipSpaces *> endOfCell)
634 propCell = try $ ColumnProperty
631 propCell = try $ ColumnProperty
635 <$> (skipSpaces
632 <$> (skipSpaces
636 *> char '<'
633 *> char '<'
@@ -684,7 +681,7 @@ rowToContent tbl row =
685 singleRowPromotedToHeader :: OrgTable
682 singleRowPromotedToHeader :: OrgTable
686 singleRowPromotedToHeader = case tbl of
683 singleRowPromotedToHeader = case tbl of
687− OrgTable{ orgTableHeader = [], orgTableRows = b:[] } ->
684+ OrgTable{ orgTableHeader = [], orgTableRows = [b] } ->
688 tbl{ orgTableHeader = b , orgTableRows = [] }
685 tbl{ orgTableHeader = b , orgTableRows = [] }
@@ -739,7 +736,7 @@ noteBlock = try $ do
739paraOrPlain :: PandocMonad m => OrgParser m (F Blocks)
736paraOrPlain :: PandocMonad m => OrgParser m (F Blocks)
740paraOrPlain = try $ do
737paraOrPlain = try $ do
741 -- Make sure we are not looking at a headline
738 -- Make sure we are not looking at a headline
742− notFollowedBy' (char '*' *> (oneOf " *"))
739+ notFollowedBy' (char '*' *> oneOf " *")
743 ils <- inlines
740 ils <- inlines
744 nl <- option False (newline *> return True)
741 nl <- option False (newline *> return True)
745 -- Read block as paragraph, except if we are in a list context and the block
742 -- Read block as paragraph, except if we are in a list context and the block
@@ -748,7 +745,7 @@ paraOrPlain = try $ do
748 try (guard nl
745 try (guard nl
749 *> notFollowedBy (inList *> (() <$ orderedListStart <|> bulletListStart))
746 *> notFollowedBy (inList *> (() <$ orderedListStart <|> bulletListStart))
750 *> return (B.para <$> ils))
747 *> return (B.para <$> ils))
751− <|> (return (B.plain <$> ils))
748+ <|> return (B.plain <$> ils)
@@ -760,16 +757,16 @@ list = choice [ definitionList, bulletList, orderedList ] <?> "list"
761definitionList :: PandocMonad m => OrgParser m (F Blocks)
758definitionList :: PandocMonad m => OrgParser m (F Blocks)
762definitionList = try $ do n <- lookAhead (bulletListStart' Nothing)
759definitionList = try $ do n <- lookAhead (bulletListStart' Nothing)
763− fmap B.definitionList . fmap compactifyDL . sequence
760+ fmap (B.definitionList . compactifyDL) . sequence
764 <$> many1 (definitionListItem $ bulletListStart' (Just n))
761 <$> many1 (definitionListItem $ bulletListStart' (Just n))
766bulletList :: PandocMonad m => OrgParser m (F Blocks)
763bulletList :: PandocMonad m => OrgParser m (F Blocks)
767bulletList = try $ do n <- lookAhead (bulletListStart' Nothing)
764bulletList = try $ do n <- lookAhead (bulletListStart' Nothing)
768− fmap B.bulletList . fmap compactify . sequence
765+ fmap (B.bulletList . compactify) . sequence
769 <$> many1 (listItem (bulletListStart' $ Just n))
766 <$> many1 (listItem (bulletListStart' $ Just n))
771orderedList :: PandocMonad m => OrgParser m (F Blocks)
768orderedList :: PandocMonad m => OrgParser m (F Blocks)
772−orderedList = fmap B.orderedList . fmap compactify . sequence
769+orderedList = fmap (B.orderedList . compactify) . sequence
773 <$> many1 (listItem orderedListStart)
770 <$> many1 (listItem orderedListStart)
775bulletListStart' :: Monad m => Maybe Int -> OrgParser m Int
772bulletListStart' :: Monad m => Maybe Int -> OrgParser m Int