    {-# LANGUAGE OverloadedStrings #-}
    {- |
       Module      : Text.Pandoc.Readers.Docx.Lists
       Copyright   : Copyright (C) 2014-2020 Jesse Rosenthal
       License     : GNU GPL, version 2 or above

       Maintainer  : Jesse Rosenthal <jrosenthal@jhu.edu>
       Stability   : alpha
       Portability : portable

    Functions for converting flat docx paragraphs into nested lists.
    -}

    module Text.Pandoc.Readers.Docx.Lists ( blocksToBullets
                                          , blocksToDefinitions
                                          , listParagraphDivs
                                          , listParagraphStyles
                                          ) where

    import Data.List
    import Data.Maybe
    import Data.String (fromString)
    import qualified Data.Text as T
    import Text.Pandoc.Generic (bottomUp)
    import Text.Pandoc.JSON
    import Text.Pandoc.Readers.Docx.Parse (ParaStyleName)
    import Text.Pandoc.Shared (trim, safeRead)

    isListItem :: Block -> Bool
    isListItem :: Block -> Bool
    isListItem (Div (_, classes :: [Text]
    classes, _) _) | "list-item" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes = Bool
    True
    isListItem _                       = Bool
    False

    getLevel :: Block -> Maybe Integer
    getLevel :: Block -> Maybe Integer
    getLevel (Div (_, _, kvs :: [(Text, Text)]
    kvs) _) =  Text -> [(Text, Text)] -> Maybe Text
    forall a b. Eq a => a -> [(a, b)] -> Maybe b
    lookup "level" [(Text, Text)]
    kvs Maybe Text -> (Text -> Maybe Integer) -> Maybe Integer
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= Text -> Maybe Integer
    forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
    safeRead
    getLevel _                   = Maybe Integer
    forall a. Maybe a
    Nothing

    getLevelN :: Block -> Integer
    getLevelN :: Block -> Integer
    getLevelN b :: Block
    b = Integer -> Maybe Integer -> Integer
    forall a. a -> Maybe a -> a
    fromMaybe (-1) (Block -> Maybe Integer
    getLevel Block
    b)

    getNumId :: Block -> Maybe Integer
    getNumId :: Block -> Maybe Integer
    getNumId (Div (_, _, kvs :: [(Text, Text)]
    kvs) _) =  Text -> [(Text, Text)] -> Maybe Text
    forall a b. Eq a => a -> [(a, b)] -> Maybe b
    lookup "num-id" [(Text, Text)]
    kvs Maybe Text -> (Text -> Maybe Integer) -> Maybe Integer
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= Text -> Maybe Integer
    forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
    safeRead
    getNumId _                   = Maybe Integer
    forall a. Maybe a
    Nothing

    getNumIdN :: Block -> Integer
    getNumIdN :: Block -> Integer
    getNumIdN b :: Block
    b = Integer -> Maybe Integer -> Integer
    forall a. a -> Maybe a -> a
    fromMaybe (-1) (Block -> Maybe Integer
    getNumId Block
    b)

    getText :: Block -> Maybe T.Text
    getText :: Block -> Maybe Text
    getText (Div (_, _, kvs :: [(Text, Text)]
    kvs) _) = Text -> [(Text, Text)] -> Maybe Text
    forall a b. Eq a => a -> [(a, b)] -> Maybe b
    lookup "text" [(Text, Text)]
    kvs
    getText _                   = Maybe Text
    forall a. Maybe a
    Nothing

    data ListType = Itemized | Enumerated ListAttributes

    listStyleMap :: [(T.Text, ListNumberStyle)]
    listStyleMap :: [(Text, ListNumberStyle)]
    listStyleMap = [("upperLetter", ListNumberStyle
    UpperAlpha),
                    ("lowerLetter", ListNumberStyle
    LowerAlpha),
                    ("upperRoman", ListNumberStyle
    UpperRoman),
                    ("lowerRoman", ListNumberStyle
    LowerRoman),
                    ("decimal", ListNumberStyle
    Decimal)]

    listDelimMap :: [(T.Text, ListNumberDelim)]
    listDelimMap :: [(Text, ListNumberDelim)]
    listDelimMap = [("%1)", ListNumberDelim
    OneParen),
                    ("(%1)", ListNumberDelim
    TwoParens),
                    ("%1.", ListNumberDelim
    Period)]

    getListType :: Block -> Maybe ListType
    getListType :: Block -> Maybe ListType
    getListType b :: Block
    b@(Div (_, _, kvs :: [(Text, Text)]
    kvs) _) | Block -> Bool
    isListItem Block
    b =
      let
        start :: Maybe Text
    start = Text -> [(Text, Text)] -> Maybe Text
    forall a b. Eq a => a -> [(a, b)] -> Maybe b
    lookup "start" [(Text, Text)]
    kvs
        frmt :: Maybe Text
    frmt = Text -> [(Text, Text)] -> Maybe Text
    forall a b. Eq a => a -> [(a, b)] -> Maybe b
    lookup "format" [(Text, Text)]
    kvs
        txt :: Maybe Text
    txt  = Text -> [(Text, Text)] -> Maybe Text
    forall a b. Eq a => a -> [(a, b)] -> Maybe b
    lookup "text" [(Text, Text)]
    kvs
      in
       case Maybe Text
    frmt of
         Just "bullet" -> ListType -> Maybe ListType
    forall a. a -> Maybe a
    Just ListType
    Itemized
         Just f :: Text
    f        ->
           case Maybe Text
    txt of
             Just t :: Text
    t -> ListType -> Maybe ListType
    forall a. a -> Maybe a
    Just (ListType -> Maybe ListType) -> ListType -> Maybe ListType
    forall a b. (a -> b) -> a -> b
    $ ListAttributes -> ListType
    Enumerated (
                      Int -> Maybe Int -> Int
    forall a. a -> Maybe a -> a
    fromMaybe 1 (Maybe Text
    start Maybe Text -> (Text -> Maybe Int) -> Maybe Int
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= Text -> Maybe Int
    forall (m :: * -> *) a. (MonadPlus m, Read a) => Text -> m a
    safeRead) :: Int,
                      ListNumberStyle -> Maybe ListNumberStyle -> ListNumberStyle
    forall a. a -> Maybe a -> a
    fromMaybe ListNumberStyle
    DefaultStyle (Text -> [(Text, ListNumberStyle)] -> Maybe ListNumberStyle
    forall a b. Eq a => a -> [(a, b)] -> Maybe b
    lookup Text
    f [(Text, ListNumberStyle)]
    listStyleMap),
                      ListNumberDelim -> Maybe ListNumberDelim -> ListNumberDelim
    forall a. a -> Maybe a -> a
    fromMaybe ListNumberDelim
    DefaultDelim (Text -> [(Text, ListNumberDelim)] -> Maybe ListNumberDelim
    forall a b. Eq a => a -> [(a, b)] -> Maybe b
    lookup Text
    t [(Text, ListNumberDelim)]
    listDelimMap))
             Nothing -> Maybe ListType
    forall a. Maybe a
    Nothing
         _ -> Maybe ListType
    forall a. Maybe a
    Nothing
    getListType _ = Maybe ListType
    forall a. Maybe a
    Nothing

    listParagraphDivs :: [T.Text]
    listParagraphDivs :: [Text]
    listParagraphDivs = ["list-paragraph"]

    listParagraphStyles :: [ParaStyleName]
    listParagraphStyles :: [ParaStyleName]
    listParagraphStyles = (Text -> ParaStyleName) -> [Text] -> [ParaStyleName]
    forall a b. (a -> b) -> [a] -> [b]
    map (String -> ParaStyleName
    forall a. IsString a => String -> a
    fromString (String -> ParaStyleName)
    -> (Text -> String) -> Text -> ParaStyleName
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> String
    T.unpack) [Text]
    listParagraphDivs

    -- This is a first stab at going through and attaching meaning to list
    -- paragraphs, without an item marker, following a list item. We
    -- assume that these are paragraphs in the same item.

    handleListParagraphs :: [Block] -> [Block]
    handleListParagraphs :: [Block] -> [Block]
    handleListParagraphs [] = []
    handleListParagraphs (
      Div attr1 :: Attr
    attr1@(_, classes1 :: [Text]
    classes1, _) blks1 :: [Block]
    blks1 :
      Div (ident2 :: Text
    ident2, classes2 :: [Text]
    classes2, kvs2 :: [(Text, Text)]
    kvs2) blks2 :: [Block]
    blks2 :
      blks :: [Block]
    blks
      ) | "list-item" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes1 Bool -> Bool -> Bool
    &&
        Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    notElem "list-item" [Text]
    classes2 Bool -> Bool -> Bool
    &&
        (Bool -> Bool
    not (Bool -> Bool) -> ([Text] -> Bool) -> [Text] -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Text] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null) ([Text]
    listParagraphDivs [Text] -> [Text] -> [Text]
    forall a. Eq a => [a] -> [a] -> [a]
    `intersect` [Text]
    classes2) =
          -- We don't want to keep this indent.
          let newDiv2 :: Block
    newDiv2 =
                Attr -> [Block] -> Block
    Div (Text
    ident2, [Text]
    classes2, ((Text, Text) -> Bool) -> [(Text, Text)] -> [(Text, Text)]
    forall a. (a -> Bool) -> [a] -> [a]
    filter (\kv :: (Text, Text)
    kv -> (Text, Text) -> Text
    forall a b. (a, b) -> a
    fst (Text, Text)
    kv Text -> Text -> Bool
    forall a. Eq a => a -> a -> Bool
    /= "indent") [(Text, Text)]
    kvs2) [Block]
    blks2
          in
           [Block] -> [Block]
    handleListParagraphs (Attr -> [Block] -> Block
    Div Attr
    attr1 ([Block]
    blks1 [Block] -> [Block] -> [Block]
    forall a. [a] -> [a] -> [a]
    ++ [Block
    newDiv2]) Block -> [Block] -> [Block]
    forall a. a -> [a] -> [a]
    : [Block]
    blks)
    handleListParagraphs (blk :: Block
    blk:blks :: [Block]
    blks) = Block
    blk Block -> [Block] -> [Block]
    forall a. a -> [a] -> [a]
    : [Block] -> [Block]
    handleListParagraphs [Block]
    blks

    separateBlocks' :: Block -> [[Block]] -> [[Block]]
    separateBlocks' :: Block -> [[Block]] -> [[Block]]
    separateBlocks' blk :: Block
    blk [[]] = [[Block
    blk]]
    separateBlocks' b :: Block
    b@(BulletList _) acc :: [[Block]]
    acc = [[Block]] -> [[Block]]
    forall a. [a] -> [a]
    init [[Block]]
    acc [[Block]] -> [[Block]] -> [[Block]]
    forall a. [a] -> [a] -> [a]
    ++ [[[Block]] -> [Block]
    forall a. [a] -> a
    last [[Block]]
    acc [Block] -> [Block] -> [Block]
    forall a. [a] -> [a] -> [a]
    ++ [Block
    b]]
    separateBlocks' b :: Block
    b@(OrderedList _ _) acc :: [[Block]]
    acc = [[Block]] -> [[Block]]
    forall a. [a] -> [a]
    init [[Block]]
    acc [[Block]] -> [[Block]] -> [[Block]]
    forall a. [a] -> [a] -> [a]
    ++ [[[Block]] -> [Block]
    forall a. [a] -> a
    last [[Block]]
    acc [Block] -> [Block] -> [Block]
    forall a. [a] -> [a] -> [a]
    ++ [Block
    b]]
    -- The following is for the invisible bullet lists. This is how
    -- pandoc-generated ooxml does multiparagraph item lists.
    separateBlocks' b :: Block
    b acc :: [[Block]]
    acc | (Text -> Text) -> Maybe Text -> Maybe Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    fmap Text -> Text
    trim (Block -> Maybe Text
    getText Block
    b) Maybe Text -> Maybe Text -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text -> Maybe Text
    forall a. a -> Maybe a
    Just "" =
      [[Block]] -> [[Block]]
    forall a. [a] -> [a]
    init [[Block]]
    acc [[Block]] -> [[Block]] -> [[Block]]
    forall a. [a] -> [a] -> [a]
    ++ [[[Block]] -> [Block]
    forall a. [a] -> a
    last [[Block]]
    acc [Block] -> [Block] -> [Block]
    forall a. [a] -> [a] -> [a]
    ++ [Block
    b]]
    separateBlocks' b :: Block
    b acc :: [[Block]]
    acc = [[Block]]
    acc [[Block]] -> [[Block]] -> [[Block]]
    forall a. [a] -> [a] -> [a]
    ++ [[Block
    b]]

    separateBlocks :: [Block] -> [[Block]]
    separateBlocks :: [Block] -> [[Block]]
    separateBlocks blks :: [Block]
    blks = (Block -> [[Block]] -> [[Block]])
    -> [[Block]] -> [Block] -> [[Block]]
    forall (t :: * -> *) a b.
    Foldable t =>
    (a -> b -> b) -> b -> t a -> b
    foldr Block -> [[Block]] -> [[Block]]
    separateBlocks' [[]] ([Block] -> [Block]
    forall a. [a] -> [a]
    reverse [Block]
    blks)

    flatToBullets' :: Integer -> [Block] -> [Block]
    flatToBullets' :: Integer -> [Block] -> [Block]
    flatToBullets' _ [] = []
    flatToBullets' num :: Integer
    num xs :: [Block]
    xs@(b :: Block
    b : elems :: [Block]
    elems)
      | Block -> Integer
    getLevelN Block
    b Integer -> Integer -> Bool
    forall a. Eq a => a -> a -> Bool
    == Integer
    num = Block
    b Block -> [Block] -> [Block]
    forall a. a -> [a] -> [a]
    : Integer -> [Block] -> [Block]
    flatToBullets' Integer
    num [Block]
    elems
      | Bool
    otherwise =
        let bNumId :: Integer
    bNumId = Block -> Integer
    getNumIdN Block
    b
            bLevel :: Integer
    bLevel = Block -> Integer
    getLevelN Block
    b
            (children :: [Block]
    children, remaining :: [Block]
    remaining) =
              (Block -> Bool) -> [Block] -> ([Block], [Block])
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    span
              (\b' :: Block
    b' ->
                Block -> Integer
    getLevelN Block
    b' Integer -> Integer -> Bool
    forall a. Ord a => a -> a -> Bool
    > Integer
    bLevel Bool -> Bool -> Bool
    ||
                 (Block -> Integer
    getLevelN Block
    b' Integer -> Integer -> Bool
    forall a. Eq a => a -> a -> Bool
    == Integer
    bLevel Bool -> Bool -> Bool
    && Block -> Integer
    getNumIdN Block
    b' Integer -> Integer -> Bool
    forall a. Eq a => a -> a -> Bool
    == Integer
    bNumId))
              [Block]
    xs
        in
         case Block -> Maybe ListType
    getListType Block
    b of
           Just (Enumerated attr :: ListAttributes
    attr) ->
             ListAttributes -> [[Block]] -> Block
    OrderedList ListAttributes
    attr ([Block] -> [[Block]]
    separateBlocks ([Block] -> [[Block]]) -> [Block] -> [[Block]]
    forall a b. (a -> b) -> a -> b
    $ Integer -> [Block] -> [Block]
    flatToBullets' Integer
    bLevel [Block]
    children) Block -> [Block] -> [Block]
    forall a. a -> [a] -> [a]
    :
             Integer -> [Block] -> [Block]
    flatToBullets' Integer
    num [Block]
    remaining
           _ ->
             [[Block]] -> Block
    BulletList ([Block] -> [[Block]]
    separateBlocks ([Block] -> [[Block]]) -> [Block] -> [[Block]]
    forall a b. (a -> b) -> a -> b
    $ Integer -> [Block] -> [Block]
    flatToBullets' Integer
    bLevel [Block]
    children) Block -> [Block] -> [Block]
    forall a. a -> [a] -> [a]
    :
             Integer -> [Block] -> [Block]
    flatToBullets' Integer
    num [Block]
    remaining

    flatToBullets :: [Block] -> [Block]
    flatToBullets :: [Block] -> [Block]
    flatToBullets elems :: [Block]
    elems = Integer -> [Block] -> [Block]
    flatToBullets' (-1) [Block]
    elems

    singleItemHeaderToHeader :: Block -> Block
    singleItemHeaderToHeader :: Block -> Block
    singleItemHeaderToHeader (OrderedList _ [[h :: Block
    h@Header{}]]) = Block
    h
    singleItemHeaderToHeader blk :: Block
    blk                            = Block
    blk

    blocksToBullets :: [Block] -> [Block]
    blocksToBullets :: [Block] -> [Block]
    blocksToBullets blks :: [Block]
    blks =
      (Block -> Block) -> [Block] -> [Block]
    forall a b. (a -> b) -> [a] -> [b]
    map Block -> Block
    singleItemHeaderToHeader ([Block] -> [Block]) -> [Block] -> [Block]
    forall a b. (a -> b) -> a -> b
    $
      ([Block] -> [Block]) -> [Block] -> [Block]
    forall a b. (Data a, Data b) => (a -> a) -> b -> b
    bottomUp [Block] -> [Block]
    removeListDivs ([Block] -> [Block]) -> [Block] -> [Block]
    forall a b. (a -> b) -> a -> b
    $[Block] -> [Block]
    flatToBullets ([Block] -> [Block]
    handleListParagraphs [Block]
    blks)

    plainParaInlines :: Block -> [Inline]
    plainParaInlines :: Block -> [Inline]
    plainParaInlines (Plain ils :: [Inline]
    ils) = [Inline]
    ils
    plainParaInlines (Para ils :: [Inline]
    ils)  = [Inline]
    ils
    plainParaInlines _           = []

    blocksToDefinitions' :: [([Inline], [[Block]])] -> [Block] -> [Block] -> [Block]
    blocksToDefinitions' :: [([Inline], [[Block]])] -> [Block] -> [Block] -> [Block]
    blocksToDefinitions' []     acc :: [Block]
    acc [] = [Block] -> [Block]
    forall a. [a] -> [a]
    reverse [Block]
    acc
    blocksToDefinitions' defAcc :: [([Inline], [[Block]])]
    defAcc acc :: [Block]
    acc [] =
      [Block] -> [Block]
    forall a. [a] -> [a]
    reverse ([Block] -> [Block]) -> [Block] -> [Block]
    forall a b. (a -> b) -> a -> b
    $ [([Inline], [[Block]])] -> Block
    DefinitionList ([([Inline], [[Block]])] -> [([Inline], [[Block]])]
    forall a. [a] -> [a]
    reverse [([Inline], [[Block]])]
    defAcc) Block -> [Block] -> [Block]
    forall a. a -> [a] -> [a]
    : [Block]
    acc
    blocksToDefinitions' defAcc :: [([Inline], [[Block]])]
    defAcc acc :: [Block]
    acc
      (Div (_, classes1 :: [Text]
    classes1, _) blks1 :: [Block]
    blks1 : Div (ident2 :: Text
    ident2, classes2 :: [Text]
    classes2, kvs2 :: [(Text, Text)]
    kvs2) blks2 :: [Block]
    blks2 : blks :: [Block]
    blks)
      | "Definition-Term" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes1 Bool -> Bool -> Bool
    && "Definition"  Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes2 =
        let remainingAttr2 :: Attr
    remainingAttr2 = (Text
    ident2, Text -> [Text] -> [Text]
    forall a. Eq a => a -> [a] -> [a]
    delete "Definition" [Text]
    classes2, [(Text, Text)]
    kvs2)
            pair :: ([Inline], [[Block]])
    pair = if Attr
    remainingAttr2 Attr -> Attr -> Bool
    forall a. Eq a => a -> a -> Bool
    == ("", [], []) then ((Block -> [Inline]) -> [Block] -> [Inline]
    forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
    concatMap Block -> [Inline]
    plainParaInlines [Block]
    blks1, [[Block]
    blks2]) else ((Block -> [Inline]) -> [Block] -> [Inline]
    forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
    concatMap Block -> [Inline]
    plainParaInlines [Block]
    blks1, [[Attr -> [Block] -> Block
    Div Attr
    remainingAttr2 [Block]
    blks2]])
        in
         [([Inline], [[Block]])] -> [Block] -> [Block] -> [Block]
    blocksToDefinitions' (([Inline], [[Block]])
    pair ([Inline], [[Block]])
    -> [([Inline], [[Block]])] -> [([Inline], [[Block]])]
    forall a. a -> [a] -> [a]
    : [([Inline], [[Block]])]
    defAcc) [Block]
    acc [Block]
    blks
    blocksToDefinitions' ((defTerm :: [Inline]
    defTerm, defItems :: [[Block]]
    defItems):defs :: [([Inline], [[Block]])]
    defs) acc :: [Block]
    acc
      (Div (ident2 :: Text
    ident2, classes2 :: [Text]
    classes2, kvs2 :: [(Text, Text)]
    kvs2) blks2 :: [Block]
    blks2 : blks :: [Block]
    blks)
      | "Definition"  Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes2 =
        let remainingAttr2 :: Attr
    remainingAttr2 = (Text
    ident2, Text -> [Text] -> [Text]
    forall a. Eq a => a -> [a] -> [a]
    delete "Definition" [Text]
    classes2, [(Text, Text)]
    kvs2)
            defItems2 :: [Block]
    defItems2 = if Attr
    remainingAttr2 Attr -> Attr -> Bool
    forall a. Eq a => a -> a -> Bool
    == ("", [], [])
              then [Block]
    blks2
              else [Attr -> [Block] -> Block
    Div Attr
    remainingAttr2 [Block]
    blks2]
            defAcc' :: [([Inline], [[Block]])]
    defAcc' = if [[Block]] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [[Block]]
    defItems
              then ([Inline]
    defTerm, [[Block]
    defItems2]) ([Inline], [[Block]])
    -> [([Inline], [[Block]])] -> [([Inline], [[Block]])]
    forall a. a -> [a] -> [a]
    : [([Inline], [[Block]])]
    defs
              else ([Inline]
    defTerm, [[Block]] -> [[Block]]
    forall a. [a] -> [a]
    init [[Block]]
    defItems [[Block]] -> [[Block]] -> [[Block]]
    forall a. [a] -> [a] -> [a]
    ++ [[[Block]] -> [Block]
    forall a. [a] -> a
    last [[Block]]
    defItems [Block] -> [Block] -> [Block]
    forall a. [a] -> [a] -> [a]
    ++ [Block]
    defItems2]) ([Inline], [[Block]])
    -> [([Inline], [[Block]])] -> [([Inline], [[Block]])]
    forall a. a -> [a] -> [a]
    : [([Inline], [[Block]])]
    defs
        in
         [([Inline], [[Block]])] -> [Block] -> [Block] -> [Block]
    blocksToDefinitions' [([Inline], [[Block]])]
    defAcc' [Block]
    acc [Block]
    blks
    blocksToDefinitions' [] acc :: [Block]
    acc (b :: Block
    b:blks :: [Block]
    blks) =
      [([Inline], [[Block]])] -> [Block] -> [Block] -> [Block]
    blocksToDefinitions' [] (Block
    bBlock -> [Block] -> [Block]
    forall a. a -> [a] -> [a]
    :[Block]
    acc) [Block]
    blks
    blocksToDefinitions' defAcc :: [([Inline], [[Block]])]
    defAcc acc :: [Block]
    acc (b :: Block
    b:blks :: [Block]
    blks) =
      [([Inline], [[Block]])] -> [Block] -> [Block] -> [Block]
    blocksToDefinitions' [] (Block
    b Block -> [Block] -> [Block]
    forall a. a -> [a] -> [a]
    : [([Inline], [[Block]])] -> Block
    DefinitionList ([([Inline], [[Block]])] -> [([Inline], [[Block]])]
    forall a. [a] -> [a]
    reverse [([Inline], [[Block]])]
    defAcc) Block -> [Block] -> [Block]
    forall a. a -> [a] -> [a]
    : [Block]
    acc) [Block]
    blks

    removeListDivs' :: Block -> [Block]
    removeListDivs' :: Block -> [Block]
    removeListDivs' (Div (ident :: Text
    ident, classes :: [Text]
    classes, kvs :: [(Text, Text)]
    kvs) blks :: [Block]
    blks)
      | "list-item" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes =
        case Text -> [Text] -> [Text]
    forall a. Eq a => a -> [a] -> [a]
    delete "list-item" [Text]
    classes of
          []       -> [Block]
    blks
          classes' :: [Text]
    classes' -> [Attr -> [Block] -> Block
    Div (Text
    ident, [Text]
    classes', [(Text, Text)]
    kvs) [Block]
    blks]
    removeListDivs' (Div (ident :: Text
    ident, classes :: [Text]
    classes, kvs :: [(Text, Text)]
    kvs) blks :: [Block]
    blks)
      | Bool -> Bool
    not (Bool -> Bool) -> Bool -> Bool
    forall a b. (a -> b) -> a -> b
    $ [Text] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null ([Text] -> Bool) -> [Text] -> Bool
    forall a b. (a -> b) -> a -> b
    $ [Text]
    listParagraphDivs [Text] -> [Text] -> [Text]
    forall a. Eq a => [a] -> [a] -> [a]
    `intersect` [Text]
    classes =
        case [Text]
    classes [Text] -> [Text] -> [Text]
    forall a. Eq a => [a] -> [a] -> [a]
    \\ [Text]
    listParagraphDivs of
          []       -> [Block]
    blks
          classes' :: [Text]
    classes' -> [Attr -> [Block] -> Block
    Div (Text
    ident, [Text]
    classes', [(Text, Text)]
    kvs) [Block]
    blks]
    removeListDivs' blk :: Block
    blk = [Block
    blk]

    removeListDivs :: [Block] -> [Block]
    removeListDivs :: [Block] -> [Block]
    removeListDivs = (Block -> [Block]) -> [Block] -> [Block]
    forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
    concatMap Block -> [Block]
    removeListDivs'

    blocksToDefinitions :: [Block] -> [Block]
    blocksToDefinitions :: [Block] -> [Block]
    blocksToDefinitions = [([Inline], [[Block]])] -> [Block] -> [Block] -> [Block]
    blocksToDefinitions' [] []