diff --git a/tools/pandoc-site.hs b/tools/pandoc-site.hs index 18454dcf5..6b9394ae1 100644 --- a/tools/pandoc-site.hs +++ b/tools/pandoc-site.hs @@ -7,18 +7,12 @@ import Text.Pandoc.Class (runPure) import Text.Pandoc.Builder (text, toList) import Text.Pandoc.Options (def) import Text.Pandoc.JSON (toJSONFilter) +import Text.Pandoc.Shared (hierarchicalize, Element(..)) import Data.Either (fromRight) -import Data.List (groupBy) -import Data.Tree (Forest, Tree(Node)) -import Data.Function (on) import Data.Maybe (fromMaybe) import Data.Text (unpack) -headerLevel :: Block -> Int -headerLevel (Header level _ _) = level -headerLevel _ = error "not a header" - collectHeaders :: Block -> [Block] collectHeaders header@(Header _ (_, classes, _) _) = if "notoc" `elem` classes @@ -26,31 +20,28 @@ collectHeaders header@(Header _ (_, classes, _) _) = else [header] collectHeaders _ = [] -groupByHierarchy :: [Block] -> Forest Block -groupByHierarchy = map (\(x:xs) -> Node x (groupByHierarchy xs)) . groupBy ((<) `on` headerLevel) - markupLink :: Attr -> [Inline] -> Inline markupLink (headerId, _, headerProperties) headerText = let linkText = fromMaybe headerText (fmap (toList . text) $ lookup "toc" headerProperties) in Link nullAttr linkText (("#" ++ headerId), headerId) -markupHeader :: Tree Block -> [Block] -markupHeader n@(Node (Header _ hAttr hText) headers) +markupElement :: Element -> [Block] +markupElement (Sec _ _ hAttr hText headers) | headers == [] = [link] - | otherwise = [link, markupHeaders headers] + | otherwise = [link, markupElements headers] where link = Plain [markupLink hAttr hText] -markupHeader n = error $ "'markupHeader' should only be passed a 'Node $ Header'\n" - ++ " saw: " ++ show n +markupElement n = error $ "'markupElement' should only be passed a 'Sec'\n" + ++ " saw: " ++ show n -markupHeaders :: Forest Block -> Block -markupHeaders = OrderedList (1, Decimal, Period) . map markupHeader +markupElements :: [Element] -> Block +markupElements = OrderedList (1, Decimal, Period) . map markupElement -createTable :: Forest Block -> Block +createTable :: [Element] -> Block createTable [] = Null createTable headers = let navBegin = "" - tocDoc = Pandoc nullMeta [Para [Str "Contents"], markupHeaders headers] + tocDoc = Pandoc nullMeta [Para [Str "Contents"], markupElements headers] tocString = unpack . fromRight mempty . runPure . writeHtml5String def $ tocDoc in RawBlock "html" (navBegin ++ "\n" ++ tocString ++ "\n" ++ navEnd) @@ -61,7 +52,7 @@ generateTOC _ x = x tableOfContents :: Pandoc -> Pandoc tableOfContents ast = let headers = query collectHeaders ast - toc = createTable . groupByHierarchy $ headers + toc = createTable . hierarchicalize $ headers in walk (generateTOC toc) ast main :: IO ()