module Utils.Pandoc.SignsCount where --import Text.HTML.TagSoup (innerText, parseTags) import Data.Text qualified as Text import Relude import Text.Pandoc.Definition signsCount :: Pandoc -> Int signsCount (Pandoc _ bs) = sum $ map goBlock bs where goBlocks = sum . map goBlock goBlocks2 = sum . map goBlocks goBlock :: Block -> Int goBlock (BlockQuote bs') = goBlocks bs' goBlock (BulletList bss) = goBlocks2 bss goBlock (CodeBlock _ s) = Text.length s goBlock (DefinitionList ls) = sum $ map (\(is, bss) -> goInlines is + goBlocks2 bss) ls goBlock (Div (id', classes, _) bs') | id' == "refs" && "references" `elem` classes = 0 | otherwise = goBlocks bs' goBlock (Header _ _ is) = goInlines is goBlock HorizontalRule = 0 goBlock (LineBlock iss) = goInlines2 iss goBlock (OrderedList _ bss) = goBlocks2 bss goBlock (Para is) = goInlines is goBlock (Plain is) = goInlines is -- TODO: goBlock (RawBlock _ s) = Text.length . innerText . parseTags $ s goBlock (RawBlock _ s) = 0 goBlock Null = 0 goBlock (Table _ (Caption msc bs') _ (TableHead _ thrs) tbs (TableFoot _ tfrs)) = goInlines (concat (maybeToList msc)) + goBlocks bs' + goRows thrs + goRows tfrs + sum (map (\(TableBody _ _ hr br) -> goRows hr + goRows br) tbs) goRows = sum . map goRow goRow (Row _ cells) = sum . map goCell $ cells goCell (Cell _ _ _ _ bs') = goBlocks bs' goInlines = sum . map goInline goInlines2 = sum . map goInlines goInline :: Inline -> Int goInline (Cite _ is) = goInlines is goInline (Code _ s) = Text.length s goInline (Emph is) = goInlines is goInline (Image _ is (_, s)) = goInlines is + Text.length s goInline (Link _ is (_, s)) = goInlines is + Text.length s goInline (Math _ s) = Text.length s goInline (Note bs') = goBlocks bs' goInline (Quoted _ is) = goInlines is goInline (RawInline _ s) = Text.length s goInline (SmallCaps is) = goInlines is goInline (Span _ s) = goInlines s goInline (Str s) = Text.length s goInline (Strikeout is) = goInlines is goInline (Strong is) = goInlines is goInline (Subscript is) = goInlines is goInline (Superscript is) = goInlines is goInline (Underline is) = goInlines is goInline LineBreak = 1 goInline SoftBreak = 1 goInline Space = 1