site: add table of contents to all wiki pages with 2+ headings

[ci skip]
This commit is contained in:
Simon Michael 2019-02-11 14:12:48 -08:00
parent 3aad6696c9
commit 2364a53f49
2 changed files with 25 additions and 16 deletions

View File

@ -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 =

View File

@ -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)