Skip to content

Commit

Permalink
Replace lookAhead with a better method. Add additional definitions to…
Browse files Browse the repository at this point in the history
… support firstOrderedListItem, while still keeping genericListItemAtDepth generic.
  • Loading branch information
vkraven committed Nov 27, 2022
1 parent ed60ed1 commit 553aa58
Showing 1 changed file with 38 additions and 7 deletions.
45 changes: 38 additions & 7 deletions src/Text/Pandoc/Readers/Textile.hs
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,21 @@ bulletListItemAtDepth = genericListItemAtDepth '*'

-- | Ordered List of given depth, depth being the number of
-- leading '#'
-- The first Ordered List Item may have a start attribute
orderedListAtDepth :: PandocMonad m => Int -> TextileParser m Blocks
orderedListAtDepth depth = try $ do
startNum <- (lookAhead (count depth (char '#') >> orderedListStart))
items <- many1 (orderedListItemAtDepth depth)
return $ B.orderedListWith (startNum, DefaultStyle, DefaultDelim) items
(startNum, firstItem) <- firstOrderedListItemAtDepth depth
moreItems <- many (orderedListItemAtDepth depth)
let listItems = firstItem : moreItems
in return $ B.orderedListWith (startNum, DefaultStyle, DefaultDelim) listItems

-- | The first Ordered List Item, which could have a start attribute
firstOrderedListItemAtDepth :: PandocMonad m => Int -> TextileParser m (Int, Blocks)
firstOrderedListItemAtDepth depth = do
startNum <- genericListStartAtDepth '#' depth
attributes >> whitespace
contents <- genericListItemContentsAtDepth depth
return (startNum, contents)

-- | Ordered List Item of given depth, depth being the number of
-- leading '#'
Expand All @@ -248,26 +258,47 @@ orderedListItemAtDepth = genericListItemAtDepth '#'
-- | Common implementation of list items
genericListItemAtDepth :: PandocMonad m => Char -> Int -> TextileParser m Blocks
genericListItemAtDepth c depth = try $ do
count depth (char c) >> (try orderedListStart) >> attributes >> whitespace
genericListItemStartAtDepth c depth
genericListItemContentsAtDepth depth

-- | Lists always start with a number of leading characters '#' or '*'
-- Ordered list start characters '#' can be followed by the start attribute
-- number, but bullet list characters '*' can not
genericListStartAtDepth :: PandocMonad m => Char -> Int -> TextileParser m Int
genericListStartAtDepth c depth = do
count depth (char c)
case c of
'#' -> try orderedListStartAttr -- ordered list
_ -> return 1 -- unordered list

genericListItemStartAtDepth :: PandocMonad m => Char -> Int -> TextileParser m ()
genericListItemStartAtDepth c depth = () <$ (genericListStartAtDepth c depth >> attributes >> whitespace)

genericListItemContentsAtDepth :: PandocMonad m => Int -> TextileParser m Blocks
genericListItemContentsAtDepth depth = do
contents <- mconcat <$> many ((B.plain . mconcat <$> many1 inline) <|>
try (newline >> codeBlockHtml))
newline
sublist <- option mempty (anyListAtDepth (depth + 1))
return $ contents <> sublist


-- | A definition list is a set of consecutive definition items
definitionList :: PandocMonad m => TextileParser m Blocks
definitionList = try $ B.definitionList <$> many1 definitionListItem

-- | List start character.
listStart :: PandocMonad m => TextileParser m ()
listStart = genericListStart '*'
<|> () <$ genericListStart '#'
<|> () <$ orderedListStart
<|> () <$ definitionListStart

genericListStart :: PandocMonad m => Char -> TextileParser m ()
genericListStart c = () <$ try (many1 (char c) >> whitespace)

orderedListStart :: PandocMonad m => TextileParser m ()
orderedListStart = () <$ try (many1 (char '#') >> try orderedListStartAttr >> whitespace)

basicDLStart :: PandocMonad m => TextileParser m ()
basicDLStart = do
char '-'
Expand Down Expand Up @@ -633,8 +664,8 @@ code2 = do
htmlTag (tagOpen (=="tt") null)
B.code . T.pack <$> manyTill anyChar' (try $ htmlTag $ tagClose (=="tt"))

orderedListStart :: PandocMonad m => TextileParser m Int
orderedListStart = do
orderedListStartAttr :: PandocMonad m => TextileParser m Int
orderedListStartAttr = do
digits <- many digit
case readMaybe digits :: Maybe Int of
Nothing -> return 1
Expand Down

0 comments on commit 553aa58

Please sign in to comment.