git.iko.soy/typeable/pandoc
on master help sign in
Org reader: Minor code clean-up
commit4ebf6f6ebf7d679252ade08203ec13e3e92c2db5 authorAlbert Krewinkel <tarleb@moltkeplatz.de> dateSun 6 Apr 2014, 19:09 (13 years ago) parent11120d6
1 file changed+21−30
src/Text/Pandoc/Readers/Org.hs+21−30
src/Text/Pandoc/Readers/Org.hs+21−30
@@ -268,8 +268,12 @@ data OrgTable = OrgTable
268268table :: OrgParser Blocks
269269table = try $ do
270270 lookAhead tableStart
271− OrgTable _ aligns heads lns <- normalizeTable . rowsToTable <$> tableRows
272− return $ B.table "" (zip aligns $ repeat 0) heads lns
271+ orgToPandocTable . normalizeTable . rowsToTable <$> tableRows
272+
273+orgToPandocTable :: OrgTable
274+ -> Blocks
275+orgToPandocTable (OrgTable _ aligns heads lns) =
276+ B.table "" (zip aligns $ repeat 0) heads lns
273277
274278tableStart :: OrgParser Char
275279tableStart = try $ skipSpaces *> char '|'
@@ -403,20 +407,14 @@ orderedListStart = genericListStart orderedListMarker
403407 -- Ordered list markers allowed in org-mode
404408 where orderedListMarker = mappend <$> many1 digit <*> (pure <$> oneOf ".)")
405409
410+-- parse raw text for one list item, excluding start marker and continuations
406411listItem :: OrgParser Int
407412 -> OrgParser Blocks
408413listItem start = try $ do
409− (markerLength, first) <- try (start >>= rawListItem)
410− rest <- many (listContinuation markerLength)
411− parseFromString parseBlocks $ concat (first:rest)
412−
413−-- parse raw text for one list item, excluding start marker and continuations
414−rawListItem :: Int
415− -> OrgParser (Int, String)
416−rawListItem markerLength = try $ do
417− firstLine <- anyLine
418− restLines <- many (listLine markerLength)
419− return (markerLength, (firstLine ++ "\n" ++ (concat restLines)))
414+ markerLength <- try start
415+ firstLine <- anyLineNewline
416+ rest <- concat <$> many (listContinuation markerLength)
417+ parseFromString parseBlocks $ firstLine ++ rest
420418
421419-- continuation of a list item - indented and separated by blankline or endline.
422420-- Note: nested lists are parsed as continuations.
@@ -424,14 +422,11 @@ listContinuation :: Int
424422 -> OrgParser String
425423listContinuation markerLength = try $
426424 mappend <$> many blankline
427− <*> (concat <$> many1 (listLine markerLength))
425+ <*> (concat <$> many1 listLine)
426+ where listLine = try $ indentWith markerLength *> anyLineNewline
428427
429−-- parse a line of a list item
430−listLine :: Int
431− -> OrgParser String
432−listLine markerLength = try $
433− indentWith markerLength *> anyLine
434− <**> pure (++ "\n")
428+anyLineNewline :: OrgParser String
429+anyLineNewline = (++ "\n") <$> anyLine
435430
436431
437432--
@@ -491,12 +486,11 @@ explicitOrImageLink = try $ do
491486 char '['
492487 src <- enclosedRaw (char '[') (char ']')
493488 title <- enclosedRaw (char '[') (char ']')
494− title' <- parseFromString (mconcat . butLast <$> many inline) (title++"\n")
489+ title' <- parseFromString (mconcat <$> many inline) title
495490 char ']'
496491 return $ if (isImage src) && (isImage title)
497492 then B.link src "" (B.image title "" "")
498493 else B.link src "" title'
499− where butLast = reverse . tail . reverse
500494
501495selflinkOrImage :: OrgParser Inlines
502496selflinkOrImage = try $ do
@@ -552,11 +546,8 @@ inlinesEnclosedBy c = try $ do
552546 updateState $ \st -> st { orgInlineCharStack = c:(orgInlineCharStack st) }
553547 res <- enclosedInlines (atStart (char c) <* endsOnThisOrNextLine c)
554548 (atEnd $ char c)
555− updateState $ \st -> st { orgInlineCharStack = shift . orgInlineCharStack $ st }
549+ updateState $ \st -> st { orgInlineCharStack = drop 1 . orgInlineCharStack $ st }
556550 return res
557− where shift xs
558− | null xs = []
559− | otherwise = tail xs
560551
561552enclosedRaw :: OrgParser a
562553 -> OrgParser b
@@ -583,14 +574,13 @@ atStart p = do
583574atEnd :: OrgParser a -> OrgParser a
584575atEnd p = try $ do
585576 p <* lookingAtEndOfWord
586− where lookingAtEndOfWord = lookAhead . oneOf =<< postWordChars
577+ where lookingAtEndOfWord =
578+ eof <|> const (return ()) =<< lookAhead . oneOf =<< postWordChars
587579
588580postWordChars :: OrgParser [Char]
589581postWordChars = do
590582 st <- getState
591− return $ "\t\n\r !\"'),-.:?}" ++ (safeSecond . orgInlineCharStack $ st)
592− where safeSecond (_:x2:_) = [x2]
593− safeSecond _ = []
583+ return $ "\t\n\r !\"'),-.:?}" ++ (take 1 . drop 1 . orgInlineCharStack $ st)
594584
595585-- FIXME: These functions are hacks and should be replaced
596586endsOnThisOrNextLine :: Char
@@ -608,6 +598,7 @@ endsOnThisLine input c doOnOtherLines = do
608598 postWordChars' <- postWordChars
609599 case break (`elem` c:"\n") input of
610600 (_,'\n':rest) -> doOnOtherLines rest
601+ (_,_:[]) -> return ()
611602 (_,_:rest@(n:_)) -> if n `elem` postWordChars'
612603 then return ()
613604 else endsOnThisLine rest c doOnOtherLines