From 2364a53f494a24049a3864c36e476165b0042159 Mon Sep 17 00:00:00 2001 From: Simon Michael Date: Mon, 11 Feb 2019 14:12:48 -0800 Subject: [PATCH] site: add table of contents to all wiki pages with 2+ headings [ci skip] --- Shake.hs | 15 ++++++++----- tools/{pandoc-site.lua => pandoc-toc.lua} | 26 +++++++++++++---------- 2 files changed, 25 insertions(+), 16 deletions(-) rename tools/{pandoc-site.lua => pandoc-toc.lua} (80%) diff --git a/Shake.hs b/Shake.hs index 6397c70d0..ad60d8857 100755 --- a/Shake.hs +++ b/Shake.hs @@ -342,7 +342,7 @@ main = do phony "mdcombinedmanual" $ need [ mdcombinedmanual ] mdcombinedmanual %> \out -> do need mdmanuals - liftIO $ writeFile mdcombinedmanual "\\$toc\\$" -- # Big Manual\n\n -- TOC style is better without main heading, + liftIO $ writeFile mdcombinedmanual $ addToc "" forM_ mdmanuals $ \f -> do -- site/hledger.md, site/journal.md cmd_ Shell ("printf '\\n\\n' >>") mdcombinedmanual cmd_ Shell pandoc f towebmd @@ -406,7 +406,7 @@ main = do phony "oldmanuals" $ need oldhtmlmanuals -- Render one website page (main or wiki) as html, saved in sites/_site/. - -- Wiki pages will have a heading prepended. + -- Wiki pages will have a heading and TOC placeholder prepended. -- All pages will have github-style wiki links hyperlinked. "site/_site//*.html" %> \out -> do let filename = takeBaseName out @@ -420,13 +420,13 @@ main = do template = "site/site.tmpl" siteRoot = if "site/_site/doc//*" ?== out then "../.." else "." need [source, template] - -- read markdown source, link any wikilinks, pipe it to pandoc, write html out - Stdin . wikiLink . (if iswikipage then addHeading pagename else id) <$> (readFile' source) >>= + -- read markdown source, link any wikilinks, maybe add a heading and TOC, pipe it to pandoc, write html out + Stdin . wikiLink . (if iswikipage then addHeading pagename . addToc else id) <$> (readFile' source) >>= (cmd Shell pandoc "-" fromsrcmd "-t html" "--template" template ("--metadata=siteRoot:" ++ siteRoot) ("--metadata=\"title:" ++ pagename ++ "\"") - "--lua-filter=tools/pandoc-site.lua" + "--lua-filter=tools/pandoc-toc.lua" "-o" out ) -- HLEDGER PACKAGES/EXECUTABLES @@ -707,6 +707,11 @@ type Markdown = String addHeading :: String -> Markdown -> Markdown addHeading h = (("# "++h++"\n\n")++) +-- | Prepend a table of contents placeholder. +addToc :: Markdown -> Markdown +addToc = ((tocMarker++"\n\n")++) + where tocMarker = "$TOC$" + -- | Convert Github-style wikilinks to hledger website links. wikiLink :: Markdown -> Markdown wikiLink = diff --git a/tools/pandoc-site.lua b/tools/pandoc-toc.lua similarity index 80% rename from tools/pandoc-site.lua rename to tools/pandoc-toc.lua index 79a008378..c544a89f4 100644 --- a/tools/pandoc-site.lua +++ b/tools/pandoc-toc.lua @@ -5,14 +5,6 @@ function Header(h) return h end -function isTocBlock(blk) - if not (blk.t == "Para") then return false end - if not blk.content[1] then return false end - if not (blk.content[1].t == "Str") then return false end - if not (blk.content[1].text == "$toc$") then return false end - return true -end - function markupLink(hAttr, headerText) local headerId = hAttr.identifier local headerProperties = hAttr.attributes @@ -43,13 +35,25 @@ function createTable(elems) return {navBegin, contentsP, markupElements(elems), navEnd} end +function isTocMarker(blk) + local tocMarker = "$TOC$" + if not (blk.t == "Para") then return false end + if not blk.content[1] then return false end + if not (blk.content[1].t == "Str") then return false end + return blk.content[1].text == tocMarker +end + function Pandoc(doc) newBlocks = {} tocBlocks = createTable(pandoc.utils.hierarchicalize(headers)) for _,blk in pairs(doc.blocks) do - if isTocBlock(blk) then - for _,tocBlk in pairs(tocBlocks) do - table.insert(newBlocks, tocBlk) + -- replace a TOC placeholder with the table of contents, + -- or nothing if there's less than two headings + if isTocMarker(blk) then + if #headers > 1 then + for _,tocBlk in pairs(tocBlocks) do + table.insert(newBlocks, tocBlk) + end end else table.insert(newBlocks, blk)