diff --git a/Shake.hs b/Shake.hs index 152899b09..027aa791b 100755 --- a/Shake.hs +++ b/Shake.hs @@ -87,7 +87,7 @@ main = do --- "tools" "pandoc-drop-html-inlines" --- "tools" "pandoc-drop-links" --- "tools" "pandoc-drop-notes" - "tools" "pandoc-drop-toc" + --- "tools" "pandoc-drop-toc" ] shakeArgs @@ -286,7 +286,7 @@ main = do cmd Shell ("printf '\\n\\n' >>") webmanall :: Action ExitCode cmd Shell "pandoc" f "-t markdown-fenced_divs --atx-headers" -- "--filter tools/pandoc-drop-man-blocks" - "--filter tools/pandoc-drop-toc" + "--lua-filter tools/pandoc-drop-toc.lua" -- "--filter tools/pandoc-capitalize-headers" "--lua-filter tools/pandoc-demote-headers.lua" ">>" webmanall :: Action ExitCode diff --git a/tools/pandoc-drop-toc.hs b/tools/pandoc-drop-toc.hs deleted file mode 100755 index b3336e993..000000000 --- a/tools/pandoc-drop-toc.hs +++ /dev/null @@ -1,44 +0,0 @@ -#!/usr/bin/env stack -{- stack runghc - --verbosity info - --stack-yaml=stack-ghc8.2.yaml - --package pandoc-types - --package safe - --package split --} --- Remove a table of contents marker --- (a bullet list item containing "toc[-N[-M]]") - -{-# LANGUAGE OverloadedStrings #-} - -import Data.Char (isDigit) -import Data.List.Split -import Data.Maybe -import Safe -import Text.Pandoc.JSON - -main :: IO () -main = toJSONFilter dropToc - -dropHtmlBlocks :: Block -> Block -dropHtmlBlocks (RawBlock (Format "html") _) = Plain [] -dropHtmlBlocks x = x - - -- BulletList - -- [ [Plain [Str "toc"]] ] -dropToc :: Block -> Block -dropToc (BulletList is) = - BulletList $ filter (not.null) $ map (filter isNotToc) is - where - isNotToc (Plain [Str s]) | isJust $ tocParams s = False - isNotToc _ = True -dropToc x = x - -tocParams :: String -> Maybe (Maybe Int, Maybe Int) -tocParams s = - case splitOn "-" s of - ["toc"] -> Just (Nothing, Nothing) - ["toc",a] | all isDigit a -> Just (Nothing, readMay a) - ["toc",a,b] | all isDigit a, all isDigit b -> Just (readMay a, readMay b) - _ -> Nothing - diff --git a/tools/pandoc-drop-toc.lua b/tools/pandoc-drop-toc.lua new file mode 100644 index 000000000..d2cbb8f47 --- /dev/null +++ b/tools/pandoc-drop-toc.lua @@ -0,0 +1,16 @@ +function keepBi(bi) + if not (bi[1].t == "Plain") then return true end + if not (bi[1].content[1].t == "Str") then return true end + if not (string.find(bi[1].content[1].text, "toc") == 1) then return true end + return false +end + +function BulletList(bl) + local newBl = { } + for i,bi in pairs(bl.content) do + if keepBi(bi) + then table.insert(newBl, bi) + end + end + return pandoc.BulletList(newBl) +end